|
| 1 | +import isEmpty from 'lodash/isEmpty' |
| 2 | +import { ServiceConnection } from '@cloudgraph/sdk' |
| 3 | +import { RawGcpCdnUrlMap } from './data' |
| 4 | +import { GLOBAL_REGION } from '../../config/constants' |
| 5 | +import services from '../../enums/services' |
| 6 | + |
| 7 | +export default ({ |
| 8 | + account, |
| 9 | + service, |
| 10 | + data, |
| 11 | + region, |
| 12 | +}: { |
| 13 | + account: string |
| 14 | + service: RawGcpCdnUrlMap |
| 15 | + data: { name: string; data: { [property: string]: any[] } }[] |
| 16 | + region: string |
| 17 | +}): { |
| 18 | + [property: string]: ServiceConnection[] |
| 19 | +} => { |
| 20 | + let connectedBackendBuckets: string[] = [] |
| 21 | + let connectedBackendServices: string[] = [] |
| 22 | + const { |
| 23 | + id, |
| 24 | + defaultService, |
| 25 | + defaultRouteAction = {}, |
| 26 | + pathMatchers = [], |
| 27 | + } = service |
| 28 | + |
| 29 | + connectedBackendBuckets.push( |
| 30 | + ...(pathMatchers?.flatMap( |
| 31 | + pathMatcher => pathMatcher?.pathRules?.flatMap( |
| 32 | + pathRule => pathRule?.service |
| 33 | + ) || [] |
| 34 | + ) || []), // backend bucket |
| 35 | + ) |
| 36 | + |
| 37 | + connectedBackendServices.push( |
| 38 | + defaultService, |
| 39 | + 'https://www.googleapis.com/compute/v1/projects/infra-volt-261615/global/backendServices/backend-service', |
| 40 | + ...(defaultRouteAction?.weightedBackendServices?.flatMap(weightedBackendService => weightedBackendService.backendService) || []), |
| 41 | + ...(pathMatchers?.flatMap(pathMatcher => pathMatcher?.defaultService) || []), |
| 42 | + ...(pathMatchers?.flatMap(pathMatcher => pathMatcher?.defaultRouteAction?.requestMirrorPolicy?.backendService) || []), // undefined |
| 43 | + ...(pathMatchers?.flatMap( |
| 44 | + pathMatcher => pathMatcher?.defaultRouteAction?.weightedBackendServices?.flatMap( |
| 45 | + weightedBackendService => weightedBackendService.backendService |
| 46 | + ) || [] |
| 47 | + ) || []), |
| 48 | + ...(pathMatchers?.flatMap( |
| 49 | + pathMatcher => pathMatcher?.pathRules?.flatMap( |
| 50 | + pathRule => pathRule?.routeAction?.requestMirrorPolicy?.backendService |
| 51 | + ) || [] |
| 52 | + ) || []), |
| 53 | + ...(pathMatchers?.flatMap( |
| 54 | + pathMatcher => pathMatcher?.pathRules?.flatMap( |
| 55 | + pathRule => pathRule?.routeAction?.weightedBackendServices?.flatMap( |
| 56 | + weightedBackendService => weightedBackendService.backendService |
| 57 | + ) || [] |
| 58 | + ) || [] |
| 59 | + ) || []), |
| 60 | + ) |
| 61 | + |
| 62 | + connectedBackendBuckets = [...new Set(connectedBackendBuckets)].filter(bucket => {return bucket}) |
| 63 | + connectedBackendServices = [...new Set(connectedBackendServices)].filter(service => {return service}) |
| 64 | + |
| 65 | + const connections: ServiceConnection[] = [] |
| 66 | + const toServiceUri = (projectId, backendService): string => |
| 67 | + `https://www.googleapis.com/compute/v1/projects/${projectId}/${GLOBAL_REGION}/backendServices/${backendService}` |
| 68 | + const toBucketUri = (projectId, bucketService): string => |
| 69 | + `https://www.googleapis.com/compute/v1/projects/${projectId}/${GLOBAL_REGION}/backendBuckets/${bucketService}` |
| 70 | + /** |
| 71 | + * Find CDN Backend Bucket |
| 72 | + * related to this Url Map |
| 73 | + */ |
| 74 | + const backendBuckets: { |
| 75 | + name: string |
| 76 | + data: { [property: string]: any[] } |
| 77 | + } = data.find(({ name }) => name === services.cdnBackendBucket) |
| 78 | + |
| 79 | + if (backendBuckets?.data?.[GLOBAL_REGION]) { |
| 80 | + |
| 81 | + const filteredCdnBackendBucketsInRegion = backendBuckets.data[GLOBAL_REGION].filter( |
| 82 | + ({ name, projectId }: RawGcpCdnUrlMap) => { |
| 83 | + return connectedBackendBuckets.includes(toBucketUri(projectId, name)) |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + if (!isEmpty(filteredCdnBackendBucketsInRegion)) { |
| 88 | + for (const cdnBackendBucket of filteredCdnBackendBucketsInRegion) { |
| 89 | + connections.push({ |
| 90 | + id: cdnBackendBucket.id, |
| 91 | + resourceType: services.cdnBackendBucket, |
| 92 | + relation: 'child', |
| 93 | + field: 'cdnBackendBucket', |
| 94 | + }) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Find CDN Backend Services |
| 101 | + * related to this Url Map |
| 102 | + */ |
| 103 | + const backendServices: { |
| 104 | + name: string |
| 105 | + data: { [property: string]: any[] } |
| 106 | + } = data.find(({ name }) => name === services.cdnBackendService) |
| 107 | + |
| 108 | + if (backendServices?.data?.[GLOBAL_REGION]) { |
| 109 | + |
| 110 | + const filteredCdnBackendServiceInRegion = backendServices.data[GLOBAL_REGION].filter( |
| 111 | + ({ name, projectId }: RawGcpCdnUrlMap) => { |
| 112 | + return connectedBackendServices.includes(toServiceUri(projectId, name)) |
| 113 | + } |
| 114 | + ) |
| 115 | + |
| 116 | + if (!isEmpty(filteredCdnBackendServiceInRegion)) { |
| 117 | + for (const cdnBackendService of filteredCdnBackendServiceInRegion) { |
| 118 | + connections.push({ |
| 119 | + id: cdnBackendService.id, |
| 120 | + resourceType: services.cdnBackendService, |
| 121 | + relation: 'child', |
| 122 | + field: 'cdnBackendService', |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + const result = { |
| 130 | + [id]: connections, |
| 131 | + } |
| 132 | + return result |
| 133 | +} |
0 commit comments