-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata.ts
More file actions
134 lines (123 loc) · 3.68 KB
/
data.ts
File metadata and controls
134 lines (123 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
OrganizationsClient,
TagKeysClient,
TagValuesClient,
} from '@google-cloud/resource-manager'
import { google } from '@google-cloud/resource-manager/build/protos/protos'
import CloudGraph from '@cloudgraph/sdk'
import groupBy from 'lodash/groupBy'
import gcpLoggerText from '../../properties/logger'
import { GcpServiceInput, TagMap } from '../../types'
import { initTestEndpoint, generateGcpErrorLog } from '../../utils'
import { GLOBAL_REGION } from '../../config/constants'
const lt = { ...gcpLoggerText }
const { logger } = CloudGraph
const serviceName = 'Organization'
const apiEndpoint = initTestEndpoint(serviceName)
export interface RawGcpOrganization
extends google.cloud.resourcemanager.v3.IOrganization {
id: string
projectId: string
region: string
tags?: TagMap
}
export const listOrganizationsData = async (
organizationsClient: OrganizationsClient,
projectId: string,
orgList: RawGcpOrganization[]
): Promise<void> =>
new Promise<void>(async resolve => {
/**
* Get all the Organizations
*/
try {
const iterable = organizationsClient.searchOrganizationsAsync()
// https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#auto-pagination
for await (const response of iterable) {
if (response) {
orgList.push({
id: response.name,
...response,
projectId,
region: GLOBAL_REGION,
})
}
}
} catch (error) {
generateGcpErrorLog(
serviceName,
'resourceManager:searchOrganizationsAsync',
error
)
}
resolve()
})
export const getTags = async ({
tagKeysClient,
tagValuesClient,
resourceId,
}: {
tagKeysClient: TagKeysClient
tagValuesClient: TagValuesClient
resourceId: string
}): Promise<TagMap> =>
new Promise(async resolve => {
const tags: TagMap = {}
try {
const iterable = tagKeysClient.listTagKeysAsync({ parent: resourceId })
for await (const response of iterable) {
if (response) {
const { name: parent, shortName: tagKey } = response
const tagValuesIterable = await tagValuesClient.listTagValuesAsync({ parent })
const tagValues: string[] = []
for await (const tagValue of tagValuesIterable) {
if (tagValue) {
tagValues.push(tagValue.shortName)
}
}
tags[tagKey] = tagValues
}
}
} catch (error) {
generateGcpErrorLog(
serviceName,
'resourceManager:listTagKeysAsync',
error
)
}
resolve(tags)
})
export default async ({
config,
}: GcpServiceInput): Promise<{
[region: string]: RawGcpOrganization[]
}> =>
new Promise(async resolve => {
const orgList: RawGcpOrganization[] = []
const tagKeysClient = new TagKeysClient({ ...config, apiEndpoint })
const tagValuesClient = new TagValuesClient({ ...config, apiEndpoint })
const tagsPromises = []
const { projectId } = config
const organizationsClient = new OrganizationsClient({
...config,
apiEndpoint,
})
// Get all Organizations
await listOrganizationsData(organizationsClient, projectId, orgList)
// Add tags to each Organization
orgList.map(({ id }, idx) => {
const tagsPromise = new Promise<void>(async resolveTags => {
const tags = await getTags({
tagKeysClient,
tagValuesClient,
resourceId: id,
})
orgList[idx].tags = tags || {}
resolveTags()
})
tagsPromises.push(tagsPromise)
})
await Promise.all(tagsPromises)
logger.debug(lt.foundResources(serviceName, orgList.length))
resolve(groupBy(orgList, 'region'))
})