|
| 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 { GroupInfo } from 'tencentcloud-sdk-nodejs/tencentcloud/services/cam/v20190116/cam_models' |
| 7 | +import loggerText from '../../properties/logger' |
| 8 | +import { TencentServiceInput } from '../../types' |
| 9 | +import { initTestEndpoint, generateTencentErrorLog } from '../../utils' |
| 10 | +import { GLOBAL_REGION } from '../../config/constants' |
| 11 | + |
| 12 | +const lt = { ...loggerText } |
| 13 | +const { logger } = CloudGraph |
| 14 | +export const serviceName = 'CamGroup' |
| 15 | +const apiEndpoint = initTestEndpoint(serviceName) |
| 16 | + |
| 17 | +export interface RawTencentCamGroup extends GroupInfo { |
| 18 | + id: string |
| 19 | + region: string |
| 20 | +} |
| 21 | + |
| 22 | +export default async ({ |
| 23 | + config, |
| 24 | +}: TencentServiceInput): Promise<{ |
| 25 | + [region: string]: RawTencentCamGroup[] |
| 26 | +}> => |
| 27 | + new Promise(async resolve => { |
| 28 | + const camGroupList: RawTencentCamGroup[] = [] |
| 29 | + |
| 30 | + try { |
| 31 | + const CamClient = tencentcloud.cam.v20190116.Client |
| 32 | + const clientConfig: ClientConfig = { credential: config, profile: { httpProfile: { endpoint: apiEndpoint } } } |
| 33 | + const cam = new CamClient(clientConfig) |
| 34 | + |
| 35 | + const response = await cam.ListGroups(null) |
| 36 | + |
| 37 | + if (response && !isEmpty(response.GroupInfo)) { |
| 38 | + for (const instance of response.GroupInfo) { |
| 39 | + camGroupList.push({ |
| 40 | + id: `${instance.GroupId}`, |
| 41 | + ...instance, |
| 42 | + region: GLOBAL_REGION, |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + generateTencentErrorLog(serviceName, 'cam:ListGroups', error) |
| 48 | + } |
| 49 | + |
| 50 | + logger.debug(lt.foundResources(serviceName, camGroupList.length)) |
| 51 | + resolve(groupBy(camGroupList, 'region')) |
| 52 | + }) |
0 commit comments