Skip to content

Commit 9dd4b75

Browse files
authored
Merge pull request #11 from cloudgraphdev/feature/CG-1124-add-tencent-vpn-gateway-route
feat(vpnGatewayRoute): add vpnGatewayRoute service
2 parents b4a985a + 17d9777 commit 9dd4b75

13 files changed

Lines changed: 199 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an
6464
| ccnAttachment | ccn |
6565
| subnet | vpc, routeTable |
6666
| vpc | subnet, vpnGateway, routeTable |
67-
| vpnGateway | vpc |
67+
| vpnGateway | vpc, vpnGatewayRoute |
68+
| vpnGatewayRoute | vpnGateway |

src/enums/schemasMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export default {
1313
[services.subnet]: 'tencentSubnet',
1414
[services.vpc]: 'tencentVpc',
1515
[services.vpnGateway]: 'tencentVpnGateway',
16+
[services.vpnGatewayRoute]: 'tencentVpnGatewayRoute',
1617
tag: 'tencentTag',
1718
}

src/enums/serviceAliases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export default {
66
subnet: 'subnets',
77
vpc: 'vpcInstances',
88
vpnGateway: 'vpnGateways',
9+
vpnGatewayRoute: 'vpnGatewayRoutes',
910
}

src/enums/serviceMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import TencentVpc from '../services/vpc'
88
import TencentTag from '../services/tag'
99
import TencentRouteTable from '../services/routeTable'
1010
import TencentVpnGateway from '../services/vpnGateway'
11+
import TencentVpnGatewayRoute from '../services/vpnGatewayRoute'
1112
import TencentCustomerGateway from '../services/customerGateway'
1213

1314
/**
@@ -24,5 +25,6 @@ export default {
2425
[services.subnet]: TencentSubnet,
2526
[services.vpc]: TencentVpc,
2627
[services.vpnGateway]: TencentVpnGateway,
28+
[services.vpnGatewayRoute]: TencentVpnGatewayRoute,
2729
tag: TencentTag,
2830
}

src/enums/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export default {
88
subnet: 'subnet',
99
vpc: 'vpc',
1010
vpnGateway: 'vpnGateway',
11+
vpnGatewayRoute: 'vpnGatewayRoute',
1112
}
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 { RawTencentVpnGateway } from './data'
3+
import services from '../../enums/services'
4+
import aliases from '../../enums/serviceAliases'
5+
import { RawTencentVpnGatewayRoute } from '../vpnGatewayRoute/data'
6+
7+
const serviceName = services.vpnGatewayRoute
8+
9+
export default ({
10+
service,
11+
data,
12+
region,
13+
}: {
14+
service: RawTencentVpnGateway
15+
data: { name: string; data: { [property: string]: any[] } }[]
16+
region: string
17+
}): {
18+
[property: string]: ServiceConnection[]
19+
} => {
20+
const { id } = service
21+
const connections: ServiceConnection[] = []
22+
23+
const vpnGatewayRouteInstances: {
24+
name: string
25+
data: { [region: string]: RawTencentVpnGatewayRoute[] }
26+
} = data.find(({ name }) => name === services.vpnGatewayRoute)
27+
if (vpnGatewayRouteInstances?.data?.[region]) {
28+
for (const vpnGatewayRouteInstanceInstance of vpnGatewayRouteInstances.data[region]) {
29+
if (id === vpnGatewayRouteInstanceInstance.vpnGatewayId) {
30+
connections.push({
31+
id: vpnGatewayRouteInstanceInstance.id,
32+
resourceType: serviceName,
33+
relation: 'child',
34+
field: aliases[serviceName] ? aliases[serviceName] : serviceName,
35+
})
36+
}
37+
}
38+
}
39+
40+
const result = {
41+
[id]: connections,
42+
}
43+
return result
44+
}

src/services/vpnGateway/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { Service } from '@cloudgraph/sdk'
22
import BaseService from '../base'
33
import format from './format'
44
import getData, { serviceName } from './data'
5+
import getConnections from './connections'
56
import { getMutation } from '../../utils'
67

78
export default class TencentVpnGateway extends BaseService implements Service {
89
format = format.bind(this)
910

1011
getData = getData.bind(this)
1112

13+
getConnections = getConnections.bind(this)
14+
1215
mutation = getMutation(serviceName)
1316
}

src/services/vpnGateway/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ type tencentVpnGateway implements tencentBaseService @key(fields: "id") {
3131
cdcId: String @search(by: [hash, regexp])
3232
maxConnection: Int @search
3333
vpcInstances: [tencentVpc] @hasInverse(field: vpnGateways)
34+
vpnGatewayRoutes: [tencentVpnGatewayRoute] @hasInverse(field: vpnGateways)
3435
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { TencentVpnGatewayRoute } from '../../types/generated'
2+
import { RawTencentVpnGatewayRoute } from './data'
3+
4+
export default ({
5+
service,
6+
region,
7+
}: {
8+
service: RawTencentVpnGatewayRoute
9+
region: string
10+
}): TencentVpnGatewayRoute => {
11+
const {
12+
id,
13+
DestinationCidrBlock: destinationCidrBlock,
14+
InstanceType: instanceType,
15+
InstanceId: instanceId,
16+
Priority: priority,
17+
Status: status,
18+
Type: type,
19+
CreateTime: createTime,
20+
UpdateTime: updateTime,
21+
} = service
22+
23+
return {
24+
id,
25+
region,
26+
destinationCidrBlock,
27+
instanceType,
28+
instanceId,
29+
priority,
30+
status,
31+
type,
32+
createTime,
33+
updateTime,
34+
}
35+
}

0 commit comments

Comments
 (0)