@spica/cli 0.9.29 → 0.10.2

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 (101) hide show
  1. package/bin/spica +1 -1
  2. package/index.js +3465 -27
  3. package/package.json +54 -35
  4. package/index.d.ts +0 -2
  5. package/index.js.map +0 -1
  6. package/src/commands/asset/apply.d.ts +0 -2
  7. package/src/commands/asset/apply.js +0 -50
  8. package/src/commands/asset/apply.js.map +0 -1
  9. package/src/commands/asset/delete.d.ts +0 -2
  10. package/src/commands/asset/delete.js +0 -34
  11. package/src/commands/asset/delete.js.map +0 -1
  12. package/src/commands/bucket/orm.d.ts +0 -45
  13. package/src/commands/bucket/orm.js +0 -481
  14. package/src/commands/bucket/orm.js.map +0 -1
  15. package/src/commands/context/ls.d.ts +0 -2
  16. package/src/commands/context/ls.js +0 -29
  17. package/src/commands/context/ls.js.map +0 -1
  18. package/src/commands/context/remove.d.ts +0 -2
  19. package/src/commands/context/remove.js +0 -18
  20. package/src/commands/context/remove.js.map +0 -1
  21. package/src/commands/context/set.d.ts +0 -2
  22. package/src/commands/context/set.js +0 -41
  23. package/src/commands/context/set.js.map +0 -1
  24. package/src/commands/context/switch.d.ts +0 -2
  25. package/src/commands/context/switch.js +0 -24
  26. package/src/commands/context/switch.js.map +0 -1
  27. package/src/commands/function/orm.d.ts +0 -2
  28. package/src/commands/function/orm.js +0 -65
  29. package/src/commands/function/orm.js.map +0 -1
  30. package/src/commands/project/ls.d.ts +0 -2
  31. package/src/commands/project/ls.js +0 -60
  32. package/src/commands/project/ls.js.map +0 -1
  33. package/src/commands/project/remove.d.ts +0 -2
  34. package/src/commands/project/remove.js +0 -84
  35. package/src/commands/project/remove.js.map +0 -1
  36. package/src/commands/project/start.d.ts +0 -2
  37. package/src/commands/project/start.js +0 -410
  38. package/src/commands/project/start.js.map +0 -1
  39. package/src/commands/project/sync.d.ts +0 -171
  40. package/src/commands/project/sync.js +0 -708
  41. package/src/commands/project/sync.js.map +0 -1
  42. package/src/commands/project/upgrade.d.ts +0 -2
  43. package/src/commands/project/upgrade.js +0 -175
  44. package/src/commands/project/upgrade.js.map +0 -1
  45. package/src/compile.d.ts +0 -22
  46. package/src/compile.js +0 -116
  47. package/src/compile.js.map +0 -1
  48. package/src/config.d.ts +0 -9
  49. package/src/config.js +0 -56
  50. package/src/config.js.map +0 -1
  51. package/src/console.d.ts +0 -14
  52. package/src/console.js +0 -52
  53. package/src/console.js.map +0 -1
  54. package/src/context.d.ts +0 -17
  55. package/src/context.js +0 -57
  56. package/src/context.js.map +0 -1
  57. package/src/function/helpers.d.ts +0 -2
  58. package/src/function/helpers.js +0 -16
  59. package/src/function/helpers.js.map +0 -1
  60. package/src/function/index.d.ts +0 -2
  61. package/src/function/index.js +0 -9
  62. package/src/function/index.js.map +0 -1
  63. package/src/function/interface.d.ts +0 -28
  64. package/src/function/interface.js +0 -7
  65. package/src/function/interface.js.map +0 -1
  66. package/src/function/modifier.d.ts +0 -41
  67. package/src/function/modifier.js +0 -69
  68. package/src/function/modifier.js.map +0 -1
  69. package/src/function/triggers/http/index.d.ts +0 -7
  70. package/src/function/triggers/http/index.js +0 -12
  71. package/src/function/triggers/http/index.js.map +0 -1
  72. package/src/function/triggers/http/services/axios.d.ts +0 -46
  73. package/src/function/triggers/http/services/axios.js +0 -169
  74. package/src/function/triggers/http/services/axios.js.map +0 -1
  75. package/src/function/triggers/http/services/index.d.ts +0 -3
  76. package/src/function/triggers/http/services/index.js +0 -10
  77. package/src/function/triggers/http/services/index.js.map +0 -1
  78. package/src/function/triggers/http/transformer.d.ts +0 -16
  79. package/src/function/triggers/http/transformer.js +0 -53
  80. package/src/function/triggers/http/transformer.js.map +0 -1
  81. package/src/function/triggers/index.d.ts +0 -4
  82. package/src/function/triggers/index.js +0 -10
  83. package/src/function/triggers/index.js.map +0 -1
  84. package/src/http.d.ts +0 -11
  85. package/src/http.js +0 -33
  86. package/src/http.js.map +0 -1
  87. package/src/project.d.ts +0 -11
  88. package/src/project.js +0 -70
  89. package/src/project.js.map +0 -1
  90. package/src/range.d.ts +0 -1
  91. package/src/range.js +0 -17
  92. package/src/range.js.map +0 -1
  93. package/src/representative.d.ts +0 -13
  94. package/src/representative.js +0 -124
  95. package/src/representative.js.map +0 -1
  96. package/src/status.d.ts +0 -19
  97. package/src/status.js +0 -47
  98. package/src/status.js.map +0 -1
  99. package/src/validator.d.ts +0 -5
  100. package/src/validator.js +0 -37
  101. package/src/validator.js.map +0 -1
