@contrail/cli 1.0.54 → 1.0.56
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 +51 -2
- package/lib/types/allUploader.d.ts +1 -0
- package/lib/types/allUploader.js +24 -0
- package/lib/types/option-set-hierarchy-uploader.d.ts +1 -0
- package/lib/types/option-set-hierarchy-uploader.js +113 -0
- package/lib/types/option-set-uploader.js +7 -4
- package/lib/types/rule-set-uploader.d.ts +1 -0
- package/lib/types/rule-set-uploader.js +172 -0
- package/lib/types/type-property-policy-uploader.d.ts +1 -0
- package/lib/types/type-property-policy-uploader.js +178 -0
- package/lib/types/types-extractor.d.ts +51 -2
- package/lib/types/types-extractor.js +223 -52
- package/lib/types/types-properties-merger.js +14 -8
- package/lib/types/types-uploader.js +7 -4
- package/lib/types/usage-info-uploader.d.ts +1 -0
- package/lib/types/usage-info-uploader.js +127 -0
- package/package.json +1 -1
- package/src/cli.ts +51 -2
- package/src/types/allUploader.ts +28 -0
- package/src/types/option-set-hierarchy-uploader.ts +122 -0
- package/src/types/option-set-uploader.ts +8 -7
- package/src/types/rule-set-uploader.ts +187 -0
- package/src/types/type-property-policy-uploader.ts +192 -0
- package/src/types/types-extractor.ts +238 -57
- package/src/types/types-properties-merger.ts +14 -9
- package/src/types/types-uploader.ts +7 -4
- package/src/types/usage-info-uploader.ts +134 -0
- /package/src/colors/{color-example.csv → colors.csv} +0 -0
package/lib/cli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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';
|
|
@@ -18,6 +18,11 @@ import { cliInstallWorkflowTemplateDefinitions, listWorkflowTemplateDefinitions
|
|
|
18
18
|
import { configureProject } from './projects/project-uploader';
|
|
19
19
|
import { ListProjects, ExportProject } from './projects/project-exporter';
|
|
20
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';
|
|
25
|
+
import { uploadAll } from './types/allUploader';
|
|
21
26
|
const cliUsage = require('command-line-usage');
|
|
22
27
|
/**
|
|
23
28
|
* Built using documentation here:
|
|
@@ -79,7 +84,11 @@ export async function cli(args) {
|
|
|
79
84
|
case 'types': {
|
|
80
85
|
switch (command) {
|
|
81
86
|
case 'getAll': {
|
|
82
|
-
|
|
87
|
+
getAll(args);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case 'uploadAll': {
|
|
91
|
+
uploadAll();
|
|
83
92
|
break;
|
|
84
93
|
}
|
|
85
94
|
case 'uploadTypes': {
|
|
@@ -106,6 +115,22 @@ export async function cli(args) {
|
|
|
106
115
|
ExportOptionSetsHierarchies();
|
|
107
116
|
break;
|
|
108
117
|
}
|
|
118
|
+
case 'uploadOptionSetsHierarchies': {
|
|
119
|
+
UploadOptionSetHierarchies(args);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case 'uploadRuleSets': {
|
|
123
|
+
uploadRuleSets(args);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case 'uploadUsageInfo': {
|
|
127
|
+
uploadUsageInfo(args);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case 'uploadTypePropertyPolicies': {
|
|
131
|
+
uploadTypePropertyPolicies(args);
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
109
134
|
case 'parseTypesFromLocalFiles': {
|
|
110
135
|
ParseTypesFromLocalFiles(args);
|
|
111
136
|
break;
|
|
@@ -403,6 +428,30 @@ const helpSections = [
|
|
|
403
428
|
name: 'exportOptionSetsHierarchies',
|
|
404
429
|
summary: 'export OptionSetHierarchies into CSVs.\n'
|
|
405
430
|
},
|
|
431
|
+
{
|
|
432
|
+
name: 'uploadAll',
|
|
433
|
+
summary: 'Upload an extracted archive of types:getAll.'
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
name: 'uploadOptionSetsHierarchies',
|
|
437
|
+
summary: 'Upload an extracted option set hierarchies.'
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
name: 'uploadRuleSets',
|
|
441
|
+
summary: 'Upload an extracted rule set.'
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
name: 'uploadUsageInfo',
|
|
445
|
+
summary: 'Upload extracted usage info.'
|
|
446
|
+
},
|
|
447
|
+
{
|
|
448
|
+
name: 'uploadTypePropertyPolicies',
|
|
449
|
+
summary: 'Upload type property policies.'
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
name: 'uploadOptionSetsHierarchyLocalFiles <File Paths> <name>',
|
|
453
|
+
summary: 'upload CSV files into OptionSetHierarchies.'
|
|
454
|
+
},
|
|
406
455
|
{
|
|
407
456
|
name: 'parseTypesFromLocalFiles <File Paths>',
|
|
408
457
|
summary: 'Parse CSV files into Types and OptionSets from local files.\n'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadAll(): Promise<void>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { authUser } from '../auth/login';
|
|
3
|
+
import { UploadOptionSets } from './option-set-uploader';
|
|
4
|
+
import { UploadOptionSetHierarchies } from './option-set-hierarchy-uploader';
|
|
5
|
+
import { uploadTypes } from './types-uploader';
|
|
6
|
+
import { uploadUsageInfo } from './usage-info-uploader';
|
|
7
|
+
import { uploadRuleSets } from './rule-set-uploader';
|
|
8
|
+
import { uploadTypePropertyPolicies } from './type-property-policy-uploader';
|
|
9
|
+
export async function uploadAll() {
|
|
10
|
+
try {
|
|
11
|
+
await authUser();
|
|
12
|
+
await UploadOptionSets(null);
|
|
13
|
+
await UploadOptionSetHierarchies(null);
|
|
14
|
+
await uploadTypes();
|
|
15
|
+
await uploadUsageInfo(null);
|
|
16
|
+
await uploadRuleSets(null);
|
|
17
|
+
await uploadTypePropertyPolicies(null);
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
console.log(`%s Types Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
console.log('%s Types Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function UploadOptionSetHierarchies(args: any): Promise<void>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
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 files = fs.readdirSync(path.join(TARGET_DIR, 'option-set-hierarchies'));
|
|
25
|
+
const OSHs = [];
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
OSHs.push(yaml.load(fs.readFileSync(path.join(TARGET_DIR, 'option-set-hierarchies', file), 'utf8')));
|
|
28
|
+
}
|
|
29
|
+
return OSHs;
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function uploadOptionSetHierarchies(loadedOSHs) {
|
|
36
|
+
const optionSets = await new Entities().get({
|
|
37
|
+
entityName: 'type-property-option-set',
|
|
38
|
+
});
|
|
39
|
+
const currentOSHs = await new Entities().get({
|
|
40
|
+
entityName: 'option-set-hierarchy',
|
|
41
|
+
});
|
|
42
|
+
const optionSetsByName = {};
|
|
43
|
+
for (const optionSet of optionSets) {
|
|
44
|
+
optionSetsByName[optionSet.name] = optionSet;
|
|
45
|
+
}
|
|
46
|
+
const currentOSHByName = {};
|
|
47
|
+
for (const currentOSH of currentOSHs) {
|
|
48
|
+
currentOSHByName[currentOSH.name] = currentOSH;
|
|
49
|
+
}
|
|
50
|
+
const loadedOSHByName = {};
|
|
51
|
+
for (const loadedOSH of loadedOSHs) {
|
|
52
|
+
loadedOSHByName[loadedOSH.name] = loadedOSH;
|
|
53
|
+
}
|
|
54
|
+
console.debug('current OSH:', currentOSHs);
|
|
55
|
+
console.debug('loaded OSH:', loadedOSHs);
|
|
56
|
+
for (const osh of loadedOSHs) {
|
|
57
|
+
try {
|
|
58
|
+
const found = currentOSHByName[osh.name];
|
|
59
|
+
if (!found) {
|
|
60
|
+
if (!transformOSHNameToID(osh.hierarchy, optionSets)) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
console.log('Creating: ', osh);
|
|
64
|
+
await new Entities().create({
|
|
65
|
+
entityName: 'option-set-hierarchy',
|
|
66
|
+
object: osh
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
if (!transformOSHNameToID(osh.hierarchy, optionSets)) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
console.log('Updating: ', osh);
|
|
74
|
+
await new Entities().update({
|
|
75
|
+
entityName: 'option-set-hierarchy',
|
|
76
|
+
id: found.id,
|
|
77
|
+
object: osh,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
throw new Error(`Error Uploading option set hierarchy: ${err}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for (const currentOSH of currentOSHs) {
|
|
86
|
+
try {
|
|
87
|
+
const found = loadedOSHByName[currentOSH.name];
|
|
88
|
+
if (!found) {
|
|
89
|
+
console.log('Deleting: ', currentOSH.name);
|
|
90
|
+
await new Entities().delete({
|
|
91
|
+
entityName: 'option-set-hierarchy',
|
|
92
|
+
id: currentOSH.id,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
throw new Error(`Error Deleting Option set hierarchy: ${err}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// transforms the passed hierarchy from names to ids, and returns true if operation succeeded.
|
|
102
|
+
function transformOSHNameToID(hierarchy, optionSets) {
|
|
103
|
+
console.log('passed hierarhy:', hierarchy);
|
|
104
|
+
for (let i = 0; i < hierarchy.length; i++) {
|
|
105
|
+
const os = optionSets.find(os => os.name === hierarchy[i]);
|
|
106
|
+
if (!os) {
|
|
107
|
+
console.error('could not find option set with name', hierarchy[i], ' at position', i, 'skipping...');
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
hierarchy[i] = os.id;
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Entities } from '@contrail/sdk';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import
|
|
3
|
+
import * as path from 'path';
|
|
4
4
|
import { authUser } from '../auth/login';
|
|
5
5
|
import chalk from 'chalk';
|
|
6
6
|
const yaml = require('js-yaml');
|
|
@@ -21,9 +21,12 @@ export async function UploadOptionSets(args) {
|
|
|
21
21
|
function loadOptionSets() {
|
|
22
22
|
const TARGET_DIR = process.cwd();
|
|
23
23
|
try {
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
24
|
+
const files = fs.readdirSync(path.join(TARGET_DIR, 'option-sets'));
|
|
25
|
+
const optionSets = [];
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
optionSets.push(yaml.load(fs.readFileSync(path.join(TARGET_DIR, 'option-sets', file), 'utf8')));
|
|
28
|
+
}
|
|
29
|
+
return optionSets;
|
|
27
30
|
}
|
|
28
31
|
catch (e) {
|
|
29
32
|
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadRuleSets(args: any): Promise<void>;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
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 ruleSets = [];
|
|
24
|
+
let ruleSetLinks = [];
|
|
25
|
+
const TARGET_DIR = process.cwd();
|
|
26
|
+
try {
|
|
27
|
+
const files = fs.readdirSync(path.join(TARGET_DIR, 'rule-sets'));
|
|
28
|
+
for (const file of files) {
|
|
29
|
+
ruleSets.push(yaml.load(fs.readFileSync(path.join(TARGET_DIR, 'rule-sets', file), 'utf8')));
|
|
30
|
+
}
|
|
31
|
+
ruleSetLinks = yaml.load(fs.readFileSync(path.join(TARGET_DIR, 'rule-set-links.yml'), 'utf8'));
|
|
32
|
+
return [ruleSets, ruleSetLinks];
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function uploadRuleSetObjects(reducedRuleSets) {
|
|
39
|
+
const currentRuleSets = await new Entities().get({
|
|
40
|
+
entityName: 'type-rule-set',
|
|
41
|
+
});
|
|
42
|
+
const currentRuleSetsByName = {};
|
|
43
|
+
for (const currentRuleSet of currentRuleSets) {
|
|
44
|
+
currentRuleSetsByName[currentRuleSet.name] = currentRuleSet;
|
|
45
|
+
}
|
|
46
|
+
const loadedRuleSetsByName = {};
|
|
47
|
+
for (const loadedRuleSet of reducedRuleSets) {
|
|
48
|
+
loadedRuleSetsByName[loadedRuleSet.name] = loadedRuleSet;
|
|
49
|
+
}
|
|
50
|
+
console.debug('current rule sets:', currentRuleSets);
|
|
51
|
+
console.debug('reduced rule sets:', reducedRuleSets);
|
|
52
|
+
for (const reducedRuleSet of reducedRuleSets) {
|
|
53
|
+
try {
|
|
54
|
+
const found = currentRuleSetsByName[reducedRuleSet.name];
|
|
55
|
+
if (!found) {
|
|
56
|
+
console.log('Creating: ', reducedRuleSet.name);
|
|
57
|
+
await new Entities().create({
|
|
58
|
+
entityName: 'type-rule-set',
|
|
59
|
+
object: reducedRuleSet
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
console.log('Updating: ', reducedRuleSet.name);
|
|
64
|
+
await new Entities().update({
|
|
65
|
+
entityName: 'type-rule-set',
|
|
66
|
+
id: found.id,
|
|
67
|
+
object: reducedRuleSet,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
throw new Error(`Error Uploading rule sets: ${err}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const currentRuleSet of currentRuleSets) {
|
|
76
|
+
try {
|
|
77
|
+
const found = loadedRuleSetsByName[currentRuleSet.name];
|
|
78
|
+
if (!found) {
|
|
79
|
+
console.log('Deleting: ', currentRuleSet.name);
|
|
80
|
+
await new Entities().delete({
|
|
81
|
+
entityName: 'type-rule-set',
|
|
82
|
+
id: currentRuleSet.id,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
throw new Error(`Error Deleting rule set: ${err}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async function uploadRuleSetLinkObjects(loadedRuleSetLinks) {
|
|
92
|
+
const types = await new Entities().get({
|
|
93
|
+
entityName: 'type',
|
|
94
|
+
});
|
|
95
|
+
const currentRuleSets = await new Entities().get({
|
|
96
|
+
entityName: 'type-rule-set',
|
|
97
|
+
});
|
|
98
|
+
const currentRuleSetLinks = await new Entities().get({
|
|
99
|
+
entityName: 'type-rule-set-link',
|
|
100
|
+
});
|
|
101
|
+
const currentLinksHashes = {};
|
|
102
|
+
for (const currentLink of currentRuleSetLinks) {
|
|
103
|
+
const foundType = types.find(t => t.id === currentLink.typeId);
|
|
104
|
+
const foundRuleSet = currentRuleSets.find(rs => rs.id === currentLink.typeRuleSetId);
|
|
105
|
+
const hash = `${foundType.slug}|${foundRuleSet.name}`;
|
|
106
|
+
currentLinksHashes[hash] = currentLink;
|
|
107
|
+
}
|
|
108
|
+
const reducedLinksByHashes = {};
|
|
109
|
+
for (const loadedLink of loadedRuleSetLinks) {
|
|
110
|
+
const hash = `${loadedLink.type}|${loadedLink.ruleSet}`;
|
|
111
|
+
reducedLinksByHashes[hash] = loadedLink;
|
|
112
|
+
}
|
|
113
|
+
console.debug('current rule set links:', currentLinksHashes);
|
|
114
|
+
console.debug('reduced rule set links:', reducedLinksByHashes);
|
|
115
|
+
for (const loadedRuleSetLink of loadedRuleSetLinks) {
|
|
116
|
+
try {
|
|
117
|
+
const found = currentLinksHashes[`${loadedRuleSetLink.type}|${loadedRuleSetLink.ruleSet}`];
|
|
118
|
+
if (!found) {
|
|
119
|
+
if (!transformReducedLink(loadedRuleSetLink, types, currentRuleSets)) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
console.log('Creating: ', loadedRuleSetLink);
|
|
123
|
+
await new Entities().create({
|
|
124
|
+
entityName: 'type-rule-set-link',
|
|
125
|
+
object: {
|
|
126
|
+
typeRuleSetId: loadedRuleSetLink.ruleSet,
|
|
127
|
+
typeId: loadedRuleSetLink.type,
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
if (!transformReducedLink(loadedRuleSetLink, types, currentRuleSets)) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
console.log('skipping updating existing rule set link: ', loadedRuleSetLink);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
throw new Error(`Error Uploading rule set links: ${err}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const [currentLinkHash, currentLink] of Object.entries(currentLinksHashes)) {
|
|
143
|
+
try {
|
|
144
|
+
const found = reducedLinksByHashes[currentLinkHash];
|
|
145
|
+
if (!found) {
|
|
146
|
+
console.log('Deleting: ', currentLinkHash);
|
|
147
|
+
await new Entities().delete({
|
|
148
|
+
entityName: 'type-rule-set-link',
|
|
149
|
+
id: currentLink['id'],
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
throw new Error(`Error Deleting rule set link: ${err}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function transformReducedLink(ruleSetLink, types, ruleSets) {
|
|
159
|
+
const foundType = types.find(t => t.slug === ruleSetLink.type);
|
|
160
|
+
if (!foundType) {
|
|
161
|
+
console.error('Could not find type with name', ruleSetLink.type);
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
ruleSetLink.type = foundType.id;
|
|
165
|
+
const foundRuleSet = ruleSets.find(rs => rs.name === ruleSetLink.ruleSet);
|
|
166
|
+
if (!foundRuleSet) {
|
|
167
|
+
console.error('Could not find rule set with name', foundType.ruleSet);
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
ruleSetLink.ruleSet = foundRuleSet.id;
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadTypePropertyPolicies(args: any): Promise<void>;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { authUser } from '../auth/login';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
import { getTypes } from './types-extractor';
|
|
7
|
+
const yaml = require('js-yaml');
|
|
8
|
+
export async function uploadTypePropertyPolicies(args) {
|
|
9
|
+
try {
|
|
10
|
+
await authUser();
|
|
11
|
+
console.log('%s Uploading', chalk.yellow.bold('TYPE PROPERTY POLICY'));
|
|
12
|
+
const [reducedTypePropertyPolicies, reducedTypePropertyPolicyLinks] = await loadTPPolicies();
|
|
13
|
+
await uploadTPPolicyObjects(reducedTypePropertyPolicies);
|
|
14
|
+
await uploadTypePropertyPolicyLinks(reducedTypePropertyPolicyLinks);
|
|
15
|
+
console.log('%s Uploaded', chalk.yellow.bold('TYPE PROPERTY POLICY'));
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.log(`%s Type Property Policy Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
console.log('%s Type Property Policy Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
22
|
+
}
|
|
23
|
+
function loadTPPolicies() {
|
|
24
|
+
const tpps = [];
|
|
25
|
+
let tppLinks = [];
|
|
26
|
+
const TARGET_DIR = process.cwd();
|
|
27
|
+
try {
|
|
28
|
+
const files = fs.readdirSync(path.join(TARGET_DIR, 'type-property-policies'));
|
|
29
|
+
for (const file of files) {
|
|
30
|
+
tpps.push(yaml.load(fs.readFileSync(path.join(TARGET_DIR, 'type-property-policies', file), 'utf8')));
|
|
31
|
+
}
|
|
32
|
+
tppLinks = yaml.load(fs.readFileSync(path.join(TARGET_DIR, 'type-property-policy-links.yml'), 'utf8'));
|
|
33
|
+
return [tpps, tppLinks];
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function uploadTPPolicyObjects(reducedTypePropertyPolicies) {
|
|
40
|
+
const currentTPPolicies = await new Entities().get({
|
|
41
|
+
entityName: 'type-property-policy',
|
|
42
|
+
});
|
|
43
|
+
const currentTPPByLabel = {};
|
|
44
|
+
for (const currentTPP of currentTPPolicies) {
|
|
45
|
+
currentTPPByLabel[currentTPP.label] = currentTPP;
|
|
46
|
+
}
|
|
47
|
+
const loadedTPPByName = {};
|
|
48
|
+
for (const loadedTPP of reducedTypePropertyPolicies) {
|
|
49
|
+
loadedTPPByName[loadedTPP.label] = loadedTPP;
|
|
50
|
+
}
|
|
51
|
+
console.debug('current tp policy:', currentTPPolicies);
|
|
52
|
+
console.debug('reduced tp policy:', reducedTypePropertyPolicies);
|
|
53
|
+
for (const [label, loadedTPP] of Object.entries(loadedTPPByName)) {
|
|
54
|
+
try {
|
|
55
|
+
const found = currentTPPByLabel[label];
|
|
56
|
+
if (!found) {
|
|
57
|
+
console.log('Creating: ', label);
|
|
58
|
+
await new Entities().create({
|
|
59
|
+
entityName: 'type-property-policy',
|
|
60
|
+
object: loadedTPP
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
console.log('Updating: ', label);
|
|
65
|
+
await new Entities().update({
|
|
66
|
+
entityName: 'type-property-policy',
|
|
67
|
+
id: found.id,
|
|
68
|
+
object: loadedTPP,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
throw new Error(`Error Uploading Type Property Policy: ${err}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const [label, currentTPP] of Object.entries(currentTPPByLabel)) {
|
|
77
|
+
try {
|
|
78
|
+
const found = loadedTPPByName[label];
|
|
79
|
+
if (!found) {
|
|
80
|
+
console.log('Deleting: ', label);
|
|
81
|
+
await new Entities().delete({
|
|
82
|
+
entityName: 'type-property-policy',
|
|
83
|
+
id: currentTPP['id'],
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
throw new Error(`Error Type Property Policy: ${err}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function uploadTypePropertyPolicyLinks(reducedTypePropertyPolicyLinks) {
|
|
93
|
+
const typesWithProperties = await getTypes();
|
|
94
|
+
const typeProperties = typesWithProperties
|
|
95
|
+
.filter(t => !!t.typeProperties)
|
|
96
|
+
.map(t => t.typeProperties)
|
|
97
|
+
.reduce((a, b) => a.concat(b));
|
|
98
|
+
const currentTPPs = await new Entities().get({
|
|
99
|
+
entityName: 'type-property-policy',
|
|
100
|
+
});
|
|
101
|
+
const currentTPPLinks = await new Entities().get({
|
|
102
|
+
entityName: 'type-property-policy-link',
|
|
103
|
+
});
|
|
104
|
+
const currentLinksHashes = {};
|
|
105
|
+
for (const currentLink of currentTPPLinks) {
|
|
106
|
+
const foundTypeProperty = typeProperties.find(tp => tp.id === currentLink.typePropertyId);
|
|
107
|
+
const foundTPP = currentTPPs.find(tpp => tpp.id === currentLink.typePropertyPolicyId);
|
|
108
|
+
if (!foundTypeProperty || !foundTPP) {
|
|
109
|
+
console.log('skipping unhashable, invalid policy link:', currentLink);
|
|
110
|
+
}
|
|
111
|
+
const hash = `${foundTypeProperty.slug}|${foundTPP.label}`;
|
|
112
|
+
currentLinksHashes[hash] = currentLink;
|
|
113
|
+
}
|
|
114
|
+
const reducedLinksByHashes = {};
|
|
115
|
+
for (const loadedLink of reducedTypePropertyPolicyLinks) {
|
|
116
|
+
const hash = `${loadedLink.typePropertySlug}|${loadedLink.typePropertyPolicyLabel}`;
|
|
117
|
+
reducedLinksByHashes[hash] = loadedLink;
|
|
118
|
+
}
|
|
119
|
+
console.debug('current type property policy links:', currentLinksHashes);
|
|
120
|
+
console.debug('reduced type property policy links:', reducedLinksByHashes);
|
|
121
|
+
for (const loadedTPPLink of reducedTypePropertyPolicyLinks) {
|
|
122
|
+
try {
|
|
123
|
+
const found = currentLinksHashes[`${loadedTPPLink.typePropertySlug}|${loadedTPPLink.typePropertyPolicyLabel}`];
|
|
124
|
+
if (!found) {
|
|
125
|
+
if (!transformReducedLink(loadedTPPLink, typeProperties, currentTPPs)) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
console.log('Creating: ', loadedTPPLink);
|
|
129
|
+
await new Entities().create({
|
|
130
|
+
entityName: 'type-property-policy-link',
|
|
131
|
+
object: {
|
|
132
|
+
typePropertyPolicyId: loadedTPPLink.typePropertyPolicyId,
|
|
133
|
+
typePropertyId: loadedTPPLink.typePropertyId,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
if (!transformReducedLink(loadedTPPLink, typeProperties, currentTPPs)) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
console.log('skipping existing link: ', loadedTPPLink);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
throw new Error(`Error Uploading type property policy links: ${err}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
for (const [currentLinkHash, currentLink] of Object.entries(currentLinksHashes)) {
|
|
149
|
+
try {
|
|
150
|
+
const found = reducedLinksByHashes[currentLinkHash];
|
|
151
|
+
if (!found) {
|
|
152
|
+
console.log('Deleting: ', currentLinkHash);
|
|
153
|
+
await new Entities().delete({
|
|
154
|
+
entityName: 'type-property-policy-link',
|
|
155
|
+
id: currentLink['id'],
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
throw new Error(`Error Deleting rule set link: ${err}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function transformReducedLink(TPPLink, typeProperties, typePropertyPolicies) {
|
|
165
|
+
const foundTP = typeProperties.find(tp => tp.slug === TPPLink.typePropertySlug);
|
|
166
|
+
if (!foundTP) {
|
|
167
|
+
console.error('Could not find type property with label', TPPLink.typePropertySlug);
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
TPPLink.typePropertyId = foundTP.id;
|
|
171
|
+
const foundTPP = typePropertyPolicies.find(tpp => tpp.label === TPPLink.typePropertyPolicyLabel);
|
|
172
|
+
if (!foundTPP) {
|
|
173
|
+
console.error('Could not find type property policy with label', TPPLink.typePropertyPolicyLabel);
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
TPPLink.typePropertyPolicyId = foundTPP.id;
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
@@ -1,2 +1,51 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
1
|
+
export interface DataHolder {
|
|
2
|
+
types: any[];
|
|
3
|
+
optionSets: any[];
|
|
4
|
+
optionSetHierarchies: any[];
|
|
5
|
+
ruleSets: any[];
|
|
6
|
+
ruleSetLinks: any[];
|
|
7
|
+
usageInfo: any[];
|
|
8
|
+
typePropertyPolicies: any[];
|
|
9
|
+
typePropertyPolicyLinks: any[];
|
|
10
|
+
}
|
|
11
|
+
export declare function getAll(args: any): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* stores reduced option sets and returns full option sets */
|
|
14
|
+
export declare function getOptionSets(): Promise<any>;
|
|
15
|
+
export declare function getTypes(): Promise<any[]>;
|
|
16
|
+
export declare function reduceTypes(dataHolder: DataHolder): any[];
|
|
17
|
+
export declare function reduceData(data: DataHolder): {
|
|
18
|
+
reducedTypes: any[];
|
|
19
|
+
reducedOptionSets: {
|
|
20
|
+
name: any;
|
|
21
|
+
isArchived: any;
|
|
22
|
+
optionSet: any;
|
|
23
|
+
}[];
|
|
24
|
+
reducedOptionSetHierarchies: {
|
|
25
|
+
hierarchy: any;
|
|
26
|
+
options: any;
|
|
27
|
+
name: any;
|
|
28
|
+
}[];
|
|
29
|
+
reducedRuleSets: {
|
|
30
|
+
ruleSet: any;
|
|
31
|
+
name: any;
|
|
32
|
+
}[];
|
|
33
|
+
reducedRuleSetLinks: any[];
|
|
34
|
+
reducedUsageInfo: {
|
|
35
|
+
used: any;
|
|
36
|
+
editable: any;
|
|
37
|
+
typeSlug: any;
|
|
38
|
+
typePropertySlug: any;
|
|
39
|
+
}[];
|
|
40
|
+
reducedTypePropertyPolicies: {
|
|
41
|
+
label: any;
|
|
42
|
+
policy: {
|
|
43
|
+
version: any;
|
|
44
|
+
statements: any;
|
|
45
|
+
};
|
|
46
|
+
}[];
|
|
47
|
+
reducedTypePropertyPolicyLinks: {
|
|
48
|
+
typePropertySlug: any;
|
|
49
|
+
typePropertyPolicyLabel: any;
|
|
50
|
+
}[];
|
|
51
|
+
};
|