|
| 1 | +import { ServiceConnection } from '@cloudgraph/sdk' |
| 2 | + |
| 3 | +import { isEmpty } from 'lodash' |
| 4 | +import services from '../../enums/services' |
| 5 | +import { RawAwsEcsCluster } from '../ecsCluster/data' |
| 6 | +import { RawAwsS3 } from '../s3/data' |
| 7 | +import { RawAwsLogGroup } from '../cloudwatchLogs/data' |
| 8 | +import { AwsKms } from '../kms/data' |
| 9 | +import { gets3BucketId } from '../../utils/ids' |
| 10 | + |
| 11 | +export default ({ |
| 12 | + service: ecsCluster, |
| 13 | + data, |
| 14 | + region, |
| 15 | +}: { |
| 16 | + service: RawAwsEcsCluster |
| 17 | + data: Array<{ name: string; data: { [property: string]: any[] } }> |
| 18 | + region: string |
| 19 | +}): { |
| 20 | + [property: string]: ServiceConnection[] |
| 21 | +} => { |
| 22 | + const { |
| 23 | + clusterArn: arn, |
| 24 | + configuration: { |
| 25 | + executeCommandConfiguration: { logConfiguration, kmsKeyId } = {}, |
| 26 | + } = {}, |
| 27 | + } = ecsCluster |
| 28 | + const connections: ServiceConnection[] = [] |
| 29 | + |
| 30 | + /** |
| 31 | + * Find S3 |
| 32 | + * related to this ecs cluster |
| 33 | + */ |
| 34 | + const buckets = data.find(({ name }) => name === services.s3) |
| 35 | + if (buckets?.data?.[region]) { |
| 36 | + const dataAtRegion: RawAwsS3[] = buckets.data[region].filter( |
| 37 | + ({ Name: name }: RawAwsS3) => name === logConfiguration?.s3BucketName |
| 38 | + ) |
| 39 | + for (const bucket of dataAtRegion) { |
| 40 | + connections.push({ |
| 41 | + id: gets3BucketId(bucket.Name), |
| 42 | + resourceType: services.s3, |
| 43 | + relation: 'child', |
| 44 | + field: 's3', |
| 45 | + }) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Find Cloudwatch Log Group |
| 51 | + * related to this ecs cluster |
| 52 | + */ |
| 53 | + const logGroups = data.find(({ name }) => name === services.cloudwatchLog) |
| 54 | + let logGroupsInRegion: RawAwsLogGroup[] = [] |
| 55 | + if (logGroups?.data?.[region]) { |
| 56 | + logGroupsInRegion = logGroups.data[region].filter( |
| 57 | + ({ logGroupName }: RawAwsLogGroup) => |
| 58 | + logGroupName === logConfiguration?.cloudWatchLogGroupName |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + if (!isEmpty(logGroupsInRegion)) { |
| 63 | + for (const logGroup of logGroupsInRegion) { |
| 64 | + connections.push({ |
| 65 | + id: logGroup.logGroupName, |
| 66 | + resourceType: services.cloudwatchLog, |
| 67 | + relation: 'child', |
| 68 | + field: 'cloudwatchLog', |
| 69 | + }) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Find MKS |
| 75 | + * related to this ecs cluster |
| 76 | + */ |
| 77 | + const kms = data.find(({ name }) => name === services.kms) |
| 78 | + if (kms?.data?.[region]) { |
| 79 | + const kmsInRegion: AwsKms = kms.data[region].find( |
| 80 | + ({ KeyArn }: AwsKms) => KeyArn === kmsKeyId |
| 81 | + ) |
| 82 | + |
| 83 | + if (kmsInRegion) { |
| 84 | + connections.push({ |
| 85 | + id: kmsInRegion.KeyId, |
| 86 | + resourceType: services.kms, |
| 87 | + relation: 'child', |
| 88 | + field: 'kms', |
| 89 | + }) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + const natResult = { |
| 94 | + [arn]: connections, |
| 95 | + } |
| 96 | + return natResult |
| 97 | +} |
0 commit comments