Skip to content

Commit cc23796

Browse files
authored
Merge pull request #4 from cloudgraphdev/feature/CG-1117-tencent-route-table
Feature/cg 1117 tencent route table
2 parents 1cd7716 + 76ff05a commit cc23796

File tree

13 files changed

+312
-3
lines changed

13 files changed

+312
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an
5656

5757
| Service | Relations |
5858
| ------------------- | ------------------- |
59+
| routeTable | vpc, subnet |
5960
| securityGroup | |
6061
| securityGroupRule | |
6162
| ccn | ccnAttachment |
6263
| ccnAttachment | ccn |
63-
| subnet | vpc |
64-
| vpc | subnet, vpnGateway |
65-
| vpnGateway | vpc |
64+
| subnet | vpc, routeTable |
65+
| vpc | subnet, vpnGateway, routeTable |
66+
| vpnGateway | vpc |

src/enums/schemasMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import services from './services'
44
* schemasMap is an object that contains schemas name by resource
55
*/
66
export default {
7+
[services.routeTable]: 'tencentRouteTable',
78
[services.securityGroup]: 'tencentSecurityGroup',
89
[services.securityGroupRule]: 'tencentSecurityGroupRule',
910
[services.ccn]: 'tencentCcn',

src/enums/serviceAliases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default {
2+
routeTable: 'routeTables',
23
securityGroup: 'securityGroups',
34
ccn: 'ccns',
45
ccnAttachment: 'ccnAttachments',

src/enums/serviceMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import TencentCcnAttachment from '../services/ccnAttachment'
66
import TencentSubnet from '../services/subnet'
77
import TencentVpc from '../services/vpc'
88
import TencentTag from '../services/tag'
9+
import TencentRouteTable from '../services/routeTable'
910
import TencentVpnGateway from '../services/vpnGateway'
1011

1112
/**
1213
* serviceMap is an object that contains all currently supported services
1314
* serviceMap is used by the serviceFactory to produce instances of service classes
1415
*/
1516
export default {
17+
[services.routeTable]: TencentRouteTable,
1618
[services.securityGroup]: TencentSecurityGroup,
1719
[services.securityGroupRule]: TencentSecurityGroupRule,
1820
[services.ccn]: TencentCcn,

src/enums/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default {
2+
routeTable: 'routeTable',
23
securityGroup: 'securityGroup',
34
securityGroupRule: 'securityGroupRule',
45
ccn: 'ccn',
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ServiceConnection } from '@cloudgraph/sdk'
2+
import { RawTencentRouteTable } 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: RawTencentRouteTable
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 subnetSets = service.AssociationSet.map(({SubnetId}) => SubnetId)
21+
22+
const instances: {
23+
name: string
24+
data: { [property: string]: any[] }
25+
} = data.find(({ name }) => name === services.subnet)
26+
27+
if (instances?.data?.[region]) {
28+
for (const service of instances.data[region]) {
29+
if (subnetSets.includes(service.id)) {
30+
connections.push({
31+
id: service.id,
32+
resourceType: services.subnet,
33+
relation: 'child',
34+
field: aliases[services.subnet]
35+
})
36+
}
37+
}
38+
}
39+
40+
const result = {
41+
[id]: connections,
42+
}
43+
return result
44+
}

src/services/routeTable/data.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
2+
import CloudGraph from '@cloudgraph/sdk'
3+
import groupBy from 'lodash/groupBy'
4+
import isEmpty from 'lodash/isEmpty'
5+
6+
import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface'
7+
import { RouteTable } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
8+
import loggerText from '../../properties/logger'
9+
import { TencentServiceInput } from '../../types'
10+
import { initTestEndpoint, generateTencentErrorLog } from '../../utils'
11+
12+
export const serviceName = 'RouteTable'
13+
14+
const lt = { ...loggerText }
15+
const apiEndpoint = initTestEndpoint(serviceName)
16+
const { logger } = CloudGraph
17+
18+
export interface RawTencentRouteTable extends RouteTable {
19+
id: string
20+
region: string
21+
}
22+
23+
export default async ({
24+
regions,
25+
config,
26+
}: TencentServiceInput): Promise<{
27+
[region: string]: RawTencentRouteTable[]
28+
}> =>
29+
new Promise(async resolve => {
30+
const routeTableList: RawTencentRouteTable[] = []
31+
32+
for (const region of regions.split(',')) {
33+
try {
34+
const VpcClient = tencentcloud.vpc.v20170312.Client
35+
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
36+
const vpc = new VpcClient(clientConfig)
37+
const response = await vpc.DescribeRouteTables({})
38+
39+
if (response && !isEmpty(response.RouteTableSet)) {
40+
for (const instance of response.RouteTableSet) {
41+
routeTableList.push({
42+
id: instance.RouteTableId,
43+
...instance,
44+
region,
45+
})
46+
}
47+
}
48+
} catch (error) {
49+
generateTencentErrorLog(serviceName, 'vpc:DescribeRouteTables', error)
50+
}
51+
}
52+
53+
logger.debug(lt.foundResources(serviceName, routeTableList.length))
54+
55+
resolve(groupBy(routeTableList, 'region'))
56+
})

src/services/routeTable/format.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import cuid from 'cuid'
2+
import { Route } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
3+
import { TencentRouteTable, TencentRouteTableRoute } from '../../types/generated'
4+
import { formatTagSet } from '../../utils/format'
5+
import { RawTencentRouteTable } from './data'
6+
7+
const formatRouteTableRoute = (route: Route): TencentRouteTableRoute => {
8+
const {
9+
DestinationCidrBlock: destinationCidrBlock,
10+
GatewayType: gatewayType,
11+
GatewayId: gatewayId,
12+
RouteId: routeId = 0,
13+
RouteDescription: routeDescription = '',
14+
Enabled: enabled = false,
15+
RouteType: routeType = '',
16+
RouteTableId: routeTableId = '',
17+
DestinationIpv6CidrBlock: destinationIpv6CidrBlock = '',
18+
RouteItemId: routeItemId = '',
19+
PublishedToVbc: publishedToVbc = false,
20+
CreatedTime: createdTime = '',
21+
} = route
22+
23+
return {
24+
id: cuid(),
25+
destinationCidrBlock,
26+
gatewayType,
27+
gatewayId,
28+
routeId,
29+
routeDescription,
30+
enabled,
31+
routeType,
32+
routeTableId,
33+
destinationIpv6CidrBlock,
34+
routeItemId,
35+
publishedToVbc,
36+
createdTime,
37+
}
38+
}
39+
40+
41+
export default ({
42+
service,
43+
region,
44+
}: {
45+
service: RawTencentRouteTable
46+
region: string
47+
}): TencentRouteTable=> {
48+
const {
49+
id,
50+
RouteTableId: routeTableId,
51+
RouteTableName: routeTableName,
52+
AssociationSet = [],
53+
RouteSet = [],
54+
Main: main,
55+
CreatedTime: createdTime = '',
56+
TagSet,
57+
LocalCidrForCcn = [],
58+
} = service
59+
60+
return {
61+
id,
62+
region,
63+
routeTableId,
64+
routeTableName,
65+
associationSet: AssociationSet.map(({SubnetId: subnetId, RouteTableId: associationRouteTableId}) => {
66+
return {
67+
id: cuid(),
68+
subnetId,
69+
routeTableId: associationRouteTableId,
70+
}
71+
}),
72+
routeSet: RouteSet.map(formatRouteTableRoute),
73+
main,
74+
createdTime,
75+
tags: formatTagSet(TagSet),
76+
localCidrForCcn: LocalCidrForCcn.map(({Cidr: cidr, PublishedToVbc: publishedToVbc}) => {
77+
return {
78+
id: cuid(),
79+
cidr,
80+
publishedToVbc,
81+
}
82+
})
83+
}
84+
}

src/services/routeTable/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 TencentRouteTable 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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
type tencentRouteTableAssociation
2+
@generate(
3+
query: { get: false, query: true, aggregate: false }
4+
mutation: { add: false, delete: false }
5+
subscription: false
6+
)
7+
@key(fields: "id") {
8+
id: String! @id
9+
subnetId: String @search(by: [hash, regexp])
10+
routeTableId: String @search(by: [hash, regexp])
11+
}
12+
13+
type tencentRouteTableRoute
14+
@generate(
15+
query: { get: false, query: true, aggregate: false }
16+
mutation: { add: false, delete: false }
17+
subscription: false
18+
)
19+
@key(fields: "id") {
20+
id: String! @id
21+
destinationCidrBlock: String @search(by: [hash, regexp])
22+
gatewayType: String @search(by: [hash, regexp])
23+
gatewayId: String @search(by: [hash, regexp])
24+
routeId: Int @search
25+
routeDescription: String @search(by: [hash, regexp])
26+
enabled: Boolean @search
27+
routeType: String @search(by: [hash, regexp])
28+
routeTableId: String @search(by: [hash, regexp])
29+
destinationIpv6CidrBlock: String @search(by: [hash, regexp])
30+
routeItemId: String @search(by: [hash, regexp])
31+
publishedToVbc: Boolean @search
32+
createdTime: String @search(by: [hash, regexp])
33+
}
34+
35+
type tencentRouteTableLocalCidrForCcnn
36+
@generate(
37+
query: { get: false, query: true, aggregate: false }
38+
mutation: { add: false, delete: false }
39+
subscription: false
40+
)
41+
@key(fields: "id") {
42+
id: String! @id
43+
cidr: String @search(by: [hash, regexp])
44+
publishedToVbc: Boolean @search
45+
}
46+
47+
type tencentRouteTable implements tencentBaseService @key(fields: "id") {
48+
routeTableId: String @search(by: [hash, regexp])
49+
routeTableName: String @search(by: [hash, regexp])
50+
associationSet: [tencentRouteTableAssociation]
51+
routeSet: [tencentRouteTableRoute]
52+
main: Boolean @search
53+
createdTime: String @search(by: [hash, regexp])
54+
tags: [tencentRawTag]
55+
localCidrForCcn: [tencentRouteTableLocalCidrForCcnn]
56+
vpcInstances: [tencentVpc] @hasInverse(field: routeTables)
57+
subnets: [tencentSubnet] @hasInverse(field: routeTables)
58+
}

0 commit comments

Comments
 (0)