Skip to content

Commit 305da4f

Browse files
feat(CG-1312): add essential contacts
1 parent d6cde0c commit 305da4f

13 files changed

Lines changed: 177 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@google-cloud/dataproc": "^3.2.0",
4343
"@google-cloud/datastore": "^6.6.2",
4444
"@google-cloud/dns": "^2.2.3",
45+
"@google-cloud/essential-contacts": "^2.0.2",
4546
"@google-cloud/firestore": "^5.0.2",
4647
"@google-cloud/functions": "^1.2.0",
4748
"@google-cloud/kms": "^2.10.0",

src/enums/schemasMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ export default {
5353
[services.apiGatewayApi]: 'gcpApiGatewayApi',
5454
[services.apiGatewayApiConfig]: 'gcpApiGatewayApiConfig',
5555
[services.firestoreDatabase]: 'gcpFirestoreDatabase',
56+
[services.essentialContacts]: 'gcpEssentialContact',
5657
tag: 'gcpTag',
5758
}

src/enums/serviceMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import GcpFirestoreDatabase from '../services/firestore'
5050
import GcpLabel from '../services/label'
5151
import GcpTag from '../services/tag'
5252
import GcpBilling from '../services/billing'
53+
import GcpEssentialContacts from '../services/essentialContacts'
5354

5455
/**
5556
* serviceMap is an object that contains all currently supported services
@@ -105,6 +106,7 @@ export default {
105106
[services.apiGatewayApi]: GcpApiGatewayApi,
106107
[services.apiGatewayApiConfig]: GcpApiGatewayApiConfig,
107108
[services.firestoreDatabase]: GcpFirestoreDatabase,
109+
[services.essentialContacts]: GcpEssentialContacts,
108110
label: GcpLabel,
109111
tag: GcpTag,
110112
}

src/enums/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default {
7575
// cloudShell: 'cloud-shell',
7676
// debug: 'debug',
7777
// deploymentManager: 'deployment-manager',
78-
// essentialContacts: 'essential-contacts',
78+
essentialContacts: 'essentialContacts',
7979
logBucket: 'logBucket',
8080
logMetric: 'logMetric',
8181
logSink: 'logSink',
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { google } from '@google-cloud/essential-contacts/build/protos/protos'
2+
import { GcpEssentialContact } from '../../types/generated'
3+
import { RawGcpEssentialContact } from './data'
4+
import { toISOString } from '../../utils/dateutils'
5+
import { enumKeyToString } from '../../utils/format'
6+
7+
export default ({
8+
service,
9+
}: {
10+
service: RawGcpEssentialContact
11+
region: string
12+
}): GcpEssentialContact => {
13+
const {
14+
id,
15+
projectId,
16+
region,
17+
name,
18+
email,
19+
notificationCategorySubscriptions,
20+
languageTag,
21+
validationState,
22+
validateTime,
23+
} = service
24+
25+
return {
26+
id,
27+
projectId,
28+
region,
29+
name,
30+
email,
31+
notificationCategorySubscriptions: notificationCategorySubscriptions.map(subscription =>
32+
enumKeyToString(google.cloud.essentialcontacts.v1.NotificationCategory, subscription)
33+
),
34+
languageTag,
35+
validationState: enumKeyToString(google.cloud.essentialcontacts.v1.ValidationState, validationState),
36+
validateTime: toISOString(validateTime?.seconds?.toString()),
37+
}
38+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Service } from '@cloudgraph/sdk'
2+
import BaseService from '../base'
3+
import format from './format'
4+
import getData from './data'
5+
import mutation from './mutation'
6+
7+
export default class GcpEssentialContacts extends BaseService implements Service {
8+
format = format.bind(this)
9+
10+
getData = getData.bind(this)
11+
12+
mutation = mutation
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default `mutation($input: [AddgcpEssentialContactInput!]!) {
2+
addgcpEssentialContact(input: $input, upsert: true) {
3+
numUids
4+
}
5+
}`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type gcpEssentialContact implements gcpBaseResource
2+
@generate(
3+
query: { get: true, query: true, aggregate: true }
4+
mutation: { add: true, delete: false }
5+
subscription: false
6+
)
7+
@key(fields: "id") {
8+
email: String @search(by: [hash, regexp])
9+
notificationCategorySubscriptions: [String] @search(by: [hash])
10+
languageTag: String @search(by: [hash, regexp])
11+
validationState: String @search(by: [hash, regexp])
12+
validateTime: String @search(by: [hash, regexp])
13+
project: [gcpProject] @hasInverse(field: essentialContacts)
14+
}

src/services/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export default class Provider extends CloudGraph.Client {
339339
this.logger.success(`${resource} scan completed`)
340340
} else {
341341
this.logger.warn(
342-
`Skipping service ${resource} as there was an issue getting data for it. Is it currently supported?`
342+
`Skipping service ${resource} as there was an issue getting data for it. Is it currently supported????`
343343
)
344344
}
345345
}

0 commit comments

Comments
 (0)