Skip to content

Commit

Permalink
feat(routeTable): add routeTable
Browse files Browse the repository at this point in the history
  • Loading branch information
james-zhou-inspire11 committed May 10, 2022
1 parent 762dc79 commit 112f1d5
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 0 deletions.
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.routeTable]: 'tencentRouteTable',
[services.subnet]: 'tencentSubnet',
[services.vpc]: 'tencentVpc',
tag: 'tencentTag',
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',
subnet: 'subnets',
vpc: 'vpcInstances',
}
2 changes: 2 additions & 0 deletions src/enums/serviceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import services from './services'
import TencentSubnet from '../services/subnet'
import TencentVpc from '../services/vpc'
import TencentTag from '../services/tag'
import TencentRouteTable from '../services/routeTable'

/**
* 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.routeTable]: TencentRouteTable,
[services.subnet]: TencentSubnet,
[services.vpc]: TencentVpc,
tag: TencentTag,
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 {
routeTable: 'routeTable',
subnet: 'subnet',
vpc: 'vpc',
}
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,
}
})
}
}
13 changes: 13 additions & 0 deletions src/services/routeTable/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 TencentRouteTable extends BaseService implements Service {
format = format.bind(this)

getData = getData.bind(this)

mutation = getMutation(serviceName)
}
57 changes: 57 additions & 0 deletions src/services/routeTable/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
type tencentRouteTableAssociation
@generate(
query: { get: false, query: true, aggregate: false }
mutation: { add: false, delete: false }
subscription: false
)
@key(fields: "id") {
id: String! @id
subnetId: String @search(by: [hash, regexp])
routeTableId: String @search(by: [hash, regexp])
}

type tencentRouteTableRoute
@generate(
query: { get: false, query: true, aggregate: false }
mutation: { add: false, delete: false }
subscription: false
)
@key(fields: "id") {
id: String! @id
destinationCidrBlock: String @search(by: [hash, regexp])
gatewayType: String @search(by: [hash, regexp])
gatewayId: String @search(by: [hash, regexp])
routeId: Int @search
routeDescription: String @search(by: [hash, regexp])
enabled: Boolean @search
routeType: String @search(by: [hash, regexp])
routeTableId: String @search(by: [hash, regexp])
destinationIpv6CidrBlock: String @search(by: [hash, regexp])
routeItemId: String @search(by: [hash, regexp])
publishedToVbc: Boolean @search
createdTime: String @search(by: [hash, regexp])
}

type tencentRouteTableLocalCidrForCcnn
@generate(
query: { get: false, query: true, aggregate: false }
mutation: { add: false, delete: false }
subscription: false
)
@key(fields: "id") {
id: String! @id
cidr: String @search(by: [hash, regexp])
publishedToVbc: Boolean @search
}

type tencentRouteTable implements tencentBaseService @key(fields: "id") {
routeTableId: String @search(by: [hash, regexp])
routeTableName: String @search(by: [hash, regexp])
associationSet: [tencentRouteTableAssociation]
routeSet: [tencentRouteTableRoute]
main: Boolean @search
createdTime: String @search(by: [hash, regexp])
tags: [tencentRawTag]
localCidrForCcn: [tencentRouteTableLocalCidrForCcnn]
vpcInstances: [tencentVpc] @hasInverse(field: routeTables)
}
1 change: 1 addition & 0 deletions src/services/vpc/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type tencentVpc implements tencentBaseService @key(fields: "id") {
tags: [tencentRawTag]
assistantCidrSet: [tencentVpcAssistantCidr]
subnets: [tencentSubnet] @hasInverse(field: vpcInstances)
routeTables: [tencentRouteTable] @hasInverse(field: vpcInstances)
}

type tencentVpcAssistantCidr
Expand Down
41 changes: 41 additions & 0 deletions src/types/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ export type TencentRawTag = {
value?: Maybe<Scalars['String']>;
};

export type TencentRouteTable = TencentBaseService & {
associationSet?: Maybe<Array<Maybe<TencentRouteTableAssociation>>>;
createdTime?: Maybe<Scalars['String']>;
localCidrForCcn?: Maybe<Array<Maybe<TencentRouteTableLocalCidrForCcnn>>>;
main?: Maybe<Scalars['Boolean']>;
routeSet?: Maybe<Array<Maybe<TencentRouteTableRoute>>>;
routeTableId?: Maybe<Scalars['String']>;
routeTableName?: Maybe<Scalars['String']>;
tags?: Maybe<Array<Maybe<TencentRawTag>>>;
vpcInstances?: Maybe<Array<Maybe<TencentVpc>>>;
};

export type TencentRouteTableAssociation = {
id: Scalars['String'];
routeTableId?: Maybe<Scalars['String']>;
subnetId?: Maybe<Scalars['String']>;
};

export type TencentRouteTableLocalCidrForCcnn = {
cidr?: Maybe<Scalars['String']>;
id: Scalars['String'];
publishedToVbc?: Maybe<Scalars['Boolean']>;
};

export type TencentRouteTableRoute = {
createdTime?: Maybe<Scalars['String']>;
destinationCidrBlock?: Maybe<Scalars['String']>;
destinationIpv6CidrBlock?: Maybe<Scalars['String']>;
enabled?: Maybe<Scalars['Boolean']>;
gatewayId?: Maybe<Scalars['String']>;
gatewayType?: Maybe<Scalars['String']>;
id: Scalars['String'];
publishedToVbc?: Maybe<Scalars['Boolean']>;
routeDescription?: Maybe<Scalars['String']>;
routeId?: Maybe<Scalars['Int']>;
routeItemId?: Maybe<Scalars['String']>;
routeTableId?: Maybe<Scalars['String']>;
routeType?: Maybe<Scalars['String']>;
};

export type TencentSubnet = TencentBaseService & {
availableIpAddressCount?: Maybe<Scalars['Int']>;
cdcId?: Maybe<Scalars['String']>;
Expand Down Expand Up @@ -67,6 +107,7 @@ export type TencentVpc = TencentBaseService & {
ipv6CidrBlock?: Maybe<Scalars['String']>;
isDefault?: Maybe<Scalars['Boolean']>;
name?: Maybe<Scalars['String']>;
routeTables?: Maybe<Array<Maybe<TencentRouteTable>>>;
subnets?: Maybe<Array<Maybe<TencentSubnet>>>;
tags?: Maybe<Array<Maybe<TencentRawTag>>>;
};
Expand Down
8 changes: 8 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import cuid from 'cuid'
import { TencentKeyValue, TencentRawTag } from '../types/generated'
import { TagMap, KeyValueMapMap } from '../types'

export const formatTagSet = (tagSet): Array<TencentKeyValue> => {
return tagSet?.map(tag => ({
id: cuid(),
key: tag.Key,
value: tag.Value,
}))
}

export const formatKeyValueMap = (keyValueMap: KeyValueMapMap): TencentKeyValue[] => {
return Object.keys(keyValueMap || {}).map(key => ({
id: cuid(),
Expand Down

0 comments on commit 112f1d5

Please sign in to comment.