Skip to content

Commit

Permalink
feat(securityGroupRule): add securityGroupRule service
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Brandt committed May 11, 2022
1 parent b154d3f commit a6e7144
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an
| Service | Relations |
| ------------------- | ------------------- |
| securityGroup | |
| securityGroupRule | |
| 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 @@ -5,6 +5,7 @@ import services from './services'
*/
export default {
[services.securityGroup]: 'tencentSecurityGroup',
[services.securityGroupRule]: 'tencentSecurityGroupRule',
[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,5 +1,6 @@
import services from './services'
import TencentSecurityGroup from '../services/securityGroup'
import TencentSecurityGroupRule from '../services/securityGroupRule'
import TencentSubnet from '../services/subnet'
import TencentVpc from '../services/vpc'
import TencentTag from '../services/tag'
Expand All @@ -10,6 +11,7 @@ import TencentTag from '../services/tag'
*/
export default {
[services.securityGroup]: TencentSecurityGroup,
[services.securityGroupRule]: TencentSecurityGroupRule,
[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,5 +1,6 @@
export default {
securityGroup: 'securityGroup',
securityGroupRule: 'securityGroupRule',
subnet: 'subnet',
vpc: 'vpc',
}
63 changes: 63 additions & 0 deletions src/services/securityGroupRule/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
import { SecurityGroupRule } from 'tencentcloud-sdk-nodejs/tencentcloud/services/cfw/v20190904/cfw_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 = 'SecurityGroupRule'
const apiEndpoint = initTestEndpoint(serviceName)
const MAX_ITEMS = '50'

export interface RawTencentSecurityGroupRule extends SecurityGroupRule {
id: string
region: string
}

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

for (const region of regions.split(',')) {
/**
* Get all security group rules
*/
try {
const CfwClient = tencentcloud.cfw.v20190904.Client
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
const cfw = new CfwClient(clientConfig)
let marker = 0
let rules = []

do {
marker++
let response = await cfw.DescribeEnterpriseSecurityGroupRule({ PageNo: marker.toString(), PageSize: MAX_ITEMS })
if (response && !isEmpty(response.Rules)) {
rules = response.Rules
for (const instance of rules) {
ruleList.push({
id: instance.Id,
...instance,
region,
})
}
}
} while (!isEmpty(rules))
} catch (error) {
generateTencentErrorLog(serviceName, 'cfw:DescribeEnterpriseSecurityGroupRule', error)
}
}

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

export default ({
service,
account,
region,
}: {
service: RawTencentSecurityGroupRule
account: string
region: string
}): TencentSecurityGroupRule => {
const {
id,
SourceContent: sourceContent,
SourceType: sourceType,
DestContent: destContent,
DestType: destType,
RuleAction: ruleAction,
Description: description,
OrderIndex: orderIndex,
Protocol: protocol,
Port: port,
ServiceTemplateId: serviceTemplateId,
Enable: enable,
} = service

return {
id,
region,
sourceContent,
sourceType,
destContent,
destType,
ruleAction,
description,
orderIndex,
protocol,
port,
serviceTemplateId,
enable,
}
}
13 changes: 13 additions & 0 deletions src/services/securityGroupRule/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 TencentSecurityGroupRule extends BaseService implements Service {
format = format.bind(this)

getData = getData.bind(this)

mutation = getMutation(serviceName)
}
13 changes: 13 additions & 0 deletions src/services/securityGroupRule/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type tencentSecurityGroupRule implements tencentBaseService @key(fields: "id") {
sourceContent: String @search(by: [hash, regexp])
sourceType: String @search(by: [hash, regexp])
destContent: String @search(by: [hash, regexp])
destType: String @search(by: [hash, regexp])
ruleAction: String @search(by: [hash, regexp])
description: String @search(by: [hash, regexp])
orderIndex: String @search(by: [hash, regexp])
protocol: String @search(by: [hash, regexp])
port: String @search(by: [hash, regexp])
serviceTemplateId: String @search(by: [hash, regexp])
enable: String @search(by: [hash, regexp])
}
14 changes: 14 additions & 0 deletions src/types/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ export type TencentSecurityGroup = TencentBaseService & {
updateTime?: Maybe<Scalars['String']>;
};

export type TencentSecurityGroupRule = TencentBaseService & {
description?: Maybe<Scalars['String']>;
destContent?: Maybe<Scalars['String']>;
destType?: Maybe<Scalars['String']>;
enable?: Maybe<Scalars['String']>;
orderIndex?: Maybe<Scalars['String']>;
port?: Maybe<Scalars['String']>;
protocol?: Maybe<Scalars['String']>;
ruleAction?: Maybe<Scalars['String']>;
serviceTemplateId?: Maybe<Scalars['String']>;
sourceContent?: Maybe<Scalars['String']>;
sourceType?: Maybe<Scalars['String']>;
};

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

0 comments on commit a6e7144

Please sign in to comment.