-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathVroRestClient.ts
More file actions
589 lines (503 loc) · 20.4 KB
/
VroRestClient.ts
File metadata and controls
589 lines (503 loc) · 20.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*!
* Copyright 2018-2020 VMware, Inc.
* SPDX-License-Identifier: MIT
*/
import * as http from "http"
import * as fs from "fs-extra"
import * as request from "request-promise-native"
import { ApiCategoryType } from "../types"
import { BaseConfiguration, BaseEnvironment } from "../platform"
import { Auth, BasicAuth, VraSsoAuth } from "./auth"
import {
ApiElement,
ContentChildrenResponse,
ContentLinksResponse,
InventoryElement,
LogMessage,
Version,
WorkflowLogsResponse,
WorkflowParam,
WorkflowState
} from "./vro-model"
import { Logger, MavenCliProxy, promise, sleep } from ".."
export class VroRestClient {
private readonly logger = Logger.get("VroRestClient")
constructor(private settings: BaseConfiguration, private environment: BaseEnvironment) {
// empty
}
private get hostname(): string {
return this.settings.activeProfile.get("vro.host")
}
private get port(): number {
return parseInt(this.settings.activeProfile.getOptional("vro.port", "8281"), 10)
}
private get authMethod(): string {
return this.settings.activeProfile.getOptional("vro.auth", "basic")
}
private async getAuth(): Promise<Record<string, unknown>> {
let auth: Auth
switch (this.authMethod.toLowerCase()) {
case "vra":
const maven = new MavenCliProxy(this.environment, this.settings.vrdev.maven, this.logger)
auth = new VraSsoAuth(await maven.getToken())
break
case "basic":
auth = new BasicAuth(
this.settings.activeProfile.get("vro.username"),
this.settings.activeProfile.get("vro.password")
)
break
default:
throw new Error(`Unsupported authentication mechanism: ${this.authMethod}`)
}
return auth.toRequestJson()
}
private async send<T = any>(
method: "GET" | "POST" | "PUT" | "DELETE",
route: string,
options?: Partial<request.OptionsWithUrl>
): Promise<T> {
const url = route.indexOf("://") > 0 ? route : `https://${this.hostname}:${this.port}/vco/api/${route}`
return request({
headers: {
Accept: "application/json"
},
json: true,
simple: true, // reject non-2xx
resolveWithFullResponse: false,
rejectUnauthorized: false,
...options,
method,
url,
auth: { ...(await this.getAuth()) }
})
}
async getVersion(): Promise<Version> {
return this.send("GET", "about")
}
async getWorkflow(id: string): Promise<any> {
return this.send("GET", `workflows/${id}`)
}
async getAction(id: string): Promise<any> {
return this.send("GET", `actions/${id}`)
}
async getConfiguration(id: string): Promise<any> {
return this.send("GET", `configurations/${id}`)
}
async executeWorkflow(id: string, ...inputParams: WorkflowParam[]): Promise<WorkflowParam[]> {
const token: string = await this.startWorkflow(id, ...inputParams)
let response = await this.getWorkflowExecution(id, token)
while (response.state === "running") {
await sleep(1000)
response = await this.getWorkflowExecution(id, token)
}
return response["output-parameters"]
}
async getWorkflowExecution(id: string, token: string): Promise<any> {
return this.send("GET", `workflows/${id}/executions/${token}`)
}
async getWorkflowExecutionState(id: string, token: string): Promise<WorkflowState> {
const response = await this.send("GET", `workflows/${id}/executions/${token}`, {
resolveWithFullResponse: false
})
return response.state
}
async startWorkflow(id: string, ...inputParams: WorkflowParam[]): Promise<string> {
const executeOptions = {
body: {} as any,
resolveWithFullResponse: true
}
if (inputParams.length > 0) {
executeOptions.body.parameters = []
for (const param of inputParams) {
executeOptions.body.parameters.push({
...param,
scope: "local"
})
}
}
const execResponse = await this.send("POST", `workflows/${id}/executions`, executeOptions)
if (execResponse.statusCode !== 202) {
throw new Error(`Expected status code 202, but got ${execResponse.statusCode}`)
}
let location: string | undefined = execResponse.headers.location
if (!location) {
throw new Error(`Missing location header in the response of POST /workflows/${id}/executions`)
}
location = location.replace(/\/$/, "") // remove trailing slash
const execToken = location.substring(location.lastIndexOf("/") + 1)
return execToken
}
async getWorkflowLogsPre76(
workflowId: string,
executionId: string,
severity: string,
timestamp: number
): Promise<LogMessage[]> {
const executeOptions = {
body: {
"severity": severity,
"older-than": timestamp
}
}
const response: WorkflowLogsResponse = await this.send(
"POST",
`workflows/${workflowId}/executions/${executionId}/syslogs`,
executeOptions
)
const messages: LogMessage[] = []
for (const log of response.logs) {
const e = log.entry
const description = e["long-description"] ? e["long-description"] : e["short-description"]
if (
e.origin === "server" || // skip server messages, as they are always included in the result
description.indexOf("*** End of execution stack.") > 0 ||
description.startsWith("__item_stack:/")
) {
continue
}
messages.push({
timestamp: e["time-stamp"],
severity: e.severity,
description
})
}
return messages
}
async getWorkflowLogsPost76(
workflowId: string,
executionId: string,
severity: string,
timestamp: number
): Promise<LogMessage[]> {
const response: WorkflowLogsResponse = await this.send(
"GET",
`workflows/${workflowId}/executions/${executionId}/syslogs` +
`?conditions=severity=${severity}` +
`&conditions=timestamp${encodeURIComponent(">")}${timestamp}` +
"&conditions=type=system"
)
const messages: LogMessage[] = []
for (const log of response.logs) {
const e = log.entry
const description = e["long-description"] ? e["long-description"] : e["short-description"]
if (description.indexOf("*** End of execution stack.") > 0 || description.startsWith("__item_stack:/")) {
continue
}
messages.push({
timestamp: e["time-stamp"],
severity: e.severity,
description
})
}
return messages
}
async importPackage(path: string): Promise<void> {
return this.send("POST", "content/packages?overwrite=true", {
formData: {
file: [fs.createReadStream(path)]
},
resolveWithFullResponse: true
})
}
async deletePackage(
name: string,
option: "deletePackage" | "deletePackageWithContent" | "deletePackageKeepingShared"
): Promise<void> {
return this.send("DELETE", `packages/${name}/?option=${option}`, {
resolveWithFullResponse: true
})
}
async getPlugins(): Promise<any> {
return this.send("GET", "plugins")
}
async getPackages(): Promise<string[]> {
const responseJson: ContentLinksResponse = await this.send("GET", "packages")
const packages: string[] = responseJson.link
.map(pkg => {
const name = pkg.attributes.find(att => att.name === "name")
return name ? name.value : undefined
})
.filter(val => val !== undefined) as string[]
return packages.sort()
}
async getActions(): Promise<{ fqn: string; id: string; version: string }[]> {
const responseJson: ContentLinksResponse = await this.send("GET", "actions")
const actions: { fqn: string; id: string; version: string }[] = responseJson.link
.map(action => {
if (!action.attributes) {
return undefined
}
const fqn = action.attributes.find(att => att.name === "fqn")
const id = action.attributes.find(att => att.name === "id")
const version = action.attributes.find(att => att.name === "version")
return {
fqn: fqn ? fqn.value : undefined,
id: id ? id.value : undefined,
version: version ? version.value : undefined
}
})
.filter(val => {
return !!val && val.fqn !== undefined && val.id !== undefined
}) as { fqn: string; id: string; version: string }[]
return actions.sort((x, y) => x.fqn.localeCompare(y.fqn))
}
async getWorkflows(): Promise<{ name: string; id: string; version: string }[]> {
const responseJson: ContentLinksResponse = await this.send("GET", "workflows")
const workflows: { name: string; id: string; version: string }[] = responseJson.link
.map(wf => {
if (!wf.attributes) {
return undefined
}
const name = wf.attributes.find(att => att.name === "name")
const id = wf.attributes.find(att => att.name === "id")
const version = wf.attributes.find(att => att.name === "version")
return {
name: name ? name.value : undefined,
id: id ? id.value : undefined,
version: version ? version.value : undefined
}
})
.filter(val => {
return !!val && val.name !== undefined && val.id !== undefined
}) as { name: string; id: string; version: string }[]
return workflows.sort((x, y) => x.name.localeCompare(y.name))
}
async getConfigurations(): Promise<{ name: string; id: string; version: string }[]> {
const responseJson: ContentLinksResponse = await this.send("GET", "configurations")
const configs: { name: string; id: string; version: string }[] = responseJson.link
.map(conf => {
if (!conf.attributes) {
return undefined
}
const name = conf.attributes.find(att => att.name === "name")
const id = conf.attributes.find(att => att.name === "id")
const version = conf.attributes.find(att => att.name === "version")
return {
name: name ? name.value : undefined,
id: id ? id.value : undefined,
version: version ? version.value : undefined
}
})
.filter(val => {
return !!val && val.name !== undefined && val.id !== undefined
}) as { name: string; id: string; version: string }[]
return configs.sort((x, y) => x.name.localeCompare(y.name))
}
async getResources(): Promise<{ name: string; id: string }[]> {
const responseJson: ContentLinksResponse = await this.send("GET", "resources")
const resources: { name: string; id: string }[] = responseJson.link
.map(res => {
if (!res.attributes) {
return undefined
}
const name = res.attributes.find(att => att.name === "name")
const id = res.attributes.find(att => att.name === "id")
return {
name: name ? name.value : undefined,
id: id ? id.value : undefined
}
})
.filter(val => {
return !!val && val.name !== undefined && val.id !== undefined
}) as { name: string; id: string }[]
return resources.sort((x, y) => x.name.localeCompare(y.name))
}
async getRootCategories(categoryType: ApiCategoryType): Promise<ApiElement[]> {
const responseJson: ContentLinksResponse = await this.send(
"GET",
`categories?isRoot=true&categoryType=${categoryType}`
)
const categories = responseJson.link
.map(child => {
const name = child.attributes.find(att => att.name === "name")
const id = child.attributes.find(att => att.name === "id")
return {
name: name ? name.value : undefined,
id: id ? id.value : undefined,
type: categoryType,
rel: child.rel
}
})
.filter(val => {
return val.name !== undefined && val.id !== undefined
}) as ApiElement[]
return categories.sort((x, y) => x.name.localeCompare(y.name))
}
async getChildrenOfCategory(categoryId: string): Promise<ApiElement[]> {
const responseJson: ContentChildrenResponse = await this.send("GET", `categories/${categoryId}`)
const children = responseJson.relations.link
.map(child => {
if (!child.attributes) {
return undefined
}
const name = child.attributes.find(att => att.name === "name")
const id = child.attributes.find(att => att.name === "id")
const type = child.attributes.find(att => att.name === "type")
return {
name: name ? name.value : undefined,
id: id ? id.value : undefined,
type: type ? type.value : undefined,
rel: child.rel
}
})
.filter(val => {
return !!val && val.name !== undefined && val.id !== undefined && val.type !== undefined
}) as ApiElement[]
return children.sort((x, y) => x.name.localeCompare(y.name))
}
async getResource(id: string): Promise<http.IncomingMessage> {
const options = {
simple: true, // reject non-2xx
resolveWithFullResponse: true,
rejectUnauthorized: false,
method: "GET",
uri: `https://${this.hostname}:${this.port}/vco/api/resources/${id}`,
auth: { ...(await this.getAuth()) },
json: false,
headers: {
Accept: "application/octet-stream"
}
}
return promise.requestPromiseStream(options)
}
async getResourceInfo(id: string): Promise<any> {
return this.send("GET", `resources/${id}`)
}
async getInventoryItem(href: string): Promise<any> {
return this.send("GET", href)
}
async getInventoryItems(href?: string): Promise<InventoryElement[]> {
const responseJson: ContentChildrenResponse = await this.send("GET", href || "inventory")
const children = responseJson.relations.link
.map(child => {
if (!child.attributes) {
return undefined
}
const id =
child.attributes.find(att => att.name === "id") ||
child.attributes.find(att => att.name === "dunesId")
const name =
child.attributes.find(att => att.name === "displayName") ||
child.attributes.find(att => att.name === "name")
const type =
child.attributes.find(att => att.name === "type") ||
child.attributes.find(att => att.name === "@type")
return {
name: name ? name.value : undefined,
id: id ? id.value : undefined,
type: type ? type.value : undefined,
rel: child.rel,
href: child.href
}
})
.filter(val => {
return !!val && val.name !== undefined && val.href !== undefined
}) as InventoryElement[]
return children.sort((x, y) => x.name.localeCompare(y.name))
}
async fetchIcon(namespace: string, type: string, targetPath: string): Promise<void> {
type = type || ""
const url = `https://${this.hostname}:${this.port}/vco/api/catalog/${namespace}/${type}/metadata/icon`
const options = {
headers: {
Accept: "application/json"
},
simple: true, // reject non-2xx
resolveWithFullResponse: false,
rejectUnauthorized: false,
json: false,
method: "GET",
url,
auth: { ...(await this.getAuth()) }
}
const stream = request(options).pipe(fs.createWriteStream(targetPath))
return new Promise((resolve, reject) => {
stream.on("finish", () => resolve())
stream.on("error", e => reject(e))
})
}
async fetchWorkflowSchema(id: string, targetPath: string): Promise<void> {
const options = {
simple: true, // reject non-2xx
resolveWithFullResponse: true,
rejectUnauthorized: false,
headers: {},
json: false,
method: "GET",
uri: `https://${this.hostname}:${this.port}/vco/api/workflows/${id}/schema`,
auth: { ...(await this.getAuth()) }
}
const stream = request(options).pipe(fs.createWriteStream(targetPath))
return new Promise((resolve, reject) => {
stream.on("finish", () => resolve())
stream.on("error", e => reject(e))
})
}
async fetchAction(id: string, targetPath: string): Promise<void> {
const uri = `https://${this.hostname}:${this.port}/vco/api/actions/${id}/`
const options = {
simple: true, // reject non-2xx
resolveWithFullResponse: false,
rejectUnauthorized: false,
headers: {
Accept: "application/zip"
},
json: false,
method: "GET",
uri,
auth: { ...(await this.getAuth()) }
}
const stream = request(options).pipe(fs.createWriteStream(targetPath))
return new Promise((resolve, reject) => {
stream.on("finish", () => resolve())
stream.on("error", e => reject(e))
})
}
async fetchWorkflow(id: string, targetPath: string): Promise<void> {
const uri = `https://${this.hostname}:${this.port}/vco/api/content/workflows/${id}/`
const options = {
simple: true, // reject non-2xx
resolveWithFullResponse: false,
rejectUnauthorized: false,
headers: {},
json: false,
method: "GET",
uri,
auth: { ...(await this.getAuth()) }
}
const stream = request(options).pipe(fs.createWriteStream(targetPath))
return new Promise((resolve, reject) => {
stream.on("finish", () => resolve())
stream.on("error", e => reject(e))
})
}
async fetchResource(id: string, targetPath: string): Promise<void> {
const uri = `https://${this.hostname}:${this.port}/vco/api/resources/${id}/`
const options = {
simple: true, // reject non-2xx
resolveWithFullResponse: false,
rejectUnauthorized: false,
headers: {
Accept: "application/octet-stream"
},
json: false,
method: "GET",
uri,
auth: { ...(await this.getAuth()) }
}
const stream = request(options).pipe(fs.createWriteStream(targetPath))
return new Promise((resolve, reject) => {
stream.on("finish", () => resolve())
stream.on("error", e => reject(e))
})
}
async getConfigElementXml(id: string): Promise<string> {
return this.send("GET", `configurations/${id}/`, {
headers: {
Accept: "application/vcoobject+xml"
},
json: false
})
}
}