|
| 1 | +import CloudGraph from '@cloudgraph/sdk' |
| 2 | +import APIGW, { |
| 3 | + ApiMapping, |
| 4 | + DomainName, |
| 5 | + GetDomainNamesRequest, |
| 6 | + GetDomainNamesResponse, |
| 7 | + GetApiMappingsRequest, |
| 8 | + GetApiMappingsResponse, |
| 9 | +} from 'aws-sdk/clients/apigatewayv2' |
| 10 | +import { AWSError } from 'aws-sdk/lib/error' |
| 11 | +import { Config } from 'aws-sdk/lib/config' |
| 12 | +import isEmpty from 'lodash/isEmpty' |
| 13 | +import groupBy from 'lodash/groupBy' |
| 14 | +import awsLoggerText from '../../properties/logger' |
| 15 | +import { initTestEndpoint, setAwsRetryOptions } from '../../utils' |
| 16 | +import AwsErrorLog from '../../utils/errorLog' |
| 17 | +import { API_GATEWAY_CUSTOM_DELAY } from '../../config/constants' |
| 18 | +import { TagMap } from '../../types' |
| 19 | + |
| 20 | +const lt = { ...awsLoggerText } |
| 21 | +const { logger } = CloudGraph |
| 22 | +const MAX_DOMAIN_NAMES = '500' |
| 23 | +const serviceName = 'API Gateway Domain Name' |
| 24 | +const errorLog = new AwsErrorLog(serviceName) |
| 25 | +const endpoint = initTestEndpoint(serviceName) |
| 26 | +const customRetrySettings = setAwsRetryOptions({ |
| 27 | + baseDelay: API_GATEWAY_CUSTOM_DELAY, |
| 28 | +}) |
| 29 | + |
| 30 | +export const getDomainNamesForRegion = async ( |
| 31 | + apiGw: APIGW |
| 32 | +): Promise<DomainName[]> => |
| 33 | + new Promise(async resolve => { |
| 34 | + const domainNameList: DomainName[] = [] |
| 35 | + const getDomainNamesOpts: GetDomainNamesRequest = {} |
| 36 | + const listAllDomainNames = (token?: string): void => { |
| 37 | + getDomainNamesOpts.MaxResults = MAX_DOMAIN_NAMES |
| 38 | + if (token) { |
| 39 | + getDomainNamesOpts.NextToken = token |
| 40 | + } |
| 41 | + try { |
| 42 | + apiGw.getDomainNames( |
| 43 | + getDomainNamesOpts, |
| 44 | + (err: AWSError, data: GetDomainNamesResponse) => { |
| 45 | + if (err) { |
| 46 | + errorLog.generateAwsErrorLog({ |
| 47 | + functionName: 'apiGw:getDomainNames', |
| 48 | + err, |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + if (isEmpty(data)) { |
| 53 | + return resolve([]) |
| 54 | + } |
| 55 | + |
| 56 | + const { NextToken: nextToken, Items: items = [] } = data || {} |
| 57 | + |
| 58 | + if (isEmpty(items)) { |
| 59 | + return resolve([]) |
| 60 | + } |
| 61 | + |
| 62 | + logger.debug(lt.fetchedApiGwDomainNames(items.length)) |
| 63 | + |
| 64 | + domainNameList.push(...items) |
| 65 | + |
| 66 | + if (nextToken) { |
| 67 | + listAllDomainNames(nextToken) |
| 68 | + } else { |
| 69 | + resolve(domainNameList) |
| 70 | + } |
| 71 | + } |
| 72 | + ) |
| 73 | + } catch (error) { |
| 74 | + resolve([]) |
| 75 | + } |
| 76 | + } |
| 77 | + listAllDomainNames() |
| 78 | + }) |
| 79 | + |
| 80 | +const getAPIMappings = ( |
| 81 | + apiGw: APIGW, |
| 82 | + domainName: string |
| 83 | +): Promise<{ domainName: string; apiMappings: ApiMapping[] }> => |
| 84 | + new Promise<{ domainName: string; apiMappings: ApiMapping[] }>(resolve => { |
| 85 | + const apiMappingList: ApiMapping[] = [] |
| 86 | + const args: GetApiMappingsRequest = { DomainName: domainName } |
| 87 | + const listAllAPIMappings = (token?: string): void => { |
| 88 | + if (token) { |
| 89 | + args.NextToken = token |
| 90 | + } |
| 91 | + try { |
| 92 | + apiGw.getApiMappings( |
| 93 | + args, |
| 94 | + (err: AWSError, data: GetApiMappingsResponse) => { |
| 95 | + if (err) { |
| 96 | + errorLog.generateAwsErrorLog({ |
| 97 | + functionName: 'apiGw:getApiMappings', |
| 98 | + err, |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + if (isEmpty(data)) { |
| 103 | + return resolve({ |
| 104 | + domainName, |
| 105 | + apiMappings: [], |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + const { NextToken: nextToken, Items: apiMappings = [] } = data || {} |
| 110 | + |
| 111 | + if (isEmpty(apiMappings)) { |
| 112 | + return resolve({ |
| 113 | + domainName, |
| 114 | + apiMappings: [], |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + apiMappingList.push(...apiMappings) |
| 119 | + |
| 120 | + if (nextToken) { |
| 121 | + listAllAPIMappings(nextToken) |
| 122 | + } else { |
| 123 | + resolve({ domainName, apiMappings: apiMappingList }) |
| 124 | + } |
| 125 | + } |
| 126 | + ) |
| 127 | + } catch (error) { |
| 128 | + resolve({ |
| 129 | + domainName, |
| 130 | + apiMappings: [], |
| 131 | + }) |
| 132 | + } |
| 133 | + } |
| 134 | + listAllAPIMappings() |
| 135 | + }) |
| 136 | + |
| 137 | +export interface RawAwsApiGatewayDomainName extends Omit<DomainName, 'tags'> { |
| 138 | + region: string |
| 139 | + Tags: TagMap |
| 140 | + ApiMappings: ApiMapping[] |
| 141 | + account |
| 142 | +} |
| 143 | + |
| 144 | +export default async ({ |
| 145 | + regions, |
| 146 | + config, |
| 147 | + account, |
| 148 | +}: { |
| 149 | + account: string |
| 150 | + regions: string |
| 151 | + config: Config |
| 152 | +}): Promise<{ |
| 153 | + [region: string]: RawAwsApiGatewayDomainName[] |
| 154 | +}> => |
| 155 | + new Promise(async resolve => { |
| 156 | + const domainNamesResult: RawAwsApiGatewayDomainName[] = [] |
| 157 | + |
| 158 | + const regionPromises = regions.split(',').map(region => { |
| 159 | + const apiGw = new APIGW({ |
| 160 | + ...config, |
| 161 | + region, |
| 162 | + endpoint, |
| 163 | + ...customRetrySettings, |
| 164 | + }) |
| 165 | + |
| 166 | + return new Promise<void>(async resolveTransitGatewayData => { |
| 167 | + // Get Custom Domains Data |
| 168 | + const customDomains = await getDomainNamesForRegion(apiGw) |
| 169 | + |
| 170 | + const mappingPromises = customDomains.map( |
| 171 | + ({ DomainName: domainName }) => getAPIMappings(apiGw, domainName) |
| 172 | + ) |
| 173 | + |
| 174 | + const mappingData = await Promise.all(mappingPromises) |
| 175 | + |
| 176 | + if (!isEmpty(customDomains)) { |
| 177 | + for (const domain of customDomains) { |
| 178 | + domainNamesResult.push({ |
| 179 | + ...domain, |
| 180 | + ApiMappings: |
| 181 | + mappingData?.find(m => m.domainName === domain.DomainName) |
| 182 | + ?.apiMappings || [], |
| 183 | + region, |
| 184 | + Tags: domain.Tags, |
| 185 | + account, |
| 186 | + }) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + resolveTransitGatewayData() |
| 191 | + }) |
| 192 | + }) |
| 193 | + |
| 194 | + await Promise.all(regionPromises) |
| 195 | + errorLog.reset() |
| 196 | + |
| 197 | + resolve(groupBy(domainNamesResult, 'region')) |
| 198 | + }) |
0 commit comments