From 1f20850176f0e9f3c48e3f0d8fe86ac533370e7d Mon Sep 17 00:00:00 2001 From: Christopher Brandt Date: Mon, 9 May 2022 14:28:44 -0700 Subject: [PATCH] feat(securityGroup): add security group service --- README.md | 1 + src/enums/schemasMap.ts | 1 + src/enums/serviceMap.ts | 2 + src/enums/services.ts | 1 + src/services/securityGroup/data.ts | 57 +++++++++++++++++++++++ src/services/securityGroup/format.ts | 40 ++++++++++++++++ src/services/securityGroup/index.ts | 13 ++++++ src/services/securityGroup/schema.graphql | 9 ++++ src/types/generated.ts | 10 ++++ 9 files changed, 134 insertions(+) create mode 100644 src/services/securityGroup/data.ts create mode 100644 src/services/securityGroup/format.ts create mode 100644 src/services/securityGroup/index.ts create mode 100644 src/services/securityGroup/schema.graphql diff --git a/README.md b/README.md index fec74e5..c2bb4dd 100644 --- a/README.md +++ b/README.md @@ -56,5 +56,6 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an | Service | Relations | | ------------------- | ------------------- | +| securityGroup | | | subnet | vpc | | vpc | subnet | \ No newline at end of file diff --git a/src/enums/schemasMap.ts b/src/enums/schemasMap.ts index 4d1970a..a7bad0e 100644 --- a/src/enums/schemasMap.ts +++ b/src/enums/schemasMap.ts @@ -4,6 +4,7 @@ import services from './services' * schemasMap is an object that contains schemas name by resource */ export default { + [services.securityGroup]: 'tencentSecurityGroup', [services.subnet]: 'tencentSubnet', [services.vpc]: 'tencentVpc', tag: 'tencentTag', diff --git a/src/enums/serviceMap.ts b/src/enums/serviceMap.ts index 83c6a12..3c3bdf2 100644 --- a/src/enums/serviceMap.ts +++ b/src/enums/serviceMap.ts @@ -1,4 +1,5 @@ import services from './services' +import TencentSecurityGroup from '../services/securityGroup' import TencentSubnet from '../services/subnet' import TencentVpc from '../services/vpc' import TencentTag from '../services/tag' @@ -8,6 +9,7 @@ import TencentTag from '../services/tag' * serviceMap is used by the serviceFactory to produce instances of service classes */ export default { + [services.securityGroup]: TencentSecurityGroup, [services.subnet]: TencentSubnet, [services.vpc]: TencentVpc, tag: TencentTag, diff --git a/src/enums/services.ts b/src/enums/services.ts index 9c55e7b..53cea39 100644 --- a/src/enums/services.ts +++ b/src/enums/services.ts @@ -1,4 +1,5 @@ export default { + securityGroup: 'securityGroup', subnet: 'subnet', vpc: 'vpc', } diff --git a/src/services/securityGroup/data.ts b/src/services/securityGroup/data.ts new file mode 100644 index 0000000..88db615 --- /dev/null +++ b/src/services/securityGroup/data.ts @@ -0,0 +1,57 @@ +import * as tencentcloud from 'tencentcloud-sdk-nodejs' +import { SecurityGroup } 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 = 'SecurityGroup' +const apiEndpoint = initTestEndpoint(serviceName) + +export interface RawTencentSecurityGroup extends SecurityGroup { + id: string + region: string +} + +export default async ({ + regions, + config, +}: TencentServiceInput): Promise<{ + [region: string]: RawTencentSecurityGroup[] +}> => + new Promise(async resolve => { + const vpcList: RawTencentSecurityGroup[] = [] + + for (const region of regions.split(',')) { + /** + * Get all security groups + */ + 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.DescribeSecurityGroups(null) + + if (response && !isEmpty(response.SecurityGroupSet)) { + for (const instance of response.SecurityGroupSet) { + vpcList.push({ + id: instance.SecurityGroupId, + ...instance, + region, + }) + } + } + + } catch (error) { + generateTencentErrorLog(serviceName, 'vpc:DescribeSecurityGroups', error) + } + } + + logger.debug(lt.foundResources(serviceName, vpcList.length)) + resolve(groupBy(vpcList, 'region')) + }) diff --git a/src/services/securityGroup/format.ts b/src/services/securityGroup/format.ts new file mode 100644 index 0000000..fc935d0 --- /dev/null +++ b/src/services/securityGroup/format.ts @@ -0,0 +1,40 @@ +import cuid from 'cuid' +import { TencentSecurityGroup } from '../../types/generated' +import { RawTencentSecurityGroup } from './data' + +export default ({ + service, + account, + region, +}: { + service: RawTencentSecurityGroup + account: string + region: string +}): TencentSecurityGroup => { + const { + id, + SecurityGroupName: name, + SecurityGroupDesc: securityGroupDesc, + ProjectId: projectId, + IsDefault: isDefault, + CreatedTime: createdTime, + TagSet, + UpdateTime: updateTime, + } = service + + return { + id, + region, + name, + securityGroupDesc, + projectId, + isDefault, + createdTime, + tags: TagSet?.map(tagSet => ({ + id: cuid(), + key: tagSet.Key, + value: tagSet.Value, + })), + updateTime, + } +} diff --git a/src/services/securityGroup/index.ts b/src/services/securityGroup/index.ts new file mode 100644 index 0000000..331b052 --- /dev/null +++ b/src/services/securityGroup/index.ts @@ -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 TencentSecurityGroup extends BaseService implements Service { + format = format.bind(this) + + getData = getData.bind(this) + + mutation = getMutation(serviceName) +} diff --git a/src/services/securityGroup/schema.graphql b/src/services/securityGroup/schema.graphql new file mode 100644 index 0000000..e4bb56f --- /dev/null +++ b/src/services/securityGroup/schema.graphql @@ -0,0 +1,9 @@ +type tencentSecurityGroup implements tencentBaseService @key(fields: "id") { + name: String @search(by: [hash, regexp]) + securityGroupDesc: String @search(by: [hash, regexp]) + projectId: String @search(by: [hash, regexp]) + isDefault: Boolean @search + createdTime: String @search(by: [hash, regexp]) + tags: [tencentRawTag] + updateTime: String @search(by: [hash, regexp]) +} diff --git a/src/types/generated.ts b/src/types/generated.ts index 32d9546..c441106 100644 --- a/src/types/generated.ts +++ b/src/types/generated.ts @@ -29,6 +29,16 @@ export type TencentRawTag = { value?: Maybe; }; +export type TencentSecurityGroup = TencentBaseService & { + createdTime?: Maybe; + isDefault?: Maybe; + name?: Maybe; + projectId?: Maybe; + securityGroupDesc?: Maybe; + tags?: Maybe>>; + updateTime?: Maybe; +}; + export type TencentSubnet = TencentBaseService & { availableIpAddressCount?: Maybe; cdcId?: Maybe;