Skip to content

Commit c0a3a4c

Browse files
authored
Merge pull request #14 from cloudgraphdev/feature/CG-1133-add-vpn-connection
feat(vpnConnection): add vpnConnection service
2 parents 50e31a2 + 6c3fe54 commit c0a3a4c

File tree

13 files changed

+377
-11
lines changed

13 files changed

+377
-11
lines changed

README.md

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

5757
| Service | Relations |
5858
| ------------------- | ------------------- |
59-
| customerGateway | |
59+
| customerGateway | vpnConnection |
6060
| routeTable | vpc, subnet |
6161
| securityGroup | |
6262
| securityGroupRule | |
6363
| ccn | ccnAttachment |
6464
| ccnAttachment | ccn |
6565
| networkAcl | subnet, vpc |
6666
| subnet | networkAcl, vpc, routeTable |
67-
| vpc | networkAcl, subnet, vpnGateway, routeTable |
68-
| vpnGateway | vpc, vpnGatewayRoute |
67+
| vpc | networkAcl, subnet, vpnGateway, routeTable, vpnConnection |
68+
| vpnConnection | vpc, vpnGateway, customerGateway |
69+
| vpnGateway | vpc, vpnGatewayRoute, vpnConnection |
6970
| vpnGatewayRoute | vpnGateway |
7071
| securityGroup | |

