|
| 1 | +import { AutoscalingPolicyServiceClient } from '@google-cloud/dataproc' |
| 2 | +import CloudGraph from '@cloudgraph/sdk' |
| 3 | +import groupBy from 'lodash/groupBy' |
| 4 | +import { google } from '@google-cloud/dataproc/build/protos/protos' |
| 5 | +import gcpLoggerText from '../../properties/logger' |
| 6 | +import { GcpServiceInput } from '../../types' |
| 7 | +import { generateGcpErrorLog } from '../../utils' |
| 8 | + |
| 9 | +const lt = { ...gcpLoggerText } |
| 10 | +const { logger } = CloudGraph |
| 11 | +const serviceName = 'Dataproc Autoscaling Policy' |
| 12 | + |
| 13 | +export interface RawGcpDataprocAutoscalingPolicy extends Omit<google.cloud.dataproc.v1.IAutoscalingPolicy, 'id'|'labels'> { |
| 14 | + id: string |
| 15 | + region: string |
| 16 | + projectId: string |
| 17 | + Labels: { [key: string]: string } |
| 18 | +} |
| 19 | + |
| 20 | +export default async ({ |
| 21 | + regions, |
| 22 | + config, |
| 23 | +}: GcpServiceInput): Promise<{ |
| 24 | + [region: string]: RawGcpDataprocAutoscalingPolicy[] |
| 25 | +}> => { |
| 26 | + const autoscalingPolicyList: RawGcpDataprocAutoscalingPolicy[] = [] |
| 27 | + const { projectId } = config |
| 28 | + |
| 29 | + for (const region of regions.split(',')) { |
| 30 | + /** |
| 31 | + * Get all the Dataproc Autoscaling Policies |
| 32 | + */ |
| 33 | + |
| 34 | + try { |
| 35 | + const dataprocClient = new AutoscalingPolicyServiceClient({ |
| 36 | + ...config, |
| 37 | + apiEndpoint: `${region}-dataproc.googleapis.com`, |
| 38 | + projectId, |
| 39 | + }) |
| 40 | + const locationName = dataprocClient.locationPath(projectId, region) |
| 41 | + const iterable = dataprocClient.listAutoscalingPoliciesAsync({ |
| 42 | + parent: locationName, |
| 43 | + }) |
| 44 | + for await (const { id, labels, ...response } of iterable) { |
| 45 | + if (response) { |
| 46 | + autoscalingPolicyList.push({ |
| 47 | + ...response, |
| 48 | + id, |
| 49 | + projectId, |
| 50 | + region, |
| 51 | + Labels: labels, |
| 52 | + }) |
| 53 | + } |
| 54 | + } |
| 55 | + } catch (error) { |
| 56 | + generateGcpErrorLog(serviceName, 'dataprocAutoscalingPolicy:listAutoscalingPoliciesAsync', error) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + logger.debug(lt.foundResources(serviceName, autoscalingPolicyList.length)) |
| 61 | + return groupBy(autoscalingPolicyList, 'region') |
| 62 | + } |
0 commit comments