@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.
Files changed (31) hide show
  1. package/lib/cli.js +59 -10
  2. package/lib/types/option-set-hierarchy-exporter.d.ts +1 -0
  3. package/lib/types/option-set-hierarchy-exporter.js +113 -0
  4. package/lib/types/option-set-hierarchy-parser.d.ts +2 -2
  5. package/lib/types/option-set-hierarchy-parser.js +9 -9
  6. package/lib/types/option-set-hierarchy-uploader.d.ts +1 -0
  7. package/lib/types/option-set-hierarchy-uploader.js +110 -0
  8. package/lib/types/option-set-uploader.js +1 -1
  9. package/lib/types/rule-set-uploader.d.ts +1 -0
  10. package/lib/types/rule-set-uploader.js +175 -0
  11. package/lib/types/type-property-policy-uploader.d.ts +1 -0
  12. package/lib/types/type-property-policy-uploader.js +173 -0
  13. package/lib/types/types-extractor.d.ts +51 -2
  14. package/lib/types/types-extractor.js +204 -48
  15. package/lib/types/types-properties-merger.js +14 -8
  16. package/lib/types/types-uploader.js +1 -1
  17. package/lib/types/usage-info-uploader.d.ts +1 -0
  18. package/lib/types/usage-info-uploader.js +128 -0
  19. package/package.json +2 -1
  20. package/src/cli.ts +59 -10
  21. package/src/types/option-set-hierarchy-exporter.ts +125 -0
  22. package/src/types/option-set-hierarchy-parser.ts +9 -9
  23. package/src/types/option-set-hierarchy-uploader.ts +119 -0
  24. package/src/types/option-set-uploader.ts +2 -4
  25. package/src/types/rule-set-uploader.ts +190 -0
  26. package/src/types/type-property-policy-uploader.ts +188 -0
  27. package/src/types/types-extractor.ts +222 -54
  28. package/src/types/types-properties-merger.ts +14 -9
  29. package/src/types/types-uploader.ts +1 -1
  30. package/src/types/usage-info-uploader.ts +135 -0
  31. /package/src/colors/{color-example.csv → colors.csv} +0 -0
@@ -1,34 +1,49 @@
1
1
  import { Entities } from '@contrail/sdk';
2
- import { ReducedType, ReducedOptionSet, ReducedTypeProperty } from './interfaces';
2
+ import { ReducedType, ReducedTypeProperty } from './interfaces';
3
3
  import chalk from 'chalk';
4
4
  import fs from 'fs';
5
5
  import { authUser } from '../auth/login';
6
6
  import { getSeparatorByPlatform } from '../common';
7
+ import pLimit from 'p-limit';
8
+ const limit = pLimit(30);
7
9
 
8
10
  const yaml = require('js-yaml');
9
11
 