@@ -1,708 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SendingPromiseStrategy = exports.PolicySynchronizer = exports.ApikeySynchronizer = exports.BucketSynchronizer = exports.BucketDataSynchronizer = exports.FunctionIndexSynchronizer = exports.FunctionDependencySynchronizer = exports.FunctionSynchronizer = exports.ResourceGroupComparisor = void 0;
4
- const core_1 = require("@caporal/core");
5
- const console_1 = require("../../console");
6
- const http_1 = require("../../http");
7
- const validator_1 = require("../../validator");
8
- const colorette_1 = require("colorette");
9
- const lodash_1 = require("lodash");
10
- let IGNORE_ERRORS;
11
- let CONCURRENCY_LIMIT;
12
- async function sync({ options: { sourceUrl, sourceApikey, targetUrl, targetApikey, modules, dryRun, syncFnEnv, ignoreErrors, concurrencyLimit } }) {
13
- IGNORE_ERRORS = ignoreErrors;
14
- CONCURRENCY_LIMIT = concurrencyLimit;
15
- const sourceService = http_1.httpService.create({
16
- baseUrl: sourceUrl.toString(),
17
- authorization: `APIKEY ${sourceApikey}`
18
- });
19
- const targetService = http_1.httpService.create({
20
- baseUrl: targetUrl.toString(),
21
- authorization: `APIKEY ${targetApikey}`
22
- });
23
- modules = modules
24
- .toString()
25
- .split(",")
26
- .map(m => m.trim());
27
- const coreSynchronizers = [
28
- FunctionSynchronizer,
29
- BucketSynchronizer,
30
- ApikeySynchronizer,
31
- PolicySynchronizer
32
- ];
33
- const synchronizers = [];
34
- for (const Ctor of coreSynchronizers) {
35
- const synchronizer = new Ctor(sourceService, targetService, { syncFnEnv });
36
- const subSynchronizers = await synchronizer.initialize().catch(e => {
37
- return Promise.reject(returnErrorMessage(e));
38
- });
39
- synchronizers.push(synchronizer);
40
- synchronizers.push(...subSynchronizers);
41
- }
42
- for (const name of modules) {
43
- const moduleSynchronizers = synchronizers.filter(s => s.moduleName == name);
44
- if (!moduleSynchronizers.length) {
45
- return Promise.reject(`Module ${name} does not exist.`);
46
- }
47
- for (const synchronizer of moduleSynchronizers) {
48
- const { insertions, updations, deletions } = await synchronizer.analyze().catch(e => {
49
- return Promise.reject(returnErrorMessage(e));
50
- });
51
- if (dryRun) {
52
- printActions({
53
- insertions,
54
- updations,
55
- deletions,
56
- field: synchronizer.primaryField,
57
- moduleName: synchronizer.getDisplayableModuleName()
58
- });
59
- }
60
- else {
61
- await synchronizer.synchronize().catch(e => Promise.reject(returnErrorMessage(e)));
62
- console.log(`\n${synchronizer.getDisplayableModuleName()} synchronization has been completed!`.toUpperCase());
63
- }
64
- }
65
- }
66
- }
67
- function default_1({ createCommand }) {
68
- return createCommand(`Synchronize selected module objects between two spica instances(local or remote).
69
- ${(0, colorette_1.red)("ATTENTION")}: Source and target instance versions must be minimum v0.9.19 and for the best results both instance versions should be the same.
70
- Also this command will perform adding, overwriting and removing actions of the target instance and it's irreversible.
71
- We highly recommend you to use --dry-run=true and check the changes that will be applied before start.`)
72
- .option("--source-url", "API address of the instance where objects will be synchronized from", {
73
- required: true,
74
- validator: core_1.CaporalValidator.STRING
75
- })
76
- .option("--source-apikey", "Apikey of the instance where objects will be synchronized from", {
77
- required: true,
78
- validator: core_1.CaporalValidator.STRING
79
- })
80
- .option("--target-url", "API address of the instance where objects will be synchronized to", {
81
- required: true,
82
- validator: core_1.CaporalValidator.STRING
83
- })
84
- .option("--target-apikey", "API address of the instance where objects will be synchronized to", {
85
- required: true,
86
- validator: core_1.CaporalValidator.STRING
87
- })
88
- .option("--modules", `Module names of objects that will be synchronized. Available modules: ${(0, colorette_1.green)(validator_1.availableSyncModules.join(","))}`, {
89
- required: true,
90
- validator: validator_1.validateSyncModules
91
- })
92
- .option("--dry-run", "Shows the changes that will be applied to the target instance.", {
93
- default: false
94
- })
95
- .option("--sync-fn-env", "Set true if you need to sync function environment variables as well.", {
96
- default: false
97
- })
98
- .option("--ignore-errors", "Set true if you don't want to interrupt sync process because of failed requests.", {
99
- default: false
100
- })
101
- .option("--concurrency-limit", "Increase(if you want to speed up), decrease(if you get some error like ECONNRESET) this value to adjust how many parallel requests that will be sent to the target instance", {
102
- default: 100,
103
- validator: core_1.CaporalValidator.NUMBER
104
- })
105
- .action(sync);
106
- }
107
- exports.default = default_1;
108
- class ResourceGroupComparisor {
109
- constructor(sources, targets, uniqueField = "_id", ignoredFields = []) {
110
- this.sources = sources;
111
- this.targets = targets;
112
- this.uniqueField = uniqueField;
113
- this.ignoredFields = ignoredFields;
114
- this.existings = [];
115
- this.existingIds = [];
116
- this.existings = targets.filter(target => sources.some(source => source[this.uniqueField] == target[this.uniqueField]));
117
- this.existingIds = this.existings.map(existing => existing[this.uniqueField]);
118
- }
119
- updations() {
120
- const updations = [];
121
- for (const existing of this.existings) {
122
- const source = this.sources.find(source => source[this.uniqueField] == existing[this.uniqueField]);
123
- if (this.ignoredFields.length) {
124
- this.ignoredFields.forEach(field => {
125
- delete source[field];
126
- delete existing[field];
127
- });
128
- }
129
- if (!(0, lodash_1.isEqual)(source, existing)) {
130
- updations.push(source);
131
- }
132
- }
133
- return updations;
134
- }
135
- insertions() {
136
- return this.sources.filter(source => this.existingIds.indexOf(source[this.uniqueField]) == -1);
137
- }
138
- deletions() {
139
- return this.targets.filter(target => this.existingIds.indexOf(target[this.uniqueField]) == -1);
140
- }
141
- }
142
- exports.ResourceGroupComparisor = ResourceGroupComparisor;
143
- class FunctionSynchronizer {
144
- constructor(sourceService, targetService, options) {
145
- this.sourceService = sourceService;
146
- this.targetService = targetService;
147
- this.options = options;
148
- this.moduleName = "function";
149
- this.primaryField = "name";
150
- this.insertions = [];
151
- this.updations = [];
152
- this.deletions = [];
153
- }
154
- async initialize() {
155
- const synchronizers = [];
156
- const sourceFns = await this.sourceService.get("function");
157
- for (const fn of sourceFns) {
158
- synchronizers.push(new FunctionDependencySynchronizer(this.sourceService, this.targetService, fn));
159
- }
160
- synchronizers.push(new FunctionIndexSynchronizer(this.sourceService, this.targetService));
161
- return synchronizers;
162
- }
163
- async analyze() {
164
- console.log();
165
- let sourceFns = await (0, console_1.spin)({
166
- text: "Fetching functions from source instance",
167
- op: () => this.sourceService.get("function")
168
- });
169
- const targetFns = await (0, console_1.spin)({
170
- text: "Fetching functions from target instance",
171
- op: () => this.targetService.get("function")
172
- });
173
- if (!this.options.syncFnEnv) {
174
- sourceFns = sourceFns.map(fn => {
175
- fn.env = {};
176
- return fn;
177
- });
178
- for (const target of targetFns) {
179
- const index = sourceFns.findIndex(srcFn => srcFn._id == target._id);
180
- if (index != -1) {
181
- sourceFns[index].env = target.env;
182
- }
183
- }
184
- }
185
- const decider = new ResourceGroupComparisor(sourceFns, targetFns);
186
- this.insertions = decider.insertions();
187
- this.updations = decider.updations();
188
- this.deletions = decider.deletions();
189
- return {
190
- insertions: this.insertions,
191
- updations: this.updations,
192
- deletions: this.deletions
193
- };
194
- }
195
- async synchronize() {
196
- console.log();
197
- const insertPromiseFactories = this.insertions.map(fn => () => this.targetService.post("function", fn).catch(e => handleRejection({
198
- action: "insert",
199
- e,
200
- objectName: this.moduleName + " " + fn.name
201
- })));
202
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting functions to the target instance");
203
- const updatePromiseFactories = this.updations.map(fn => () => this.targetService.put(`function/${fn._id}`, fn).catch(e => handleRejection({
204
- action: "update",
205
- e,
206
- objectName: this.moduleName + " " + fn.name
207
- })));
208
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating target instance functions");
209
- const deletePromiseFactories = this.deletions.map(fn => () => this.targetService.delete(`function/${fn._id}`).catch(e => handleRejection({
210
- action: "delete",
211
- objectName: this.moduleName + " " + fn.name,
212
- e
213
- })));
214
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting target instance functions");
215
- }
216
- getDisplayableModuleName() {
217
- return this.moduleName;
218
- }
219
- }
220
- exports.FunctionSynchronizer = FunctionSynchronizer;
221
- class FunctionDependencySynchronizer {
222
- constructor(sourceService, targetService, fn) {
223
- this.sourceService = sourceService;
224
- this.targetService = targetService;
225
- this.fn = fn;
226
- this.moduleName = "function";
227
- this.subModuleName = "dependency";
228
- this.primaryField = "name";
229
- this.insertions = [];
230
- this.updations = [];
231
- this.deletions = [];
232
- }
233
- initialize() {
234
- return Promise.resolve([]);
235
- }
236
- async analyze() {
237
- const deleteTypes = deps => deps.map(d => {
238
- delete d.types;
239
- return d;
240
- });
241
- const sourceDeps = await this.sourceService
242
- .get(`function/${this.fn._id}/dependencies`)
243
- .then(deleteTypes);
244
- const targetDeps = await this.targetService
245
- .get(`function/${this.fn._id}/dependencies`)
246
- .then(deleteTypes)
247
- .catch(e => {
248
- if (isNotFoundException(e)) {
249
- return [];
250
- }
251
- return Promise.reject(e.data);
252
- });
253
- const decider = new ResourceGroupComparisor(sourceDeps, targetDeps, "name");
254
- this.insertions = decider.insertions();
255
- this.updations = decider.updations();
256
- this.deletions = decider.deletions();
257
- return {
258
- insertions: this.insertions,
259
- updations: this.updations,
260
- deletions: this.deletions
261
- };
262
- }
263
- async synchronize() {
264
- console.log();
265
- const promiseFactories = [];
266
- const insertBody = [...this.insertions, ...this.updations].reduce((acc, dep) => {
267
- const depName = `${dep.name}@${dep.version.slice(1)}`;
268
- acc.name.push(depName);
269
- return acc;
270
- }, { name: [] });
271
- if (insertBody.name.length) {
272
- promiseFactories.push(() => this.targetService.post(`function/${this.fn._id}/dependencies`, insertBody).catch(e => {
273
- handleRejection({
274
- action: "insert",
275
- e,
276
- objectName: this.getDisplayableModuleName()
277
- });
278
- }));
279
- }
280
- const deletePromiseFactories = this.deletions.map(dep => () => this.targetService.delete(`function/${this.fn._id}/dependencies/${dep.name}`).catch(e => handleRejection({
281
- action: "update",
282
- e,
283
- objectName: this.getDisplayableModuleName()
284
- })));
285
- if (deletePromiseFactories.length) {
286
- promiseFactories.push(...deletePromiseFactories);
287
- }
288
- return spinUntilPromiseEnd(promiseFactories, `Updating function '${this.fn.name}' dependencies`);
289
- }
290
- getDisplayableModuleName() {
291
- return `${this.moduleName} '${this.fn.name}' ${this.subModuleName}`;
292
- }
293
- }
294
- exports.FunctionDependencySynchronizer = FunctionDependencySynchronizer;
295
- class FunctionIndexSynchronizer {
296
- constructor(sourceService, targetService) {
297
- this.sourceService = sourceService;
298
- this.targetService = targetService;
299
- this.moduleName = "function";
300
- this.subModuleName = "index";
301
- this.primaryField = "name";
302
- this.insertions = [];
303
- this.updations = [];
304
- this.deletions = [];
305
- }
306
- initialize() {
307
- return Promise.resolve([]);
308
- }
309
- async analyze() {
310
- const sourceFns = await this.sourceService.get("function");
311
- const sourceFnIndexes = await Promise.all(sourceFns.map(fn => this.sourceService.get(`function/${fn._id}/index`).then(res => {
312
- return {
313
- _id: fn._id,
314
- name: fn.name,
315
- index: res.index
316
- };
317
- })));
318
- const targetFnIndexes = await Promise.all(sourceFns.map(fn => this.targetService
319
- .get(`function/${fn._id}/index`)
320
- .then(res => {
321
- return {
322
- _id: fn._id,
323
- name: fn.name,
324
- index: res.index
325
- };
326
- })
327
- .catch(e => {
328
- if (isNotFoundException(e)) {
329
- return false;
330
- }
331
- return Promise.reject(e.data);
332
- }))).then(indexes => indexes.filter(Boolean));
333
- const decider = new ResourceGroupComparisor(sourceFnIndexes, targetFnIndexes);
334
- this.insertions = decider.insertions();
335
- this.updations = decider.updations();
336
- this.deletions = decider.deletions();
337
- return {
338
- insertions: this.insertions,
339
- updations: this.updations,
340
- deletions: this.deletions
341
- };
342
- }
343
- synchronize() {
344
- const promiseFactories = [...this.insertions, ...this.updations].map(fn => () => this.targetService.post(`function/${fn._id}/index`, { index: fn.index }).catch(e => handleRejection({
345
- action: "insert",
346
- objectName: this.getDisplayableModuleName() + " " + fn.name,
347
- e
348
- })));
349
- return spinUntilPromiseEnd(promiseFactories, "Writing indexes to the target instance functions");
350
- }
351
- getDisplayableModuleName() {
352
- return this.moduleName + " " + this.subModuleName;
353
- }
354
- }
355
- exports.FunctionIndexSynchronizer = FunctionIndexSynchronizer;
356
- class BucketDataSynchronizer {
357
- constructor(sourceService, targetService, bucket) {
358
- this.sourceService = sourceService;
359
- this.targetService = targetService;
360
- this.bucket = bucket;
361
- this.moduleName = "bucket-data";
362
- this.insertions = [];
363
- this.updations = [];
364
- this.deletions = [];
365
- this.primaryField = this.bucket.primary;
366
- }
367
- initialize() {
368
- return Promise.resolve([]);
369
- }
370
- async analyze() {
371
- const params = {
372
- localize: false
373
- };
374
- const sourceData = await this.sourceService.get(`bucket/${this.bucket._id}/data`, {
375
- params
376
- });
377
- const targetData = await this.targetService
378
- .get(`bucket/${this.bucket._id}/data`, {
379
- params
380
- })
381
- .catch(e => {
382
- if (isNotFoundException(e)) {
383
- return [];
384
- }
385
- return Promise.reject(e.data);
386
- });
387
- const decider = new ResourceGroupComparisor(sourceData, targetData);
388
- this.insertions = decider.insertions();
389
- this.updations = decider.updations();
390
- this.deletions = decider.deletions();
391
- return {
392
- insertions: this.insertions,
393
- updations: this.updations,
394
- deletions: this.deletions
395
- };
396
- }
397
- async synchronize() {
398
- console.log();
399
- const insertPromiseFactories = this.insertions.map(data => () => this.targetService.post(`bucket/${this.bucket._id}/data`, data).catch(e => handleRejection({
400
- action: "insert",
401
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField],
402
- e
403
- })));
404
- await spinUntilPromiseEnd(insertPromiseFactories, `Inserting bucket ${this.bucket.title} data to the target instance`);
405
- const updatePromiseFactories = this.updations.map(data => () => this.targetService.put(`bucket/${this.bucket._id}/data/${data._id}`, data).catch(e => handleRejection({
406
- action: "update",
407
- e,
408
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField]
409
- })));
410
- await spinUntilPromiseEnd(updatePromiseFactories, `Updating bucket ${this.bucket.title} data on the target instance`);
411
- const deletePromiseFactories = this.deletions.map(data => () => this.targetService.delete(`bucket/${this.bucket._id}/data/${data._id}`).catch(e => handleRejection({
412
- action: "delete",
413
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField],
414
- e
415
- })));
416
- await spinUntilPromiseEnd(deletePromiseFactories, `Deleting bucket ${this.bucket.title} data from the target instance`);
417
- }
418
- getDisplayableModuleName() {
419
- return `${this.moduleName} '${this.bucket.title}'`;
420
- }
421
- }
422
- exports.BucketDataSynchronizer = BucketDataSynchronizer;
423
- class BucketSynchronizer {
424
- constructor(sourceService, targetService) {
425
- this.sourceService = sourceService;
426
- this.targetService = targetService;
427
- this.moduleName = "bucket";
428
- this.primaryField = "title";
429
- this.insertions = [];
430
- this.updations = [];
431
- this.deletions = [];
432
- }
433
- async initialize() {
434
- const synchronizers = [];
435
- const sourceBuckets = await this.sourceService.get("bucket");
436
- for (const bucket of sourceBuckets) {
437
- synchronizers.push(new BucketDataSynchronizer(this.sourceService, this.targetService, bucket));
438
- }
439
- return synchronizers;
440
- }
441
- async analyze() {
442
- console.log();
443
- const sourceBuckets = await (0, console_1.spin)({
444
- text: "Fetching buckets from source instance",
445
- op: () => this.sourceService.get("bucket")
446
- });
447
- const targetBuckets = await (0, console_1.spin)({
448
- text: "Fetching buckets from target instance",
449
- op: () => this.targetService.get("bucket")
450
- });
451
- const decider = new ResourceGroupComparisor(sourceBuckets, targetBuckets);
452
- this.insertions = decider.insertions();
453
- this.updations = decider.updations();
454
- this.deletions = decider.deletions();
455
- return {
456
- insertions: this.insertions,
457
- updations: this.updations,
458
- deletions: this.deletions
459
- };
460
- }
461
- async synchronize() {
462
- console.log();
463
- const insertPromiseFactories = this.insertions.map(bucket => () => this.targetService.post("bucket", bucket).catch(e => handleRejection({
464
- action: "insert",
465
- objectName: this.getDisplayableModuleName() + " " + bucket.title,
466
- e
467
- })));
468
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting buckets to the target instance");
469
- const updatePromiseFactories = this.updations.map(bucket => () => this.targetService.put(`bucket/${bucket._id}`, bucket).catch(e => handleRejection({
470
- action: "update",
471
- objectName: this.getDisplayableModuleName() + " " + bucket.title,
472
- e
473
- })));
474
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating buckets on the target instance");
475
- const deletePromiseFactories = this.deletions.map(bucket => () => this.targetService.delete(`bucket/${bucket._id}`).catch(e => handleRejection({
476
- action: "delete",
477
- objectName: bucket.title,
478
- e
479
- })));
480
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting bucket from the target instance");
481
- }
482
- getDisplayableModuleName() {
483
- return this.moduleName;
484
- }
485
- }
486
- exports.BucketSynchronizer = BucketSynchronizer;
487
- class ApikeySynchronizer {
488
- constructor(sourceService, targetService) {
489
- this.sourceService = sourceService;
490
- this.targetService = targetService;
491
- this.moduleName = "apikey";
492
- this.primaryField = "name";
493
- this.insertions = [];
494
- this.updations = [];
495
- this.deletions = [];
496
- }
497
- initialize() {
498
- return Promise.resolve([]);
499
- }
500
- async analyze() {
501
- console.log();
502
- const sourceApikeys = await (0, console_1.spin)({
503
- text: "Fetching apikeys from source instance",
504
- op: () => this.sourceService
505
- .get("passport/apikey")
506
- .then(r => r.data)
507
- });
508
- const targetApikeys = await (0, console_1.spin)({
509
- text: "Fetching apikeys from target instance",
510
- op: () => this.targetService
511
- .get("passport/apikey")
512
- .then(r => r.data)
513
- });
514
- const decider = new ResourceGroupComparisor(sourceApikeys, targetApikeys);
515
- this.insertions = decider.insertions();
516
- this.updations = decider.updations();
517
- this.deletions = decider.deletions();
518
- return {
519
- insertions: this.insertions,
520
- updations: this.updations,
521
- deletions: this.deletions
522
- };
523
- }
524
- async synchronize() {
525
- console.log();
526
- const insertPromiseFactories = this.insertions.map(apikey => () => {
527
- const insertRejectionHandler = e => handleRejection({
528
- action: "insert",
529
- objectName: this.getDisplayableModuleName() + " " + apikey.name,
530
- e
531
- });
532
- const apikeyInsertPromise = this.targetService
533
- .post("passport/apikey", apikey)
534
- .catch(e => insertRejectionHandler(e));
535
- return apikeyInsertPromise.then(() => {
536
- const policyAttachPromises = apikey.policies.map(policy => this.targetService
537
- .put(`passport/apikey/${apikey._id}/policy/${policy}`)
538
- .catch(e => insertRejectionHandler(e)));
539
- return Promise.all(policyAttachPromises);
540
- });
541
- });
542
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting apikeys to the target instance");
543
- const updatePromiseFactories = this.updations.map(apikey => () => {
544
- const rejectionHandler = e => {
545
- handleRejection({
546
- action: "update",
547
- objectName: this.getDisplayableModuleName() + " " + apikey.name,
548
- e
549
- });
550
- };
551
- const apikeyDeletePromise = this.targetService
552
- .delete(`passport/apikey/${apikey._id}`)
553
- .catch(e => rejectionHandler(e));
554
- const apikeyInsertPromise = apikeyDeletePromise.then(() => this.targetService.post(`passport/apikey`, apikey).catch(e => rejectionHandler(e)));
555
- return apikeyInsertPromise.then(() => {
556
- const policyAttachPromises = apikey.policies.map(policy => this.targetService
557
- .put(`passport/apikey/${apikey._id}/policy/${policy}`)
558
- .catch(e => rejectionHandler(e)));
559
- return Promise.all(policyAttachPromises);
560
- });
561
- });
562
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating apikeys on the target instance");
563
- const deletePromiseFactories = this.deletions.map(apikey => () => this.targetService.delete(`passport/apikey/${apikey._id}`).catch(e => handleRejection({
564
- action: "delete",
565
- objectName: apikey.name,
566
- e
567
- })));
568
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting apikeys from the target instance");
569
- }
570
- getDisplayableModuleName() {
571
- return this.moduleName;
572
- }
573
- }
574
- exports.ApikeySynchronizer = ApikeySynchronizer;
575
- class PolicySynchronizer {
576
- constructor(sourceService, targetService) {
577
- this.sourceService = sourceService;
578
- this.targetService = targetService;
579
- this.moduleName = "policy";
580
- this.primaryField = "name";
581
- this.insertions = [];
582
- this.updations = [];
583
- this.deletions = [];
584
- }
585
- initialize() {
586
- return Promise.resolve([]);
587
- }
588
- async analyze() {
589
- console.log();
590
- const sourcePolicies = await (0, console_1.spin)({
591
- text: "Fetching policies from source instance",
592
- op: () => this.sourceService
593
- .get("passport/policy")
594
- .then(r => r.data)
595
- });
596
- const targetPolicies = await (0, console_1.spin)({
597
- text: "Fetching policies from target instance",
598
- op: () => this.targetService
599
- .get("passport/policy")
600
- .then(r => r.data)
601
- });
602
- const decider = new ResourceGroupComparisor(sourcePolicies, targetPolicies);
603
- this.insertions = decider.insertions();
604
- this.updations = decider.updations();
605
- this.deletions = decider.deletions();
606
- return {
607
- insertions: this.insertions,
608
- updations: this.updations,
609
- deletions: this.deletions
610
- };
611
- }
612
- async synchronize() {
613
- console.log();
614
- const insertPromiseFactories = this.insertions.map(policy => () => this.targetService.post("passport/policy", policy).catch(e => handleRejection({
615
- action: "insert",
616
- objectName: this.getDisplayableModuleName() + " " + policy.name,
617
- e
618
- })));
619
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting policies to the target instance");
620
- const updatePromiseFactories = this.updations.map(policy => () => this.targetService.put(`passport/policy/${policy._id}`, policy).catch(e => handleRejection({
621
- action: "update",
622
- objectName: this.getDisplayableModuleName() + " " + policy.name,
623
- e
624
- })));
625
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating policies on the target instance");
626
- const deletePromiseFactories = this.deletions.map(policy => () => this.targetService.delete(`passport/policy/${policy._id}`).catch(e => handleRejection({
627
- action: "delete",
628
- objectName: policy.name,
629
- e
630
- })));
631
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting policies from the target instance");
632
- }
633
- getDisplayableModuleName() {
634
- return this.moduleName;
635
- }
636
- }
637
- exports.PolicySynchronizer = PolicySynchronizer;
638
- function printActions({ insertions, updations, deletions, field, moduleName }) {
639
- const joinActions = (actions) => {
640
- const MAX_LINES = 20;
641
- actions = actions.map(a => `- ${a[field]}`);
642
- if (actions.length > MAX_LINES) {
643
- actions = actions.slice(0, MAX_LINES);
644
- actions.push("...more");
645
- }
646
- return actions.join("\n");
647
- };
648
- console.log();
649
- console.log(`----- ${moduleName.toUpperCase()} -----`);
650
- console.log(`\n* Found ${(0, colorette_1.bold)(insertions.length)} objects to ${(0, colorette_1.bold)("insert")}:
651
- ${joinActions(insertions)}`);
652
- console.log(`\n* Found ${(0, colorette_1.bold)(updations.length)} objects to ${(0, colorette_1.bold)("update")}:
653
- ${joinActions(updations)}`);
654
- console.log(`\n* Found ${(0, colorette_1.bold)(deletions.length)} objects to ${(0, colorette_1.bold)("delete")}:
655
- ${joinActions(deletions)}`);
656
- }
657
- var SendingPromiseStrategy;
658
- (function (SendingPromiseStrategy) {
659
- SendingPromiseStrategy[SendingPromiseStrategy["All"] = 0] = "All";
660
- SendingPromiseStrategy[SendingPromiseStrategy["OneByOne"] = 1] = "OneByOne";
661
- SendingPromiseStrategy[SendingPromiseStrategy["Batch"] = 2] = "Batch";
662
- SendingPromiseStrategy[SendingPromiseStrategy["Retry"] = 3] = "Retry";
663
- })(SendingPromiseStrategy = exports.SendingPromiseStrategy || (exports.SendingPromiseStrategy = {}));
664
- function spinUntilPromiseEnd(promiseFactories, label) {
665
- if (!promiseFactories.length) {
666
- return;
667
- }
668
- return (0, console_1.spin)({
669
- text: label,
670
- op: async (spinner) => {
671
- let progress = 0;
672
- let count = 0;
673
- const concurrency = Math.min(promiseFactories.length, CONCURRENCY_LIMIT);
674
- let batchPromises = [];
675
- for (let i = 0; i < promiseFactories.length; i++) {
676
- batchPromises.push(promiseFactories[i]);
677
- if (batchPromises.length == concurrency || i == promiseFactories.length - 1) {
678
- await Promise.all(batchPromises.map(b => b()));
679
- batchPromises = [];
680
- }
681
- count++;
682
- progress = (100 / promiseFactories.length) * count;
683
- spinner.text = `${label} (%${Math.round(progress)})`;
684
- }
685
- return;
686
- }
687
- });
688
- }
689
- function handleRejection({ action, objectName, e }) {
690
- const errorMessage = returnErrorMessage(e);
691
- const msg = `
692
- Failed to ${action} ${(0, colorette_1.bold)(objectName)}.
693
- ${errorMessage}`;
694
- if (IGNORE_ERRORS) {
695
- return console.warn(msg);
696
- }
697
- else {
698
- return Promise.reject(msg);
699
- }
700
- }
701
- function returnErrorMessage(e) {
702
- return e.data ? e.data.message || e.data : e;
703
- }
704
- function isNotFoundException(e) {
705
- const code = 404;
706
- return e.status == code || e.statusCode == code || (e.data && e.data.statusCode == code);
707
- }
708
- //# sourceMappingURL=sync.js.map