Skip to content

Commit b154d3f

Browse files
author
Christopher Brandt
authored
Merge pull request #3 from cloudgraphdev/feature/CG-1122
Feature/CG-1122: add security group service
2 parents 762dc79 + 87d3ed7 commit b154d3f

11 files changed

Lines changed: 137 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an
5656

5757
| Service | Relations |
5858
| ------------------- | ------------------- |
59+
| securityGroup | |
5960
| subnet | vpc |
6061
| vpc | subnet |

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.securityGroup]: 'tencentSecurityGroup',
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+
securityGroup: 'securityGroups',
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
@@ -1,4 +1,5 @@
11
import services from './services'
2+
import TencentSecurityGroup from '../services/securityGroup'
23
import TencentSubnet from '../services/subnet'
34
import TencentVpc from '../services/vpc'
45
import TencentTag from '../services/tag'
@@ -8,6 +9,7 @@ import TencentTag from '../services/tag'
89
* serviceMap is used by the serviceFactory to produce instances of service classes
910
*/
1011
export default {
12+
[services.securityGroup]: TencentSecurityGroup,
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+
securityGroup: 'securityGroup',
23
subnet: 'subnet',
34
vpc: 'vpc',
45
}

src/services/securityGroup/data.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
2+
import { SecurityGroup } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
3+
import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface'
4+
import CloudGraph from '@cloudgraph/sdk'
5+
import groupBy from 'lodash/groupBy'
6+
import isEmpty from 'lodash/isEmpty'
7+
import loggerText from '../../properties/logger'
8+
import { TencentServiceInput } from '../../types'
9+
import { initTestEndpoint, generateTencentErrorLog } from '../../utils'
10+
11+
const lt = { ...loggerText }
12+
const { logger } = CloudGraph
13+
export const serviceName = 'SecurityGroup'
14+
const apiEndpoint = initTestEndpoint(serviceName)
15+
16+
export interface RawTencentSecurityGroup extends SecurityGroup {
17+
id: string
18+
region: string
19+
}
20+
21+
export default async ({
22+
regions,
23+
config,
24+
}: TencentServiceInput): Promise<{
25+
[region: string]: RawTencentSecurityGroup[]
26+
}> =>
27+
new Promise(async resolve => {
28+
const vpcList: RawTencentSecurityGroup[] = []
29+
30+
for (const region of regions.split(',')) {
31+
/**
32+
* Get all security groups
33+
*/
34+
try {
35+
const VpcClient = tencentcloud.vpc.v20170312.Client
36+
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
37+
const vpc = new VpcClient(clientConfig)
38+
const response = await vpc.DescribeSecurityGroups(null)
39+
40+
if (response && !isEmpty(response.SecurityGroupSet)) {
41+
for (const instance of response.SecurityGroupSet) {
42+
vpcList.push({
43+
id: instance.SecurityGroupId,
44+
...instance,
45+
region,
46+
})
47+
}
48+
}
49+
50+
} catch (error) {
51+
generateTencentErrorLog(serviceName, 'vpc:DescribeSecurityGroups', error)
52+
}
53+
}
54+
55+
logger.debug(lt.foundResources(serviceName, vpcList.length))
56+
resolve(groupBy(vpcList, 'region'))
57+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import cuid from 'cuid'
2+
import { TencentSecurityGroup } from '../../types/generated'
3+
import { RawTencentSecurityGroup } from './data'
4+
5+
export default ({
6+
service,
7+
account,
8+
region,
9+
}: {
10+
service: RawTencentSecurityGroup
11+
account: string
12+
region: string
13+
}): TencentSecurityGroup => {
14+
const {
15+
id,
16+
SecurityGroupName: name,
17+
SecurityGroupDesc: securityGroupDesc,
18+
ProjectId: projectId,
19+
IsDefault: isDefault,
20+
CreatedTime: createdTime,
21+
TagSet,
22+
UpdateTime: updateTime,
23+
} = service
24+
25+
return {
26+
id,
27+
region,
28+
name,
29+
securityGroupDesc,
30+
projectId,
31+
isDefault,
32+
createdTime,
33+
tags: TagSet?.map(tagSet => ({
34+
id: cuid(),
35+
key: tagSet.Key,
36+
value: tagSet.Value,
37+
})),
38+
updateTime,
39+
}
40+
}
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 TencentSecurityGroup extends BaseService implements Service {
8+
format = format.bind(this)
9+
10+
getData = getData.bind(this)
11+
12+
mutation = getMutation(serviceName)
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type tencentSecurityGroup implements tencentBaseService @key(fields: "id") {
2+
name: String @search(by: [hash, regexp])
3+
securityGroupDesc: String @search(by: [hash, regexp])
4+
projectId: String @search(by: [hash, regexp])
5+
isDefault: Boolean @search
6+
createdTime: String @search(by: [hash, regexp])
7+
tags: [tencentRawTag]
8+
updateTime: String @search(by: [hash, regexp])
9+
}

src/services/tag/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ type tencentTag implements tencentBaseService @key(fields: "id") {
33
value: String! @search(by: [hash, regexp])
44
subnets: [tencentSubnet]
55
vpcInstances: [tencentVpc]
6+
securityGroups: [tencentSecurityGroup]
67
}
78

89
type tencentRawTag

0 commit comments

Comments
 (0)