Skip to content

Commit

Permalink
feat(gatewayRoute): merge alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
james-zhou-inspire11 committed May 24, 2022
2 parents ba42cbe + b4a985a commit 17d9777
Show file tree
Hide file tree
Showing 19 changed files with 449 additions and 4 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# [1.0.0-alpha.3](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/compare/1.0.0-alpha.2...1.0.0-alpha.3) (2022-05-19)


### Features

* **customerGateway:** add customerGateway service ([cdee2e8](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/commit/cdee2e85baa41ce5364051641345af0563e2601f))

# [1.0.0-alpha.2](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/compare/1.0.0-alpha.1...1.0.0-alpha.2) (2022-05-18)


### Features

* **routeTable:** add connection from routeTable to subnet ([6a34907](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/commit/6a3490754dc5da4aa3ebe68072b2f6fe5063130b))
* **routeTable:** add routeTable ([112f1d5](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/commit/112f1d561208e122b36b51e842f2802cf1ee76be))
* **routeTable:** add routeTable service ([76ff05a](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/commit/76ff05ac8cb7cad4ccdf68b5d74633248a350aa8))
* **routeTable:** update README ([f32ffec](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/commit/f32ffecf3e67551826fae5025b3c2bf0e2a1feb4))
* **routeTable:** update README ([3fd9c90](/~https://github.com/cloudgraphdev/cloudgraph-provider-tencent/commit/3fd9c9080b6ee007ec598b86b108f301e34e41af))

# 1.0.0-alpha.1 (2022-05-16)


Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an

| Service | Relations |
| ------------------- | ------------------- |
| customerGateway | |
| routeTable | vpc, subnet |
| securityGroup | |
| securityGroupRule | |
| ccn | ccnAttachment |
| ccnAttachment | ccn |
| subnet | vpc |
| vpc | subnet, vpnGateway |
| subnet | vpc, routeTable |
| vpc | subnet, vpnGateway, routeTable |
| vpnGateway | vpc, vpnGatewayRoute |
| vpnGatewayRoute | vpnGateway |
| vpnGatewayRoute | vpnGateway |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudgraph/cg-provider-tencent",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.3",
"description": "CloudGraph provider plugin for Tencent Cloud used to fetch Tencent Cloud data.",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
2 changes: 2 additions & 0 deletions src/enums/schemasMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import services from './services'
* schemasMap is an object that contains schemas name by resource
*/
export default {
[services.customerGateway]: 'tencentCustomerGateway',
[services.routeTable]: 'tencentRouteTable',
[services.securityGroup]: 'tencentSecurityGroup',
[services.securityGroupRule]: 'tencentSecurityGroupRule',
[services.ccn]: 'tencentCcn',
Expand Down
1 change: 1 addition & 0 deletions src/enums/serviceAliases.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
routeTable: 'routeTables',
securityGroup: 'securityGroups',
ccn: 'ccns',
ccnAttachment: 'ccnAttachments',
Expand Down
4 changes: 4 additions & 0 deletions src/enums/serviceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import TencentCcnAttachment from '../services/ccnAttachment'
import TencentSubnet from '../services/subnet'
import TencentVpc from '../services/vpc'
import TencentTag from '../services/tag'
import TencentRouteTable from '../services/routeTable'
import TencentVpnGateway from '../services/vpnGateway'
import TencentVpnGatewayRoute from '../services/vpnGatewayRoute'
import TencentCustomerGateway from '../services/customerGateway'

/**
* serviceMap is an object that contains all currently supported services
* serviceMap is used by the serviceFactory to produce instances of service classes
*/
export default {
[services.customerGateway]: TencentCustomerGateway,
[services.routeTable]: TencentRouteTable,
[services.securityGroup]: TencentSecurityGroup,
[services.securityGroupRule]: TencentSecurityGroupRule,
[services.ccn]: TencentCcn,
Expand Down
2 changes: 2 additions & 0 deletions src/enums/services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
customerGateway: 'customerGateway',
routeTable: 'routeTable',
securityGroup: 'securityGroup',
securityGroupRule: 'securityGroupRule',
ccn: 'ccn',
Expand Down
59 changes: 59 additions & 0 deletions src/services/customerGateway/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
import { CustomerGateway, Tag } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface'
import CloudGraph from '@cloudgraph/sdk'
import groupBy from 'lodash/groupBy'
import isEmpty from 'lodash/isEmpty'
import loggerText from '../../properties/logger'
import { TencentServiceInput } from '../../types'
import { initTestEndpoint, generateTencentErrorLog } from '../../utils'

const lt = { ...loggerText }
const { logger } = CloudGraph
export const serviceName = 'CustomerGateway'
const apiEndpoint = initTestEndpoint(serviceName)

export interface RawTencentCustomerGateway extends CustomerGateway {
id: string
region: string
TagSet: Array<Tag>
}

export default async ({
regions,
config,
}: TencentServiceInput): Promise<{
[region: string]: RawTencentCustomerGateway[]
}> =>
new Promise(async resolve => {
const customerGatewayList: RawTencentCustomerGateway[] = []

for (const region of regions.split(',')) {
/**
* Get all the vpn gateways
*/
try {
const VpcClient = tencentcloud.vpc.v20170312.Client
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
const vpc = new VpcClient(clientConfig)
const response = await vpc.DescribeCustomerGateways(null)

if (response && !isEmpty(response.CustomerGatewaySet)) {
for (const instance of response.CustomerGatewaySet) {
customerGatewayList.push({
id: instance.CustomerGatewayId,
...instance,
region,
TagSet: (instance as any).TagSet,
})
}
}

} catch (error) {
generateTencentErrorLog(serviceName, 'vpc:DescribeCustomerGateways', error)
}
}

logger.debug(lt.foundResources(serviceName, customerGatewayList.length))
resolve(groupBy(customerGatewayList, 'region'))
})
28 changes: 28 additions & 0 deletions src/services/customerGateway/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TencentCustomerGateway } from '../../types/generated'
import { formatTagSet } from '../../utils/format'
import { RawTencentCustomerGateway } from './data'

export default ({
service,
region,
}: {
service: RawTencentCustomerGateway
region: string
}): TencentCustomerGateway=> {
const {
id,
CustomerGatewayName: name,
IpAddress: ipAddress,
CreatedTime: createdTime,
TagSet,
} = service

return {
id,
region,
name,
ipAddress,
createdTime,
tags: formatTagSet(TagSet),
}
}
13 changes: 13 additions & 0 deletions src/services/customerGateway/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Service } from '@cloudgraph/sdk'
import BaseService from '../base'
import format from './format'
import getData, { serviceName } from './data'
import { getMutation } from '../../utils'

export default class TencentCustomerGateway extends BaseService implements Service {
format = format.bind(this)

getData = getData.bind(this)

mutation = getMutation(serviceName)
}
6 changes: 6 additions & 0 deletions src/services/customerGateway/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type tencentCustomerGateway implements tencentBaseService @key(fields: "id") {
name: String @search(by: [hash, regexp])
ipAddress: String @search(by: [hash, regexp])
createdTime: String @search(by: [hash, regexp])
tags: [tencentRawTag]
}
44 changes: 44 additions & 0 deletions src/services/routeTable/connections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ServiceConnection } from '@cloudgraph/sdk'
import { RawTencentRouteTable } from './data'
import services from '../../enums/services'
import aliases from '../../enums/serviceAliases'

export default ({
service,
data,
region,
}: {
service: RawTencentRouteTable
data: { name: string; data: { [property: string]: any[] } }[]
region: string
}): {
[property: string]: ServiceConnection[]
} => {
const { id } = service
const connections: ServiceConnection[] = []

const subnetSets = service.AssociationSet.map(({SubnetId}) => SubnetId)

const instances: {
name: string
data: { [property: string]: any[] }
} = data.find(({ name }) => name === services.subnet)

if (instances?.data?.[region]) {
for (const service of instances.data[region]) {
if (subnetSets.includes(service.id)) {
connections.push({
id: service.id,
resourceType: services.subnet,
relation: 'child',
field: aliases[services.subnet]
})
}
}
}

const result = {
[id]: connections,
}
return result
}
56 changes: 56 additions & 0 deletions src/services/routeTable/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
import CloudGraph from '@cloudgraph/sdk'
import groupBy from 'lodash/groupBy'
import isEmpty from 'lodash/isEmpty'

import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface'
import { RouteTable } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
import loggerText from '../../properties/logger'
import { TencentServiceInput } from '../../types'
import { initTestEndpoint, generateTencentErrorLog } from '../../utils'

export const serviceName = 'RouteTable'

const lt = { ...loggerText }
const apiEndpoint = initTestEndpoint(serviceName)
const { logger } = CloudGraph

export interface RawTencentRouteTable extends RouteTable {
id: string
region: string
}

export default async ({
regions,
config,
}: TencentServiceInput): Promise<{
[region: string]: RawTencentRouteTable[]
}> =>
new Promise(async resolve => {
const routeTableList: RawTencentRouteTable[] = []

for (const region of regions.split(',')) {
try {
const VpcClient = tencentcloud.vpc.v20170312.Client
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
const vpc = new VpcClient(clientConfig)
const response = await vpc.DescribeRouteTables({})

if (response && !isEmpty(response.RouteTableSet)) {
for (const instance of response.RouteTableSet) {
routeTableList.push({
id: instance.RouteTableId,
...instance,
region,
})
}
}
} catch (error) {
generateTencentErrorLog(serviceName, 'vpc:DescribeRouteTables', error)
}
}

logger.debug(lt.foundResources(serviceName, routeTableList.length))

resolve(groupBy(routeTableList, 'region'))
})
84 changes: 84 additions & 0 deletions src/services/routeTable/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import cuid from 'cuid'
import { Route } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
import { TencentRouteTable, TencentRouteTableRoute } from '../../types/generated'
import { formatTagSet } from '../../utils/format'
import { RawTencentRouteTable } from './data'

const formatRouteTableRoute = (route: Route): TencentRouteTableRoute => {
const {
DestinationCidrBlock: destinationCidrBlock,
GatewayType: gatewayType,
GatewayId: gatewayId,
RouteId: routeId = 0,
RouteDescription: routeDescription = '',
Enabled: enabled = false,
RouteType: routeType = '',
RouteTableId: routeTableId = '',
DestinationIpv6CidrBlock: destinationIpv6CidrBlock = '',
RouteItemId: routeItemId = '',
PublishedToVbc: publishedToVbc = false,
CreatedTime: createdTime = '',
} = route

return {
id: cuid(),
destinationCidrBlock,
gatewayType,
gatewayId,
routeId,
routeDescription,
enabled,
routeType,
routeTableId,
destinationIpv6CidrBlock,
routeItemId,
publishedToVbc,
createdTime,
}
}


export default ({
service,
region,
}: {
service: RawTencentRouteTable
region: string
}): TencentRouteTable=> {
const {
id,
RouteTableId: routeTableId,
RouteTableName: routeTableName,
AssociationSet = [],
RouteSet = [],
Main: main,
CreatedTime: createdTime = '',
TagSet,
LocalCidrForCcn = [],
} = service

return {
id,
region,
routeTableId,
routeTableName,
associationSet: AssociationSet.map(({SubnetId: subnetId, RouteTableId: associationRouteTableId}) => {
return {
id: cuid(),
subnetId,
routeTableId: associationRouteTableId,
}
}),
routeSet: RouteSet.map(formatRouteTableRoute),
main,
createdTime,
tags: formatTagSet(TagSet),
localCidrForCcn: LocalCidrForCcn.map(({Cidr: cidr, PublishedToVbc: publishedToVbc}) => {
return {
id: cuid(),
cidr,
publishedToVbc,
}
})
}
}
Loading

0 comments on commit 17d9777

Please sign in to comment.