Skip to content

Commit 9313b4a

Browse files
hjaraujoftyler-dunkel
authored andcommitted
feat: add additional connections mutation to insert edges between existing entities
1 parent e06f663 commit 9313b4a

8 files changed

Lines changed: 2732 additions & 2956 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"bugs": "https://github.com/cloudgraphdev/cli/issues",
1717
"dependencies": {
18-
"@cloudgraph/sdk": "0.10.4",
18+
"@cloudgraph/sdk": "^0.10.6",
1919
"@graphql-tools/load-files": "^6.3.2",
2020
"@graphql-tools/merge": "^8.2.0",
2121
"@oclif/command": "^1",

src/commands/load.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isEmpty } from 'lodash'
66

77
import Command from './base'
88
import { fileUtils } from '../utils'
9-
import { processConnectionsBetweenEntities } from '../utils/data'
9+
import { loadAllData } from '../utils/data'
1010

1111
export default class Load extends Command {
1212
static description = 'Load a specific version of your CloudGraph data'
@@ -81,8 +81,9 @@ export default class Load extends Command {
8181
this.logger.info(
8282
`Beginning ${chalk.italic.green('LOAD')} for ${provider}`
8383
)
84-
const { client, schemasMap } = await this.getProviderClient(provider)
85-
if (!client) {
84+
const { client: providerClient, schemasMap: schemaMap } =
85+
await this.getProviderClient(provider)
86+
if (!providerClient) {
8687
continue // eslint-disable-line no-continue
8788
}
8889

@@ -186,26 +187,16 @@ export default class Load extends Command {
186187
)} loaded successfully for ${chalk.italic.green(provider)}`
187188
)
188189

189-
/**
190-
* Loop through the providerData entities and for each entity:
191-
* Look in providerData.connections for [key = entity.id]
192-
* Loop through the connections for entity and determine its resource type
193-
* Find entity in providerData.entities that matches the id found in connections
194-
* Build connectedEntity by pushing the matched entity into the field corresponding to that entity (alb.ec2Instance => [ec2Instance])
195-
* Push connected entity into dgraph
196-
*/
197-
this.logger.startSpinner(
198-
`Making service connections for ${chalk.italic.green(provider)}`
199-
)
200-
processConnectionsBetweenEntities({
201-
provider,
202-
providerData,
203-
storageEngine,
204-
storageRunning,
205-
schemaMap: schemasMap,
206-
})
207-
this.logger.successSpinner(
208-
`Connections made successfully for ${chalk.italic.green(provider)}`
190+
loadAllData(
191+
providerClient,
192+
{
193+
provider,
194+
providerData,
195+
storageEngine,
196+
storageRunning,
197+
schemaMap,
198+
},
199+
this.logger
209200
)
210201
}
211202

src/commands/scan.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { range } from 'lodash'
66

77
import Command from './base'
88
import { fileUtils } from '../utils'
9-
import { processConnectionsBetweenEntities } from '../utils/data'
109
import DgraphEngine from '../storage/dgraph'
1110
import { scanReport } from '../reports'
11+
import { loadAllData, processConnectionsBetweenEntities } from '../utils/data'
1212

1313
export default class Scan extends Command {
1414
static description =
@@ -112,10 +112,9 @@ export default class Scan extends Command {
112112
this.logger.info(
113113
`Beginning ${chalk.italic.green('SCAN')} for ${provider}`
114114
)
115-
const { client, schemasMap, serviceKey } = await this.getProviderClient(
116-
provider
117-
)
118-
if (!client) {
115+
const { client: providerClient, schemasMap, serviceKey } =
116+
await this.getProviderClient(provider)
117+
if (!providerClient) {
119118
failedProviderList.push(provider)
120119
this.logger.warn(`No valid client found for ${provider}, skipping...`)
121120
continue // eslint-disable-line no-continue
@@ -142,7 +141,9 @@ export default class Scan extends Command {
142141
})
143142

144143
// Get the Plugin Manager
145-
const pluginManager = this.getPluginManager(cloudGraphPlugin[key])
144+
const pluginManager = this.getPluginManager(
145+
cloudGraphPlugin[key]
146+
)
146147

147148
// Configure
148149
await PluginInstance.configure(pluginManager)
@@ -167,7 +168,7 @@ export default class Scan extends Command {
167168
provider
168169
)}`
169170
)
170-
const providerData = await client.getData({
171+
const providerData = await providerClient.getData({
171172
opts,
172173
})
173174
this.logger.successSpinner(
@@ -180,7 +181,7 @@ export default class Scan extends Command {
180181
provider
181182
)}`
182183
)
183-
const providerSchema: string = client.getSchema()
184+
const providerSchema: string = providerClient.getSchema()
184185
if (!providerSchema) {
185186
this.logger.warn(`No schema found for ${provider}, moving on`)
186187
continue // eslint-disable-line no-continue
@@ -231,18 +232,16 @@ export default class Scan extends Command {
231232
this.exit()
232233
}
233234

234-
this.logger.startSpinner(
235-
`Making service connections for ${chalk.italic.green(provider)}`
236-
)
237-
processConnectionsBetweenEntities({
238-
provider,
239-
providerData,
240-
storageEngine,
241-
storageRunning,
242-
schemaMap: schemasMap,
243-
})
244-
this.logger.successSpinner(
245-
`Connections made successfully for ${chalk.italic.green(provider)}`
235+
loadAllData(
236+
providerClient,
237+
{
238+
provider,
239+
providerData,
240+
storageEngine,
241+
storageRunning,
242+
schemaMap: schemasMap,
243+
},
244+
this.logger
246245
)
247246
}
248247

src/storage/dgraph/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
StorageEngineConfig,
44
StorageEngine,
55
GraphQLInputData,
6+
GraphQLQueryData
67
} from '@cloudgraph/sdk'
78

89
import DGraphClientWrapper from './base'
@@ -151,11 +152,12 @@ export default class DgraphEngine
151152
* Add Service Mutation to axiosPromises Array
152153
*/
153154
push(data: GraphQLInputData): void {
154-
const { query, connectedData } = data
155-
const queryData = {
155+
const { query, input, patch } = data
156+
const queryData: GraphQLQueryData = {
156157
query,
157158
variables: {
158-
input: connectedData,
159+
input,
160+
patch,
159161
},
160162
}
161163
this.axiosPromises.push(() =>

src/storage/dgraph/utils.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,35 @@ export function processGQLExecutionResult({
9595
let executedMutationNames: string[] = []
9696
if (!isEmpty(mutationResultData)) {
9797
executedMutationNames = Object.keys(mutationResultData) || []
98-
executedMutationNames.forEach(mutationName => {
99-
if (mutationResultData[mutationName]) {
100-
const { numUids } = mutationResultData[mutationName]
101-
const numUidsString = numUids ? `numUids affected: ${numUids}` : ''
102-
logger.debug(
103-
`mutation ${chalk.green(
104-
mutationName
105-
)} completed successfully. ${numUidsString}`
106-
)
107-
}
108-
})
98+
if (
99+
!isEmpty(executedMutationNames) &&
100+
executedMutationNames[0].includes('add')
101+
) {
102+
executedMutationNames.forEach(mutationName => {
103+
if (mutationResultData[mutationName]) {
104+
const { numUids } = mutationResultData[mutationName]
105+
const numUidsString = numUids ? `numUids affected: ${numUids}` : ''
106+
logger.debug(
107+
`mutation ${chalk.green(
108+
mutationName
109+
)} completed successfully. ${numUidsString}`
110+
)
111+
}
112+
})
113+
}
114+
// Leaving this block here in case we need/want
115+
// to print the output the result of the patch mutations
116+
//
117+
// if (executedMutationNames[0].includes('update')) {
118+
// executedMutationNames.forEach(mutation => {
119+
// const serviceName = mutation.split('update')[1]
120+
// const filter = mutationResultData[mutation][serviceName]
121+
// if (filter && filter[0]) {
122+
// const { id } = filter[0]
123+
// logger.debug(`Connections added for id ${chalk.green(id)}.`)
124+
// }
125+
// })
126+
// }
109127
}
110128
processErrorArrayIfExists({
111129
errors: dataErrors,

src/types/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
import { ProviderData, StorageEngine } from '@cloudgraph/sdk'
2+
13
export interface CloudGraphConfig {
24
[key: string]: unknown | Record<NonNullable<string | number>, unknown>
35
}
46

57
export type SchemaMap = {
68
[schemaName: string]: string
79
}
10+
11+
export interface DataToLoad {
12+
provider: string
13+
providerData: ProviderData
14+
storageEngine: StorageEngine
15+
storageRunning: boolean
16+
schemaMap: SchemaMap | undefined
17+
}

0 commit comments

Comments
 (0)