-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdata.ts
More file actions
267 lines (257 loc) · 7.8 KB
/
data.ts
File metadata and controls
267 lines (257 loc) · 7.8 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import chalk from 'chalk'
import isEmpty from 'lodash/isEmpty'
import CloudGraph, {
ProviderData,
StorageEngine,
SchemaMap,
ServiceConnection,
Client,
Logger,
EntityMutations,
} from '@cloudgraph/sdk'
import { scanReport, scanDataType, scanResult } from '../reports'
import { generateMutation, generateUpdateVarsObject } from './mutation'
import { DataToLoad } from '../types'
const { logger } = CloudGraph
/**
* Filters connections acording to the afterNodeInsertion boolean
* this is used to filter connections that need to:
* 1. Be inserted in the add mutation, afterNodeInsertion = false
* 2. Be inserted in the patch mutation, afterNodeInsertion = true
*/
export const filterConnectionsByPriorityOfInsertion = (
connections: { [key: string]: ServiceConnection[] },
afterNodeInsertion: boolean
): { [key: string]: ServiceConnection[] } => {
const filteredConnections: { [key: string]: ServiceConnection[] } = {}
Object.entries(connections).map(([id, sConnections]) => {
const fConnections = sConnections.filter(
(i: ServiceConnection) =>
!!i.insertAfterNodeInsertion === afterNodeInsertion
)
if (!isEmpty(fConnections)) {
filteredConnections[id] = fConnections
}
})
return filteredConnections
}
// the afterNodeInsertion flag provides input
// to whether filter connections that need to be inserted
// in the main add mutation(batch mutation, that pushes fresh nodes and connections)
// or in the patch mutation(list of mutations that patches each node and its connections with others)
export function getConnectedEntity(
service: any,
{ entities, connections: allConnections }: ProviderData,
initiatorServiceName: string,
afterNodeInsertion = false
): Record<string, unknown> {
logger.debug(
`Getting connected entities for ${chalk.green(
initiatorServiceName
)} id = ${chalk.green(service.id)}`
)
const connections: ServiceConnection[] =
filterConnectionsByPriorityOfInsertion(allConnections, afterNodeInsertion)[
service.id
]
const connectedEntity: any = { ...(afterNodeInsertion ? {} : service) }
let connectionsStatus = scanResult.pass
if (!isEmpty(connections)) {
for (const connection of connections) {
const entityData = entities.find(
({ name }: { name: string }) => name === connection.resourceType
)
if (entityData && entityData.data) {
const entityForConnection = entityData.data.find(
({ id }: { id: string }) => connection.id === id
)
if (!isEmpty(entityForConnection)) {
if (!connectedEntity[connection.field]) {
connectedEntity[connection.field] = []
}
connectedEntity[connection.field].push(entityForConnection)
logger.debug(
`(${initiatorServiceName}) ${service.id} ${chalk.green(
'<----->'
)} ${connection.id} (${connection.resourceType})`
)
} else {
connectionsStatus = scanResult.warn
const error = `Malformed connection found between ${chalk.red(
initiatorServiceName
)} && ${chalk.red(connection.resourceType)} services.`
logger.warn(error)
logger.warn(
`(${initiatorServiceName}) ${service.id} ${chalk.red('<-///->')} ${
connection.id
} (${connection.resourceType})`
)
}
}
}
}
scanReport.pushData({
service: initiatorServiceName,
type: scanDataType.status,
result: connectionsStatus,
})
return connectedEntity
}
export const processConnectionsBetweenEntities = ({
provider,
providerData,
storageEngine,
storageRunning,
schemaMap,
}: {
provider?: string
providerData: ProviderData
storageEngine: StorageEngine
storageRunning: boolean
schemaMap?: SchemaMap
}): void => {
for (const entity of providerData.entities) {
const { data, name, mutation } = entity
let connectedData
if (data instanceof Array) {
connectedData = data.map((service: any) => {
scanReport.pushData({
service: name,
type: scanDataType.count,
result: scanResult.pass,
})
return getConnectedEntity(service, providerData, name)
})
} else {
connectedData = data
}
if (storageRunning) {
// Add service mutation to promises array
const query: string =
(mutation as EntityMutations)?.upsert || (mutation as string)
storageEngine.push({
query:
query ||
(provider &&
generateMutation({ type: 'add', provider, entity, schemaMap })) ||
'',
input: connectedData,
name,
})
}
}
}
export function insertEntitiesAndConnections({
provider,
providerData,
storageEngine,
storageRunning,
schemaMap,
}: DataToLoad): void {
for (const entity of providerData.entities) {
try {
const { data, mutation, name } = entity
const connectedData = data.map((service: any) => {
scanReport.pushData({
service: name,
type: scanDataType.count,
result: scanResult.pass,
})
return getConnectedEntity(service, providerData, name)
})
if (storageRunning) {
const query: string =
(mutation as EntityMutations)?.upsert || (mutation as string)
storageEngine.push({
query:
query ||
generateMutation({ type: 'add', provider, entity, schemaMap }) ||
'',
input: connectedData,
name,
})
}
} catch (error) {
logger.debug(error)
}
}
}
export function processConnectionsAfterInitialInsertion({
provider,
providerData,
storageEngine,
storageRunning,
schemaMap,
}: DataToLoad): void {
const additionalConnections: {
[key: string]: ServiceConnection[]
} = filterConnectionsByPriorityOfInsertion(providerData.connections, true)
if (!isEmpty(additionalConnections)) {
// Filter resourceTypes that have additional connections to process
const resourcesWithAdditionalConnections = new Set(
Object.values(additionalConnections)
.flat()
.map(({ resourceType }) => resourceType)
)
// Filter entities that match filtered resourceTypes
const entities = providerData.entities.filter(({ name }) =>
resourcesWithAdditionalConnections.has(name)
)
for (const entity of entities) {
try {
const { data, name } = entity
data.map((service: any) => {
const connections = getConnectedEntity(
service,
providerData,
name,
true
)
if (!isEmpty(connections)) {
if (storageRunning) {
const query = generateMutation({
type: 'update',
provider,
entity,
schemaMap,
})
const patch = generateUpdateVarsObject(service, connections)
// Add service mutation to promises array
storageEngine.push({ query, patch, name })
}
}
})
} catch (error) {
logger.debug(error)
}
}
}
}
export const loadAllData = (
providerClient: Client,
data: DataToLoad,
loggerInstance: Logger
): void => {
loggerInstance.startSpinner(
`Inserting entities and connections for ${chalk.italic.green(
data.provider
)}`
)
insertEntitiesAndConnections(data)
loggerInstance.successSpinner(
`Entities and connections inserted successfully for ${chalk.italic.green(
data.provider
)}`
)
loggerInstance.startSpinner(
`Processing additional service connections for ${chalk.italic.green(
data.provider
)}`
)
processConnectionsAfterInitialInsertion(data)
loggerInstance.successSpinner(
`Additional connections processed successfully for ${chalk.italic.green(
data.provider
)}`
)
}