|
| 1 | +import groupBy from 'lodash/groupBy' |
| 2 | +import { ApiGatewayServiceClient } from '@google-cloud/api-gateway' |
| 3 | +import CloudGraph from '@cloudgraph/sdk' |
| 4 | +import { google } from '@google-cloud/api-gateway/build/protos/protos' |
| 5 | +import gcpLoggerText from '../../properties/logger' |
| 6 | +import { GcpServiceInput, LabelMap } from '../../types' |
| 7 | +import { generateGcpErrorLog, initTestEndpoint } from '../../utils' |
| 8 | +import { GLOBAL_REGION } from '../../config/constants' |
| 9 | + |
| 10 | +const lt = { ...gcpLoggerText } |
| 11 | +const { logger } = CloudGraph |
| 12 | +const serviceName = 'API Gateway Api' |
| 13 | +const apiEndpoint = initTestEndpoint(serviceName) |
| 14 | + |
| 15 | +export interface RawGcpApiGatewayApi extends |
| 16 | +Omit<google.cloud.apigateway.v1.IApi, 'labels'> { |
| 17 | + id: string, |
| 18 | + region: string, |
| 19 | + projectId: string, |
| 20 | + Labels: LabelMap, |
| 21 | +} |
| 22 | + |
| 23 | +export const listApiGatewayApiData = async ( |
| 24 | + apiGatewayClient: ApiGatewayServiceClient, |
| 25 | + projectId: string, |
| 26 | + apiData: RawGcpApiGatewayApi[], |
| 27 | +): Promise<void> => { |
| 28 | + try { |
| 29 | + const apiIter = apiGatewayClient.listApisAsync({ |
| 30 | + parent: `projects/${projectId}/locations/${GLOBAL_REGION}`, |
| 31 | + }) |
| 32 | + for await (const {name, labels, ...api} of apiIter) { |
| 33 | + apiData.push({ |
| 34 | + id: name, |
| 35 | + projectId, |
| 36 | + region: GLOBAL_REGION, |
| 37 | + Labels: labels, |
| 38 | + ...api, |
| 39 | + }) |
| 40 | + } |
| 41 | + } catch (error) { |
| 42 | + generateGcpErrorLog(serviceName, 'ApiGateway:listApisAsync', error) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +export default async ({ |
| 47 | + config, |
| 48 | +}: GcpServiceInput): Promise<{ |
| 49 | + [region: string]: RawGcpApiGatewayApi[] |
| 50 | +}> => { |
| 51 | + const { projectId } = config |
| 52 | + |
| 53 | + const apiData: RawGcpApiGatewayApi[] = [] |
| 54 | + const apiGatewayClient = new ApiGatewayServiceClient({ ...config, apiEndpoint }) |
| 55 | + |
| 56 | + await listApiGatewayApiData(apiGatewayClient, projectId, apiData) |
| 57 | + |
| 58 | + logger.debug(lt.foundResources(serviceName, apiData.length)) |
| 59 | + |
| 60 | + return groupBy(apiData, 'region') |
| 61 | +} |
0 commit comments