@contrail/cli 3.4.11-alpha.0f34b47 → 3.4.11-alpha.11e1d43

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uploadTypePropertyPolicies = void 0;
3
+ exports.findAllOccurrences = exports.uploadTypePropertyPolicies = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const sdk_1 = require("@contrail/sdk");
6
6
  const fs_1 = tslib_1.__importDefault(require("fs"));
@@ -8,10 +8,12 @@ const path = tslib_1.__importStar(require("path"));
8
8
  const login_1 = require("../../auth/login");
9
9
  const chalk_1 = tslib_1.__importDefault(require("chalk"));
10
10
  const core_1 = require("@oclif/core");
11
+ const type_extractor_1 = require("../../helpers/types/type-extractor");
11
12
  const users_1 = require("../../api/users");
12
13
  const basecommand_1 = require("../../basecommand");
13
14
  const logger_1 = tslib_1.__importDefault(require("../../logger"));
14
15
  const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
16
+ const _ = tslib_1.__importStar(require("lodash"));
15
17
  class UploadTypePropertyPolicies extends basecommand_1.BaseCommand {
16
18
  static description = 'Upload type property policies';
17
19
  static examples = [
@@ -36,12 +38,14 @@ async function uploadTypePropertyPolicies(dir) {
36
38
  try {
37
39
  await (0, login_1.authUser)();
38
40
  logger_1.default.info(`${chalk_1.default.yellow.bold('TYPE PROPERTY POLICY')} Uploading`);
39
- const reducedTypePropertyPolicies = await loadTPPolicies(dir);
41
+ const [reducedTypePropertyPolicies, reducedTypePropertyPolicyLinks] = await loadTPPolicies(dir);
40
42
  await uploadTPPolicyObjects(reducedTypePropertyPolicies);
43
+ await uploadTypePropertyPolicyLinks(reducedTypePropertyPolicyLinks);
41
44
  logger_1.default.info(`${chalk_1.default.yellow.bold('TYPE PROPERTY POLICY')} Uploaded`);
42
45
  }
43
46
  catch (e) {
44
47
  logger_1.default.info(`${chalk_1.default.red.bold('FAIL')} Type Property Policy Upload Failed . ${e}`);
48
+ //TODO make this only print if a debug flag is set
45
49
  console.trace(e);
46
50
  return;
47
51
  }
@@ -50,13 +54,15 @@ async function uploadTypePropertyPolicies(dir) {
50
54
  exports.uploadTypePropertyPolicies = uploadTypePropertyPolicies;
51
55
  function loadTPPolicies(fromDir) {
52
56
  const tpps = [];
57
+ let tppLinks = [];
53
58
  const dir = path.join(fromDir, 'type-property-policies');
54
59
  try {
55
60
  const files = fs_1.default.readdirSync(dir);
56
61
  for (const file of files) {
57
62
  tpps.push(js_yaml_1.default.load(fs_1.default.readFileSync(path.join(dir, file), 'utf8')));
58
63
  }
59
- return tpps;
64
+ tppLinks = js_yaml_1.default.load(fs_1.default.readFileSync(path.join(fromDir, 'type-property-policy-links.yml'), 'utf8'));
65
+ return [tpps, tppLinks];
60
66
  }
61
67
  catch (e) {
62
68
  throw new Error(`ERROR PARSING YAML: ${e}`);
@@ -149,3 +155,133 @@ async function uploadTPPolicyObjects(reducedTypePropertyPolicies) {
149
155
  }
150
156
  }
151
157
  }
158
+ async function uploadTypePropertyPolicyLinks(reducedTypePropertyPolicyLinks) {
159
+ const typesWithProperties = await (0, type_extractor_1.getTypes)();
160
+ const typeProperties = [];
161
+ typesWithProperties.forEach((type) => {
162
+ if (type.typeProperties) {
163
+ for (const tp of type.typeProperties) {
164
+ tp.typeId = type.id;
165
+ tp.typeSlug = type.slug;
166
+ typeProperties.push(tp);
167
+ }
168
+ }
169
+ });
170
+ const currentTPPs = await new sdk_1.Entities().get({
171
+ entityName: 'type-property-policy',
172
+ });
173
+ const currentTPPLinks = await new sdk_1.Entities().get({
174
+ entityName: 'type-property-policy-link',
175
+ });
176
+ logger_1.default.info(`found ${currentTPPLinks.length} current type property policy links`);
177
+ logger_1.default.info(`found ${reducedTypePropertyPolicyLinks.length} type property policy links to load`);
178
+ const currentLinksHashes = {};
179
+ for (const currentLink of currentTPPLinks) {
180
+ const foundTPs = findAllOccurrences(typeProperties, (tp) => tp.id === currentLink.typePropertyId);
181
+ for (const foundTP of foundTPs) {
182
+ const foundTPP = currentTPPs.find((tpp) => tpp.id === currentLink.typePropertyPolicyId);
183
+ if (!foundTP || !foundTPP) {
184
+ logger_1.default.info(`skipping unhashable, invalid policy link: ${JSON.stringify(currentLink, null, 2)}`);
185
+ continue;
186
+ }
187
+ const hash = `${foundTP.typeSlug}|${foundTP.slug}|${foundTPP.identifier}`;
188
+ currentLinksHashes[hash] = currentLink;
189
+ }
190
+ }
191
+ logger_1.default.debug(`current links hashes: ${JSON.stringify(currentLinksHashes, null, 2)}`);
192
+ const reducedLinksByHashes = {};
193
+ for (const loadedLink of reducedTypePropertyPolicyLinks) {
194
+ const hash = `${loadedLink.typeSlug}|${loadedLink.typePropertySlug}|${loadedLink.typePropertyPolicyIdentifier}`;
195
+ reducedLinksByHashes[hash] = loadedLink;
196
+ }
197
+ const toCreate = Object.keys(reducedLinksByHashes)
198
+ .filter((x) => !currentLinksHashes[x])
199
+ .map((x) => {
200
+ return {
201
+ [x]: reducedLinksByHashes[x],
202
+ };
203
+ })
204
+ .reduce((a, b) => Object.assign(a, b), {});
205
+ const toDelete = _.uniqBy(Object.keys(currentLinksHashes)
206
+ .filter((x) => !reducedLinksByHashes[x])
207
+ .map((x) => {
208
+ return {
209
+ [x]: currentLinksHashes[x],
210
+ };
211
+ }), 'id').reduce((a, b) => Object.assign(a, b), {});
212
+ logger_1.default.debug(`reduced links hashes: ${JSON.stringify(reducedLinksByHashes, null, 2)}`);
213
+ logger_1.default.info(`found ${Object.keys(toCreate).length} type property policy links to create`);
214
+ logger_1.default.info(`found ${Object.keys(toDelete).length} type property policy links to delete`);
215
+ logger_1.default.debug(`to create: ${JSON.stringify(toCreate, null, 2)}`);
216
+ logger_1.default.debug(`to delete: ${JSON.stringify(toDelete, null, 2)}`);
217
+ logger_1.default.debug(`current links hashes to delete: ${JSON.stringify(Object.keys(currentLinksHashes).filter((x) => !reducedLinksByHashes[x]), null, 2)}`);
218
+ // fs.writeFileSync('toCreate.json', JSON.stringify(toCreate, null, 2));
219
+ // fs.writeFileSync('toDelete.json', JSON.stringify(toDelete, null, 2));
220
+ // fs.writeFileSync('currentLinksHashes.json', JSON.stringify(currentLinksHashes, null, 2));
221
+ // fs.writeFileSync('reducedLinksByHashes.json', JSON.stringify(reducedLinksByHashes, null, 2));
222
+ // fs.writeFileSync(
223
+ // 'current_link_hashes_to_delete.json',
224
+ // JSON.stringify(
225
+ // Object.keys(currentLinksHashes).filter((x) => !reducedLinksByHashes[x]),
226
+ // null,
227
+ // 2,
228
+ // ),
229
+ // );
230
+ // return;
231
+ for (const [hash, link] of Object.entries(toCreate)) {
232
+ try {
233
+ if (!transformReducedLink(link, typeProperties, currentTPPs)) {
234
+ continue;
235
+ }
236
+ logger_1.default.info(`Creating: ${hash}: ${JSON.stringify(link, null, 2)}`);
237
+ await new sdk_1.Entities().create({
238
+ entityName: 'type-property-policy-link',
239
+ object: {
240
+ typePropertyPolicyId: link.typePropertyPolicyId,
241
+ typePropertyId: link.typePropertyId,
242
+ },
243
+ });
244
+ }
245
+ catch (err) {
246
+ logger_1.default.error(`Error Uploading type property policy links: ${err}`);
247
+ }
248
+ }
249
+ for (const [hash, link] of Object.entries(toDelete)) {
250
+ try {
251
+ logger_1.default.info(`Deleting: ${hash}`);
252
+ await new sdk_1.Entities().delete({
253
+ entityName: 'type-property-policy-link',
254
+ id: link['id'],
255
+ });
256
+ }
257
+ catch (err) {
258
+ logger_1.default.error(`Error Deleting type property policy link: ${err}`);
259
+ }
260
+ }
261
+ function transformReducedLink(TPPLink, typeProperties, typePropertyPolicies) {
262
+ const foundTP = typeProperties.find((tp) => tp.slug === TPPLink.typePropertySlug && tp.typeSlug === TPPLink.typeSlug);
263
+ if (!foundTP) {
264
+ logger_1.default.error(`Could not find type property with slug ${TPPLink.typePropertySlug} `);
265
+ return false;
266
+ }
267
+ TPPLink.typePropertyId = foundTP.id;
268
+ const foundTPP = typePropertyPolicies.find((tpp) => tpp.identifier === TPPLink.typePropertyPolicyIdentifier);
269
+ if (!foundTPP) {
270
+ logger_1.default.error(`Could not find type property policy with identifier ${TPPLink.typePropertyPolicyIdentifier} `);
271
+ return false;
272
+ }
273
+ TPPLink.typePropertyPolicyId = foundTPP.id;
274
+ return true;
275
+ }
276
+ }
277
+ function findAllOccurrences(array, evaluator) {
278
+ const found = [];
279
+ for (let i = 0; i < array.length; i++) {
280
+ const element = array[i];
281
+ if (evaluator(element)) {
282
+ found.push(element);
283
+ }
284
+ }
285
+ return found;
286
+ }
287
+ exports.findAllOccurrences = findAllOccurrences;
@@ -2,9 +2,6 @@ import { BaseCommand } from '../../basecommand';
2
2
  import { PersistReducedUsageInfo } from '../../interfaces';
3
3
  export default class UploadUsageInfo extends BaseCommand {
4
4
  static description: string;
5
- static deprecationOptions: {
6
- message: string;
7
- };
8
5
  static examples: string[];
9
6
  static flags: {
10
7
  dir: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
@@ -13,10 +13,7 @@ const basecommand_1 = require("../../basecommand");
13
13
  const logger_1 = tslib_1.__importDefault(require("../../logger"));
14
14
  const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
15
15
  class UploadUsageInfo extends basecommand_1.BaseCommand {
16
- static description = '[DEPRECATED] Upload extracted usage info. Use uploadTypeOverrides instead.';
17
- static deprecationOptions = {
18
- message: 'uploadUsageInfo is deprecated. Usage info is now managed as type overrides. Use `contrail types uploadTypeOverrides` instead.',
19
- };
16
+ static description = 'Upload extracted usage info';
20
17
  static examples = [
21
18
  `$ contrail types uploadUsageInfo
22
19
  `,