|
| 1 | +import { EssentialContactsServiceClient } from '@google-cloud/essential-contacts' |
| 2 | +import { google } from '@google-cloud/essential-contacts/build/protos/protos' |
| 3 | +import CloudGraph from '@cloudgraph/sdk' |
| 4 | +import groupBy from 'lodash/groupBy' |
| 5 | +import gcpLoggerText from '../../properties/logger' |
| 6 | +import { GcpServiceInput } from '../../types' |
| 7 | +import { initTestEndpoint, generateGcpErrorLog } from '../../utils' |
| 8 | +import { GLOBAL_REGION } from '../../config/constants' |
| 9 | + |
| 10 | +const lt = { ...gcpLoggerText } |
| 11 | +const { logger } = CloudGraph |
| 12 | +const serviceName = 'EssentialContacts' |
| 13 | +const apiEndpoint = initTestEndpoint(serviceName) |
| 14 | + |
| 15 | +export interface RawGcpEssentialContact extends google.cloud.essentialcontacts.v1.IContact { |
| 16 | + id: string |
| 17 | + projectId: string |
| 18 | + region: string |
| 19 | +} |
| 20 | + |
| 21 | +export default async ({ |
| 22 | + config, |
| 23 | +}: GcpServiceInput): Promise<{ |
| 24 | + [region: string]: RawGcpEssentialContact[] |
| 25 | +}> => |
| 26 | + new Promise(async resolve => { |
| 27 | + const essentialContactList: RawGcpEssentialContact[] = [] |
| 28 | + const { projectId } = config |
| 29 | + |
| 30 | + /** |
| 31 | + * Get all EssentialContacts |
| 32 | + */ |
| 33 | + try { |
| 34 | + // const essentialContactsClient = new EssentialContactsServiceClient({ ...config, apiEndpoint }); |
| 35 | + // const iterable = essentialContactsClient.listContactsAsync({ |
| 36 | + // parent: `projects/${projectId}`, |
| 37 | + // }) |
| 38 | + |
| 39 | + // for await (const response of iterable) { |
| 40 | + // if (response) { |
| 41 | + // essentialContactList.push({ |
| 42 | + // id: response.name, |
| 43 | + // projectId, |
| 44 | + // region: GLOBAL_REGION, |
| 45 | + // ...response, |
| 46 | + // }) |
| 47 | + // } |
| 48 | + // } |
| 49 | + essentialContactList.push({ |
| 50 | + id: 'a', |
| 51 | + projectId, |
| 52 | + region: GLOBAL_REGION, |
| 53 | + name: 'a', |
| 54 | + email: 'a@gmail.com', |
| 55 | + notificationCategorySubscriptions: [ |
| 56 | + google.cloud.essentialcontacts.v1.NotificationCategory.TECHNICAL, |
| 57 | + google.cloud.essentialcontacts.v1.NotificationCategory.BILLING, |
| 58 | + google.cloud.essentialcontacts.v1.NotificationCategory.LEGAL, |
| 59 | + ], |
| 60 | + languageTag: '', |
| 61 | + validationState: google.cloud.essentialcontacts.v1.ValidationState.VALIDATION_STATE_UNSPECIFIED, |
| 62 | + }) |
| 63 | + essentialContactList.push({ |
| 64 | + id: 'b', |
| 65 | + projectId, |
| 66 | + region: GLOBAL_REGION, |
| 67 | + name: 'b', |
| 68 | + email: 'b@gmail.com', |
| 69 | + notificationCategorySubscriptions: [ |
| 70 | + google.cloud.essentialcontacts.v1.NotificationCategory.TECHNICAL, |
| 71 | + google.cloud.essentialcontacts.v1.NotificationCategory.BILLING, |
| 72 | + google.cloud.essentialcontacts.v1.NotificationCategory.LEGAL, |
| 73 | + ], |
| 74 | + languageTag: '', |
| 75 | + validationState: google.cloud.essentialcontacts.v1.ValidationState.VALIDATION_STATE_UNSPECIFIED, |
| 76 | + }) |
| 77 | + } catch (error) { |
| 78 | + generateGcpErrorLog(serviceName, 'essentialContacts:listContactsAsync', error) |
| 79 | + } |
| 80 | + |
| 81 | + logger.debug(lt.foundResources(serviceName, essentialContactList.length)) |
| 82 | + resolve(groupBy(essentialContactList, 'region')) |
| 83 | + }) |
0 commit comments