@contrail/cli 1.0.53 → 1.0.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cli.js +59 -10
- package/lib/types/option-set-hierarchy-exporter.d.ts +1 -0
- package/lib/types/option-set-hierarchy-exporter.js +113 -0
- package/lib/types/option-set-hierarchy-parser.d.ts +2 -2
- package/lib/types/option-set-hierarchy-parser.js +9 -9
- package/lib/types/option-set-hierarchy-uploader.d.ts +1 -0
- package/lib/types/option-set-hierarchy-uploader.js +110 -0
- package/lib/types/option-set-uploader.js +1 -1
- package/lib/types/rule-set-uploader.d.ts +1 -0
- package/lib/types/rule-set-uploader.js +175 -0
- package/lib/types/type-property-policy-uploader.d.ts +1 -0
- package/lib/types/type-property-policy-uploader.js +173 -0
- package/lib/types/types-extractor.d.ts +51 -2
- package/lib/types/types-extractor.js +204 -48
- package/lib/types/types-properties-merger.js +14 -8
- package/lib/types/types-uploader.js +1 -1
- package/lib/types/usage-info-uploader.d.ts +1 -0
- package/lib/types/usage-info-uploader.js +128 -0
- package/package.json +2 -1
- package/src/cli.ts +59 -10
- package/src/types/option-set-hierarchy-exporter.ts +125 -0
- package/src/types/option-set-hierarchy-parser.ts +9 -9
- package/src/types/option-set-hierarchy-uploader.ts +119 -0
- package/src/types/option-set-uploader.ts +2 -4
- package/src/types/rule-set-uploader.ts +190 -0
- package/src/types/type-property-policy-uploader.ts +188 -0
- package/src/types/types-extractor.ts +222 -54
- package/src/types/types-properties-merger.ts +14 -9
- package/src/types/types-uploader.ts +1 -1
- package/src/types/usage-info-uploader.ts +135 -0
- /package/src/colors/{color-example.csv → colors.csv} +0 -0
package/lib/cli.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { create } from './apps/create-app/create';
|
|
2
2
|
import { deployApp, removeApp } from './apps/deploy-app/deploy';
|
|
3
|
-
import {
|
|
3
|
+
import { getAll } from './types/types-extractor';
|
|
4
4
|
import { uploadTypes } from './types/types-uploader';
|
|
5
5
|
import { UploadOptionSets } from './types/option-set-uploader';
|
|
6
6
|
import { ParseOptionSetsLocalFiles } from './types/option-set-parser';
|
|
7
|
-
import {
|
|
7
|
+
import { ParseOptionSetsHierarchyLocalFile, UploadOptionSetsHierarchyLocalFile } from './types/option-set-hierarchy-parser';
|
|
8
|
+
import { ExportOptionSetsHierarchies } from './types/option-set-hierarchy-exporter';
|
|
8
9
|
import { MergeOptionSets } from './types/option-set-merger';
|
|
9
10
|
import { MergeTypeProperties } from './types/types-properties-merger';
|
|
10
11
|
import { ParseTypesFromFiles, ParseTypesFromLocalFiles, ParseTypesFromTemplateFromLocalFiles, ParseTypesFromTemplateAndDataFromLocalFiles } from './types/parse-types';
|
|
@@ -17,6 +18,10 @@ import { cliInstallWorkflowTemplateDefinitions, listWorkflowTemplateDefinitions
|
|
|
17
18
|
import { configureProject } from './projects/project-uploader';
|
|
18
19
|
import { ListProjects, ExportProject } from './projects/project-exporter';
|
|
19
20
|
import { Load, UploadAndLoad } from './loader-process/loader-process';
|
|
21
|
+
import { uploadUsageInfo } from './types/usage-info-uploader';
|
|
22
|
+
import { uploadRuleSets } from './types/rule-set-uploader';
|
|
23
|
+
import { UploadOptionSetHierarchies } from './types/option-set-hierarchy-uploader';
|
|
24
|
+
import { uploadTypePropertyPolicies } from './types/type-property-policy-uploader';
|
|
20
25
|
const cliUsage = require('command-line-usage');
|
|
21
26
|
/**
|
|
22
27
|
* Built using documentation here:
|
|
@@ -78,7 +83,7 @@ export async function cli(args) {
|
|
|
78
83
|
case 'types': {
|
|
79
84
|
switch (command) {
|
|
80
85
|
case 'getAll': {
|
|
81
|
-
|
|
86
|
+
getAll(args);
|
|
82
87
|
break;
|
|
83
88
|
}
|
|
84
89
|
case 'uploadTypes': {
|
|
@@ -93,12 +98,32 @@ export async function cli(args) {
|
|
|
93
98
|
ParseOptionSetsLocalFiles(args);
|
|
94
99
|
break;
|
|
95
100
|
}
|
|
96
|
-
case '
|
|
97
|
-
|
|
101
|
+
case 'parseOptionSetsHierarchyLocalFile': {
|
|
102
|
+
ParseOptionSetsHierarchyLocalFile(args);
|
|
98
103
|
break;
|
|
99
104
|
}
|
|
100
|
-
case '
|
|
101
|
-
|
|
105
|
+
case 'uploadOptionSetsHierarchyLocalFile': {
|
|
106
|
+
UploadOptionSetsHierarchyLocalFile(args);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case 'exportOptionSetsHierarchies': {
|
|
110
|
+
ExportOptionSetsHierarchies();
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case 'uploadOptionSetsHierarchies': {
|
|
114
|
+
UploadOptionSetHierarchies(args);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case 'uploadRuleSets': {
|
|
118
|
+
uploadRuleSets(args);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
case 'uploadUsageInfo': {
|
|
122
|
+
uploadUsageInfo(args);
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case 'uploadTypePropertyPolicies': {
|
|
126
|
+
uploadTypePropertyPolicies(args);
|
|
102
127
|
break;
|
|
103
128
|
}
|
|
104
129
|
case 'parseTypesFromLocalFiles': {
|
|
@@ -384,11 +409,35 @@ const helpSections = [
|
|
|
384
409
|
},
|
|
385
410
|
{
|
|
386
411
|
name: 'parseOptionSetsLocalFiles <File Paths>',
|
|
387
|
-
summary: 'Parse CSV files into OptionSets
|
|
412
|
+
summary: 'Parse CSV files into OptionSets.\n'
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
name: 'parseOptionSetsHierarchyLocalFile <File Path>',
|
|
416
|
+
summary: 'Parse CSV file into OptionSetHierarchy.'
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
name: 'uploadOptionSetsHierarchyLocalFile <File Path> <name>',
|
|
420
|
+
summary: 'upload CSV file into OptionSetHierarchy.'
|
|
421
|
+
},
|
|
422
|
+
{
|
|
423
|
+
name: 'exportOptionSetsHierarchies',
|
|
424
|
+
summary: 'export OptionSetHierarchies into CSVs.\n'
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
name: 'uploadOptionSetsHierarchies',
|
|
428
|
+
summary: 'Upload an extracted option set hierarchies.'
|
|
429
|
+
},
|
|
430
|
+
{
|
|
431
|
+
name: 'uploadRuleSets',
|
|
432
|
+
summary: 'Upload an extracted rule set.'
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
name: 'uploadUsageInfo',
|
|
436
|
+
summary: 'Upload extracted usage info.'
|
|
388
437
|
},
|
|
389
438
|
{
|
|
390
|
-
name: '
|
|
391
|
-
summary: '
|
|
439
|
+
name: 'uploadTypePropertyPolicies',
|
|
440
|
+
summary: 'Upload type property policies.'
|
|
392
441
|
},
|
|
393
442
|
{
|
|
394
443
|
name: 'uploadOptionSetsHierarchyLocalFiles <File Paths> <name>',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function ExportOptionSetsHierarchies(): Promise<void>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { getSeparatorByPlatform } from '../common';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { Entities } from '@contrail/sdk';
|
|
5
|
+
import { authUser } from '../auth/login';
|
|
6
|
+
import { ObjectUtil, StringUtil } from '@contrail/util';
|
|
7
|
+
const { Parser } = require('json2csv');
|
|
8
|
+
export async function ExportOptionSetsHierarchies() {
|
|
9
|
+
try {
|
|
10
|
+
await authUser();
|
|
11
|
+
console.log('%s Exporting local file', chalk.yellow.bold('OPTION SET HIERARCHY EXPORT'));
|
|
12
|
+
await exportOptionSetHierarchies();
|
|
13
|
+
console.log('%s Exported', chalk.yellow.bold('OPTION SET HIERARCHY EXPORT'));
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
console.log(`%s EXPORT Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
console.log('%s EXPORT Complete.', chalk.green.bold('COMPLETE'));
|
|
20
|
+
}
|
|
21
|
+
async function exportOptionSetHierarchies() {
|
|
22
|
+
const allOptionSetHierarchies = await new Entities().get({
|
|
23
|
+
entityName: 'option-set-hierarchy',
|
|
24
|
+
});
|
|
25
|
+
const currentOptionSets = await new Entities().get({
|
|
26
|
+
entityName: 'type-property-option-set',
|
|
27
|
+
});
|
|
28
|
+
for (const optionSetHierarchy of allOptionSetHierarchies) {
|
|
29
|
+
const levelNameInOrder = [];
|
|
30
|
+
for (const optionSetId of optionSetHierarchy.hierarchy) {
|
|
31
|
+
const os = currentOptionSets.find(os => os.id === optionSetId);
|
|
32
|
+
levelNameInOrder.push(StringUtil.convertToCamelCase(os.name));
|
|
33
|
+
}
|
|
34
|
+
transformAndSave(optionSetHierarchy, levelNameInOrder);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function transformAndSave(optionSetHierarchy, levelNameInOrder) {
|
|
38
|
+
const finalObjects = [];
|
|
39
|
+
transform(optionSetHierarchy.options, levelNameInOrder, [], finalObjects);
|
|
40
|
+
saveData(optionSetHierarchy.name, finalObjects, levelNameInOrder);
|
|
41
|
+
}
|
|
42
|
+
/*
|
|
43
|
+
Transform will take in a set if options, the names left to process, the partly completed objects, and the list to place the final objects.
|
|
44
|
+
On the first pass the list of names will be full and the list of partial objects will be empty. On this pass we create objects
|
|
45
|
+
with one field, the name of the field is the first name in levelNameInOrder. These objects are added to the workingObjects list
|
|
46
|
+
for the next run. On the following (recursive) runs there will be one less name in the levelNameInOrder (we remove the name we just processed)
|
|
47
|
+
and there will be objects to clone and add to in the workingObjects list. When all the objects have been created (There are no names
|
|
48
|
+
left in the levelNameInOrder list), all the objects are added to the finalObjects list.
|
|
49
|
+
|
|
50
|
+
Initial Input:
|
|
51
|
+
options: {value: "BTP", options: [{value: "The Great"}, {value: "The Best"}]}
|
|
52
|
+
levelNameInOrder: ["name", "title"]
|
|
53
|
+
workingObjects: []
|
|
54
|
+
finalObjects: []
|
|
55
|
+
|
|
56
|
+
Level 2 Input 1:
|
|
57
|
+
options: [{value: "The Great"}]
|
|
58
|
+
levelNameInOrder: ["title"]
|
|
59
|
+
workingObjects: [{name: "BTP"}]
|
|
60
|
+
finalObjects: []
|
|
61
|
+
|
|
62
|
+
Level 2 Input 2:
|
|
63
|
+
options: [{value: "The Best"}]
|
|
64
|
+
levelNameInOrder: ["title"]
|
|
65
|
+
workingObjects: [{name: "BTP"}]
|
|
66
|
+
finalObjects: []
|
|
67
|
+
|
|
68
|
+
Final Objects:
|
|
69
|
+
[{name: "BTP", title: "The Great"}, {name: "BTP", title: "The Best"}]
|
|
70
|
+
*/
|
|
71
|
+
function transform(options, levelNameInOrder, workingObjects, finalObjects) {
|
|
72
|
+
for (const option of options) {
|
|
73
|
+
const localObjects = [];
|
|
74
|
+
if (workingObjects.length === 0) {
|
|
75
|
+
localObjects.push({ [levelNameInOrder[0]]: option.value });
|
|
76
|
+
if (levelNameInOrder.length <= 1) {
|
|
77
|
+
finalObjects.push(...localObjects);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
transform(option.options, levelNameInOrder.slice(1), localObjects, finalObjects);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
for (const workingObject of workingObjects) {
|
|
85
|
+
const clone = ObjectUtil.cloneDeep(workingObject);
|
|
86
|
+
clone[levelNameInOrder[0]] = option.value;
|
|
87
|
+
localObjects.push(clone);
|
|
88
|
+
}
|
|
89
|
+
if (levelNameInOrder.length <= 1) {
|
|
90
|
+
finalObjects.push(...localObjects);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
transform(option.options, levelNameInOrder.slice(1), localObjects, finalObjects);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function saveData(name, data, fieldNames) {
|
|
99
|
+
const CURRENT_DIR = process.cwd();
|
|
100
|
+
const separator = getSeparatorByPlatform();
|
|
101
|
+
const opts = { fieldNames };
|
|
102
|
+
try {
|
|
103
|
+
const parser = new Parser(opts);
|
|
104
|
+
const csv = parser.parse(data);
|
|
105
|
+
fs.mkdirSync(CURRENT_DIR + separator + 'option-set-hierarchies', { recursive: true });
|
|
106
|
+
fs.writeFileSync(CURRENT_DIR + separator + 'option-set-hierarchies' + separator + name + '.csv', csv, {
|
|
107
|
+
flag: 'w',
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
console.error(err);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function
|
|
2
|
-
export declare function
|
|
1
|
+
export declare function ParseOptionSetsHierarchyLocalFile(args: any): Promise<void>;
|
|
2
|
+
export declare function UploadOptionSetsHierarchyLocalFile(args: any): Promise<void>;
|
|
@@ -5,7 +5,7 @@ const csv = require('csvtojson');
|
|
|
5
5
|
import { Entities } from '@contrail/sdk';
|
|
6
6
|
import { StringUtil } from '@contrail/util';
|
|
7
7
|
import { authUser } from '../auth/login';
|
|
8
|
-
export async function
|
|
8
|
+
export async function ParseOptionSetsHierarchyLocalFile(args) {
|
|
9
9
|
try {
|
|
10
10
|
await authUser();
|
|
11
11
|
if ((args === null || args === void 0 ? void 0 : args.length) < 4) {
|
|
@@ -14,7 +14,7 @@ export async function ParseOptionSetsHierarchyLocalFiles(args) {
|
|
|
14
14
|
console.log('%s Importing local file', chalk.yellow.bold('OPTION SET HIERARCHY PARSE'));
|
|
15
15
|
const fileLocation = args[3];
|
|
16
16
|
console.log('Using File: ', fileLocation);
|
|
17
|
-
await
|
|
17
|
+
await parseAndSaveOptionSetHierarchiesLocalFile(fileLocation);
|
|
18
18
|
console.log('%s Parsed', chalk.yellow.bold('OPTION SET HIERARCHY PARSE'));
|
|
19
19
|
}
|
|
20
20
|
catch (e) {
|
|
@@ -23,7 +23,7 @@ export async function ParseOptionSetsHierarchyLocalFiles(args) {
|
|
|
23
23
|
}
|
|
24
24
|
console.log('%s PARSE Complete.', chalk.green.bold('COMPLETE'));
|
|
25
25
|
}
|
|
26
|
-
export async function
|
|
26
|
+
export async function UploadOptionSetsHierarchyLocalFile(args) {
|
|
27
27
|
try {
|
|
28
28
|
await authUser();
|
|
29
29
|
if ((args === null || args === void 0 ? void 0 : args.length) < 4) {
|
|
@@ -34,7 +34,7 @@ export async function UploadOptionSetsHierarchyLocalFiles(args) {
|
|
|
34
34
|
const name = args[4];
|
|
35
35
|
console.log('Using File: ', fileLocation);
|
|
36
36
|
console.log('Using Name: ', name);
|
|
37
|
-
await
|
|
37
|
+
await uploadOptionSetHierarchiesLocalFile(fileLocation, name);
|
|
38
38
|
console.log('%s Uploaded', chalk.yellow.bold('OPTION SET HIERARCHY UPLOAD'));
|
|
39
39
|
}
|
|
40
40
|
catch (e) {
|
|
@@ -55,7 +55,7 @@ async function loadOptionSetHierarchyCSV(fileLocation) {
|
|
|
55
55
|
throw new Error(`ERROR PARSING CSV: ${e}`);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
async function
|
|
58
|
+
async function parseOptionSetHierarchiesLocalFile(fileLocation) {
|
|
59
59
|
const uploadedOptionHierarchyData = await loadOptionSetHierarchyCSV(fileLocation);
|
|
60
60
|
const optionSetHierarchy = {
|
|
61
61
|
hierarchy: [],
|
|
@@ -90,12 +90,12 @@ async function parseOptionSetHierarchiesLocalFiles(fileLocation) {
|
|
|
90
90
|
}
|
|
91
91
|
return optionSetHierarchy;
|
|
92
92
|
}
|
|
93
|
-
async function
|
|
94
|
-
const optionSetHierarchy = await
|
|
93
|
+
async function parseAndSaveOptionSetHierarchiesLocalFile(fileLocation) {
|
|
94
|
+
const optionSetHierarchy = await parseOptionSetHierarchiesLocalFile(fileLocation);
|
|
95
95
|
saveData(optionSetHierarchy.options);
|
|
96
96
|
}
|
|
97
|
-
async function
|
|
98
|
-
const optionSetHierarchy = await
|
|
97
|
+
async function uploadOptionSetHierarchiesLocalFile(fileLocation, name) {
|
|
98
|
+
const optionSetHierarchy = await parseOptionSetHierarchiesLocalFile(fileLocation);
|
|
99
99
|
optionSetHierarchy.name = name;
|
|
100
100
|
const currentOptionSetHierarchies = await new Entities().get({
|
|
101
101
|
entityName: 'option-set-hierarchy',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function UploadOptionSetHierarchies(args: any): Promise<void>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { getSeparatorByPlatform } from '../common';
|
|
4
|
+
import { authUser } from '../auth/login';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
const yaml = require('js-yaml');
|
|
7
|
+
export async function UploadOptionSetHierarchies(args) {
|
|
8
|
+
try {
|
|
9
|
+
await authUser();
|
|
10
|
+
console.log('%s Uploading', chalk.yellow.bold('OPTION SET HIERARCHIES'));
|
|
11
|
+
const optionSetHierarchies = await loadOptionSetHierarchies();
|
|
12
|
+
await uploadOptionSetHierarchies(optionSetHierarchies);
|
|
13
|
+
console.log('%s Uploaded', chalk.yellow.bold('OPTION SET HIERARCHIES'));
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
console.log(`%s Option set hierarchy Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
console.log('%s Option set hierarchy Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
20
|
+
}
|
|
21
|
+
function loadOptionSetHierarchies() {
|
|
22
|
+
const TARGET_DIR = process.cwd();
|
|
23
|
+
try {
|
|
24
|
+
const separator = getSeparatorByPlatform();
|
|
25
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
|
|
26
|
+
return doc.reducedOptionSetHierarchies;
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function uploadOptionSetHierarchies(loadedOSHs) {
|
|
33
|
+
const optionSets = await new Entities().get({
|
|
34
|
+
entityName: 'type-property-option-set',
|
|
35
|
+
});
|
|
36
|
+
const currentOSHs = await new Entities().get({
|
|
37
|
+
entityName: 'option-set-hierarchy',
|
|
38
|
+
});
|
|
39
|
+
const optionSetsByName = {};
|
|
40
|
+
for (const optionSet of optionSets) {
|
|
41
|
+
optionSetsByName[optionSet.name] = optionSet;
|
|
42
|
+
}
|
|
43
|
+
const currentOSHByName = {};
|
|
44
|
+
for (const currentOSH of currentOSHs) {
|
|
45
|
+
currentOSHByName[currentOSH.name] = currentOSH;
|
|
46
|
+
}
|
|
47
|
+
const loadedOSHByName = {};
|
|
48
|
+
for (const loadedOSH of loadedOSHs) {
|
|
49
|
+
loadedOSHByName[loadedOSH.name] = loadedOSH;
|
|
50
|
+
}
|
|
51
|
+
console.debug('current OSH:', currentOSHs);
|
|
52
|
+
console.debug('loaded OSH:', loadedOSHs);
|
|
53
|
+
for (const osh of loadedOSHs) {
|
|
54
|
+
try {
|
|
55
|
+
const found = currentOSHByName[osh.name];
|
|
56
|
+
if (!found) {
|
|
57
|
+
if (!transformOSHNameToID(osh.hierarchy, optionSets)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
console.log('Creating: ', osh);
|
|
61
|
+
await new Entities().create({
|
|
62
|
+
entityName: 'option-set-hierarchy',
|
|
63
|
+
object: osh
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
if (!transformOSHNameToID(osh.hierarchy, optionSets)) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
console.log('Updating: ', osh);
|
|
71
|
+
await new Entities().update({
|
|
72
|
+
entityName: 'option-set-hierarchy',
|
|
73
|
+
id: found.id,
|
|
74
|
+
object: osh,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
throw new Error(`Error Uploading option set hierarchy: ${err}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
for (const currentOSH of currentOSHs) {
|
|
83
|
+
try {
|
|
84
|
+
const found = loadedOSHByName[currentOSH.name];
|
|
85
|
+
if (!found) {
|
|
86
|
+
console.log('Deleting: ', currentOSH.name);
|
|
87
|
+
await new Entities().delete({
|
|
88
|
+
entityName: 'option-set-hierarchy',
|
|
89
|
+
id: currentOSH.id,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
throw new Error(`Error Deleting Option set hierarchy: ${err}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// transforms the passed hierarchy from names to ids, and returns true if operation succeeded.
|
|
99
|
+
function transformOSHNameToID(hierarchy, optionSets) {
|
|
100
|
+
console.log('passed hierarhy:', hierarchy);
|
|
101
|
+
for (let i = 0; i < hierarchy.length; i++) {
|
|
102
|
+
const os = optionSets.find(os => os.name === hierarchy[i]);
|
|
103
|
+
if (!os) {
|
|
104
|
+
console.error('could not find option set with name', hierarchy[i], ' at position', i, 'skipping...');
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
hierarchy[i] = os.id;
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
@@ -23,7 +23,7 @@ function loadOptionSets() {
|
|
|
23
23
|
try {
|
|
24
24
|
const separator = getSeparatorByPlatform();
|
|
25
25
|
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'option_sets.yml', 'utf8'));
|
|
26
|
-
return doc.
|
|
26
|
+
return doc.reducedOptionSets;
|
|
27
27
|
}
|
|
28
28
|
catch (e) {
|
|
29
29
|
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadRuleSets(args: any): Promise<void>;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { getSeparatorByPlatform } from '../common';
|
|
4
|
+
import { authUser } from '../auth/login';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
const yaml = require('js-yaml');
|
|
7
|
+
export async function uploadRuleSets(args) {
|
|
8
|
+
try {
|
|
9
|
+
await authUser();
|
|
10
|
+
console.log('%s Uploading', chalk.yellow.bold('RULE SET'));
|
|
11
|
+
const [reducedRuleSets, reducedRuleSetLinks] = await loadReducedRuleSets();
|
|
12
|
+
await uploadRuleSetObjects(reducedRuleSets);
|
|
13
|
+
await uploadRuleSetLinkObjects(reducedRuleSetLinks);
|
|
14
|
+
console.log('%s Uploaded', chalk.yellow.bold('RULE SET'));
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
console.log(`%s Rule Set Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
console.log('%s Rule set Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
21
|
+
}
|
|
22
|
+
function loadReducedRuleSets() {
|
|
23
|
+
const TARGET_DIR = process.cwd();
|
|
24
|
+
try {
|
|
25
|
+
const separator = getSeparatorByPlatform();
|
|
26
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
|
|
27
|
+
return [doc.reducedRuleSets, doc.reducedRuleSetLinks];
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async function uploadRuleSetObjects(reducedRuleSets) {
|
|
34
|
+
const currentRuleSets = await new Entities().get({
|
|
35
|
+
entityName: 'type-rule-set',
|
|
36
|
+
});
|
|
37
|
+
const currentRuleSetsByName = {};
|
|
38
|
+
for (const currentRuleSet of currentRuleSets) {
|
|
39
|
+
currentRuleSetsByName[currentRuleSet.name] = currentRuleSet;
|
|
40
|
+
}
|
|
41
|
+
const loadedRuleSetsByName = {};
|
|
42
|
+
for (const loadedRuleSet of reducedRuleSets) {
|
|
43
|
+
loadedRuleSetsByName[loadedRuleSet.name] = loadedRuleSet;
|
|
44
|
+
}
|
|
45
|
+
console.debug('current rule sets:', currentRuleSets);
|
|
46
|
+
console.debug('reduced rule sets:', reducedRuleSets);
|
|
47
|
+
for (const reducedRuleSet of reducedRuleSets) {
|
|
48
|
+
try {
|
|
49
|
+
const found = currentRuleSetsByName[reducedRuleSet.name];
|
|
50
|
+
if (!found) {
|
|
51
|
+
console.log('Creating: ', reducedRuleSet.name);
|
|
52
|
+
await new Entities().create({
|
|
53
|
+
entityName: 'type-rule-set',
|
|
54
|
+
object: reducedRuleSet
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
console.log('Updating: ', reducedRuleSet.name);
|
|
59
|
+
await new Entities().update({
|
|
60
|
+
entityName: 'type-rule-set',
|
|
61
|
+
id: found.id,
|
|
62
|
+
object: reducedRuleSet,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
throw new Error(`Error Uploading rule sets: ${err}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (const currentRuleSet of currentRuleSets) {
|
|
71
|
+
try {
|
|
72
|
+
const found = loadedRuleSetsByName[currentRuleSet.name];
|
|
73
|
+
if (!found) {
|
|
74
|
+
console.log('Deleting: ', currentRuleSet.name);
|
|
75
|
+
await new Entities().delete({
|
|
76
|
+
entityName: 'type-rule-set',
|
|
77
|
+
id: currentRuleSet.id,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
throw new Error(`Error Deleting rule set: ${err}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function uploadRuleSetLinkObjects(loadedRuleSetLinks) {
|
|
87
|
+
const types = await new Entities().get({
|
|
88
|
+
entityName: 'type',
|
|
89
|
+
});
|
|
90
|
+
const currentRuleSets = await new Entities().get({
|
|
91
|
+
entityName: 'type-rule-set',
|
|
92
|
+
});
|
|
93
|
+
const currentRuleSetLinks = await new Entities().get({
|
|
94
|
+
entityName: 'type-rule-set-link',
|
|
95
|
+
});
|
|
96
|
+
const currentLinksHashes = {};
|
|
97
|
+
for (const currentLink of currentRuleSetLinks) {
|
|
98
|
+
const foundType = types.find(t => t.id === currentLink.typeId);
|
|
99
|
+
const foundRuleSet = currentRuleSets.find(rs => rs.id === currentLink.typeRuleSetId);
|
|
100
|
+
const hash = `${foundType.slug}|${foundRuleSet.name}`;
|
|
101
|
+
currentLinksHashes[hash] = currentLink;
|
|
102
|
+
}
|
|
103
|
+
const reducedLinksByHashes = {};
|
|
104
|
+
for (const loadedLink of loadedRuleSetLinks) {
|
|
105
|
+
const hash = `${loadedLink.type}|${loadedLink.ruleSet}`;
|
|
106
|
+
reducedLinksByHashes[hash] = loadedLink;
|
|
107
|
+
}
|
|
108
|
+
console.debug('current rule set links:', currentLinksHashes);
|
|
109
|
+
console.debug('reduced rule set links:', reducedLinksByHashes);
|
|
110
|
+
for (const loadedRuleSetLink of loadedRuleSetLinks) {
|
|
111
|
+
try {
|
|
112
|
+
const found = currentLinksHashes[`${loadedRuleSetLink.type}|${loadedRuleSetLink.ruleSet}`];
|
|
113
|
+
if (!found) {
|
|
114
|
+
if (!transformReducedLink(loadedRuleSetLink, types, currentRuleSets)) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
console.log('Creating: ', loadedRuleSetLink);
|
|
118
|
+
await new Entities().create({
|
|
119
|
+
entityName: 'type-rule-set-link',
|
|
120
|
+
object: {
|
|
121
|
+
typeRuleSetId: loadedRuleSetLink.ruleSet,
|
|
122
|
+
typeId: loadedRuleSetLink.type,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
if (!transformReducedLink(loadedRuleSetLink, types, currentRuleSets)) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
console.log('Updating: ', loadedRuleSetLink);
|
|
131
|
+
await new Entities().update({
|
|
132
|
+
entityName: 'type-rule-set-link',
|
|
133
|
+
id: found.id,
|
|
134
|
+
object: {
|
|
135
|
+
typeRuleSetId: loadedRuleSetLink.ruleSet,
|
|
136
|
+
typeId: loadedRuleSetLink.type,
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
throw new Error(`Error Uploading rule set links: ${err}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
for (const [currentLinkHash, currentLink] of Object.entries(currentLinksHashes)) {
|
|
146
|
+
try {
|
|
147
|
+
const found = reducedLinksByHashes[currentLinkHash];
|
|
148
|
+
if (!found) {
|
|
149
|
+
console.log('Deleting: ', currentLinkHash);
|
|
150
|
+
await new Entities().delete({
|
|
151
|
+
entityName: 'type-rule-set-link',
|
|
152
|
+
id: currentLink['id'],
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
throw new Error(`Error Deleting rule set link: ${err}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function transformReducedLink(ruleSetLink, types, ruleSets) {
|
|
162
|
+
const foundType = types.find(t => t.slug === ruleSetLink.type);
|
|
163
|
+
if (!foundType) {
|
|
164
|
+
console.error('Could not find type with name', ruleSetLink.type);
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
ruleSetLink.type = foundType.id;
|
|
168
|
+
const foundRuleSet = ruleSets.find(rs => rs.name === ruleSetLink.ruleSet);
|
|
169
|
+
if (!foundRuleSet) {
|
|
170
|
+
console.error('Could not find rule set with name', foundType.ruleSet);
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
ruleSetLink.ruleSet = foundRuleSet.id;
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadTypePropertyPolicies(args: any): Promise<void>;
|