|
| 1 | +import * as tencentcloud from 'tencentcloud-sdk-nodejs' |
| 2 | +import { NetworkAcl } 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 | + |
| 11 | +const lt = { ...loggerText } |
| 12 | +const { logger } = CloudGraph |
| 13 | +export const serviceName = 'NetworkAcl' |
| 14 | +const apiEndpoint = initTestEndpoint(serviceName) |
| 15 | + |
| 16 | +export interface RawTencentNetworkAcl extends NetworkAcl { |
| 17 | + id: string |
| 18 | + region: string |
| 19 | + subnets: string[] |
| 20 | +} |
| 21 | + |
| 22 | +export default async ({ |
| 23 | + regions, |
| 24 | + config, |
| 25 | +}: TencentServiceInput): Promise<{ |
| 26 | + [region: string]: RawTencentNetworkAcl[] |
| 27 | +}> => |
| 28 | + new Promise(async resolve => { |
| 29 | + const naclList: RawTencentNetworkAcl[] = [] |
| 30 | + |
| 31 | + for (const region of regions.split(',')) { |
| 32 | + /** |
| 33 | + * Get all NetworkACLs |
| 34 | + */ |
| 35 | + try { |
| 36 | + const VpcClient = tencentcloud.vpc.v20170312.Client |
| 37 | + const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } } |
| 38 | + const vpc = new VpcClient(clientConfig) |
| 39 | + const response = await vpc.DescribeNetworkAcls(null) |
| 40 | + |
| 41 | + if (response && !isEmpty(response.NetworkAclSet)) { |
| 42 | + for (const instance of response.NetworkAclSet) { |
| 43 | + naclList.push({ |
| 44 | + id: instance.NetworkAclId, |
| 45 | + ...instance, |
| 46 | + subnets: instance?.SubnetSet?.map(subnet => subnet.SubnetId), |
| 47 | + region, |
| 48 | + }) |
| 49 | + } |
| 50 | + } |
| 51 | + } catch (error) { |
| 52 | + generateTencentErrorLog(serviceName, 'vpc:DescribeNetworkAcls', error) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + logger.debug(lt.foundResources(serviceName, naclList.length)) |
| 57 | + resolve(groupBy(naclList, 'region')) |
| 58 | + }) |
0 commit comments