1+ import * as tencentcloud from 'tencentcloud-sdk-nodejs'
2+ import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface'
3+ import CloudGraph from '@cloudgraph/sdk'
4+ import groupBy from 'lodash/groupBy'
5+ import isEmpty from 'lodash/isEmpty'
6+ import { CCN } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
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 = 'Ccn'
14+ const apiEndpoint = initTestEndpoint ( serviceName )
15+
16+ export interface RawTencentCcn extends CCN {
17+ id : string
18+ region : string
19+ }
20+
21+ export default async ( {
22+ regions,
23+ config,
24+ } : TencentServiceInput ) : Promise < {
25+ [ region : string ] : RawTencentCcn [ ]
26+ } > =>
27+ new Promise ( async resolve => {
28+ const ccnList : RawTencentCcn [ ] = [ ]
29+
30+ for ( const region of regions . split ( ',' ) ) {
31+ try {
32+ const VpcClient = tencentcloud . vpc . v20170312 . Client
33+ const clientConfig : ClientConfig = { credential : config , region, profile : { httpProfile : { endpoint : apiEndpoint } } }
34+ const vpc = new VpcClient ( clientConfig )
35+ const response = await vpc . DescribeCcns ( null )
36+
37+ if ( response && ! isEmpty ( response . CcnSet ) ) {
38+ for ( const instance of response . CcnSet ) {
39+ ccnList . push ( {
40+ id : instance . CcnId ,
41+ ...instance ,
42+ region,
43+ } )
44+ }
45+ }
46+
47+ } catch ( error ) {
48+ generateTencentErrorLog ( serviceName , 'vpc:DescribeCcns' , error )
49+ }
50+ }
51+
52+ logger . debug ( lt . foundResources ( serviceName , ccnList . length ) )
53+ resolve ( groupBy ( ccnList , 'region' ) )
54+ } )
0 commit comments