@contrail/cli 3.6.0-alpha-8-alpha.2ab4f80 → 3.6.0-alpha-8-alpha.351376b
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/README.md +80 -58
- package/dist/api/feature-flags.d.ts +3 -0
- package/dist/api/feature-flags.js +29 -0
- package/dist/api/users.d.ts +1 -0
- package/dist/api/users.js +10 -1
- package/dist/auth/login.js +44 -26
- package/dist/auth/login.spec.js +124 -0
- package/dist/basecommand.js +3 -3
- package/dist/commands/app/getApiKey.js +2 -2
- package/dist/commands/entities/create.d.ts +1 -13
- package/dist/commands/entities/create.js +31 -98
- package/dist/commands/entities/create.spec.js +3 -32
- package/dist/commands/entities/delete.d.ts +2 -6
- package/dist/commands/entities/delete.js +53 -72
- package/dist/commands/entities/get.d.ts +0 -4
- package/dist/commands/entities/get.js +15 -66
- package/dist/commands/entities/update.d.ts +2 -15
- package/dist/commands/entities/update.js +44 -80
- package/dist/commands/feature-flags/export.d.ts +19 -0
- package/dist/commands/feature-flags/export.js +66 -0
- package/dist/commands/feature-flags/export.spec.js +109 -0
- package/dist/commands/feature-flags/import.d.ts +42 -0
- package/dist/commands/feature-flags/import.js +191 -0
- package/dist/commands/feature-flags/import.spec.js +194 -0
- package/dist/commands/instance/export.d.ts +9 -0
- package/dist/commands/instance/export.js +18 -3
- package/dist/commands/instance/export.spec.js +32 -0
- package/dist/commands/instance/import.d.ts +3 -0
- package/dist/commands/instance/import.js +5 -0
- package/dist/commands/instance/import.spec.js +4 -0
- package/dist/common.d.ts +0 -3
- package/dist/common.js +1 -18
- package/dist/helpers/config/config.d.ts +2 -0
- package/dist/helpers/config/config.js +6 -1
- package/dist/helpers/entities/assortment-items.d.ts +2 -4
- package/dist/helpers/entities/assortment-items.js +1 -4
- package/dist/helpers/file-helpers.js +2 -2
- package/dist/helpers/propertySpecifications/propertySpecifications.d.ts +0 -8
- package/dist/helpers/propertySpecifications/propertySpecifications.js +9 -21
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.js +10 -20
- package/dist/logger.js +1 -4
- package/node_modules/@contrail/entity-compare/package.json +3 -6
- package/node_modules/@contrail/loader/lib/common/conditional-columns.js +1 -1
- package/node_modules/@contrail/loader/lib/constants.d.ts +8 -0
- package/node_modules/@contrail/loader/lib/constants.js +9 -1
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.d.ts +4 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.js +57 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.spec.js +492 -34
- package/node_modules/@contrail/loader/lib/pipeline/validate.js +2 -52
- package/node_modules/@contrail/loader/lib/pipeline/validate.spec.js +0 -85
- package/node_modules/@contrail/loader/package.json +2 -2
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/image-search/image-search.d.ts +34 -0
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/image-search/image-search.js +42 -0
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/image-search/index.d.ts +1 -0
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/image-search/index.js +17 -0
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/index.d.ts +1 -0
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/index.js +1 -0
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/package.json +1 -1
- package/oclif.manifest.json +137 -99
- package/package.json +4 -1
- package/dist/commands/entities/grammar.spec.js +0 -80
- package/dist/commands/entities/output.spec.js +0 -72
- package/dist/logger.spec.js +0 -12
- /package/dist/commands/{entities/grammar.spec.d.ts → feature-flags/export.spec.d.ts} +0 -0
- /package/dist/commands/{entities/output.spec.d.ts → feature-flags/import.spec.d.ts} +0 -0
- /package/dist/{logger.spec.d.ts → commands/instance/export.spec.d.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PropertyLevel
|
|
1
|
+
import { PropertyLevel } from '@contrail/types';
|
|
2
2
|
import { ColonDelimited, TypeProperty as ExtendedTypeProperty, TypeReferencesCacheByPath } from '../../api/types';
|
|
3
3
|
import { TypeWithProperties } from '../../api/types';
|
|
4
4
|
import { BaseCommand } from '../../basecommand';
|
|
@@ -14,8 +14,6 @@ export default class CreateEntity extends BaseCommand {
|
|
|
14
14
|
generateValues: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
15
15
|
readableIds: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
16
16
|
skipHydrateRelationships: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
17
|
-
suffix: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
18
|
-
apiVersion: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
19
17
|
alwaysConfirmEnv: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
20
18
|
logLevel: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
21
19
|
};
|
|
@@ -23,17 +21,8 @@ export default class CreateEntity extends BaseCommand {
|
|
|
23
21
|
path: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
24
22
|
};
|
|
25
23
|
static strict: boolean;
|
|
26
|
-
init(): Promise<void>;
|
|
27
24
|
run(): Promise<void>;
|
|
28
25
|
}
|
|
29
|
-
export type CreateSummary = {
|
|
30
|
-
created: any[];
|
|
31
|
-
failed: {
|
|
32
|
-
input: any;
|
|
33
|
-
error: string;
|
|
34
|
-
}[];
|
|
35
|
-
};
|
|
36
|
-
export declare function summarizeSettledCreates(inputs: any[], settled: PromiseSettledResult<any>[]): CreateSummary;
|
|
37
26
|
export type WithId = {
|
|
38
27
|
id: string;
|
|
39
28
|
[key: string]: any;
|
|
@@ -41,7 +30,6 @@ export type WithId = {
|
|
|
41
30
|
export type ObjectReferenceCache = Map<string, WithId>;
|
|
42
31
|
type ObjectReferenceCachesByTypePath = Map<string, ObjectReferenceCache>;
|
|
43
32
|
export declare function pickRandomValueForProperty(property: ExtendedTypeProperty, objectReferencesCache: ObjectReferenceCachesByTypePath, typeReferencesCache: TypeReferencesCacheByPath): number | string | string[] | boolean | null;
|
|
44
|
-
export declare function castValueToPropertyType(value: string | number | string[] | number[], propertyType: PropertyType): any;
|
|
45
33
|
export declare function getReferencedObjectPaths(type: TypeWithProperties): ColonDelimited[];
|
|
46
34
|
export declare function buildObjectReferenceCache(entities: any[]): ObjectReferenceCache;
|
|
47
35
|
export declare function selectRandomObject(cache: ObjectReferenceCache): WithId;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.doesPropertyLevelMatchRole = exports.selectRandomObject = exports.buildObjectReferenceCache = exports.getReferencedObjectPaths = exports.
|
|
3
|
+
exports.doesPropertyLevelMatchRole = exports.selectRandomObject = exports.buildObjectReferenceCache = exports.getReferencedObjectPaths = exports.pickRandomValueForProperty = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
6
6
|
const unique_names_generator_1 = require("unique-names-generator");
|
|
@@ -10,7 +10,6 @@ const types_2 = require("../../api/types");
|
|
|
10
10
|
const core_1 = require("@oclif/core");
|
|
11
11
|
const types_3 = require("../../api/types");
|
|
12
12
|
const login_1 = require("../../auth/login");
|
|
13
|
-
const common_1 = require("../../common");
|
|
14
13
|
const basecommand_1 = require("../../basecommand");
|
|
15
14
|
const sdk_1 = require("@contrail/sdk");
|
|
16
15
|
const entities_1 = require("../../helpers/entities");
|
|
@@ -27,7 +26,7 @@ const baseValidationMap = {
|
|
|
27
26
|
itemId: async (_slug, itemId) => (0, entities_1.throwIfInvalidItemId)(itemId),
|
|
28
27
|
projectId: async (_slug, projectId) => (0, entities_1.throwIfInvalidProjectId)(projectId),
|
|
29
28
|
};
|
|
30
|
-
const throwIfInvalidPropertyLevelFn = async (_slug, maybeRoles) =>
|
|
29
|
+
const throwIfInvalidPropertyLevelFn = async (_slug, maybeRoles) => maybeRoles.forEach((maybeRole) => (0, entities_1.throwIfInvalidItemLevel)(maybeRole));
|
|
31
30
|
const throwIfInvalidUserOrgRoleFn = async (_slug, maybeRole) => {
|
|
32
31
|
if (maybeRole !== 'MEMBER' && maybeRole !== 'ADMIN') {
|
|
33
32
|
throw new Error(`${maybeRole} is not a valid User Org Role`); // TODO: get these roles from a shared library
|
|
@@ -145,7 +144,7 @@ const FIND_VALID_VALUE_MAP = {
|
|
|
145
144
|
],
|
|
146
145
|
};
|
|
147
146
|
class CreateEntity extends basecommand_1.BaseCommand {
|
|
148
|
-
static description = 'Create entities';
|
|
147
|
+
static description = '(BETA) Create entities';
|
|
149
148
|
static examples = [
|
|
150
149
|
'contrail entities create color -g -n=10 # creates 10 random colors',
|
|
151
150
|
'contrail entities create color -- name="Electric Blue" green=1 red=2 blue=3 # creates one color with specified properties only',
|
|
@@ -195,20 +194,11 @@ class CreateEntity extends basecommand_1.BaseCommand {
|
|
|
195
194
|
default: false,
|
|
196
195
|
}),
|
|
197
196
|
skipHydrateRelationships: core_1.Flags.boolean({
|
|
198
|
-
char: '
|
|
197
|
+
char: 's',
|
|
199
198
|
description: 'If true, does not hydrate relationships for the created entities',
|
|
200
199
|
required: false,
|
|
201
200
|
default: false,
|
|
202
201
|
}),
|
|
203
|
-
suffix: core_1.Flags.string({
|
|
204
|
-
description: 'Append a sub-route to the entity endpoint (e.g. --suffix=synchronous targets /<entities>/synchronous)',
|
|
205
|
-
required: false,
|
|
206
|
-
}),
|
|
207
|
-
apiVersion: core_1.Flags.string({
|
|
208
|
-
description: 'Api-Version header to send; selects the V1 or V2 variant of the endpoint',
|
|
209
|
-
options: ['1', '2'],
|
|
210
|
-
required: false,
|
|
211
|
-
}),
|
|
212
202
|
};
|
|
213
203
|
static args = {
|
|
214
204
|
path: core_1.Args.string({
|
|
@@ -218,14 +208,11 @@ class CreateEntity extends basecommand_1.BaseCommand {
|
|
|
218
208
|
}),
|
|
219
209
|
};
|
|
220
210
|
static strict = false;
|
|
221
|
-
async init() {
|
|
222
|
-
(0, common_1.routeConsoleLogToStderr)();
|
|
223
|
-
await super.init();
|
|
224
|
-
}
|
|
225
211
|
async run() {
|
|
212
|
+
this.log('🚨 This command is in BETA and may change or be removed at any time.');
|
|
226
213
|
await (0, login_1.authUser)();
|
|
227
214
|
const { flags, argv, args } = await this.parse(CreateEntity);
|
|
228
|
-
const { count, dryRun: isDryRun, inputFile: inputFilename, outputFile: outputFilename, generateValues: shouldGenerateMissingValues, readableIds: shouldUseReadableIds, skipHydrateRelationships,
|
|
215
|
+
const { count, dryRun: isDryRun, inputFile: inputFilename, outputFile: outputFilename, generateValues: shouldGenerateMissingValues, readableIds: shouldUseReadableIds, skipHydrateRelationships, } = flags;
|
|
229
216
|
const { path: entityTypePath } = args;
|
|
230
217
|
// Global flags is a little hacky, but it was easier
|
|
231
218
|
// and it doesn't see *that* bad because they are only valid
|
|
@@ -234,11 +221,10 @@ class CreateEntity extends basecommand_1.BaseCommand {
|
|
|
234
221
|
SHOULD_USE_READABLE_IDS = shouldUseReadableIds;
|
|
235
222
|
SHOULD_SKIP_HYDRATE_RELATIONSHIPS = skipHydrateRelationships;
|
|
236
223
|
SHOULD_GENERATE_MISSING_VALUES = shouldGenerateMissingValues;
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const propertySpecBySlug = (0, propertySpecifications_1.parsePropertySpecificationsIntoMap)(specs);
|
|
224
|
+
const propertySpecificationArgs = argv
|
|
225
|
+
.slice(1, argv.length)
|
|
226
|
+
.filter(propertySpecifications_1.isValidPropertySpecification);
|
|
227
|
+
const propertySpecBySlug = (0, propertySpecifications_1.parsePropertySpecificationsIntoMap)(propertySpecificationArgs);
|
|
242
228
|
// Validate args
|
|
243
229
|
if (!entityTypePath) {
|
|
244
230
|
this.error('You must specify an entity type path.');
|
|
@@ -271,20 +257,13 @@ class CreateEntity extends basecommand_1.BaseCommand {
|
|
|
271
257
|
}
|
|
272
258
|
try {
|
|
273
259
|
const entitiesToCreate = await buildEntitiesToCreate(entityTypePath, partialEntities, propertySpecBySlug);
|
|
274
|
-
const
|
|
275
|
-
suffix,
|
|
276
|
-
apiVersion: apiVersion,
|
|
277
|
-
});
|
|
260
|
+
const createdEntities = await createEntities(entityName, entitiesToCreate);
|
|
278
261
|
if (outputFilename) {
|
|
279
|
-
fs_1.default.writeFileSync(outputFilename, JSON.stringify(
|
|
280
|
-
|
|
262
|
+
fs_1.default.writeFileSync(outputFilename, JSON.stringify(createdEntities, null, 2));
|
|
263
|
+
this.log(`Output saved to ${outputFilename}`);
|
|
281
264
|
}
|
|
282
265
|
else {
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
if (summary.failed.length > 0) {
|
|
286
|
-
console.error(`${summary.failed.length} of ${entitiesToCreate.length} entities failed.`);
|
|
287
|
-
process.exitCode = 1;
|
|
266
|
+
console.log(`Created ${entityTypePath}(s), Entity: ${JSON.stringify(createdEntities, null, 2)}`);
|
|
288
267
|
}
|
|
289
268
|
}
|
|
290
269
|
catch (error) {
|
|
@@ -293,28 +272,15 @@ class CreateEntity extends basecommand_1.BaseCommand {
|
|
|
293
272
|
}
|
|
294
273
|
}
|
|
295
274
|
exports.default = CreateEntity;
|
|
296
|
-
function
|
|
297
|
-
const
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
created.push(result.value);
|
|
302
|
-
}
|
|
303
|
-
else {
|
|
304
|
-
failed.push({ input: inputs[index], error: (0, common_1.sdkErrorMessage)(result.reason) });
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
return { created, failed };
|
|
308
|
-
}
|
|
309
|
-
exports.summarizeSettledCreates = summarizeSettledCreates;
|
|
310
|
-
async function createEntities(entityName, entities, routing = {}) {
|
|
275
|
+
async function createEntities(entityName, entities) {
|
|
276
|
+
const limit = (0, p_limit_1.default)(20);
|
|
277
|
+
const createdEntities = IS_DRY_RUN
|
|
278
|
+
? entities
|
|
279
|
+
: await Promise.allSettled(entities.map(async (e) => limit(() => createEntity(entityName, e))));
|
|
311
280
|
if (IS_DRY_RUN) {
|
|
312
|
-
console.
|
|
313
|
-
return { created: entities, failed: [] };
|
|
281
|
+
console.log(`Dry run mode: not creating entities, but simulating creation for ${entityName}`);
|
|
314
282
|
}
|
|
315
|
-
|
|
316
|
-
const settled = await Promise.allSettled(entities.map(async (e) => limit(() => createEntity(entityName, e, routing))));
|
|
317
|
-
return summarizeSettledCreates(entities, settled);
|
|
283
|
+
return createdEntities;
|
|
318
284
|
}
|
|
319
285
|
async function buildEntitiesToCreate(entityTypePath, entities, propertySpecificationBySlug) {
|
|
320
286
|
let type = await (0, types_3.getTypeByPath)(entityTypePath);
|
|
@@ -337,7 +303,7 @@ async function buildEntitiesToCreate(entityTypePath, entities, propertySpecifica
|
|
|
337
303
|
await Promise.all(populatedEntities.map(async () => limit(() => throwIfEntityIsInvalid)));
|
|
338
304
|
// Skip system value generation unless -g flag is set (generateValues mode)
|
|
339
305
|
if (!SHOULD_GENERATE_MISSING_VALUES) {
|
|
340
|
-
console.
|
|
306
|
+
console.log('Using only specified properties. Use --generate-values to auto-generate missing values.');
|
|
341
307
|
return populatedEntities;
|
|
342
308
|
}
|
|
343
309
|
// Add system values if needed
|
|
@@ -346,13 +312,13 @@ async function buildEntitiesToCreate(entityTypePath, entities, propertySpecifica
|
|
|
346
312
|
for (const { slug, findValidValuesFn, applyValueToEntityFn } of valueFinderApplierForEntity) {
|
|
347
313
|
// If the property is already specified in command-line args, skip it
|
|
348
314
|
if (propertySpecificationBySlug?.has(slug)) {
|
|
349
|
-
console.
|
|
315
|
+
console.log(`Skipping property ${slug} because it is already specified.`);
|
|
350
316
|
continue;
|
|
351
317
|
}
|
|
352
318
|
// If all entities already have this property, skip finding values
|
|
353
319
|
const allEntitiesHaveProperty = populatedEntities.every((entity) => entity[slug] !== undefined && entity[slug] !== null);
|
|
354
320
|
if (allEntitiesHaveProperty) {
|
|
355
|
-
console.
|
|
321
|
+
console.log(`Skipping property ${slug} because all entities already have this value.`);
|
|
356
322
|
continue;
|
|
357
323
|
}
|
|
358
324
|
const values = await findValidValuesFn(populatedEntities, entities.length);
|
|
@@ -366,16 +332,15 @@ async function buildEntitiesToCreate(entityTypePath, entities, propertySpecifica
|
|
|
366
332
|
return populatedEntities;
|
|
367
333
|
}
|
|
368
334
|
function createEntityWithPopulatedValues(type, typeReferencesCache, objectReferencesCache, propertySpecificationBySlug) {
|
|
369
|
-
const
|
|
370
|
-
const roles = rolesValue === undefined ? undefined : [].concat(rolesValue);
|
|
335
|
+
const roles = propertySpecificationBySlug?.get('roles');
|
|
371
336
|
const entity = SHOULD_GENERATE_MISSING_VALUES
|
|
372
337
|
? pickRandomPropertyValues(type, objectReferencesCache, typeReferencesCache, roles)
|
|
373
338
|
: {};
|
|
374
339
|
// Handle property specifications
|
|
375
340
|
for (const [propertySlug, specifiedValue] of propertySpecificationBySlug.entries()) {
|
|
376
341
|
const correspondingProperty = type.typeProperties.find((prop) => getPropertySlug(prop) === propertySlug);
|
|
377
|
-
if (specifiedValue
|
|
378
|
-
|
|
342
|
+
if (!specifiedValue) {
|
|
343
|
+
throw new Error(`Value for property ${propertySlug} is required`);
|
|
379
344
|
}
|
|
380
345
|
entity[propertySlug] = castValueToPropertyType(specifiedValue, correspondingProperty?.propertyType || types_1.PropertyType.String);
|
|
381
346
|
}
|
|
@@ -383,16 +348,6 @@ function createEntityWithPopulatedValues(type, typeReferencesCache, objectRefere
|
|
|
383
348
|
if (SHOULD_USE_READABLE_IDS) {
|
|
384
349
|
entity.specifiedId = generateRandomReadableString();
|
|
385
350
|
}
|
|
386
|
-
// roles is a multi-value system property with no type property to drive
|
|
387
|
-
// coercion; a single value still has to reach the API as an array
|
|
388
|
-
if (typeof entity.roles === 'string') {
|
|
389
|
-
if (entity.roles.includes(',')) {
|
|
390
|
-
console.error(`roles value "${entity.roles}" contains a comma and is treated as one literal value. To pass several roles, repeat the key: roles=${entity.roles
|
|
391
|
-
.split(',')
|
|
392
|
-
.join(' roles=')}`);
|
|
393
|
-
}
|
|
394
|
-
entity.roles = [entity.roles];
|
|
395
|
-
}
|
|
396
351
|
// Support specifiedId
|
|
397
352
|
if (propertySpecificationBySlug?.has('specifiedId')) {
|
|
398
353
|
const specifiedId = propertySpecificationBySlug.get('specifiedId');
|
|
@@ -524,9 +479,6 @@ function generateRandomDateString() {
|
|
|
524
479
|
return new Date().toISOString();
|
|
525
480
|
}
|
|
526
481
|
function castValueToPropertyType(value, propertyType) {
|
|
527
|
-
if (Array.isArray(value) && propertyType !== types_1.PropertyType.MultiSelect) {
|
|
528
|
-
return value.map((element) => castValueToPropertyType(element, propertyType));
|
|
529
|
-
}
|
|
530
482
|
switch (propertyType) {
|
|
531
483
|
case types_1.PropertyType.Boolean:
|
|
532
484
|
return value.toString().toLowerCase() === 'true';
|
|
@@ -538,35 +490,16 @@ function castValueToPropertyType(value, propertyType) {
|
|
|
538
490
|
return numValue;
|
|
539
491
|
case types_1.PropertyType.Date:
|
|
540
492
|
return new Date(value.toString()).toISOString();
|
|
541
|
-
case types_1.PropertyType.MultiSelect:
|
|
542
|
-
if (Array.isArray(value)) {
|
|
543
|
-
return value;
|
|
544
|
-
}
|
|
545
|
-
// the old comma syntax parses as one literal value; warn instead of guessing at a split
|
|
546
|
-
if (typeof value === 'string' && value.includes(',')) {
|
|
547
|
-
console.error(`Value "${value}" for a multi-value property contains a comma and is treated as one literal value. To pass several values, repeat the key: slug=${value
|
|
548
|
-
.split(',')
|
|
549
|
-
.join(' slug=')}`);
|
|
550
|
-
}
|
|
551
|
-
return [value];
|
|
552
493
|
default:
|
|
553
494
|
return value; // For string and other types, return as is
|
|
554
495
|
}
|
|
555
496
|
}
|
|
556
|
-
|
|
557
|
-
async function createEntity(entityName, entity, routing = {}) {
|
|
497
|
+
async function createEntity(entityName, entity) {
|
|
558
498
|
const client = new sdk_1.Entities();
|
|
559
499
|
const skipHydrateRelationships = SHOULD_SKIP_HYDRATE_RELATIONSHIPS;
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
const response = await client.create({
|
|
564
|
-
entityName,
|
|
565
|
-
object: entity,
|
|
566
|
-
skipHydrateRelationships,
|
|
567
|
-
suffix,
|
|
568
|
-
apiVersion,
|
|
569
|
-
});
|
|
500
|
+
if (entityName === 'assortment-item')
|
|
501
|
+
return (0, entities_1.createAssortmentItem)(entity, { skipHydrateRelationships }); // special case
|
|
502
|
+
const response = await client.create({ entityName, object: entity, skipHydrateRelationships });
|
|
570
503
|
return response;
|
|
571
504
|
}
|
|
572
505
|
function getReferencedObjectPaths(type) {
|
|
@@ -270,8 +270,8 @@ describe('Entity Creation E2E Happy Path', () => {
|
|
|
270
270
|
expect(result.get('quantity')).toBe('10');
|
|
271
271
|
expect(result.get('isActive')).toBe('true');
|
|
272
272
|
});
|
|
273
|
-
it('should
|
|
274
|
-
const specs = ['roles=option
|
|
273
|
+
it('should handle roles as comma-separated array', () => {
|
|
274
|
+
const specs = ['roles=option,family', 'name=Test'];
|
|
275
275
|
const result = (0, propertySpecifications_1.parsePropertySpecificationsIntoMap)(specs);
|
|
276
276
|
expect(result.get('roles')).toEqual(['option', 'family']);
|
|
277
277
|
expect(result.get('name')).toBe('Test');
|
|
@@ -333,7 +333,7 @@ describe('Entity Creation E2E Happy Path', () => {
|
|
|
333
333
|
const propertySpecs = (0, propertySpecifications_1.parsePropertySpecificationsIntoMap)([
|
|
334
334
|
'roles=option',
|
|
335
335
|
]);
|
|
336
|
-
const roles =
|
|
336
|
+
const roles = propertySpecs.get('roles');
|
|
337
337
|
// Test that property level matching works with specified roles
|
|
338
338
|
expect((0, create_1.doesPropertyLevelMatchRole)(types_1.PropertyLevel.OPTION, roles[0])).toBe(true);
|
|
339
339
|
expect((0, create_1.doesPropertyLevelMatchRole)(types_1.PropertyLevel.FAMILY, roles[0])).toBe(false);
|
|
@@ -514,32 +514,3 @@ describe('Entity Creation E2E Happy Path', () => {
|
|
|
514
514
|
});
|
|
515
515
|
});
|
|
516
516
|
});
|
|
517
|
-
describe('castValueToPropertyType array coercion', () => {
|
|
518
|
-
it('should coerce a single value on a multi-value property to a one-element array', () => {
|
|
519
|
-
expect((0, create_1.castValueToPropertyType)('option', types_1.PropertyType.MultiSelect)).toEqual(['option']);
|
|
520
|
-
});
|
|
521
|
-
it('should pass arrays through on multi-value properties', () => {
|
|
522
|
-
expect((0, create_1.castValueToPropertyType)(['option', 'family'], types_1.PropertyType.MultiSelect)).toEqual([
|
|
523
|
-
'option',
|
|
524
|
-
'family',
|
|
525
|
-
]);
|
|
526
|
-
});
|
|
527
|
-
it('should keep a comma value literal on a multi-value property and warn with the repeated-key form', () => {
|
|
528
|
-
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
529
|
-
expect((0, create_1.castValueToPropertyType)('option,family', types_1.PropertyType.MultiSelect)).toEqual([
|
|
530
|
-
'option,family',
|
|
531
|
-
]);
|
|
532
|
-
expect(consoleError).toHaveBeenCalledWith(expect.stringContaining('slug=option slug=family'));
|
|
533
|
-
consoleError.mockRestore();
|
|
534
|
-
});
|
|
535
|
-
it('should cast each element of a repeated-key array on scalar property types', () => {
|
|
536
|
-
expect((0, create_1.castValueToPropertyType)(['1', '2'], types_1.PropertyType.Number)).toEqual([1, 2]);
|
|
537
|
-
expect((0, create_1.castValueToPropertyType)(['true', 'false'], types_1.PropertyType.Boolean)).toEqual([true, false]);
|
|
538
|
-
});
|
|
539
|
-
it('should keep commas literal on string properties without warning', () => {
|
|
540
|
-
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
541
|
-
expect((0, create_1.castValueToPropertyType)('my,name', types_1.PropertyType.String)).toBe('my,name');
|
|
542
|
-
expect(consoleError).not.toHaveBeenCalled();
|
|
543
|
-
consoleError.mockRestore();
|
|
544
|
-
});
|
|
545
|
-
});
|
|
@@ -4,17 +4,13 @@ export default class DeleteEntity extends BaseCommand {
|
|
|
4
4
|
static examples: string[];
|
|
5
5
|
static flags: {
|
|
6
6
|
dryRun: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
apiVersion: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
7
|
+
input: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
8
|
+
id: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
10
9
|
alwaysConfirmEnv: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
11
10
|
logLevel: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
12
11
|
};
|
|
13
12
|
static args: {
|
|
14
13
|
entityName: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
15
|
-
ids: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
16
14
|
};
|
|
17
|
-
static strict: boolean;
|
|
18
|
-
init(): Promise<void>;
|
|
19
15
|
run(): Promise<void>;
|
|
20
16
|
}
|
|
@@ -6,37 +6,27 @@ const p_limit_1 = tslib_1.__importDefault(require("p-limit"));
|
|
|
6
6
|
const basecommand_1 = require("../../basecommand");
|
|
7
7
|
const sdk_1 = require("@contrail/sdk");
|
|
8
8
|
const login_1 = require("../../auth/login");
|
|
9
|
-
const common_1 = require("../../common");
|
|
10
9
|
const file_helpers_1 = require("../../helpers/file-helpers");
|
|
11
10
|
let IS_DRY_RUN = false;
|
|
12
11
|
class DeleteEntity extends basecommand_1.BaseCommand {
|
|
13
|
-
static description = 'Delete
|
|
14
|
-
static examples = [
|
|
15
|
-
'contrail entities delete color 123',
|
|
16
|
-
'contrail entities delete color id1 id2 id3',
|
|
17
|
-
'contrail entities delete item -i ids.json',
|
|
18
|
-
];
|
|
12
|
+
static description = '(BETA) Delete an entity';
|
|
13
|
+
static examples = ['contrail entities delete color 123'];
|
|
19
14
|
static flags = {
|
|
20
15
|
...basecommand_1.BaseCommand.baseFlags,
|
|
21
16
|
dryRun: core_1.Flags.boolean({
|
|
22
17
|
char: 'd',
|
|
23
|
-
description: 'If true, does not delete the
|
|
18
|
+
description: 'If true, does not delete the entity but simulates the deletion',
|
|
24
19
|
required: false,
|
|
25
20
|
default: false,
|
|
26
21
|
}),
|
|
27
|
-
|
|
22
|
+
input: core_1.Flags.string({
|
|
28
23
|
char: 'i',
|
|
29
|
-
description:
|
|
24
|
+
description: 'A JSON array of objects where each object is one entity to create. Cannot be used with --count. Unless --precise is specified, any missing values will be populated with random values.',
|
|
30
25
|
required: false,
|
|
31
26
|
multiple: false,
|
|
32
27
|
}),
|
|
33
|
-
|
|
34
|
-
description: '
|
|
35
|
-
required: false,
|
|
36
|
-
}),
|
|
37
|
-
apiVersion: core_1.Flags.string({
|
|
38
|
-
description: 'Api-Version header to send; selects the V1 or V2 variant of the endpoint',
|
|
39
|
-
options: ['1', '2'],
|
|
28
|
+
id: core_1.Flags.string({
|
|
29
|
+
description: 'The unique identifier of the entity',
|
|
40
30
|
required: false,
|
|
41
31
|
}),
|
|
42
32
|
};
|
|
@@ -45,93 +35,84 @@ class DeleteEntity extends basecommand_1.BaseCommand {
|
|
|
45
35
|
description: 'The name of the entity type to delete',
|
|
46
36
|
required: true,
|
|
47
37
|
}),
|
|
48
|
-
ids: core_1.Args.string({
|
|
49
|
-
description: 'One or more entity ids to delete',
|
|
50
|
-
required: false,
|
|
51
|
-
}),
|
|
52
38
|
};
|
|
53
|
-
static strict = false;
|
|
54
|
-
async init() {
|
|
55
|
-
(0, common_1.routeConsoleLogToStderr)();
|
|
56
|
-
await super.init();
|
|
57
|
-
}
|
|
58
39
|
async run() {
|
|
40
|
+
this.log('🚨 This command is in BETA and may change or be removed at any time.');
|
|
59
41
|
await (0, login_1.authUser)();
|
|
60
|
-
const { args, flags
|
|
42
|
+
const { args, flags } = await this.parse(DeleteEntity);
|
|
61
43
|
const { entityName } = args;
|
|
62
|
-
const {
|
|
44
|
+
const { id, input: inputFilename } = flags;
|
|
63
45
|
if (flags.dryRun) {
|
|
64
46
|
IS_DRY_RUN = true;
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
const tokens = argv.slice(1);
|
|
68
|
-
const valueTokens = tokens.filter((token) => token.includes('='));
|
|
69
|
-
if (valueTokens.length > 0) {
|
|
70
|
-
this.error(`delete takes no -- values (got: ${valueTokens.join(' ')}); pass ids positionally or use an input file (-i).`);
|
|
71
|
-
}
|
|
72
|
-
const positionalIds = tokens;
|
|
73
|
-
if (inputFilename && positionalIds.length > 0) {
|
|
74
|
-
this.error('You cannot use --inputFile and positional ids together. Please specify one or the other.');
|
|
75
|
-
}
|
|
76
|
-
if (!inputFilename && positionalIds.length === 0) {
|
|
77
|
-
this.error('You must provide one or more ids or an input file (-i) of entities to delete.');
|
|
47
|
+
this.log('Running in dry run mode. No entities will be deleted.');
|
|
78
48
|
}
|
|
79
49
|
const jsonInput = inputFilename ? (0, file_helpers_1.parseFileAsJsonArray)(inputFilename) : undefined;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
console.error(`${jsonInput.length - rowsWithId.length} row(s) without an id were skipped.`);
|
|
50
|
+
if (!jsonInput && !id) {
|
|
51
|
+
this.error('You must provide either an ID or a JSON input file with entities to delete.');
|
|
83
52
|
}
|
|
84
|
-
const ids =
|
|
53
|
+
const ids = jsonInput ? jsonInput.map((item) => item.id) : [id];
|
|
85
54
|
try {
|
|
86
|
-
const response = await deleteEntityByIds(entityName, ids
|
|
87
|
-
suffix,
|
|
88
|
-
apiVersion: apiVersion,
|
|
89
|
-
});
|
|
55
|
+
const response = await deleteEntityByIds(entityName, ids);
|
|
90
56
|
if (IS_DRY_RUN) {
|
|
91
|
-
|
|
57
|
+
this.log('simulated deletion of entities:');
|
|
92
58
|
}
|
|
93
59
|
else {
|
|
94
|
-
|
|
60
|
+
this.log('deleted entities:');
|
|
95
61
|
}
|
|
96
62
|
this.log(JSON.stringify(response, null, 2));
|
|
97
|
-
if (response.failed.length > 0) {
|
|
98
|
-
console.error(`${response.failed.length} of ${ids.length} deletes failed.`);
|
|
99
|
-
process.exitCode = 1;
|
|
100
|
-
}
|
|
101
63
|
}
|
|
102
64
|
catch (e) {
|
|
103
|
-
this.error(`Error
|
|
65
|
+
this.error(`Error fetching entity: ${e.message}`);
|
|
104
66
|
}
|
|
105
67
|
}
|
|
106
68
|
}
|
|
107
69
|
exports.default = DeleteEntity;
|
|
108
|
-
async function deleteEntityByIds(entityName, ids
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
const
|
|
70
|
+
async function deleteEntityByIds(entityName, ids) {
|
|
71
|
+
const idsDeleted = new Set();
|
|
72
|
+
const idsNotFound = new Set();
|
|
73
|
+
const idsErrored = new Set();
|
|
112
74
|
const limit = (0, p_limit_1.default)(10);
|
|
113
|
-
const deletePromises = ids.map((id) => limit(() => deleteEntityById(entityName, id
|
|
75
|
+
const deletePromises = ids.map((id) => limit(() => deleteEntityById(entityName, id).then((entity) => {
|
|
114
76
|
if (!entity) {
|
|
115
|
-
|
|
116
|
-
return;
|
|
77
|
+
idsNotFound.add(id);
|
|
78
|
+
return null;
|
|
117
79
|
}
|
|
118
|
-
|
|
80
|
+
idsDeleted.add(id);
|
|
81
|
+
return entity;
|
|
119
82
|
}, (error) => {
|
|
120
|
-
if (error.message
|
|
121
|
-
|
|
83
|
+
if (error.message.includes('not found')) {
|
|
84
|
+
idsNotFound.add(id);
|
|
122
85
|
}
|
|
123
86
|
else {
|
|
124
|
-
|
|
87
|
+
idsErrored.add(id);
|
|
125
88
|
}
|
|
89
|
+
return null;
|
|
126
90
|
})));
|
|
127
91
|
await Promise.all(deletePromises);
|
|
128
|
-
return {
|
|
92
|
+
return {
|
|
93
|
+
idsDeleted: Array.from(idsDeleted),
|
|
94
|
+
idsNotFound: Array.from(idsNotFound),
|
|
95
|
+
idsErrored: Array.from(idsErrored),
|
|
96
|
+
};
|
|
129
97
|
}
|
|
130
|
-
async function deleteEntityById(entityName, id
|
|
98
|
+
async function deleteEntityById(entityName, id) {
|
|
131
99
|
const client = new sdk_1.Entities();
|
|
132
100
|
if (IS_DRY_RUN) {
|
|
133
|
-
return client.get({ entityName, id, ignore404: true });
|
|
101
|
+
return client.get({ entityName, id, ignore404: true }).catch(throwIfNot404);
|
|
134
102
|
}
|
|
135
|
-
|
|
136
|
-
|
|
103
|
+
return client.delete({ entityName, id, ignore404: true }).catch(throwIfNot404);
|
|
104
|
+
}
|
|
105
|
+
function throwIfNot404(e) {
|
|
106
|
+
const { statusCode } = parseSDKErrorMessage(e);
|
|
107
|
+
if (statusCode === 404) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
throw e;
|
|
111
|
+
}
|
|
112
|
+
function parseSDKErrorMessage(e) {
|
|
113
|
+
const parsed = JSON.parse(e.details);
|
|
114
|
+
return {
|
|
115
|
+
statusCode: parsed.statusCode,
|
|
116
|
+
message: parsed.message,
|
|
117
|
+
};
|
|
137
118
|
}
|
|
@@ -10,16 +10,12 @@ export default class GetEntity extends BaseCommand {
|
|
|
10
10
|
skipHydrateRelationships: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
11
11
|
take: import("@oclif/core/lib/interfaces").OptionFlag<number, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
12
12
|
relations: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
13
|
-
suffix: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
14
|
-
apiVersion: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
15
13
|
alwaysConfirmEnv: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
16
14
|
logLevel: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
17
15
|
};
|
|
18
16
|
static args: {
|
|
19
17
|
typePath: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
20
|
-
ids: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
21
18
|
};
|
|
22
19
|
static strict: boolean;
|
|
23
|
-
init(): Promise<void>;
|
|
24
20
|
run(): Promise<void>;
|
|
25
21
|
}
|