|
| 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 | + }) |
0 commit comments