Skip to content

Commit

Permalink
feat(camGroup): add camGroup service
Browse files Browse the repository at this point in the history
  • Loading branch information
james-zhou-inspire11 committed Jun 9, 2022
1 parent 6a2f0a7 commit 3c64503
Show file tree
Hide file tree
Showing 9 changed files with 107 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.camGroup]: 'tencentCamGroup',
[services.camPolicy]: 'tencentCamPolicy',
[services.camUser]: 'tencentCamUser',
[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 {
camGroup: 'camGroups',
camPolicy: 'camPolicies',
ccn: 'ccns',
ccnAttachment: 'ccnAttachments',
Expand Down
2 changes: 2 additions & 0 deletions src/enums/serviceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import TencentVpnGatewayRoute from '../services/vpnGatewayRoute'
import TencentCustomerGateway from '../services/customerGateway'
import TencentCamUser from '../services/camUser'
import TencentVpnConnection from '../services/vpnConnection'
import TencentCamGroup from '../services/camGroup'

/**
* 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.camGroup]: TencentCamGroup,
[services.camPolicy]: TencentCamPolicy,
[services.camUser]: TencentCamUser,
[services.ccn]: TencentCcn,
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 {
camGroup: 'camGroup',
camPolicy: 'camPolicy',
camUser: 'camUser',
ccn: 'ccn',
Expand Down
52 changes: 52 additions & 0 deletions src/services/camGroup/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
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 { GroupInfo } from 'tencentcloud-sdk-nodejs/tencentcloud/services/cam/v20190116/cam_models'
import loggerText from '../../properties/logger'
import { TencentServiceInput } from '../../types'
import { initTestEndpoint, generateTencentErrorLog } from '../../utils'
import { GLOBAL_REGION } from '../../config/constants'

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

export interface RawTencentCamGroup extends GroupInfo {
id: string
region: string
}

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

try {
const CamClient = tencentcloud.cam.v20190116.Client
const clientConfig: ClientConfig = { credential: config, profile: { httpProfile: { endpoint: apiEndpoint } } }
const cam = new CamClient(clientConfig)

const response = await cam.ListGroups(null)

if (response && !isEmpty(response.GroupInfo)) {
for (const instance of response.GroupInfo) {
camGroupList.push({
id: `${instance.GroupId}`,
...instance,
region: GLOBAL_REGION,
})
}
}
} catch (error) {
generateTencentErrorLog(serviceName, 'cam:ListGroups', error)
}

logger.debug(lt.foundResources(serviceName, camGroupList.length))
resolve(groupBy(camGroupList, 'region'))
})
24 changes: 24 additions & 0 deletions src/services/camGroup/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TencentCamGroup } from '../../types/generated'
import { RawTencentCamGroup } from './data'

export default ({
service,
}: {
service: RawTencentCamGroup
}): TencentCamGroup => {
const {
id,
region,
GroupName: name,
CreateTime: createTime,
Remark: remark
} = service

return {
id,
region,
name,
createTime,
remark,
}
}
15 changes: 15 additions & 0 deletions src/services/camGroup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Service } from '@cloudgraph/sdk'
import BaseService from '../base'
import format from './format'
import getData from './data'
import { getMutation } from '../../utils'
import services from '../../enums/services'
import schemasMap from '../../enums/schemasMap'

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

getData = getData.bind(this)

mutation = getMutation(schemasMap[services.camGroup])
}
5 changes: 5 additions & 0 deletions src/services/camGroup/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type tencentCamGroup implements tencentBaseService @key(fields: "id") {
name: String @search(by: [hash, regexp])
createTime: String @search(by: [hash, regexp])
remark: String @search(by: [hash, regexp])
}
6 changes: 6 additions & 0 deletions src/types/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export type TencentBaseService = {
region?: Maybe<Scalars['String']>;
};

export type TencentCamGroup = TencentBaseService & {
createTime?: Maybe<Scalars['String']>;
name?: Maybe<Scalars['String']>;
remark?: Maybe<Scalars['String']>;
};

export type TencentCamPolicy = TencentBaseService & {
addTime?: Maybe<Scalars['String']>;
attachEntityBoundaryCount?: Maybe<Scalars['Int']>;
Expand Down

0 comments on commit 3c64503

Please sign in to comment.