codify-plugin-lib 1.0.86 → 1.0.88

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,5 @@
1
1
  import { Plan } from '../plan/plan.js';
2
2
  import { ResourceController } from '../resource/resource-controller.js';
3
- import { splitUserConfig } from '../utils/utils.js';
4
3
  export class Plugin {
5
4
  name;
6
5
  resourceControllers;
@@ -34,10 +33,9 @@ export class Plugin {
34
33
  if (!this.resourceControllers.has(config.type)) {
35
34
  throw new Error(`Resource type not found: ${config.type}`);
36
35
  }
37
- const { parameters, coreParameters } = splitUserConfig(config);
38
36
  const validation = await this.resourceControllers
39
37
  .get(config.type)
40
- .validate(parameters, coreParameters);
38
+ .validate(config);
41
39
  validationResults.push(validation);
42
40
  }
43
41
  await this.crossValidateResources(data.configs);
@@ -14,7 +14,7 @@ export declare class ResourceController<T extends StringIndexedObject> {
14
14
  protected schemaValidator?: ValidateFunction;
15
15
  constructor(resource: Resource<T>);
16
16
  initialize(): Promise<void>;
17
- validate(parameters: Partial<T>, resourceMetaData: ResourceConfig): Promise<ValidateResponseData['resourceValidations'][0]>;
17
+ validate(desiredConfig: Partial<T> & ResourceConfig): Promise<ValidateResponseData['resourceValidations'][0]>;
18
18
  plan(desiredConfig: Partial<T> & ResourceConfig | null, stateConfig?: Partial<T> & ResourceConfig | null, statefulMode?: boolean): Promise<Plan<T>>;
19
19
  apply(plan: Plan<T>): Promise<void>;
20
20
  private applyCreate;
@@ -31,14 +31,18 @@ export class ResourceController {
31
31
  async initialize() {
32
32
  return this.resource.initialize();
33
33
  }
34
- async validate(parameters, resourceMetaData) {
34
+ async validate(desiredConfig) {
35
+ const configToValidate = { ...desiredConfig };
36
+ await this.applyTransformParameters(configToValidate);
37
+ const { parameters, coreParameters } = splitUserConfig(configToValidate);
38
+ this.addDefaultValues(parameters);
35
39
  if (this.schemaValidator) {
36
40
  const isValid = this.schemaValidator(parameters);
37
41
  if (!isValid) {
38
42
  return {
39
43
  isValid: false,
40
- resourceName: resourceMetaData.name,
41
- resourceType: resourceMetaData.type,
44
+ resourceName: coreParameters.name,
45
+ resourceType: coreParameters.type,
42
46
  schemaValidationErrors: this.schemaValidator?.errors ?? [],
43
47
  };
44
48
  }
@@ -56,15 +60,15 @@ export class ResourceController {
56
60
  return {
57
61
  customValidationErrorMessage,
58
62
  isValid: false,
59
- resourceName: resourceMetaData.name,
60
- resourceType: resourceMetaData.type,
63
+ resourceName: coreParameters.name,
64
+ resourceType: coreParameters.type,
61
65
  schemaValidationErrors: this.schemaValidator?.errors ?? [],
62
66
  };
63
67
  }
64
68
  return {
65
69
  isValid: true,
66
- resourceName: resourceMetaData.name,
67
- resourceType: resourceMetaData.type,
70
+ resourceName: coreParameters.name,
71
+ resourceType: coreParameters.type,
68
72
  schemaValidationErrors: [],
69
73
  };
70
74
  }
@@ -72,6 +76,8 @@ export class ResourceController {
72
76
  this.validatePlanInputs(desiredConfig, stateConfig, statefulMode);
73
77
  this.addDefaultValues(desiredConfig);
74
78
  await this.applyTransformParameters(desiredConfig);
79
+ this.addDefaultValues(stateConfig);
80
+ await this.applyTransformParameters(stateConfig);
75
81
  // Parse data from the user supplied config
76
82
  const parsedConfig = new ConfigParser(desiredConfig, stateConfig, this.parsedSettings.statefulParameters);
77
83
  const { coreParameters, desiredParameters, stateParameters, allParameters, allNonStatefulParameters, allStatefulParameters, } = parsedConfig;
@@ -184,30 +190,30 @@ ${JSON.stringify(refresh, null, 2)}
184
190
  `);
185
191
  }
186
192
  }
187
- async applyTransformParameters(desired) {
188
- if (!desired) {
193
+ async applyTransformParameters(config) {
194
+ if (!config) {
189
195
  return;
190
196
  }
191
197
  for (const [key, inputTransformation] of Object.entries(this.parsedSettings.inputTransformations)) {
192
- if (desired[key] === undefined || !inputTransformation) {
198
+ if (config[key] === undefined || !inputTransformation) {
193
199
  continue;
194
200
  }
195
- desired[key] = await inputTransformation(desired[key]);
201
+ config[key] = await inputTransformation(config[key]);
196
202
  }
197
203
  if (this.settings.inputTransformation) {
198
- const { parameters, coreParameters } = splitUserConfig(desired);
204
+ const { parameters, coreParameters } = splitUserConfig(config);
199
205
  const transformed = await this.settings.inputTransformation(parameters);
200
- Object.keys(desired).forEach((k) => delete desired[k]);
201
- Object.assign(desired, transformed, coreParameters);
206
+ Object.keys(config).forEach((k) => delete config[k]);
207
+ Object.assign(config, transformed, coreParameters);
202
208
  }
203
209
  }
204
- addDefaultValues(desired) {
205
- if (!desired) {
210
+ addDefaultValues(config) {
211
+ if (!config) {
206
212
  return;
207
213
  }
208
214
  for (const [key, defaultValue] of Object.entries(this.parsedSettings.defaultValues)) {
209
- if (defaultValue !== undefined && (desired[key] === undefined || desired[key] === null)) {
210
- desired[key] = defaultValue;
215
+ if (defaultValue !== undefined && (config[key] === undefined || config[key] === null)) {
216
+ config[key] = defaultValue;
211
217
  }
212
218
  }
213
219
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.86",
3
+ "version": "1.0.88",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -57,10 +57,10 @@ export class ResourceController<T extends StringIndexedObject> {
57
57
  desiredConfig: Partial<T> & ResourceConfig,
58
58
  ): Promise<ValidateResponseData['resourceValidations'][0]> {
59
59
  const configToValidate = { ...desiredConfig };
60
- const { parameters, coreParameters } = splitUserConfig(configToValidate);
60
+ await this.applyTransformParameters(configToValidate);
61
61
 
62
+ const { parameters, coreParameters } = splitUserConfig(configToValidate);
62
63
  this.addDefaultValues(parameters);
63
- await this.applyTransformParameters(configToValidate);
64
64
 
65
65
  if (this.schemaValidator) {
66
66
  const isValid = this.schemaValidator(parameters);
@@ -112,6 +112,9 @@ export class ResourceController<T extends StringIndexedObject> {
112
112
  this.addDefaultValues(desiredConfig);
113
113
  await this.applyTransformParameters(desiredConfig);
114
114
 
115
+ this.addDefaultValues(stateConfig);
116
+ await this.applyTransformParameters(stateConfig);
117
+
115
118
  // Parse data from the user supplied config
116
119
  const parsedConfig = new ConfigParser(desiredConfig, stateConfig, this.parsedSettings.statefulParameters)
117
120
  const {
@@ -258,36 +261,36 @@ ${JSON.stringify(refresh, null, 2)}
258
261
  }
259
262
  }
260
263
 
261
- private async applyTransformParameters(desired: Partial<T> & ResourceConfig | null): Promise<void> {
262
- if (!desired) {
264
+ private async applyTransformParameters(config: Partial<T> & ResourceConfig | null): Promise<void> {
265
+ if (!config) {
263
266
  return;
264
267
  }
265
268
 
266
269
  for (const [key, inputTransformation] of Object.entries(this.parsedSettings.inputTransformations)) {
267
- if (desired[key] === undefined || !inputTransformation) {
270
+ if (config[key] === undefined || !inputTransformation) {
268
271
  continue;
269
272
  }
270
273
 
271
- (desired as Record<string, unknown>)[key] = await inputTransformation(desired[key]);
274
+ (config as Record<string, unknown>)[key] = await inputTransformation(config[key]);
272
275
  }
273
276
 
274
277
  if (this.settings.inputTransformation) {
275
- const { parameters, coreParameters } = splitUserConfig(desired);
278
+ const { parameters, coreParameters } = splitUserConfig(config);
276
279
 
277
280
  const transformed = await this.settings.inputTransformation(parameters)
278
- Object.keys(desired).forEach((k) => delete desired[k])
279
- Object.assign(desired, transformed, coreParameters);
281
+ Object.keys(config).forEach((k) => delete config[k])
282
+ Object.assign(config, transformed, coreParameters);
280
283
  }
281
284
  }
282
285
 
283
- private addDefaultValues(desired: Partial<T> | null): void {
284
- if (!desired) {
286
+ private addDefaultValues(config: Partial<T> | null): void {
287
+ if (!config) {
285
288
  return;
286
289
  }
287
290
 
288
291
  for (const [key, defaultValue] of Object.entries(this.parsedSettings.defaultValues)) {
289
- if (defaultValue !== undefined && (desired[key] === undefined || desired[key] === null)) {
290
- (desired as Record<string, unknown>)[key] = defaultValue;
292
+ if (defaultValue !== undefined && (config[key] === undefined || config[key] === null)) {
293
+ (config as Record<string, unknown>)[key] = defaultValue;
291
294
  }
292
295
  }
293
296
  }
@@ -491,4 +491,37 @@ describe('Resource parameter tests', () => {
491
491
 
492
492
  expect(plan.changeSet.operation).to.eq(ResourceOperation.NOOP);
493
493
  })
494
+
495
+ it('Supports transform parameters for state parameters', async () => {
496
+ const resource = spy(new class extends TestResource {
497
+ getSettings(): ResourceSettings<TestConfig> {
498
+ return {
499
+ id: 'resourceType',
500
+ inputTransformation: (desired) => ({
501
+ propA: 'propA',
502
+ propB: 10,
503
+ })
504
+ }
505
+ }
506
+
507
+ async refresh(): Promise<Partial<TestConfig> | null> {
508
+ return {
509
+ propA: 'propA',
510
+ propB: 10,
511
+ }
512
+ }
513
+ });
514
+
515
+ const controller = new ResourceController(resource);
516
+ const plan = await controller.plan(null, { type: 'resourceType', propC: 'abc' } as any, true);
517
+
518
+ expect(resource.refresh.called).to.be.true;
519
+ expect(resource.refresh.getCall(0).firstArg['propA']).to.exist;
520
+ expect(resource.refresh.getCall(0).firstArg['propB']).to.exist;
521
+ expect(resource.refresh.getCall(0).firstArg['propC']).to.not.exist;
522
+
523
+ expect(plan.currentConfig?.propA).to.eq('propA');
524
+ expect(plan.currentConfig?.propB).to.eq(10);
525
+ expect(plan.currentConfig?.propC).to.be.undefined;
526
+ })
494
527
  })