|
| 1 | +import CloudGraph from '@cloudgraph/sdk' |
| 2 | +import groupBy from 'lodash/groupBy' |
| 3 | +import isEmpty from 'lodash/isEmpty' |
| 4 | + |
| 5 | +import EC2, { |
| 6 | + DeliveryChannel, |
| 7 | + DeliveryChannelList, |
| 8 | + DescribeDeliveryChannelsResponse, |
| 9 | +} from 'aws-sdk/clients/configservice' |
| 10 | + |
| 11 | +import { Config } from 'aws-sdk/lib/config' |
| 12 | +import { AWSError } from 'aws-sdk/lib/error' |
| 13 | + |
| 14 | +import { initTestEndpoint } from '../../utils' |
| 15 | +import AwsErrorLog from '../../utils/errorLog' |
| 16 | +import awsLoggerText from '../../properties/logger' |
| 17 | + |
| 18 | +const lt = { ...awsLoggerText } |
| 19 | +const { logger } = CloudGraph |
| 20 | +const serviceName = 'Configuration Delivery Channel' |
| 21 | +const errorLog = new AwsErrorLog(serviceName) |
| 22 | +const endpoint = initTestEndpoint(serviceName) |
| 23 | + |
| 24 | +export interface RawAwsConfigurationDeliveryChannel extends DeliveryChannel { |
| 25 | + region: string |
| 26 | +} |
| 27 | + |
| 28 | +const listConfigurationDeliveryChannelData = async ({ |
| 29 | + ec2, |
| 30 | +}: { |
| 31 | + ec2: EC2 |
| 32 | +}): Promise<DeliveryChannelList> => |
| 33 | + new Promise<DeliveryChannelList>(resolve => { |
| 34 | + try { |
| 35 | + ec2.describeDeliveryChannels( |
| 36 | + {}, |
| 37 | + (err: AWSError, data: DescribeDeliveryChannelsResponse) => { |
| 38 | + if (err) { |
| 39 | + errorLog.generateAwsErrorLog({ |
| 40 | + functionName: 'ec2:describeDeliveryChannels', |
| 41 | + err, |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + if (isEmpty(data)) { |
| 46 | + return resolve([]) |
| 47 | + } |
| 48 | + |
| 49 | + const { DeliveryChannels: channels = [] } = data || {} |
| 50 | + |
| 51 | + resolve(channels) |
| 52 | + } |
| 53 | + ) |
| 54 | + } catch (error) { |
| 55 | + resolve([]) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | +export default async ({ |
| 60 | + regions, |
| 61 | + config, |
| 62 | +}: { |
| 63 | + regions: string |
| 64 | + config: Config |
| 65 | +}): Promise<{ |
| 66 | + [region: string]: RawAwsConfigurationDeliveryChannel[] |
| 67 | +}> => |
| 68 | + new Promise(async resolve => { |
| 69 | + const configurationDeliveryChannelsResult: RawAwsConfigurationDeliveryChannel[] = |
| 70 | + [] |
| 71 | + |
| 72 | + const regionPromises = regions.split(',').map(region => { |
| 73 | + const ec2 = new EC2({ ...config, region, endpoint }) |
| 74 | + |
| 75 | + return new Promise<void>( |
| 76 | + async resolveConfigurationDeliveryChannelData => { |
| 77 | + // Get Configuration Delivery Channel Data |
| 78 | + const configurationDeliveryChannels = |
| 79 | + await listConfigurationDeliveryChannelData({ |
| 80 | + ec2, |
| 81 | + }) |
| 82 | + |
| 83 | + if (!isEmpty(configurationDeliveryChannels)) { |
| 84 | + for (const configurationDeliveryChannel of configurationDeliveryChannels) { |
| 85 | + configurationDeliveryChannelsResult.push({ |
| 86 | + ...configurationDeliveryChannel, |
| 87 | + region, |
| 88 | + }) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + resolveConfigurationDeliveryChannelData() |
| 93 | + } |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + await Promise.all(regionPromises) |
| 98 | + logger.debug( |
| 99 | + lt.fetchedCOnfigurationDeliveryChannels( |
| 100 | + configurationDeliveryChannelsResult.length |
| 101 | + ) |
| 102 | + ) |
| 103 | + errorLog.reset() |
| 104 | + |
| 105 | + resolve(groupBy(configurationDeliveryChannelsResult, 'region')) |
| 106 | + }) |
0 commit comments