@contrail/cli 1.0.54 → 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.
@@ -3,23 +3,29 @@ import chalk from 'chalk';
3
3
  import fs from 'fs';
4
4
  import { authUser } from '../auth/login';
5
5
  import { getSeparatorByPlatform } from '../common';
6
+ import pLimit from 'p-limit';
7
+ const limit = pLimit(30);
6
8
  const yaml = require('js-yaml');
7
- export async function getTypes(args) {
9
+ export async function getAll(args) {
8
10
  try {
9
11
  const userConfig = await authUser();
10
12
  console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
11
- console.log('%s Retrieving', chalk.yellow.bold('OPTION SETS'));
12
- const optionSets = await new Entities().get({
13
- entityName: 'type-property-option-set',
14
- });
15
- await getOptionSets(optionSets);
16
- console.log('%s Retrieved', chalk.yellow.bold('OPTION SETS'));
17
- console.log('%s Retrieving', chalk.yellow.bold('TYPES'));
18
- const reducedTypes = await getReducedTypes(optionSets);
19
- const typesHolder = {
20
- types: reducedTypes,
13
+ // make directory to house exports
14
+ const dir = `./types-${Date.now()}`;
15
+ fs.mkdirSync(dir);
16
+ const data = {
17
+ types: await getTypes(),
18
+ optionSets: await getOptionSets(),
19
+ optionSetHierarchies: await getOptionSetHierarchies(),
20
+ ruleSets: await getRuleSets(),
21
+ ruleSetLinks: await getRuleSetLinks(),
22
+ usageInfo: await getUsageInfo(),
23
+ typePropertyPolicies: await getTypePropertyPolicies(),
24
+ typePropertyPolicyLinks: await getTypePropertyPolicyLinks(),
21
25
  };
22
- saveData(typesHolder);
26
+ const reducedData = reduceData(data);
27
+ storeOptionSets(data, dir);
28
+ saveData(reducedData, dir);
23
29
  console.log('%s Retrieved', chalk.yellow.bold('TYPES'));
24
30
  }
25
31
  catch (e) {
@@ -28,43 +34,170 @@ export async function getTypes(args) {
28
34
  }
29
35
  console.log('%s Types Retrieval Complete.', chalk.green.bold('COMPLETE'));
30
36
  }
31
- async function getOptionSets(optionSets) {
32
- let reducedOptionSets = [];
33
- for (const optionSet of optionSets) {
34
- const reducedOptionSet = {
37
+ /**
38
+ * stores reduced option sets and returns full option sets */
39
+ export async function getOptionSets() {
40
+ console.log('%s Retrieving', chalk.yellow.bold('OPTION SETS'));
41
+ const optionSets = await new Entities().get({
42
+ entityName: 'type-property-option-set',
43
+ });
44
+ console.log('%s Retrieved', chalk.yellow.bold('OPTION SETS'));
45
+ return optionSets;
46
+ }
47
+ export async function getTypes() {
48
+ console.log('%s Retrieving', chalk.yellow.bold('TYPES'));
49
+ const entitiesClient = new Entities();
50
+ const types = await entitiesClient.get({
51
+ entityName: 'type',
52
+ });
53
+ console.log(`${chalk.yellow.bold('TYPES')} Found ${types.length} types`);
54
+ const typesWithProperties = [];
55
+ const promises = [];
56
+ for (const type of types) {
57
+ const promise = limit(async () => {
58
+ const t = await entitiesClient.get({
59
+ entityName: 'type',
60
+ id: type.id,
61
+ relations: ['typeProperties'],
62
+ });
63
+ typesWithProperties.push(t);
64
+ });
65
+ promises.push(promise);
66
+ }
67
+ await Promise.all(promises);
68
+ return typesWithProperties;
69
+ }
70
+ async function getOptionSetHierarchies() {
71
+ const entitiesClient = new Entities();
72
+ return await entitiesClient.get({
73
+ entityName: 'option-set-hierarchy',
74
+ });
75
+ }
76
+ async function getRuleSets() {
77
+ const entitiesClient = new Entities();
78
+ return await entitiesClient.get({
79
+ entityName: 'type-rule-set',
80
+ });
81
+ }
82
+ async function getRuleSetLinks() {
83
+ const entitiesClient = new Entities();
84
+ return await entitiesClient.get({
85
+ entityName: 'type-rule-set-link',
86
+ });
87
+ }
88
+ async function getUsageInfo() {
89
+ const entitiesClient = new Entities();
90
+ return await entitiesClient.get({
91
+ entityName: 'type-property-usage-info',
92
+ });
93
+ }
94
+ async function getTypePropertyPolicies() {
95
+ const entitiesClient = new Entities();
96
+ return await entitiesClient.get({
97
+ entityName: 'type-property-policy',
98
+ });
99
+ }
100
+ async function getTypePropertyPolicyLinks() {
101
+ const entitiesClient = new Entities();
102
+ return await entitiesClient.get({
103
+ entityName: 'type-property-policy-link',
104
+ });
105
+ }
106
+ function reduceTypePropertyPolicies(data) {
107
+ var _a;
108
+ return (_a = data === null || data === void 0 ? void 0 : data.typePropertyPolicies) === null || _a === void 0 ? void 0 : _a.map(x => {
109
+ return {
110
+ label: x.label,
111
+ policy: {
112
+ version: x.policy.version,
113
+ statements: x.policy.statements,
114
+ }
115
+ };
116
+ });
117
+ }
118
+ function reduceTypePropertyPolicyLinks(data) {
119
+ var _a, _b;
120
+ const typeProperties = (_a = data === null || data === void 0 ? void 0 : data.types) === null || _a === void 0 ? void 0 : _a.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
121
+ return (_b = data === null || data === void 0 ? void 0 : data.typePropertyPolicyLinks) === null || _b === void 0 ? void 0 : _b.map(x => {
122
+ var _a, _b;
123
+ return {
124
+ typePropertySlug: (_a = typeProperties.find(y => y.id === x.typePropertyId)) === null || _a === void 0 ? void 0 : _a.slug,
125
+ typePropertyPolicyLabel: (_b = data.typePropertyPolicies.find(y => y.id === x.typePropertyPolicyId)) === null || _b === void 0 ? void 0 : _b.label,
126
+ };
127
+ });
128
+ }
129
+ function reduceUsageInfo(data) {
130
+ var _a, _b;
131
+ const typeProperties = (_a = data === null || data === void 0 ? void 0 : data.types) === null || _a === void 0 ? void 0 : _a.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
132
+ return (_b = data === null || data === void 0 ? void 0 : data.usageInfo) === null || _b === void 0 ? void 0 : _b.map(x => {
133
+ var _a, _b;
134
+ return {
135
+ used: x.used,
136
+ editable: x.editable,
137
+ typeSlug: (_a = data.types.find(y => y.id === x.typeId)) === null || _a === void 0 ? void 0 : _a.slug,
138
+ typePropertySlug: (_b = typeProperties.find(y => y.id === x.typePropertyId)) === null || _b === void 0 ? void 0 : _b.slug,
139
+ };
140
+ });
141
+ }
142
+ function reduceRuleSetLinks(data) {
143
+ var _a, _b;
144
+ const reducedRuleSetLinks = [];
145
+ if (!(data === null || data === void 0 ? void 0 : data.ruleSetLinks.length)) {
146
+ return [];
147
+ }
148
+ for (const ruleSetLink of data.ruleSetLinks) {
149
+ const ruleSet = (_a = data === null || data === void 0 ? void 0 : data.ruleSets) === null || _a === void 0 ? void 0 : _a.find(x => x.id === ruleSetLink.typeRuleSetId);
150
+ const type = (_b = data === null || data === void 0 ? void 0 : data.types) === null || _b === void 0 ? void 0 : _b.find(x => x.id === ruleSetLink.typeId);
151
+ if (!ruleSet || !type) {
152
+ console.log('%s Skipping bad rule set link', ruleSetLink.id, chalk.red.bold('TYPES'));
153
+ }
154
+ reducedRuleSetLinks.push({
155
+ ruleSet: ruleSet.name,
156
+ type: type.slug
157
+ });
158
+ }
159
+ return reducedRuleSetLinks;
160
+ }
161
+ function reduceRuleSets(data) {
162
+ var _a;
163
+ return (_a = data === null || data === void 0 ? void 0 : data.ruleSets) === null || _a === void 0 ? void 0 : _a.map(x => {
164
+ return {
165
+ ruleSet: x.ruleSet,
166
+ name: x.name,
167
+ };
168
+ });
169
+ }
170
+ function reduceOptionSets(data) {
171
+ var _a;
172
+ return (_a = data.optionSets) === null || _a === void 0 ? void 0 : _a.map(optionSet => {
173
+ return {
35
174
  name: optionSet.name,
36
175
  isArchived: optionSet.isArchived,
37
176
  optionSet: optionSet.optionSet,
38
177
  };
39
- reducedOptionSets.push(reducedOptionSet);
40
- }
41
- const optionSetsHolder = {
42
- optionSets: reducedOptionSets,
43
- };
44
- reducedOptionSets = reducedOptionSets.sort((one, two) => (one.name < two.name ? -1 : 1));
45
- const CURRENT_DIR = process.cwd();
46
- const separator = getSeparatorByPlatform();
47
- const yamlStr = yaml.dump(optionSetsHolder);
48
- fs.writeFileSync(CURRENT_DIR + separator + 'option_sets.yml', yamlStr, 'utf8');
178
+ });
49
179
  }
50
- export async function getReducedTypes(optionSets) {
51
- const types = await new Entities().get({
52
- entityName: 'type',
180
+ function reduceOptionSetHierarchies(data) {
181
+ var _a;
182
+ return (_a = data.optionSetHierarchies) === null || _a === void 0 ? void 0 : _a.map(x => {
183
+ const hierarchyByName = x.hierarchy.map(optionSetId => { var _a, _b; return (_b = (_a = data.optionSets) === null || _a === void 0 ? void 0 : _a.find(x => x.id === optionSetId)) === null || _b === void 0 ? void 0 : _b.name; });
184
+ return {
185
+ hierarchy: hierarchyByName,
186
+ options: x.options,
187
+ name: x.name
188
+ };
53
189
  });
54
- console.log('Found types: ', types.length);
190
+ }
191
+ export function reduceTypes(dataHolder) {
192
+ console.log('Found types: ', dataHolder.types.length);
55
193
  let reducedTypes = [];
56
- for (const type of types) {
57
- const typeWithProperties = await new Entities().get({
58
- entityName: 'type',
59
- id: type.id,
60
- relations: ['typeProperties'],
61
- });
62
- const optionSetNameById = {};
63
- for (const optionSet of optionSets) {
64
- optionSetNameById[optionSet.id] = optionSet.name;
65
- }
194
+ const optionSetNameById = {};
195
+ for (const optionSet of dataHolder.optionSets) {
196
+ optionSetNameById[optionSet.id] = optionSet.name;
197
+ }
198
+ for (const type of dataHolder.types) {
66
199
  const reducedTypeProperties = [];
67
- for (const typeProperty of typeWithProperties.typeProperties) {
200
+ for (const typeProperty of type.typeProperties) {
68
201
  const reducedTypeProperty = {
69
202
  slug: typeProperty.slug,
70
203
  id: typeProperty.id,
@@ -86,10 +219,10 @@ export async function getReducedTypes(optionSets) {
86
219
  reducedTypeProperties.push(reducedTypeProperty);
87
220
  }
88
221
  const reducedType = {
89
- typePath: typeWithProperties.typePath,
90
- slug: typeWithProperties.slug,
91
- label: typeWithProperties.label,
92
- isArchived: typeWithProperties.isArchived,
222
+ typePath: type.typePath,
223
+ slug: type.slug,
224
+ label: type.label,
225
+ isArchived: type.isArchived,
93
226
  typeProperties: reducedTypeProperties,
94
227
  };
95
228
  reducedTypes.push(reducedType);
@@ -97,9 +230,32 @@ export async function getReducedTypes(optionSets) {
97
230
  reducedTypes = reducedTypes.sort((one, two) => (one.typePath < two.typePath ? -1 : 1));
98
231
  return reducedTypes;
99
232
  }
100
- function saveData(data) {
101
- const yamlStr = yaml.dump(data);
233
+ export function reduceData(data) {
234
+ return {
235
+ reducedTypes: reduceTypes(data),
236
+ reducedOptionSets: reduceOptionSets(data),
237
+ reducedOptionSetHierarchies: reduceOptionSetHierarchies(data),
238
+ reducedRuleSets: reduceRuleSets(data),
239
+ reducedRuleSetLinks: reduceRuleSetLinks(data),
240
+ reducedUsageInfo: reduceUsageInfo(data),
241
+ reducedTypePropertyPolicies: reduceTypePropertyPolicies(data),
242
+ reducedTypePropertyPolicyLinks: reduceTypePropertyPolicyLinks(data),
243
+ };
244
+ }
245
+ function storeOptionSets(data, dir) {
246
+ let reducedOptionSets = reduceOptionSets(data);
247
+ reducedOptionSets = reducedOptionSets.sort((one, two) => (one.name < two.name ? -1 : 1));
248
+ const optionSetsHolder = {
249
+ reducedOptionSets,
250
+ };
251
+ const CURRENT_DIR = process.cwd();
252
+ const separator = getSeparatorByPlatform();
253
+ const yamlStr = yaml.dump(optionSetsHolder);
254
+ fs.writeFileSync(CURRENT_DIR + separator + dir + separator + 'option_sets.yml', yamlStr, 'utf8');
255
+ }
256
+ function saveData(reducedData, dir) {
257
+ const yamlStr = yaml.dump(reducedData);
102
258
  const CURRENT_DIR = process.cwd();
103
259
  const separator = getSeparatorByPlatform();
104
- fs.writeFileSync(CURRENT_DIR + separator + 'types.yml', yamlStr, 'utf8');
260
+ fs.writeFileSync(CURRENT_DIR + separator + dir + separator + 'types.yml', yamlStr, 'utf8');
105
261
  }
@@ -1,5 +1,4 @@
1
- import { Entities } from '@contrail/sdk';
2
- import { getReducedTypes } from './types-extractor';
1
+ import { getOptionSets, getTypes, reduceData } from './types-extractor';
3
2
  import fs from 'fs';
4
3
  import { getSeparatorByPlatform } from '../common';
5
4
  import { authUser } from '../auth/login';
@@ -40,18 +39,25 @@ function loadTypeProperties(fileLocation) {
40
39
  async function mergeTypeProperties(fileLocation, typePaths) {
41
40
  const uploadedTypeProperties = loadTypeProperties(fileLocation);
42
41
  await authUser();
43
- const optionSets = await new Entities().get({
44
- entityName: 'type-property-option-set',
45
- });
46
- const reducedTypes = await getReducedTypes(optionSets);
42
+ const data = {
43
+ types: await getTypes(),
44
+ optionSets: await getOptionSets(),
45
+ optionSetHierarchies: [],
46
+ ruleSets: [],
47
+ ruleSetLinks: [],
48
+ usageInfo: [],
49
+ typePropertyPolicies: [],
50
+ typePropertyPolicyLinks: [],
51
+ };
52
+ const reducedData = await reduceData(data);
47
53
  for (const typePath of typePaths) {
48
- const foundType = reducedTypes.find(reducedTypes => reducedTypes.typePath === typePath);
54
+ const foundType = reducedData.reducedTypes.find(reducedTypes => reducedTypes.typePath === typePath);
49
55
  if (!foundType) {
50
56
  console.log(`%s No Type found for: ${typePath}`, chalk.red.bold('WARNING'));
51
57
  }
52
58
  mergeType(foundType, uploadedTypeProperties);
53
59
  }
54
- saveData({ types: reducedTypes });
60
+ saveData({ types: reducedData.reducedTypes });
55
61
  }
56
62
  function mergeType(reducedType, typePropertiesToAdd) {
57
63
  for (const typeProperty of typePropertiesToAdd) {
@@ -24,7 +24,7 @@ function loadReducedTypes() {
24
24
  try {
25
25
  const separator = getSeparatorByPlatform();
26
26
  const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
27
- return doc.types;
27
+ return doc.reducedTypes;
28
28
  }
29
29
  catch (e) {
30
30
  throw new Error(`ERROR PARSING YAML: ${e}`);
@@ -0,0 +1 @@
1
+ export declare function uploadUsageInfo(args: any): Promise<void>;
@@ -0,0 +1,128 @@
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
+ import { getTypes } from './types-extractor';
7
+ const yaml = require('js-yaml');
8
+ export async function uploadUsageInfo(args) {
9
+ try {
10
+ await authUser();
11
+ console.log('%s Uploading', chalk.yellow.bold('USAGE INFO'));
12
+ const reducedUsageInfo = await loadUsageInfo();
13
+ await uploadUsageInfoObjects(reducedUsageInfo);
14
+ console.log('%s Uploaded', chalk.yellow.bold('USAGE INFO'));
15
+ }
16
+ catch (e) {
17
+ console.log(`%s Usage Info Upload Failed. ${e}`, chalk.red.bold('FAIL'));
18
+ return;
19
+ }
20
+ console.log('%s Usage Info Upload Complete.', chalk.green.bold('COMPLETE'));
21
+ }
22
+ function loadUsageInfo() {
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.reducedUsageInfo;
28
+ }
29
+ catch (e) {
30
+ throw new Error(`ERROR PARSING YAML: ${e}`);
31
+ }
32
+ }
33
+ async function uploadUsageInfoObjects(loadedUsageInfo) {
34
+ const typesWithProperties = await getTypes();
35
+ const typeProperties = typesWithProperties
36
+ .filter(t => !!t.typeProperties)
37
+ .map(t => t.typeProperties)
38
+ .reduce((a, b) => a.concat(b));
39
+ const currentUsageInfo = await new Entities().get({
40
+ entityName: 'type-property-usage-info',
41
+ });
42
+ const currentUsageInfosHashes = {};
43
+ for (const currentUI of currentUsageInfo) {
44
+ const foundType = typesWithProperties.find(t => t.id === currentUI.typeId);
45
+ const foundTypeProperty = typeProperties.find(tp => tp.id === currentUI.typePropertyId);
46
+ if (!foundType || !foundTypeProperty) {
47
+ console.log('skipping unhashable, invalid usage info:', currentUI);
48
+ continue;
49
+ }
50
+ const hash = `${foundType.slug}|${foundTypeProperty.slug}`;
51
+ currentUsageInfosHashes[hash] = currentUI;
52
+ }
53
+ const reducedUsageInfoHashes = {};
54
+ for (const loadedUI of loadedUsageInfo) {
55
+ const hash = `${loadedUI.typeSlug}|${loadedUI.typePropertySlug}`;
56
+ reducedUsageInfoHashes[hash] = loadedUI;
57
+ }
58
+ console.debug('current usage info:', currentUsageInfo);
59
+ console.debug('loaded usage info:', loadedUsageInfo);
60
+ for (const loadedUI of loadedUsageInfo) {
61
+ try {
62
+ const found = currentUsageInfosHashes[`${loadedUI.typeSlug}|${loadedUI.typePropertySlug}`];
63
+ if (!found) {
64
+ if (!transformReducedUI(loadedUI, typesWithProperties, typeProperties)) {
65
+ continue;
66
+ }
67
+ console.debug('Creating: ', loadedUI);
68
+ await new Entities().create({
69
+ entityName: 'type-property-usage-info',
70
+ object: {
71
+ typePropertyId: loadedUI.typePropertyId,
72
+ typeId: loadedUI.typeId,
73
+ editable: loadedUI.editable,
74
+ used: loadedUI.used,
75
+ }
76
+ });
77
+ }
78
+ else {
79
+ if (!transformReducedUI(loadedUI, typesWithProperties, typeProperties)) {
80
+ continue;
81
+ }
82
+ console.log('Updating: ', loadedUI);
83
+ await new Entities().update({
84
+ entityName: 'type-property-usage-info',
85
+ id: found.id,
86
+ object: {
87
+ editable: loadedUI.editable,
88
+ used: loadedUI.used,
89
+ }
90
+ });
91
+ }
92
+ }
93
+ catch (err) {
94
+ throw new Error(`Error Uploading usage info: ${err}`);
95
+ }
96
+ }
97
+ for (const [hash, currentUI] of Object.entries(currentUsageInfosHashes)) {
98
+ try {
99
+ const found = reducedUsageInfoHashes[hash];
100
+ if (!found) {
101
+ console.debug('Deleting: ', hash);
102
+ await new Entities().delete({
103
+ entityName: 'type-property-usage-info',
104
+ id: currentUI['id'],
105
+ });
106
+ }
107
+ }
108
+ catch (err) {
109
+ throw new Error(`Error Deleting usage info: ${err}`);
110
+ }
111
+ }
112
+ }
113
+ // transforms the reduced Usage Info from name slugs to ids
114
+ function transformReducedUI(reducedUsageInfo, types, typeProperties) {
115
+ const foundType = types.find(t => t.slug === reducedUsageInfo.typeSlug);
116
+ if (!foundType) {
117
+ console.error('Could not find type with slug', reducedUsageInfo.typeSlug, 'while transforming', reducedUsageInfo);
118
+ return false;
119
+ }
120
+ reducedUsageInfo.typeId = foundType.id;
121
+ const foundTypeProperty = typeProperties.find(tp => tp.slug === reducedUsageInfo.typePropertySlug);
122
+ if (!foundTypeProperty) {
123
+ console.error('Could not find type property with slug', reducedUsageInfo.typePropertySlug, 'while transforming', reducedUsageInfo);
124
+ return false;
125
+ }
126
+ reducedUsageInfo.typePropertyId = foundTypeProperty.id;
127
+ return true;
128
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/cli",
3
- "version": "1.0.54",
3
+ "version": "1.0.55",
4
4
  "description": "VibeIQ's Contrail Platform CLI.",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
package/src/cli.ts CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { create } from './apps/create-app/create';
3
3
  import { deployApp, removeApp } from './apps/deploy-app/deploy';
4
- import { getTypes } from './types/types-extractor';
4
+ import { getAll } from './types/types-extractor';
5
5
  import { uploadTypes } from './types/types-uploader';
6
6
  import { UploadOptionSets } from './types/option-set-uploader';
7
7
  import { ParseOptionSetsLocalFiles } from './types/option-set-parser';
@@ -19,6 +19,10 @@ import { cliInstallWorkflowTemplateDefinitions, listWorkflowTemplateDefinitions
19
19
  import { configureProject } from './projects/project-uploader';
20
20
  import { ListProjects, ExportProject } from './projects/project-exporter';
21
21
  import { Load, UploadAndLoad } from './loader-process/loader-process';
22
+ import { uploadUsageInfo } from './types/usage-info-uploader';
23
+ import { uploadRuleSets } from './types/rule-set-uploader';
24
+ import { UploadOptionSetHierarchies } from './types/option-set-hierarchy-uploader';
25
+ import { uploadTypePropertyPolicies } from './types/type-property-policy-uploader';
22
26
 
23
27
  const cliUsage = require('command-line-usage');
24
28
 
@@ -89,7 +93,7 @@ export async function cli(args) {
89
93
  case 'types': {
90
94
  switch (command) {
91
95
  case 'getAll': {
92
- getTypes(args);
96
+ getAll(args);
93
97
  break;
94
98
  }
95
99
  case 'uploadTypes': {
@@ -116,6 +120,22 @@ export async function cli(args) {
116
120
  ExportOptionSetsHierarchies();
117
121
  break;
118
122
  }
123
+ case 'uploadOptionSetsHierarchies': {
124
+ UploadOptionSetHierarchies(args);
125
+ break;
126
+ }
127
+ case 'uploadRuleSets': {
128
+ uploadRuleSets(args);
129
+ break;
130
+ }
131
+ case 'uploadUsageInfo': {
132
+ uploadUsageInfo(args);
133
+ break;
134
+ }
135
+ case 'uploadTypePropertyPolicies': {
136
+ uploadTypePropertyPolicies(args);
137
+ break;
138
+ }
119
139
  case 'parseTypesFromLocalFiles': {
120
140
  ParseTypesFromLocalFiles(args);
121
141
  break;
@@ -418,6 +438,26 @@ const helpSections = [
418
438
  name: 'exportOptionSetsHierarchies',
419
439
  summary: 'export OptionSetHierarchies into CSVs.\n'
420
440
  },
441
+ {
442
+ name: 'uploadOptionSetsHierarchies',
443
+ summary: 'Upload an extracted option set hierarchies.'
444
+ },
445
+ {
446
+ name: 'uploadRuleSets',
447
+ summary: 'Upload an extracted rule set.'
448
+ },
449
+ {
450
+ name: 'uploadUsageInfo',
451
+ summary: 'Upload extracted usage info.'
452
+ },
453
+ {
454
+ name: 'uploadTypePropertyPolicies',
455
+ summary: 'Upload type property policies.'
456
+ },
457
+ {
458
+ name: 'uploadOptionSetsHierarchyLocalFiles <File Paths> <name>',
459
+ summary: 'upload CSV files into OptionSetHierarchies.'
460
+ },
421
461
  {
422
462
  name: 'parseTypesFromLocalFiles <File Paths>',
423
463
  summary: 'Parse CSV files into Types and OptionSets from local files.\n'
@@ -0,0 +1,119 @@
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
+
7
+ const yaml = require('js-yaml');
8
+
9
+ export async function UploadOptionSetHierarchies(args) {
10
+ try {
11
+ await authUser();
12
+
13
+ console.log('%s Uploading', chalk.yellow.bold('OPTION SET HIERARCHIES'));
14
+ const optionSetHierarchies = await loadOptionSetHierarchies();
15
+ await uploadOptionSetHierarchies(optionSetHierarchies);
16
+ console.log('%s Uploaded', chalk.yellow.bold('OPTION SET HIERARCHIES'));
17
+
18
+ }
19
+ catch (e) {
20
+ console.log(`%s Option set hierarchy Upload Failed. ${e}`, chalk.red.bold('FAIL'));
21
+ return;
22
+ }
23
+
24
+ console.log('%s Option set hierarchy Upload Complete.', chalk.green.bold('COMPLETE'));
25
+ }
26
+
27
+ function loadOptionSetHierarchies() {
28
+ const TARGET_DIR = process.cwd();
29
+ try {
30
+ const separator = getSeparatorByPlatform();
31
+ const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
32
+ return doc.reducedOptionSetHierarchies;
33
+ } catch (e) {
34
+ throw new Error(`ERROR PARSING YAML: ${e}`);
35
+ }
36
+ }
37
+
38
+ async function uploadOptionSetHierarchies(loadedOSHs) {
39
+
40
+ const optionSets = await new Entities().get({
41
+ entityName: 'type-property-option-set',
42
+ });
43
+
44
+ const currentOSHs = await new Entities().get({
45
+ entityName: 'option-set-hierarchy',
46
+ });
47
+
48
+ const optionSetsByName = {};
49
+ for (const optionSet of optionSets) {
50
+ optionSetsByName[optionSet.name] = optionSet;
51
+ }
52
+
53
+ const currentOSHByName = {};
54
+ for (const currentOSH of currentOSHs) {
55
+ currentOSHByName[currentOSH.name] = currentOSH;
56
+ }
57
+
58
+ const loadedOSHByName = {};
59
+ for (const loadedOSH of loadedOSHs) {
60
+ loadedOSHByName[loadedOSH.name] = loadedOSH;
61
+ }
62
+
63
+ console.debug('current OSH:', currentOSHs);
64
+ console.debug('loaded OSH:', loadedOSHs);
65
+
66
+ for (const osh of loadedOSHs) {
67
+ try {
68
+ const found = currentOSHByName[osh.name];
69
+ if (!found) {
70
+ if (!transformOSHNameToID(osh.hierarchy, optionSets)) { continue; }
71
+ console.log('Creating: ', osh);
72
+ await new Entities().create({
73
+ entityName: 'option-set-hierarchy',
74
+ object: osh
75
+ });
76
+ } else {
77
+ if (!transformOSHNameToID(osh.hierarchy, optionSets)) { continue; }
78
+ console.log('Updating: ', osh);
79
+ await new Entities().update({
80
+ entityName: 'option-set-hierarchy',
81
+ id: found.id,
82
+ object: osh,
83
+ });
84
+ }
85
+ } catch (err) {
86
+ throw new Error(`Error Uploading option set hierarchy: ${err}`);
87
+ }
88
+ }
89
+
90
+ for (const currentOSH of currentOSHs) {
91
+ try {
92
+ const found = loadedOSHByName[currentOSH.name];
93
+ if (!found) {
94
+ console.log('Deleting: ', currentOSH.name);
95
+ await new Entities().delete({
96
+ entityName: 'option-set-hierarchy',
97
+ id: currentOSH.id,
98
+ });
99
+ }
100
+ } catch (err) {
101
+ throw new Error(`Error Deleting Option set hierarchy: ${err}`);
102
+ }
103
+ }
104
+
105
+ }
106
+
107
+ // transforms the passed hierarchy from names to ids, and returns true if operation succeeded.
108
+ function transformOSHNameToID(hierarchy, optionSets) {
109
+ console.log('passed hierarhy:', hierarchy);
110
+ for (let i = 0; i < hierarchy.length; i++) {
111
+ const os = optionSets.find(os => os.name === hierarchy[i]);
112
+ if (!os) {
113
+ console.error('could not find option set with name', hierarchy[i], ' at position', i, 'skipping...');
114
+ return false;
115
+ }
116
+ hierarchy[i] = os.id;
117
+ }
118
+ return true;
119
+ }