src/enums/schemasMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default {
1515
[services.securityGroupRule]: 'tencentSecurityGroupRule',
1616
[services.subnet]: 'tencentSubnet',
1717
[services.vpc]: 'tencentVpc',
18+
[services.vpnConnection]: 'tencentVpnConnection',
1819
[services.vpnGateway]: 'tencentVpnGateway',
1920
[services.vpnGatewayRoute]: 'tencentVpnGatewayRoute',
2021
tag: 'tencentTag',

src/enums/serviceAliases.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
export default {
2-
routeTable: 'routeTables',
3-
securityGroup: 'securityGroups',
42
ccn: 'ccns',
53
ccnAttachment: 'ccnAttachments',
4+
customerGateway: 'customerGateways',
5+
routeTable: 'routeTables',
6+
securityGroup: 'securityGroups',
67
subnet: 'subnets',
78
vpc: 'vpcInstances',
9+
vpnConnection: 'vpnConnections',
810
vpnGateway: 'vpnGateways',
911
vpnGatewayRoute: 'vpnGatewayRoutes',
1012
}

src/enums/serviceMap.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ import TencentRouteTable from '../services/routeTable'
1111
import TencentVpnGateway from '../services/vpnGateway'
1212
import TencentVpnGatewayRoute from '../services/vpnGatewayRoute'
1313
import TencentCustomerGateway from '../services/customerGateway'
14+
import TencentVpnConnection from '../services/vpnConnection'
1415

1516
/**
1617
* serviceMap is an object that contains all currently supported services
1718
* serviceMap is used by the serviceFactory to produce instances of service classes
1819
*/
1920
export default {
20-
[services.customerGateway]: TencentCustomerGateway,
21-
[services.routeTable]: TencentRouteTable,
22-
[services.securityGroup]: TencentSecurityGroup,
23-
[services.securityGroupRule]: TencentSecurityGroupRule,
2421
[services.ccn]: TencentCcn,
2522
[services.ccnAttachment]: TencentCcnAttachment,
23+
[services.customerGateway]: TencentCustomerGateway,
2624
[services.networkAcl]: TencentNetworkAcl,
25+
[services.routeTable]: TencentRouteTable,
2726
[services.securityGroup]: TencentSecurityGroup,
2827
[services.securityGroupRule]: TencentSecurityGroupRule,
2928
[services.subnet]: TencentSubnet,
3029
[services.vpc]: TencentVpc,
30+
[services.vpnConnection]: TencentVpnConnection,
3131
[services.vpnGateway]: TencentVpnGateway,
3232
[services.vpnGatewayRoute]: TencentVpnGatewayRoute,
3333
tag: TencentTag,

src/enums/services.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
export default {
2+
ccn: 'ccn',
3+
ccnAttachment: 'ccnAttachment',
24
customerGateway: 'customerGateway',
35
routeTable: 'routeTable',
46
networkAcl: 'networkAcl',
57
securityGroup: 'securityGroup',
68
securityGroupRule: 'securityGroupRule',
7-
ccn: 'ccn',
8-
ccnAttachment: 'ccnAttachment',
99
subnet: 'subnet',
1010
vpc: 'vpc',
11+
vpnConnection: 'vpnConnection',
1112
vpnGateway: 'vpnGateway',
1213
vpnGatewayRoute: 'vpnGatewayRoute',
1314
}

src/services/customerGateway/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ type tencentCustomerGateway implements tencentBaseService @key(fields: "id") {
33
ipAddress: String @search(by: [hash, regexp])
44
createdTime: String @search(by: [hash, regexp])
55
tags: [tencentRawTag]
6+
vpnConnections: [tencentVpnConnection] @hasInverse(field: customerGateways)
67
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { ServiceConnection } from '@cloudgraph/sdk'
2+
import { RawTencentVpnConnection } from './data'
3+
import services from '../../enums/services'
4+
import aliases from '../../enums/serviceAliases'
5+
import { RawTencentVpnGateway } from '../vpnGateway/data'
6+
import { RawTencentCustomerGateway } from '../customerGateway/data'
7+
8+
export default ({
9+
service,
10+
data,
11+
region,
12+
}: {
13+
service: RawTencentVpnConnection
14+
data: { name: string; data: { [property: string]: any[] } }[]
15+
region: string
16+
}): {
17+
[property: string]: ServiceConnection[]
18+
} => {
19+
const { id } = service
20+
const connections: ServiceConnection[] = []
21+
22+
const vpnGatewayId = service.VpnGatewayId
23+
const vpnGatewayInstances = data.find(({ name }) => name === services.vpnGateway)
24+
25+
if (vpnGatewayInstances?.data?.[region]) {
26+
const instance: RawTencentVpnGateway = vpnGatewayInstances.data[region].find(({id: serviceId}) => serviceId === vpnGatewayId)
27+
connections.push({
28+
id: instance.id,
29+
resourceType: services.vpnGateway,
30+
relation: 'child',
31+
field: aliases[services.vpnGateway]
32+
})
33+
}
34+
35+
const customerGatewayId = service.CustomerGatewayId
36+
const customerGatewayInstances = data.find(({ name }) => name === services.customerGateway)
37+
38+
if (customerGatewayInstances?.data?.[region]) {
39+
const instance: RawTencentCustomerGateway = customerGatewayInstances.data[region]
40+
.find(({id: serviceId}) => serviceId === customerGatewayId)
41+
connections.push({
42+
id: instance.id,
43+
resourceType: services.customerGateway,
44+
relation: 'child',
45+
field: aliases[services.customerGateway]
46+
})
47+
}
48+
49+
const result = {
50+
[id]: connections,
51+
}
52+
return result
53+
}

src/services/vpnConnection/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 { VpnConnection } 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 = 'VpnConnection'
14+
const apiEndpoint = initTestEndpoint(serviceName)
15+
16+
export interface RawTencentVpnConnection extends VpnConnection {
17+
id: string
18+
region: string
19+
}
20+
21+
export default async ({
22+
regions,
23+
config,
24+
}: TencentServiceInput): Promise<{
25+
[region: string]: RawTencentVpnConnection[]
26+
}> =>
27+
new Promise(async resolve => {
28+
const vpnConnectionList: RawTencentVpnConnection[] = []
29+
30+
for (const region of regions.split(',')) {
31+
/**
32+
* Get all the vpn gateways
33+
*/
34+
try {
35+
const VpcClient = tencentcloud.vpc.v20170312.Client
36+
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
37+
const vpc = new VpcClient(clientConfig)
38+
const response = await vpc.DescribeVpnConnections(null)
39+
40+
if (response && !isEmpty(response.VpnConnectionSet)) {
41+
for (const instance of response.VpnConnectionSet) {
42+
vpnConnectionList.push({
43+
id: instance.VpnConnectionId,
44+
...instance,
45+
region,
46+
})
47+
}
48+
}
49+
} catch (error) {
50+
generateTencentErrorLog(serviceName, 'vpc:DescribeVpnConnections', error)
51+
}
52+
}
53+
54+
logger.debug(lt.foundResources(serviceName, vpnConnectionList.length))
55+
resolve(groupBy(vpnConnectionList, 'region'))
56+
})
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import cuid from 'cuid'
2+
import {
3+
IKEOptionsSpecification,
4+
IPSECOptionsSpecification,
5+
} from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models';
6+
import {
7+
TencentVpnConnection,
8+
TencentVpnConnectionIkeOptionsSpecification,
9+
TencentVpnConnectionIpsecOptionsSpecification,
10+
} from '../../types/generated'
11+
import { RawTencentVpnConnection } from './data'
12+
13+
const formatIKEOptionsSpecification = (ikeOptionsSpecification: IKEOptionsSpecification): TencentVpnConnectionIkeOptionsSpecification => {
14+
const {
15+
PropoEncryAlgorithm: propoEncryAlgorithm = '',
16+
PropoAuthenAlgorithm: propoAuthenAlgorithm = '',
17+
ExchangeMode: exchangeMode = '',
18+
LocalIdentity: localIdentity = '',
19+
RemoteIdentity: remoteIdentity = '',
20+
LocalAddress: localAddress = '',
21+
RemoteAddress: remoteAddress = '',
22+
LocalFqdnName: localFqdnName = '',
23+
RemoteFqdnName: remoteFqdnName = '',
24+
DhGroupName: dhGroupName = '',
25+
IKESaLifetimeSeconds: ikeSaLifetimeSeconds = 0,
26+
IKEVersion: ikeVersion = '',
27+
} = ikeOptionsSpecification
28+
29+
return {
30+
propoEncryAlgorithm,
31+
propoAuthenAlgorithm,
32+
exchangeMode,
33+
localIdentity,
34+
remoteIdentity,
35+
localAddress,
36+
remoteAddress,
37+
localFqdnName,
38+
remoteFqdnName,
39+
dhGroupName,
40+
ikeSaLifetimeSeconds,
41+
ikeVersion,
42+
}
43+
}
44+
45+
const formatIPSECOptionsSpecification = (
46+
ipsecOptionsSpecification: IPSECOptionsSpecification,
47+
): TencentVpnConnectionIpsecOptionsSpecification => {
48+
const {
49+
EncryptAlgorithm: encryptAlgorithm = '',
50+
IntegrityAlgorith: integrityAlgorith = '',
51+
IPSECSaLifetimeSeconds: ipsecSaLifetimeSeconds = 0,
52+
PfsDhGroup: pfsDhGroup = '',
53+
IPSECSaLifetimeTraffic: ipsecSaLifetimeTraffic = 0,
54+
} = ipsecOptionsSpecification
55+
56+
return {
57+
encryptAlgorithm,
58+
integrityAlgorith,
59+
ipsecSaLifetimeSeconds,
60+
pfsDhGroup,
61+
ipsecSaLifetimeTraffic,
62+
}
63+
}
64+
65+
export default ({
66+
service,
67+
region,
68+
}: {
69+
service: RawTencentVpnConnection
70+
region: string
71+
}): TencentVpnConnection=> {
72+
const {
73+
id,
74+
VpnConnectionName: name,
75+
PreShareKey: preShareKey,
76+
VpnProto: vpnProto,
77+
EncryptProto: encryptProto,
78+
RouteType: routeType,
79+
CreatedTime: createdTime,
80+
State: state,
81+
NetStatus: netStatus,
82+
SecurityPolicyDatabaseSet = [],
83+
IKEOptionsSpecification: ikeOptionsSpecification = {},
84+
IPSECOptionsSpecification: ipsecOptionsSpecification = {},
85+
EnableHealthCheck: enableHealthCheck,
86+
HealthCheckLocalIp: healthCheckLocalIp,
87+
HealthCheckRemoteIp: healthCheckRemoteIp,
88+
HealthCheckStatus: healthCheckStatus,
89+
} = service
90+
91+
return {
92+
id,
93+
region,
94+
name,
95+
preShareKey,
96+
vpnProto,
97+
encryptProto,
98+
routeType,
99+
createdTime,
100+
state,
101+
netStatus,
102+
securityPolicyDatabaseSet: SecurityPolicyDatabaseSet.map(({
103+
LocalCidrBlock: localCidrBlock,
104+
RemoteCidrBlock: remoteCidrBlock,
105+
}) => {
106+
return {
107+
id: cuid(),
108+
localCidrBlock,
109+
remoteCidrBlock,
110+
}
111+
}),
112+
ikeOptionsSpecification: formatIKEOptionsSpecification(ikeOptionsSpecification),
113+
ipsecOptionsSpecification: formatIPSECOptionsSpecification(ipsecOptionsSpecification),
114+
enableHealthCheck,
115+
healthCheckLocalIp,
116+
healthCheckRemoteIp,
117+
healthCheckStatus,
118+
}
119+
}
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 TencentVpnConnection 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+
}

0 commit comments

Comments
 (0)