Skip to content

Commit

Permalink
feat(customerGateway): add customerGateway service
Browse files Browse the repository at this point in the history
  • Loading branch information
james-zhou-inspire11 committed May 19, 2022
1 parent 9cfc772 commit cdee2e8
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an

| Service | Relations |
| ------------------- | ------------------- |
| customerGateway | |
| routeTable | vpc, subnet |
| securityGroup | |
| securityGroupRule | |
Expand Down
1 change: 1 addition & 0 deletions src/enums/schemasMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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',
Expand Down
2 changes: 2 additions & 0 deletions src/enums/serviceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import TencentVpc from '../services/vpc'
import TencentTag from '../services/tag'
import TencentRouteTable from '../services/routeTable'
import TencentVpnGateway from '../services/vpnGateway'
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,
Expand Down
1 change: 1 addition & 0 deletions src/enums/services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
customerGateway: 'customerGateway',
routeTable: 'routeTable',
securityGroup: 'securityGroup',
securityGroupRule: 'securityGroupRule',
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]
}
7 changes: 7 additions & 0 deletions src/types/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export type TencentCcnAttachment = TencentBaseService & {
state?: Maybe<Scalars['String']>;
};

export type TencentCustomerGateway = TencentBaseService & {
createdTime?: Maybe<Scalars['String']>;
ipAddress?: Maybe<Scalars['String']>;
name?: Maybe<Scalars['String']>;
tags?: Maybe<Array<Maybe<TencentRawTag>>>;
};

export type TencentKeyValue = {
id: Scalars['String'];
key: Scalars['String'];
Expand Down

0 comments on commit cdee2e8

Please sign in to comment.