10
- export async function getTypes(args) {
12
+ export interface DataHolder {
13
+ types: any[],
14
+ optionSets: any[],
15
+ optionSetHierarchies: any[],
16
+ ruleSets: any[],
17
+ ruleSetLinks: any[],
18
+ usageInfo: any[],
19
+ typePropertyPolicies: any[],
20
+ typePropertyPolicyLinks: any[],
21
+ }
22
+
23
+ export async function getAll(args) {
11
24
  try {
12
25
  const userConfig = await authUser();
13
26
  console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
14
- console.log('%s Retrieving', chalk.yellow.bold('OPTION SETS'));
15
-
16
- const optionSets = await new Entities().get({
17
- entityName: 'type-property-option-set',
18
- });
19
- await getOptionSets(optionSets);
20
27
 
21
- console.log('%s Retrieved', chalk.yellow.bold('OPTION SETS'));
22
- console.log('%s Retrieving', chalk.yellow.bold('TYPES'));
28
+ // make directory to house exports
29
+ const dir = `./types-${Date.now()}`;
30
+ fs.mkdirSync(dir);
23
31
 
24
- const reducedTypes = await getReducedTypes(optionSets);
25
- const typesHolder = {
26
- types: reducedTypes,
32
+ const data: DataHolder = {
33
+ types: await getTypes(),
34
+ optionSets: await getOptionSets(),
35
+ optionSetHierarchies: await getOptionSetHierarchies(),
36
+ ruleSets: await getRuleSets(),
37
+ ruleSetLinks: await getRuleSetLinks(),
38
+ usageInfo: await getUsageInfo(),
39
+ typePropertyPolicies: await getTypePropertyPolicies(),
40
+ typePropertyPolicyLinks: await getTypePropertyPolicyLinks(),
27
41
  };
28
- saveData(typesHolder);
42
+ const reducedData = reduceData(data);
43
+ storeOptionSets(data, dir);
44
+ saveData(reducedData, dir);
29
45
 
30
46
  console.log('%s Retrieved', chalk.yellow.bold('TYPES'));
31
-
32
47
  }
33
48
  catch (e) {
34
49
  console.log(`%s Types Retrieval Failed. ${e}`, chalk.red.bold('FAIL'));
@@ -38,53 +53,179 @@ export async function getTypes(args) {
38
53
  console.log('%s Types Retrieval Complete.', chalk.green.bold('COMPLETE'));
39
54
  }
40
55
 
41
- async function getOptionSets(optionSets) {
42
- let reducedOptionSets = [];
43
- for (const optionSet of optionSets) {
44
- const reducedOptionSet: ReducedOptionSet = {
45
- name: optionSet.name,
46
- isArchived: optionSet.isArchived,
47
- optionSet: optionSet.optionSet,
48
- };
56
+ /**
57
+ * stores reduced option sets and returns full option sets */
58
+ export async function getOptionSets() {
59
+ console.log('%s Retrieving', chalk.yellow.bold('OPTION SETS'));
60
+ const optionSets = await new Entities().get({
61
+ entityName: 'type-property-option-set',
62
+ });
63
+ console.log('%s Retrieved', chalk.yellow.bold('OPTION SETS'));
64
+ return optionSets;
65
+ }
49
66
 
50
- reducedOptionSets.push(reducedOptionSet);
67
+ export async function getTypes() {
68
+ console.log('%s Retrieving', chalk.yellow.bold('TYPES'));
69
+ const entitiesClient = new Entities();
70
+ const types = await entitiesClient.get({
71
+ entityName: 'type',
72
+ });
73
+ console.log(`${chalk.yellow.bold('TYPES')} Found ${types.length} types`);
74
+ const typesWithProperties = [];
75
+ const promises = [];
76
+ for (const type of types) {
77
+ const promise = limit(async () => {
78
+ const t = await entitiesClient.get({
79
+ entityName: 'type',
80
+ id: type.id,
81
+ relations: ['typeProperties'],
82
+ });
83
+ typesWithProperties.push(t);
84
+ });
85
+ promises.push(promise);
51
86
  }
87
+ await Promise.all(promises);
88
+ return typesWithProperties;
89
+ }
52
90
 
53
- const optionSetsHolder = {
54
- optionSets: reducedOptionSets,
55
- };
91
+ async function getOptionSetHierarchies() {
92
+ const entitiesClient = new Entities();
93
+ return await entitiesClient.get({
94
+ entityName: 'option-set-hierarchy',
95
+ });
96
+ }
56
97
 
57
- reducedOptionSets = reducedOptionSets.sort((one, two) => (one.name < two.name ? -1 : 1));
98
+ async function getRuleSets() {
99
+ const entitiesClient = new Entities();
100
+ return await entitiesClient.get({
101
+ entityName: 'type-rule-set',
102
+ });
103
+ }
58
104
 
59
- const CURRENT_DIR = process.cwd();
60
- const separator = getSeparatorByPlatform();
105
+ async function getRuleSetLinks() {
106
+ const entitiesClient = new Entities();
107
+ return await entitiesClient.get({
108
+ entityName: 'type-rule-set-link',
109
+ });
110
+ }
61
111
 
62
- const yamlStr = yaml.dump(optionSetsHolder);
63
- fs.writeFileSync(CURRENT_DIR + separator + 'option_sets.yml', yamlStr, 'utf8');
112
+ async function getUsageInfo() {
113
+ const entitiesClient = new Entities();
114
+ return await entitiesClient.get({
115
+ entityName: 'type-property-usage-info',
116
+ });
64
117
  }
65
118
 
66
- export async function getReducedTypes(optionSets) {
67
- const types = await new Entities().get({
68
- entityName: 'type',
119
+ async function getTypePropertyPolicies() {
120
+ const entitiesClient = new Entities();
121
+ return await entitiesClient.get({
122
+ entityName: 'type-property-policy',
123
+ });
124
+ }
125
+ async function getTypePropertyPolicyLinks() {
126
+ const entitiesClient = new Entities();
127
+ return await entitiesClient.get({
128
+ entityName: 'type-property-policy-link',
69
129
  });
130
+ }
70
131
 
71
- console.log('Found types: ', types.length);
132
+ function reduceTypePropertyPolicies(data: DataHolder) {
133
+ return data?.typePropertyPolicies?.map(x => {
134
+ return {
135
+ label: x.label,
136
+ policy: {
137
+ version: x.policy.version,
138
+ statements: x.policy.statements,
139
+ }
140
+ };
141
+ });
142
+ }
72
143
 
73
- let reducedTypes = [];
74
- for (const type of types) {
75
- const typeWithProperties = await new Entities().get({
76
- entityName: 'type',
77
- id: type.id,
78
- relations: ['typeProperties'],
79
- });
144
+ function reduceTypePropertyPolicyLinks(data: DataHolder) {
145
+ const typeProperties = data?.types?.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
146
+ return data?.typePropertyPolicyLinks?.map(x => {
147
+ return {
148
+ typePropertySlug: typeProperties.find(y => y.id === x.typePropertyId)?.slug,
149
+ typePropertyPolicyLabel: data.typePropertyPolicies.find(y => y.id === x.typePropertyPolicyId)?.label,
150
+ };
151
+ });
152
+ }
153
+
154
+ function reduceUsageInfo(data: DataHolder) {
155
+ const typeProperties = data?.types?.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
156
+ return data?.usageInfo?.map(x => {
157
+ return {
158
+ used: x.used,
159
+ editable: x.editable,
160
+ typeSlug: data.types.find(y => y.id === x.typeId)?.slug,
161
+ typePropertySlug: typeProperties.find(y => y.id === x.typePropertyId)?.slug,
162
+ };
163
+ });
164
+ }
80
165
 
81
- const optionSetNameById = {};
82
- for (const optionSet of optionSets) {
83
- optionSetNameById[optionSet.id] = optionSet.name;
166
+ function reduceRuleSetLinks(data: DataHolder) {
167
+ const reducedRuleSetLinks = [];
168
+ if (!data?.ruleSetLinks.length) {
169
+ return [];
170
+ }
171
+ for (const ruleSetLink of data.ruleSetLinks) {
172
+ const ruleSet = data?.ruleSets?.find(x => x.id === ruleSetLink.typeRuleSetId);
173
+ const type = data?.types?.find(x => x.id === ruleSetLink.typeId);
174
+
175
+ if (!ruleSet || !type) {
176
+ console.log('%s Skipping bad rule set link', ruleSetLink.id, chalk.red.bold('TYPES'));
84
177
  }
178
+ reducedRuleSetLinks.push({
179
+ ruleSet: ruleSet.name,
180
+ type: type.slug
181
+ });
182
+ }
183
+ return reducedRuleSetLinks;
184
+ }
185
+
186
+ function reduceRuleSets(data: DataHolder) {
187
+ return data?.ruleSets?.map(x => {
188
+ return {
189
+ ruleSet: x.ruleSet,
190
+ name: x.name,
191
+ };
192
+ });
193
+ }
194
+
195
+ function reduceOptionSets(data: DataHolder) {
196
+ return data.optionSets?.map(optionSet => {
197
+ return {
198
+ name: optionSet.name,
199
+ isArchived: optionSet.isArchived,
200
+ optionSet: optionSet.optionSet,
201
+ };
202
+ });
203
+ }
85
204
 
205
+ function reduceOptionSetHierarchies(data: DataHolder) {
206
+ return data.optionSetHierarchies?.map(x => {
207
+ const hierarchyByName = x.hierarchy.map(
208
+ optionSetId => data.optionSets?.find(x => x.id === optionSetId)?.name
209
+ );
210
+ return {
211
+ hierarchy: hierarchyByName,
212
+ options: x.options,
213
+ name: x.name
214
+ };
215
+ });
216
+ }
217
+
218
+ export function reduceTypes(dataHolder: DataHolder) {
219
+ console.log('Found types: ', dataHolder.types.length);
220
+
221
+ let reducedTypes = [];
222
+ const optionSetNameById = {};
223
+ for (const optionSet of dataHolder.optionSets) {
224
+ optionSetNameById[optionSet.id] = optionSet.name;
225
+ }
226
+ for (const type of dataHolder.types) {
86
227
  const reducedTypeProperties = [];
87
- for (const typeProperty of typeWithProperties.typeProperties) {
228
+ for (const typeProperty of type.typeProperties) {
88
229
  const reducedTypeProperty: ReducedTypeProperty = {
89
230
  slug: typeProperty.slug,
90
231
  id: typeProperty.id,
@@ -108,10 +249,10 @@ export async function getReducedTypes(optionSets) {
108
249
  }
109
250
 
110
251
  const reducedType: ReducedType = {
111
- typePath: typeWithProperties.typePath,
112
- slug: typeWithProperties.slug,
113
- label: typeWithProperties.label,
114
- isArchived: typeWithProperties.isArchived,
252
+ typePath: type.typePath,
253
+ slug: type.slug,
254
+ label: type.label,
255
+ isArchived: type.isArchived,
115
256
  typeProperties: reducedTypeProperties,
116
257
  };
117
258
 
@@ -122,9 +263,36 @@ export async function getReducedTypes(optionSets) {
122
263
  return reducedTypes;
123
264
  }
124
265
 
125
- function saveData(data) {
126
- const yamlStr = yaml.dump(data);
266
+ export function reduceData(data: DataHolder) {
267
+ return {
268
+ reducedTypes: reduceTypes(data),
269
+ reducedOptionSets: reduceOptionSets(data),
270
+ reducedOptionSetHierarchies: reduceOptionSetHierarchies(data),
271
+ reducedRuleSets: reduceRuleSets(data),
272
+ reducedRuleSetLinks: reduceRuleSetLinks(data),
273
+ reducedUsageInfo: reduceUsageInfo(data),
274
+ reducedTypePropertyPolicies: reduceTypePropertyPolicies(data),
275
+ reducedTypePropertyPolicyLinks: reduceTypePropertyPolicyLinks(data),
276
+ };
277
+ }
278
+
279
+ function storeOptionSets(data: DataHolder, dir: string) {
280
+ let reducedOptionSets = reduceOptionSets(data);
281
+ reducedOptionSets = reducedOptionSets.sort((one, two) => (one.name < two.name ? -1 : 1));
282
+ const optionSetsHolder = {
283
+ reducedOptionSets,
284
+ };
285
+
286
+ const CURRENT_DIR = process.cwd();
287
+ const separator = getSeparatorByPlatform();
288
+
289
+ const yamlStr = yaml.dump(optionSetsHolder);
290
+ fs.writeFileSync(CURRENT_DIR + separator + dir + separator + 'option_sets.yml', yamlStr, 'utf8');
291
+ }
292
+
293
+ function saveData(reducedData, dir: string) {
294
+ const yamlStr = yaml.dump(reducedData);
127
295
  const CURRENT_DIR = process.cwd();
128
296
  const separator = getSeparatorByPlatform();
129
- fs.writeFileSync(CURRENT_DIR + separator + 'types.yml', yamlStr, 'utf8');
297
+ fs.writeFileSync(CURRENT_DIR + separator + dir + separator + 'types.yml', yamlStr, 'utf8');
130
298
  }
@@ -1,5 +1,4 @@
1
- import { Entities } from '@contrail/sdk';
2
- import { getReducedTypes } from './types-extractor';
1
+ import { DataHolder, getOptionSets, getTypes, reduceData } from './types-extractor';
3
2
  import { ReducedTypeProperty, ReducedType } from './interfaces';
4
3
  import fs from 'fs';
5
4
  import { getSeparatorByPlatform } from '../common';
@@ -51,21 +50,27 @@ async function mergeTypeProperties(fileLocation, typePaths) {
51
50
 
52
51
  await authUser();
53
52
 
54
- const optionSets = await new Entities().get({
55
- entityName: 'type-property-option-set',
56
- });
57
-
58
- const reducedTypes = await getReducedTypes(optionSets);
53
+ const data: DataHolder = {
54
+ types: await getTypes(),
55
+ optionSets: await getOptionSets(),
56
+ optionSetHierarchies: [],
57
+ ruleSets: [],
58
+ ruleSetLinks: [],
59
+ usageInfo: [],
60
+ typePropertyPolicies: [],
61
+ typePropertyPolicyLinks: [],
62
+ };
63
+ const reducedData = await reduceData(data);
59
64
 
60
65
  for (const typePath of typePaths) {
61
- const foundType = reducedTypes.find(reducedTypes => reducedTypes.typePath === typePath);
66
+ const foundType = reducedData.reducedTypes.find(reducedTypes => reducedTypes.typePath === typePath);
62
67
  if (!foundType){
63
68
  console.log(`%s No Type found for: ${typePath}`, chalk.red.bold('WARNING'));
64
69
  }
65
70
  mergeType(foundType, uploadedTypeProperties);
66
71
  }
67
72
 
68
- saveData({types: reducedTypes});
73
+ saveData({types: reducedData.reducedTypes});
69
74
  }
70
75
 
71
76
  function mergeType(reducedType: ReducedType, typePropertiesToAdd: ReducedTypeProperty[]) {
@@ -31,7 +31,7 @@ function loadReducedTypes(){
31
31
  try {
32
32
  const separator = getSeparatorByPlatform();
33
33
  const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
34
- return doc.types;
34
+ return doc.reducedTypes;
35
35
  } catch (e) {
36
36
  throw new Error(`ERROR PARSING YAML: ${e}`);
37
37
  }
@@ -0,0 +1,135 @@
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
+
8
+ const yaml = require('js-yaml');
9
+
10
+ export async function uploadUsageInfo(args) {
11
+ try {
12
+ await authUser();
13
+
14
+ console.log('%s Uploading', chalk.yellow.bold('USAGE INFO'));
15
+ const reducedUsageInfo = await loadUsageInfo();
16
+ await uploadUsageInfoObjects(reducedUsageInfo);
17
+ console.log('%s Uploaded', chalk.yellow.bold('USAGE INFO'));
18
+
19
+ }
20
+ catch (e) {
21
+ console.log(`%s Usage Info Upload Failed. ${e}`, chalk.red.bold('FAIL'));
22
+ return;
23
+ }
24
+
25
+ console.log('%s Usage Info Upload Complete.', chalk.green.bold('COMPLETE'));
26
+ }
27
+
28
+ function loadUsageInfo() {
29
+ const TARGET_DIR = process.cwd();
30
+ try {
31
+ const separator = getSeparatorByPlatform();
32
+ const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
33
+ return doc.reducedUsageInfo;
34
+ } catch (e) {
35
+ throw new Error(`ERROR PARSING YAML: ${e}`);
36
+ }
37
+ }
38
+
39
+ async function uploadUsageInfoObjects(loadedUsageInfo) {
40
+ const typesWithProperties = await getTypes();
41
+ const typeProperties = typesWithProperties
42
+ .filter(t=>!!t.typeProperties)
43
+ .map(t => t.typeProperties)
44
+ .reduce((a, b) => a.concat(b));
45
+
46
+ const currentUsageInfo = await new Entities().get({
47
+ entityName: 'type-property-usage-info',
48
+ });
49
+
50
+ const currentUsageInfosHashes = {};
51
+ for(const currentUI of currentUsageInfo) {
52
+ const foundType = typesWithProperties.find(t => t.id === currentUI.typeId);
53
+ const foundTypeProperty = typeProperties.find(tp => tp.id === currentUI.typePropertyId);
54
+ if(!foundType || !foundTypeProperty) {
55
+ console.log('skipping unhashable, invalid usage info:', currentUI);
56
+ continue;
57
+ }
58
+ const hash = `${foundType.slug}|${foundTypeProperty.slug}`;
59
+ currentUsageInfosHashes[hash] = currentUI;
60
+ }
61
+
62
+ const reducedUsageInfoHashes = {};
63
+ for(const loadedUI of loadedUsageInfo) {
64
+ const hash = `${loadedUI.typeSlug}|${loadedUI.typePropertySlug}`;
65
+ reducedUsageInfoHashes[hash] = loadedUI;
66
+ }
67
+ console.debug('current usage info:', currentUsageInfo);
68
+ console.debug('loaded usage info:', loadedUsageInfo);
69
+
70
+ for (const loadedUI of loadedUsageInfo) {
71
+ try {
72
+ const found = currentUsageInfosHashes[`${loadedUI.typeSlug}|${loadedUI.typePropertySlug}`];
73
+ if (!found) {
74
+ if (!transformReducedUI(loadedUI, typesWithProperties, typeProperties)) { continue; }
75
+ console.debug('Creating: ', loadedUI);
76
+ await new Entities().create({
77
+ entityName: 'type-property-usage-info',
78
+ object: {
79
+ typePropertyId: loadedUI.typePropertyId,
80
+ typeId: loadedUI.typeId,
81
+ editable: loadedUI.editable,
82
+ used: loadedUI.used,
83
+ }
84
+ });
85
+ } else {
86
+ if (!transformReducedUI(loadedUI, typesWithProperties, typeProperties)) { continue; }
87
+ console.log('Updating: ', loadedUI);
88
+ await new Entities().update({
89
+ entityName: 'type-property-usage-info',
90
+ id: found.id,
91
+ object: {
92
+ editable: loadedUI.editable,
93
+ used: loadedUI.used,
94
+ }
95
+ });
96
+ }
97
+ } catch (err) {
98
+ throw new Error(`Error Uploading usage info: ${err}`);
99
+ }
100
+ }
101
+
102
+ for (const [hash, currentUI] of Object.entries(currentUsageInfosHashes)) {
103
+ try {
104
+ const found = reducedUsageInfoHashes[hash];
105
+ if (!found) {
106
+ console.debug('Deleting: ', hash);
107
+ await new Entities().delete({
108
+ entityName: 'type-property-usage-info',
109
+ id: currentUI['id'],
110
+ });
111
+ }
112
+ } catch (err) {
113
+ throw new Error(`Error Deleting usage info: ${err}`);
114
+ }
115
+ }
116
+
117
+ }
118
+
119
+ // transforms the reduced Usage Info from name slugs to ids
120
+ function transformReducedUI(reducedUsageInfo, types, typeProperties) {
121
+ const foundType = types.find(t => t.slug === reducedUsageInfo.typeSlug);
122
+ if (!foundType) {
123
+ console.error('Could not find type with slug', reducedUsageInfo.typeSlug, 'while transforming', reducedUsageInfo);
124
+ return false;
125
+ }
126
+ reducedUsageInfo.typeId = foundType.id;
127
+
128
+ const foundTypeProperty = typeProperties.find(tp => tp.slug === reducedUsageInfo.typePropertySlug);
129
+ if (!foundTypeProperty) {
130
+ console.error('Could not find type property with slug', reducedUsageInfo.typePropertySlug, 'while transforming', reducedUsageInfo);
131
+ return false;
132
+ }
133
+ reducedUsageInfo.typePropertyId = foundTypeProperty.id;
134
+ return true;
135
+ }
File without changes