codify-plugin-lib 1.0.145 → 1.0.147

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.
@@ -64,6 +64,9 @@ export class Plugin {
64
64
  if (!resource) {
65
65
  throw new Error(`Resource of type ${resourceConfig.core.type} could not be found for match`);
66
66
  }
67
+ if (!resource.settings.allowMultiple) {
68
+ return { match: array.find((r) => r.core.type === resourceConfig.core.type) };
69
+ }
67
70
  const parameterMatcher = resource?.parsedSettings.matcher;
68
71
  const match = array.find((r) => {
69
72
  if (resourceConfig.core.type !== r.core.type) {
@@ -45,7 +45,6 @@ export class ParsedResourceSettings {
45
45
  ...v,
46
46
  controller: spController,
47
47
  nestedSettings: spController?.parsedSettings,
48
- definition: undefined,
49
48
  };
50
49
  return [k, parsed];
51
50
  }
@@ -114,10 +113,10 @@ export class ParsedResourceSettings {
114
113
  this.validateParameterEqualsFn(v, k);
115
114
  }
116
115
  }
117
- if (this.allowMultiple
118
- && Object.values(this.parameterSettings).some((v) => v.type === 'stateful')) {
119
- throw new Error(`Resource: ${this.id}. Stateful parameters are not allowed if multiples of a resource exist`);
120
- }
116
+ // if (this.allowMultiple
117
+ // && Object.values(this.parameterSettings).some((v) => v.type === 'stateful')) {
118
+ // throw new Error(`Resource: ${this.id}. Stateful parameters are not allowed if multiples of a resource exist`)
119
+ // }
121
120
  const schema = this.settings.schema;
122
121
  if (!this.settings.importAndDestroy && (schema?.oneOf
123
122
  && Array.isArray(schema.oneOf)
@@ -172,6 +172,10 @@ export class ResourceController {
172
172
  || currentParametersArray === undefined
173
173
  || this.settings.allowMultiple // Stateful parameters are not supported currently if allowMultiple is true
174
174
  || currentParametersArray.filter(Boolean).length === 0) {
175
+ for (const result of currentParametersArray ?? []) {
176
+ await this.applyTransformParameters(result, true);
177
+ this.removeDefaultValues(result, parameters);
178
+ }
175
179
  return currentParametersArray
176
180
  ?.map((r) => ({ core, parameters: r }))
177
181
  ?? null;
@@ -91,6 +91,9 @@ export function resolveMatcher(settings) {
91
91
  if (!desired || !current) {
92
92
  return false;
93
93
  }
94
+ if (!settings.allowMultiple) {
95
+ throw new Error(`Matching only works when allow multiple is enabled. Type: ${settings.id}`);
96
+ }
94
97
  const requiredParameters = typeof settings.allowMultiple === 'object'
95
98
  ? settings.allowMultiple?.identifyingParameters ?? settings.schema?.required ?? []
96
99
  : settings.schema?.required ?? [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.145",
3
+ "version": "1.0.147",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -5,7 +5,7 @@ import { Resource } from '../resource/resource.js';
5
5
  import { Plan } from '../plan/plan.js';
6
6
  import { spy } from 'sinon';
7
7
  import { ResourceSettings } from '../resource/resource-settings.js';
8
- import { TestConfig } from '../utils/test-utils.test.js';
8
+ import { TestConfig, TestStatefulParameter } from '../utils/test-utils.test.js';
9
9
  import { getPty } from '../pty/index.js';
10
10
 
11
11
  interface TestConfig extends StringIndexedObject {
@@ -389,5 +389,63 @@ describe('Plugin tests', () => {
389
389
  parameters: { path: '/my/path', propA: 'hig' },
390
390
  })
391
391
 
392
+ const match2 = await testPlugin.match({
393
+ resource: {
394
+ core: { type: 'testResource' },
395
+ parameters: { path: '/my/path', propA: 'abc' },
396
+ },
397
+ array: []
398
+ })
399
+
400
+ expect(match2).toMatchObject({
401
+ match: undefined,
402
+ })
403
+ })
404
+
405
+ it('Can match resources together 2', { timeout: 3000000 }, async () => {
406
+ const resource = spy(new class extends TestResource {
407
+ getSettings(): ResourceSettings<TestConfig> {
408
+ return {
409
+ id: 'ssh-config',
410
+ parameterSettings: {
411
+ hosts: { type: 'stateful', definition: new TestStatefulParameter() }
412
+ },
413
+ importAndDestroy: {
414
+ refreshKeys: ['hosts'],
415
+ defaultRefreshValues: { hosts: [] },
416
+ requiredParameters: []
417
+ },
418
+ dependencies: ['ssh-key'],
419
+ allowMultiple: {
420
+ matcher: (a, b) => a.hosts === b.hosts
421
+ }
422
+ }
423
+ }
424
+ })
425
+
426
+ const testPlugin = Plugin.create('testPlugin', [resource as any]);
427
+
428
+ const { match } = await testPlugin.match({
429
+ resource: {
430
+ core: { type: 'ssh-config' },
431
+ parameters: { hosts: 'a' },
432
+ },
433
+ array: [
434
+ {
435
+ core: { type: 'ssh-config' },
436
+ parameters: { hosts: 'b' },
437
+ },
438
+ {
439
+ core: { type: 'ssh-config' },
440
+ parameters: { hosts: 'a' },
441
+ },
442
+ {
443
+ core: { type: 'ssh-config' },
444
+ parameters: { hosts: 'c' },
445
+ },
446
+ ]
447
+ })
448
+
449
+ console.log(match)
392
450
  })
393
451
  });
@@ -101,6 +101,10 @@ export class Plugin {
101
101
  throw new Error(`Resource of type ${resourceConfig.core.type} could not be found for match`);
102
102
  }
103
103
 
104
+ if (!resource.settings.allowMultiple) {
105
+ return { match: array.find((r) => r.core.type === resourceConfig.core.type) }
106
+ }
107
+
104
108
  const parameterMatcher = resource?.parsedSettings.matcher;
105
109
  const match = array.find((r) => {
106
110
  if (resourceConfig.core.type !== r.core.type) {
@@ -92,7 +92,6 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Re
92
92
  ...v,
93
93
  controller: spController,
94
94
  nestedSettings: spController?.parsedSettings,
95
- definition: undefined,
96
95
  };
97
96
 
98
97
  return [k, parsed as ParsedStatefulParameterSetting];
@@ -189,10 +188,10 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Re
189
188
  }
190
189
  }
191
190
 
192
- if (this.allowMultiple
193
- && Object.values(this.parameterSettings).some((v) => v.type === 'stateful')) {
194
- throw new Error(`Resource: ${this.id}. Stateful parameters are not allowed if multiples of a resource exist`)
195
- }
191
+ // if (this.allowMultiple
192
+ // && Object.values(this.parameterSettings).some((v) => v.type === 'stateful')) {
193
+ // throw new Error(`Resource: ${this.id}. Stateful parameters are not allowed if multiples of a resource exist`)
194
+ // }
196
195
 
197
196
  const schema = this.settings.schema as JSONSchemaType<any>;
198
197
  if (!this.settings.importAndDestroy && (schema?.oneOf
@@ -252,6 +252,11 @@ export class ResourceController<T extends StringIndexedObject> {
252
252
  || this.settings.allowMultiple // Stateful parameters are not supported currently if allowMultiple is true
253
253
  || currentParametersArray.filter(Boolean).length === 0
254
254
  ) {
255
+ for (const result of currentParametersArray ?? []) {
256
+ await this.applyTransformParameters(result, true);
257
+ this.removeDefaultValues(result, parameters)
258
+ }
259
+
255
260
  return currentParametersArray
256
261
  ?.map((r) => ({ core, parameters: r }))
257
262
  ?? null;
@@ -412,13 +412,16 @@ export function resolveParameterTransformFn(
412
412
  export function resolveMatcher<T extends StringIndexedObject>(
413
413
  settings: ResourceSettings<T>
414
414
  ): (desired: Partial<T>, current: Partial<T>) => boolean {
415
-
416
415
  return typeof settings.allowMultiple === 'boolean' || !settings.allowMultiple?.matcher
417
416
  ? ((desired: Partial<T>, current: Partial<T>) => {
418
417
  if (!desired || !current) {
419
418
  return false;
420
419
  }
421
420
 
421
+ if (!settings.allowMultiple) {
422
+ throw new Error(`Matching only works when allow multiple is enabled. Type: ${settings.id}`)
423
+ }
424
+
422
425
  const requiredParameters = typeof settings.allowMultiple === 'object'
423
426
  ? settings.allowMultiple?.identifyingParameters ?? (settings.schema?.required as string[]) ?? []
424
427
  : (settings.schema?.required as string[]) ?? []