|
| 1 | +import * as tencentcloud from 'tencentcloud-sdk-nodejs' |
| 2 | +import { VpnGatewayRoute } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models' |
| 3 | +import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface' |
| 4 | +import CloudGraph from '@cloudgraph/sdk' |
| 5 | +import groupBy from 'lodash/groupBy' |
| 6 | +import isEmpty from 'lodash/isEmpty' |
| 7 | +import loggerText from '../../properties/logger' |
| 8 | +import { TencentServiceInput } from '../../types' |
| 9 | +import { initTestEndpoint, generateTencentErrorLog } from '../../utils' |
| 10 | +import services from '../../enums/services' |
| 11 | +import { RawTencentVpnGateway } from '../vpnGateway/data' |
| 12 | + |
| 13 | +const lt = { ...loggerText } |
| 14 | +const { logger } = CloudGraph |
| 15 | +export const serviceName = 'VpnGatewayRoute' |
| 16 | +const apiEndpoint = initTestEndpoint(serviceName) |
| 17 | + |
| 18 | +export interface RawTencentVpnGatewayRoute extends VpnGatewayRoute { |
| 19 | + id: string |
| 20 | + vpnGatewayId: string |
| 21 | + region: string |
| 22 | +} |
| 23 | + |
| 24 | +export default async ({ |
| 25 | + regions, |
| 26 | + config, |
| 27 | + rawData, |
| 28 | +}: TencentServiceInput): Promise<{ |
| 29 | + [region: string]: RawTencentVpnGatewayRoute[] |
| 30 | +}> => |
| 31 | + new Promise(async resolve => { |
| 32 | + const vpnGatewayRouteList: RawTencentVpnGatewayRoute[] = [] |
| 33 | + |
| 34 | + const allVpnGateways: RawTencentVpnGateway[] = |
| 35 | + rawData.find(({ name }) => name === services.vpnGateway)?.data |
| 36 | + |
| 37 | + for (const region of regions.split(',')) { |
| 38 | + const vpnGatewaysInRegion = allVpnGateways[region] || [] |
| 39 | + |
| 40 | + for (const vpnGateway of vpnGatewaysInRegion) { |
| 41 | + const vpnGatewayId = vpnGateway.id |
| 42 | + |
| 43 | + /** |
| 44 | + * Get all the VPN Gateway Route |
| 45 | + */ |
| 46 | + try { |
| 47 | + const VpcClient = tencentcloud.vpc.v20170312.Client |
| 48 | + const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } } |
| 49 | + const vpc = new VpcClient(clientConfig) |
| 50 | + const response = await vpc.DescribeVpnGatewayRoutes({ |
| 51 | + VpnGatewayId: vpnGatewayId, |
| 52 | + }) |
| 53 | + |
| 54 | + if (response && !isEmpty(response.Routes)) { |
| 55 | + for (const instance of response.Routes) { |
| 56 | + vpnGatewayRouteList.push({ |
| 57 | + id: instance.RouteId, |
| 58 | + vpnGatewayId, |
| 59 | + ...instance, |
| 60 | + region, |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + } catch (error) { |
| 65 | + generateTencentErrorLog(serviceName, 'vpc:DescribeVpnGatewayRoutes', error) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + logger.debug(lt.foundResources(serviceName, vpnGatewayRouteList.length)) |
| 71 | + resolve(groupBy(vpnGatewayRouteList, 'region')) |
| 72 | + }) |
0 commit comments