|
| 1 | +import * as tencentcloud from 'tencentcloud-sdk-nodejs' |
| 2 | +import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface' |
| 3 | +import CloudGraph from '@cloudgraph/sdk' |
| 4 | +import groupBy from 'lodash/groupBy' |
| 5 | +import isEmpty from 'lodash/isEmpty' |
| 6 | +import { StrategyInfo } from 'tencentcloud-sdk-nodejs/tencentcloud/services/cam/v20190116/cam_models' |
| 7 | +import cuid from 'cuid' |
| 8 | +import loggerText from '../../properties/logger' |
| 9 | +import { TencentServiceInput } from '../../types' |
| 10 | +import { initTestEndpoint, generateTencentErrorLog } from '../../utils' |
| 11 | + |
| 12 | +const lt = { ...loggerText } |
| 13 | +const { logger } = CloudGraph |
| 14 | +export const serviceName = 'CamPolicy' |
| 15 | +const apiEndpoint = initTestEndpoint(serviceName) |
| 16 | + |
| 17 | +export interface RawTencentCamPolicy extends StrategyInfo { |
| 18 | + id: string |
| 19 | + region: string |
| 20 | +} |
| 21 | + |
| 22 | +export default async ({ |
| 23 | + regions, |
| 24 | + config, |
| 25 | +}: TencentServiceInput): Promise<{ |
| 26 | + [region: string]: RawTencentCamPolicy[] |
| 27 | +}> => |
| 28 | + new Promise(async resolve => { |
| 29 | + const camPolicyList: RawTencentCamPolicy[] = [] |
| 30 | + |
| 31 | + for (const region of regions.split(',')) { |
| 32 | + try { |
| 33 | + const CamClient = tencentcloud.cam.v20190116.Client |
| 34 | + const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } } |
| 35 | + const cam = new CamClient(clientConfig) |
| 36 | + |
| 37 | + const response = await cam.ListPolicies(null) |
| 38 | + |
| 39 | + if (response && !isEmpty(response.List)) { |
| 40 | + for (const instance of response.List) { |
| 41 | + camPolicyList.push({ |
| 42 | + id: cuid(), |
| 43 | + ...instance, |
| 44 | + region, |
| 45 | + }) |
| 46 | + } |
| 47 | + } |
| 48 | + } catch (error) { |
| 49 | + generateTencentErrorLog(serviceName, 'cam:ListPolicies', error) |
| 50 | + } |
| 51 | + } |
| 52 | + logger.debug(lt.foundResources(serviceName, camPolicyList.length)) |
| 53 | + resolve(groupBy(camPolicyList, 'region')) |
| 54 | + }) |
0 commit comments