codify-plugin-lib 1.0.68 → 1.0.70

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,11 +1,4 @@
1
- import { Plan } from './plan.js';
2
- import { StringIndexedObject } from 'codify-schemas';
3
1
  export declare class SudoError extends Error {
4
2
  command: string;
5
3
  constructor(command: string);
6
4
  }
7
- export declare class ApplyValidationError<T extends StringIndexedObject> extends Error {
8
- desiredPlan: Plan<T>;
9
- validatedPlan: Plan<T>;
10
- constructor(desiredPlan: Plan<T>, validatedPlan: Plan<T>);
11
- }
@@ -5,12 +5,3 @@ export class SudoError extends Error {
5
5
  this.command = command;
6
6
  }
7
7
  }
8
- export class ApplyValidationError extends Error {
9
- desiredPlan;
10
- validatedPlan;
11
- constructor(desiredPlan, validatedPlan) {
12
- super();
13
- this.desiredPlan = desiredPlan;
14
- this.validatedPlan = validatedPlan;
15
- }
16
- }
@@ -1,7 +1,5 @@
1
- import { ResourceOperation } from 'codify-schemas';
2
1
  import { Plan } from './plan.js';
3
2
  import { splitUserConfig } from '../utils/utils.js';
4
- import { ApplyValidationError } from './errors.js';
5
3
  export class Plugin {
6
4
  name;
7
5
  resources;
@@ -64,10 +62,6 @@ export class Plugin {
64
62
  throw new Error('Malformed plan with resource that cannot be found');
65
63
  }
66
64
  await resource.apply(plan);
67
- const validationPlan = await resource.plan(plan.desiredConfig, plan.currentConfig, true);
68
- if (validationPlan.changeSet.operation !== ResourceOperation.NOOP) {
69
- throw new ApplyValidationError(plan, validationPlan);
70
- }
71
65
  }
