Skip to content

Commit 112f1d5

Browse files
feat(routeTable): add routeTable
1 parent 762dc79 commit 112f1d5

11 files changed

Lines changed: 265 additions & 0 deletions

File tree

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.subnet]: 'tencentSubnet',
89
[services.vpc]: 'tencentVpc',
910
tag: 'tencentTag',

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
subnet: 'subnets',
34
vpc: 'vpcInstances',
45
}

src/enums/serviceMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import services from './services'
22
import TencentSubnet from '../services/subnet'
33
import TencentVpc from '../services/vpc'
44
import TencentTag from '../services/tag'
5+
import TencentRouteTable from '../services/routeTable'
56

67
/**
78
* serviceMap is an object that contains all currently supported services
89
* serviceMap is used by the serviceFactory to produce instances of service classes
910
*/
1011
export default {
12+
[services.routeTable]: TencentRouteTable,
1113
[services.subnet]: TencentSubnet,
1214
[services.vpc]: TencentVpc,
1315
tag: TencentTag,

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
subnet: 'subnet',
34
vpc: 'vpc',
45
}

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Service } from '@cloudgraph/sdk'
2+
import BaseService from '../base'
3+
import format from './format'
4+
import getData, { serviceName } from './data'
5+
import { getMutation } from '../../utils'
6+
7+
export default class TencentRouteTable extends BaseService implements Service {
8+
format = format.bind(this)
9+
10+
getData = getData.bind(this)
11+
12+
mutation = getMutation(serviceName)
13+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

src/services/vpc/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type tencentVpc implements tencentBaseService @key(fields: "id") {
1212
tags: [tencentRawTag]
1313
assistantCidrSet: [tencentVpcAssistantCidr]
1414
subnets: [tencentSubnet] @hasInverse(field: vpcInstances)
15+
routeTables: [tencentRouteTable] @hasInverse(field: vpcInstances)
1516
}
1617

1718
type tencentVpcAssistantCidr

src/types/generated.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,46 @@ export type TencentRawTag = {
2929
value?: Maybe<Scalars['String']>;
3030
};
3131

32+
export type TencentRouteTable = TencentBaseService & {
33+
associationSet?: Maybe<Array<Maybe<TencentRouteTableAssociation>>>;
34+
createdTime?: Maybe<Scalars['String']>;
35+
localCidrForCcn?: Maybe<Array<Maybe<TencentRouteTableLocalCidrForCcnn>>>;
36+
main?: Maybe<Scalars['Boolean']>;
37+
routeSet?: Maybe<Array<Maybe<TencentRouteTableRoute>>>;
38+
routeTableId?: Maybe<Scalars['String']>;
39+
routeTableName?: Maybe<Scalars['String']>;
40+
tags?: Maybe<Array<Maybe<TencentRawTag>>>;
41+
vpcInstances?: Maybe<Array<Maybe<TencentVpc>>>;
42+
};
43+
44+
export type TencentRouteTableAssociation = {
45+
id: Scalars['String'];
46+
routeTableId?: Maybe<Scalars['String']>;
47+
subnetId?: Maybe<Scalars['String']>;
48+
};
49+
50+
export type TencentRouteTableLocalCidrForCcnn = {
51+
cidr?: Maybe<Scalars['String']>;
52+
id: Scalars['String'];
53+
publishedToVbc?: Maybe<Scalars['Boolean']>;
54+
};
55+
56+
export type TencentRouteTableRoute = {
57+
createdTime?: Maybe<Scalars['String']>;
58+
destinationCidrBlock?: Maybe<Scalars['String']>;
59+
destinationIpv6CidrBlock?: Maybe<Scalars['String']>;
60+
enabled?: Maybe<Scalars['Boolean']>;
61+
gatewayId?: Maybe<Scalars['String']>;
62+
gatewayType?: Maybe<Scalars['String']>;
63+
id: Scalars['String'];
64+
publishedToVbc?: Maybe<Scalars['Boolean']>;
65+
routeDescription?: Maybe<Scalars['String']>;
66+
routeId?: Maybe<Scalars['Int']>;
67+
routeItemId?: Maybe<Scalars['String']>;
68+
routeTableId?: Maybe<Scalars['String']>;
69+
routeType?: Maybe<Scalars['String']>;
70+
};
71+
3272
export type TencentSubnet = TencentBaseService & {
3373
availableIpAddressCount?: Maybe<Scalars['Int']>;
3474
cdcId?: Maybe<Scalars['String']>;
@@ -67,6 +107,7 @@ export type TencentVpc = TencentBaseService & {
67107
ipv6CidrBlock?: Maybe<Scalars['String']>;
68108
isDefault?: Maybe<Scalars['Boolean']>;
69109
name?: Maybe<Scalars['String']>;
110+
routeTables?: Maybe<Array<Maybe<TencentRouteTable>>>;
70111
subnets?: Maybe<Array<Maybe<TencentSubnet>>>;
71112
tags?: Maybe<Array<Maybe<TencentRawTag>>>;
72113
};

0 commit comments

Comments
 (0)