Skip to content

Commit fd2def1

Browse files
authored
Merge pull request #5 from cloudgraphdev/feature/CG-1129
feat(securityGroupRule): add securityGroupRule service
2 parents b154d3f + a6e7144 commit fd2def1

9 files changed

Lines changed: 151 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an
5757
| Service | Relations |
5858
| ------------------- | ------------------- |
5959
| securityGroup | |
60+
| securityGroupRule | |
6061
| subnet | vpc |
6162
| vpc | subnet |

src/enums/schemasMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import services from './services'
55
*/
66
export default {
77
[services.securityGroup]: 'tencentSecurityGroup',
8+
[services.securityGroupRule]: 'tencentSecurityGroupRule',
89
[services.subnet]: 'tencentSubnet',
910
[services.vpc]: 'tencentVpc',
1011
tag: 'tencentTag',

src/enums/serviceMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import services from './services'
22
import TencentSecurityGroup from '../services/securityGroup'
3+
import TencentSecurityGroupRule from '../services/securityGroupRule'
34
import TencentSubnet from '../services/subnet'
45
import TencentVpc from '../services/vpc'
56
import TencentTag from '../services/tag'
@@ -10,6 +11,7 @@ import TencentTag from '../services/tag'
1011
*/
1112
export default {
1213
[services.securityGroup]: TencentSecurityGroup,
14+
[services.securityGroupRule]: TencentSecurityGroupRule,
1315
[services.subnet]: TencentSubnet,
1416
[services.vpc]: TencentVpc,
1517
tag: TencentTag,

src/enums/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
securityGroup: 'securityGroup',
3+
securityGroupRule: 'securityGroupRule',
34
subnet: 'subnet',
45
vpc: 'vpc',
56
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
2+
import { SecurityGroupRule } from 'tencentcloud-sdk-nodejs/tencentcloud/services/cfw/v20190904/cfw_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 = 'SecurityGroupRule'
14+
const apiEndpoint = initTestEndpoint(serviceName)
15+
const MAX_ITEMS = '50'
16+
17+
export interface RawTencentSecurityGroupRule extends SecurityGroupRule {
18+
id: string
19+
region: string
20+
}
21+
22+
export default async ({
23+
regions,
24+
config,
25+
}: TencentServiceInput): Promise<{
26+
[region: string]: RawTencentSecurityGroupRule[]
27+
}> =>
28+
new Promise(async resolve => {
29+
const ruleList: RawTencentSecurityGroupRule[] = []
30+
31+
for (const region of regions.split(',')) {
32+
/**
33+
* Get all security group rules
34+
*/
35+
try {
36+
const CfwClient = tencentcloud.cfw.v20190904.Client
37+
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
38+
const cfw = new CfwClient(clientConfig)
39+
let marker = 0
40+
let rules = []
41+
42+
do {
43+
marker++
44+
let response = await cfw.DescribeEnterpriseSecurityGroupRule({ PageNo: marker.toString(), PageSize: MAX_ITEMS })
45+
if (response && !isEmpty(response.Rules)) {
46+
rules = response.Rules
47+
for (const instance of rules) {
48+
ruleList.push({
49+
id: instance.Id,
50+
...instance,
51+
region,
52+
})
53+
}
54+
}
55+
} while (!isEmpty(rules))
56+
} catch (error) {
57+
generateTencentErrorLog(serviceName, 'cfw:DescribeEnterpriseSecurityGroupRule', error)
58+
}
59+
}
60+
61+
logger.debug(lt.foundResources(serviceName, ruleList.length))
62+
resolve(groupBy(ruleList, 'region'))
63+
})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { TencentSecurityGroupRule } from '../../types/generated'
2+
import { RawTencentSecurityGroupRule } from './data'
3+
4+
export default ({
5+
service,
6+
account,
7+
region,
8+
}: {
9+
service: RawTencentSecurityGroupRule
10+
account: string
11+
region: string
12+
}): TencentSecurityGroupRule => {
13+
const {
14+
id,
15+
SourceContent: sourceContent,
16+
SourceType: sourceType,
17+
DestContent: destContent,
18+
DestType: destType,
19+
RuleAction: ruleAction,
20+
Description: description,
21+
OrderIndex: orderIndex,
22+
Protocol: protocol,
23+
Port: port,
24+
ServiceTemplateId: serviceTemplateId,
25+
Enable: enable,
26+
} = service
27+
28+
return {
29+
id,
30+
region,
31+
sourceContent,
32+
sourceType,
33+
destContent,
34+
destType,
35+
ruleAction,
36+
description,
37+
orderIndex,
38+
protocol,
39+
port,
40+
serviceTemplateId,
41+
enable,
42+
}
43+
}
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 TencentSecurityGroupRule extends BaseService implements Service {
8+
format = format.bind(this)
9+
10+
getData = getData.bind(this)
11+
12+
mutation = getMutation(serviceName)
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type tencentSecurityGroupRule implements tencentBaseService @key(fields: "id") {
2+
sourceContent: String @search(by: [hash, regexp])
3+
sourceType: String @search(by: [hash, regexp])
4+
destContent: String @search(by: [hash, regexp])
5+
destType: String @search(by: [hash, regexp])
6+
ruleAction: String @search(by: [hash, regexp])
7+
description: String @search(by: [hash, regexp])
8+
orderIndex: String @search(by: [hash, regexp])
9+
protocol: String @search(by: [hash, regexp])
10+
port: String @search(by: [hash, regexp])
11+
serviceTemplateId: String @search(by: [hash, regexp])
12+
enable: String @search(by: [hash, regexp])
13+
}

src/types/generated.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ export type TencentSecurityGroup = TencentBaseService & {
3939
updateTime?: Maybe<Scalars['String']>;
4040
};
4141

42+
export type TencentSecurityGroupRule = TencentBaseService & {
43+
description?: Maybe<Scalars['String']>;
44+
destContent?: Maybe<Scalars['String']>;
45+
destType?: Maybe<Scalars['String']>;
46+
enable?: Maybe<Scalars['String']>;
47+
orderIndex?: Maybe<Scalars['String']>;
48+
port?: Maybe<Scalars['String']>;
49+
protocol?: Maybe<Scalars['String']>;
50+
ruleAction?: Maybe<Scalars['String']>;
51+
serviceTemplateId?: Maybe<Scalars['String']>;
52+
sourceContent?: Maybe<Scalars['String']>;
53+
sourceType?: Maybe<Scalars['String']>;
54+
};
55+
4256
export type TencentSubnet = TencentBaseService & {
4357
availableIpAddressCount?: Maybe<Scalars['Int']>;
4458
cdcId?: Maybe<Scalars['String']>;

0 commit comments

Comments
 (0)