72
66
  resolvePlan(data) {
73
67
  const { planId, plan: planRequest } = data;
@@ -23,7 +23,7 @@ export declare abstract class Resource<T extends StringIndexedObject> {
23
23
  protected schemaValidator?: ValidateFunction;
24
24
  protected constructor(options: ResourceOptions<T>);
25
25
  onInitialize(): Promise<void>;
26
- validateResource(parameters: unknown): Promise<ValidationResult>;
26
+ validateResource(parameters: Partial<T>): Promise<ValidationResult>;
27
27
  plan(desiredConfig: Partial<T> & ResourceConfig | null, currentConfig?: Partial<T> & ResourceConfig | null, statefulMode?: boolean): Promise<Plan<T>>;
28
28
  apply(plan: Plan<T>): Promise<void>;
29
29
  private _applyCreate;
@@ -35,8 +35,8 @@ export declare abstract class Resource<T extends StringIndexedObject> {
35
35
  private refreshNonStatefulParameters;
36
36
  private refreshStatefulParameters;
37
37
  private validatePlanInputs;
38
- validate(parameters: unknown): Promise<ValidationResult>;
39
- abstract refresh(values: Map<keyof T, T[keyof T]>): Promise<Partial<T> | null>;
38
+ validate(parameters: Partial<T>): Promise<ValidationResult>;
39
+ abstract refresh(values: Partial<T>): Promise<Partial<T> | null>;
40
40
  abstract applyCreate(plan: CreatePlan<T>): Promise<void>;
41
41
  applyModify(pc: ParameterChange<T>, plan: ModifyPlan<T>): Promise<void>;
42
42
  abstract applyDestroy(plan: DestroyPlan<T>): Promise<void>;
@@ -139,11 +139,11 @@ export class Resource {
139
139
  }
140
140
  await this.applyDestroy(plan);
141
141
  }
142
- validateRefreshResults(refresh, desiredMap) {
142
+ validateRefreshResults(refresh, desired) {
143
143
  if (!refresh) {
144
144
  return;
145
145
  }
146
- const desiredKeys = new Set(desiredMap.keys());
146
+ const desiredKeys = new Set(Object.keys(refresh));
147
147
  const refreshKeys = new Set(Object.keys(refresh));
148
148
  if (!setsEqual(desiredKeys, refreshKeys)) {
149
149
  throw new Error(`Resource ${this.typeId}
@@ -184,9 +184,8 @@ Additional: ${[...refreshKeys].filter(k => !desiredKeys.has(k))};`);
184
184
  });
185
185
  }
186
186
  async refreshNonStatefulParameters(resourceParameters) {
187
- const entriesToRefresh = new Map(Object.entries(resourceParameters));
188
- const currentParameters = await this.refresh(entriesToRefresh);
189
- this.validateRefreshResults(currentParameters, entriesToRefresh);
187
+ const currentParameters = await this.refresh(resourceParameters);
188
+ this.validateRefreshResults(currentParameters, resourceParameters);
190
189
  return currentParameters;
191
190
  }
192
191
  async refreshStatefulParameters(statefulParametersConfig, isStatefulMode) {
@@ -1,7 +1,7 @@
1
1
  import addFormats from 'ajv-formats';
2
2
  import { ApplyRequestDataSchema, ApplyResponseDataSchema, InitializeRequestDataSchema, InitializeResponseDataSchema, IpcMessageSchema, MessageStatus, PlanRequestDataSchema, PlanResponseDataSchema, ResourceSchema, ValidateRequestDataSchema, ValidateResponseDataSchema } from 'codify-schemas';
3
3
  import Ajv2020 from 'ajv/dist/2020.js';
4
- import { ApplyValidationError, SudoError } from '../entities/errors.js';
4
+ import { SudoError } from '../entities/errors.js';
5
5
  const SupportedRequests = {
6
6
  'initialize': {
7
7
  requestValidator: InitializeRequestDataSchema,
@@ -89,20 +89,6 @@ export class MessageHandler {
89
89
  data: `Plugin: '${this.plugin.name}'. Forbidden usage of sudo for command '${e.command}'. Please contact the plugin developer to fix this.`,
90
90
  });
91
91
  }
92
- if (e instanceof ApplyValidationError) {
93
- return process.send?.({
94
- cmd,
95
- status: MessageStatus.ERROR,
96
- data: `Plugin: '${this.plugin.name}'. Apply validation was not successful (additional changes are needed to match the desired plan).
97
-
98
- Validation plan:
99
- ${JSON.stringify(e.validatedPlan, null, 2)},
100
-
101
- User desired plan:
102
- ${JSON.stringify(e.desiredPlan, null, 2)}
103
- `
104
- });
105
- }
106
92
  const isDebug = process.env.DEBUG?.includes('*') ?? false;
107
93
  process.send?.({
108
94
  cmd,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.68",
3
+ "version": "1.0.70",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -1,6 +1,3 @@
1
- import { Plan } from './plan.js';
2
- import { StringIndexedObject } from 'codify-schemas';
3
-
4
1
  export class SudoError extends Error {
5
2
  command: string;
6
3
 
@@ -9,17 +6,3 @@ export class SudoError extends Error {
9
6
  this.command = command;
10
7
  }
11
8
  }
12
-
13
- export class ApplyValidationError<T extends StringIndexedObject> extends Error {
14
- desiredPlan: Plan<T>;
15
- validatedPlan: Plan<T>;
16
-
17
- constructor(
18
- desiredPlan: Plan<T>,
19
- validatedPlan: Plan<T>
20
- ) {
21
- super();
22
- this.desiredPlan = desiredPlan;
23
- this.validatedPlan = validatedPlan;
24
- }
25
- }
@@ -5,13 +5,11 @@ import {
5
5
  PlanRequestData,
6
6
  PlanResponseData,
7
7
  ResourceConfig,
8
- ResourceOperation,
9
8
  ValidateRequestData,
10
9
  ValidateResponseData
11
10
  } from 'codify-schemas';
12
11
  import { Plan } from './plan.js';
13
12
  import { splitUserConfig } from '../utils/utils.js';
14
- import { ApplyValidationError } from './errors.js';
15
13
 
16
14
  export class Plugin {
17
15
  planStorage: Map<string, Plan<any>>;
@@ -92,17 +90,6 @@ export class Plugin {
92
90
  }
93
91
 
94
92
  await resource.apply(plan);
95
-
96
- // Perform a validation check after to ensure that the plan was properly applied.
97
- // Sometimes no errors are returned (exit code 0) but the apply was not successful
98
- const validationPlan = await resource.plan(
99
- plan.desiredConfig,
100
- plan.currentConfig,
101
- true,
102
- );
103
- if (validationPlan.changeSet.operation !== ResourceOperation.NOOP) {
104
- throw new ApplyValidationError(plan, validationPlan);
105
- }
106
93
  }
107
94
 
108
95
  private resolvePlan(data: ApplyRequestData): Plan<ResourceConfig> {
@@ -59,7 +59,7 @@ export abstract class Resource<T extends StringIndexedObject> {
59
59
 
60
60
  async onInitialize(): Promise<void> {}
61
61
 
62
- async validateResource(parameters: unknown): Promise<ValidationResult> {
62
+ async validateResource(parameters: Partial<T>): Promise<ValidationResult> {
63
63
  if (this.schemaValidator) {
64
64
  const isValid = this.schemaValidator(parameters);
65
65
 
@@ -215,12 +215,12 @@ export abstract class Resource<T extends StringIndexedObject> {
215
215
  await this.applyDestroy(plan as DestroyPlan<T>);
216
216
  }
217
217
 
218
- private validateRefreshResults(refresh: Partial<T> | null, desiredMap: Map<keyof T, T[keyof T]>) {
218
+ private validateRefreshResults(refresh: Partial<T> | null, desired: Partial<T>) {
219
219
  if (!refresh) {
220
220
  return;
221
221
  }
222
222
 
223
- const desiredKeys = new Set<keyof T>(desiredMap.keys());
223
+ const desiredKeys = new Set(Object.keys(refresh)) as Set<keyof T>;
224
224
  const refreshKeys = new Set(Object.keys(refresh)) as Set<keyof T>;
225
225
 
226
226
  if (!setsEqual(desiredKeys, refreshKeys)) {
@@ -278,11 +278,8 @@ Additional: ${[...refreshKeys].filter(k => !desiredKeys.has(k))};`
278
278
  }
279
279
 
280
280
  private async refreshNonStatefulParameters(resourceParameters: Partial<T>): Promise<Partial<T> | null> {
281
- const entriesToRefresh = new Map<keyof T, T[keyof T]>(
282
- Object.entries(resourceParameters)
283
- )
284
- const currentParameters = await this.refresh(entriesToRefresh);
285
- this.validateRefreshResults(currentParameters, entriesToRefresh);
281
+ const currentParameters = await this.refresh(resourceParameters);
282
+ this.validateRefreshResults(currentParameters, resourceParameters);
286
283
  return currentParameters;
287
284
  }
288
285
 
@@ -340,13 +337,13 @@ Additional: ${[...refreshKeys].filter(k => !desiredKeys.has(k))};`
340
337
  }
341
338
  }
342
339
 
343
- async validate(parameters: unknown): Promise<ValidationResult> {
340
+ async validate(parameters: Partial<T>): Promise<ValidationResult> {
344
341
  return {
345
342
  isValid: true,
346
343
  }
347
344
  };
348
345
 
349
- abstract refresh(values: Map<keyof T, T[keyof T]>): Promise<Partial<T> | null>;
346
+ abstract refresh(values: Partial<T>): Promise<Partial<T> | null>;
350
347
 
351
348
  abstract applyCreate(plan: CreatePlan<T>): Promise<void>;
352
349
 
@@ -15,7 +15,7 @@ import {
15
15
  ValidateResponseDataSchema
16
16
  } from 'codify-schemas';
17
17
  import Ajv2020, { SchemaObject, ValidateFunction } from 'ajv/dist/2020.js';
18
- import { ApplyValidationError, SudoError } from '../entities/errors.js';
18
+ import { SudoError } from '../entities/errors.js';
19
19
 
20
20
  const SupportedRequests: Record<string, { requestValidator: SchemaObject; responseValidator: SchemaObject; handler: (plugin: Plugin, data: any) => Promise<unknown> }> = {
21
21
  'initialize': {
@@ -124,21 +124,6 @@ export class MessageHandler {
124
124
  })
125
125
  }
126
126
 
127
- if (e instanceof ApplyValidationError) {
128
- return process.send?.({
129
- cmd,
130
- status: MessageStatus.ERROR,
131
- data: `Plugin: '${this.plugin.name}'. Apply validation was not successful (additional changes are needed to match the desired plan).
132
-
133
- Validation plan:
134
- ${JSON.stringify(e.validatedPlan, null, 2)},
135
-
136
- User desired plan:
137
- ${JSON.stringify(e.desiredPlan, null, 2)}
138
- `
139
- })
140
- }
141
-
142
127
  const isDebug = process.env.DEBUG?.includes('*') ?? false;
143
128
 
144
129
  process.send?.({