Skip to content

Commit

Permalink
feat(securityGroup): add security group service
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Brandt committed May 9, 2022
1 parent 762dc79 commit 1f20850
Show file tree
Hide file tree
Showing 9 changed files with 134 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,5 +56,6 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an

| Service | Relations |
| ------------------- | ------------------- |
| securityGroup | |
| subnet | vpc |
| vpc | subnet |
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.securityGroup]: 'tencentSecurityGroup',
[services.subnet]: 'tencentSubnet',
[services.vpc]: 'tencentVpc',
tag: 'tencentTag',
Expand Down
2 changes: 2 additions & 0 deletions src/enums/serviceMap.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
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 {
securityGroup: 'securityGroup',
subnet: 'subnet',
vpc: 'vpc',
}
57 changes: 57 additions & 0 deletions src/services/securityGroup/data.ts
Original file line number Diff line number Diff line change
@@ -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'))
})
40 changes: 40 additions & 0 deletions src/services/securityGroup/format.ts
Original file line number Diff line number Diff line change
@@ -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,
}
}
13 changes: 13 additions & 0 deletions src/services/securityGroup/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 TencentSecurityGroup extends BaseService implements Service {
format = format.bind(this)

getData = getData.bind(this)

mutation = getMutation(serviceName)
}
9 changes: 9 additions & 0 deletions src/services/securityGroup/schema.graphql
Original file line number Diff line number Diff line change
@@ -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])
}
10 changes: 10 additions & 0 deletions src/types/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export type TencentRawTag = {
value?: Maybe<Scalars['String']>;
};

export type TencentSecurityGroup = TencentBaseService & {
createdTime?: Maybe<Scalars['String']>;
isDefault?: Maybe<Scalars['Boolean']>;
name?: Maybe<Scalars['String']>;
projectId?: Maybe<Scalars['String']>;
securityGroupDesc?: Maybe<Scalars['String']>;
tags?: Maybe<Array<Maybe<TencentRawTag>>>;
updateTime?: Maybe<Scalars['String']>;
};

export type TencentSubnet = TencentBaseService & {
availableIpAddressCount?: Maybe<Scalars['Int']>;
cdcId?: Maybe<Scalars['String']>;
Expand Down

0 comments on commit 1f20850

Please sign in to comment.