Skip to content

Commit d8adeec

Browse files
authored
Merge pull request #7 from cloudgraphdev/feature/CG-1127-tencent-ccn
Feature/cg 1127 tencent ccn
2 parents 762dc79 + 0403c4f commit d8adeec

16 files changed

Lines changed: 352 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an
5656

5757
| Service | Relations |
5858
| ------------------- | ------------------- |
59+
| ccn | ccnAttachment |
60+
| ccnAttachment | ccn |
5961
| subnet | vpc |
6062
| vpc | subnet |

src/enums/schemasMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import services from './services'
44
* schemasMap is an object that contains schemas name by resource
55
*/
66
export default {
7+
[services.ccn]: 'tencentCcn',
8+
[services.ccnAttachment]: 'tencentCcnAttachment',
79
[services.subnet]: 'tencentSubnet',
810
[services.vpc]: 'tencentVpc',
911
tag: 'tencentTag',

src/enums/serviceAliases.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export default {
2+
ccn: 'ccns',
3+
ccnAttachment: 'ccnAttachments',
24
subnet: 'subnets',
35
vpc: 'vpcInstances',
46
}

src/enums/serviceMap.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import services from './services'
2+
import TencentCcn from '../services/ccn'
3+
import TencentCcnAttachment from '../services/ccnAttachment'
24
import TencentSubnet from '../services/subnet'
35
import TencentVpc from '../services/vpc'
46
import TencentTag from '../services/tag'
@@ -8,6 +10,8 @@ import TencentTag from '../services/tag'
810
* serviceMap is used by the serviceFactory to produce instances of service classes
911
*/
1012
export default {
13+
[services.ccn]: TencentCcn,
14+
[services.ccnAttachment]: TencentCcnAttachment,
1115
[services.subnet]: TencentSubnet,
1216
[services.vpc]: TencentVpc,
1317
tag: TencentTag,

src/enums/services.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export default {
2+
ccn: 'ccn',
3+
ccnAttachment: 'ccnAttachment',
24
subnet: 'subnet',
35
vpc: 'vpc',
46
}

src/services/ccn/connections.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ServiceConnection } from '@cloudgraph/sdk'
2+
import { RawTencentCcn } from './data'
3+
import services from '../../enums/services'
4+
import aliases from '../../enums/serviceAliases'
5+
6+
export default ({
7+
service,
8+
data,
9+
region,
10+
}: {
11+
service: RawTencentCcn
12+
data: { name: string; data: { [property: string]: any[] } }[]
13+
region: string
14+
}): {
15+
[property: string]: ServiceConnection[]
16+
} => {
17+
const { id } = service
18+
const connections: ServiceConnection[] = []
19+
20+
const instances: {
21+
name: string
22+
data: { [property: string]: any[] }
23+
} = data.find(({ name }) => name === services.ccnAttachment)
24+
25+
if (instances?.data?.[region]) {
26+
for (const service of instances.data[region]) {
27+
if (id === service.CcnId) {
28+
connections.push({
29+
id: service.id,
30+
resourceType: serviceName,
31+
relation: 'child',
32+
field: aliases[serviceName] ? aliases[serviceName] : serviceName,
33+
})
34+
}
35+
}
36+
}
37+
38+
const result = {
39+
[id]: connections,
40+
}
41+
return result
42+
}

src/services/ccn/data.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
})

src/services/ccn/format.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { TencentCcn } from '../../types/generated'
2+
import { formatTagSet } from '../../utils/format'
3+
import { RawTencentCcn } from './data'
4+
5+
export default ({
6+
service,
7+
region,
8+
}: {
9+
service: RawTencentCcn
10+
region: string
11+
}): TencentCcn => {
12+
const {
13+
id,
14+
CcnName: ccnName,
15+
CcnDescription: ccnDescription,
16+
InstanceCount: instanceCount,
17+
CreateTime: createTime,
18+
State: state,
19+
QosLevel: qosLevel,
20+
InstanceChargeType: instanceChargeType,
21+
BandwidthLimitType: bandwidthLimitType,
22+
TagSet,
23+
RoutePriorityFlag: routePriorityFlag,
24+
RouteTableCount: routeTableCount,
25+
RouteTableFlag: routeTableFlag,
26+
} = service
27+
28+
return {
29+
id,
30+
region,
31+
ccnName,
32+
ccnDescription,
33+
instanceCount,
34+
createTime,
35+
state,
36+
qosLevel,
37+
instanceChargeType,
38+
bandwidthLimitType,
39+
tags: formatTagSet(TagSet),
40+
routePriorityFlag,
41+
routeTableCount,
42+
routeTableFlag,
43+
}
44+
}

src/services/ccn/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Service} from '@cloudgraph/sdk'
2+
import BaseService from '../base'
3+
import format from './format'
4+
import getData, { serviceName } from './data'
5+
import getConnections from './connections'
6+
import { getMutation } from '../../utils'
7+
8+
export default class TencentCcn extends BaseService implements Service {
9+
format = format.bind(this)
10+
11+
getData = getData.bind(this)
12+
13+
getConnections = getConnections.bind(this)
14+
15+
mutation = getMutation(serviceName)
16+
}

src/services/ccn/schema.graphql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type tencentCcn implements tencentBaseService @key(fields: "id") {
2+
ccnName: String @search(by: [hash, regexp])
3+
ccnDescription: String @search(by: [hash, regexp])
4+
instanceCount: Int @search
5+
createTime: String @search(by: [hash, regexp])
6+
state: String @search(by: [hash, regexp])
7+
qosLevel: String @search(by: [hash, regexp])
8+
instanceChargeType: String @search(by: [hash, regexp])
9+
bandwidthLimitType: String @search(by: [hash, regexp])
10+
tags: [tencentRawTag]
11+
routePriorityFlag: Boolean @search
12+
routeTableCount: Int @search
13+
routeTableFlag: Boolean @search
14+
ccnAttachments: [tencentCcnAttachment] @hasInverse(field: ccns)
15+
}

0 commit comments

Comments
 (0)