codify-plugin-lib 1.0.177 → 1.0.178

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 (35) hide show
  1. package/dist/messages/sender.d.ts +1 -0
  2. package/dist/messages/sender.js +10 -0
  3. package/dist/resource/resource-controller.d.ts +1 -1
  4. package/dist/resource/resource-controller.js +10 -10
  5. package/package.json +2 -2
  6. package/src/messages/sender.ts +13 -0
  7. package/src/resource/resource-controller.ts +11 -11
  8. package/dist/entities/change-set.d.ts +0 -24
  9. package/dist/entities/change-set.js +0 -152
  10. package/dist/entities/errors.d.ts +0 -4
  11. package/dist/entities/errors.js +0 -7
  12. package/dist/entities/plan-types.d.ts +0 -25
  13. package/dist/entities/plan-types.js +0 -1
  14. package/dist/entities/plan.d.ts +0 -15
  15. package/dist/entities/plan.js +0 -127
  16. package/dist/entities/plugin.d.ts +0 -16
  17. package/dist/entities/plugin.js +0 -80
  18. package/dist/entities/resource-options.d.ts +0 -31
  19. package/dist/entities/resource-options.js +0 -76
  20. package/dist/entities/resource-types.d.ts +0 -11
  21. package/dist/entities/resource-types.js +0 -1
  22. package/dist/entities/resource.d.ts +0 -42
  23. package/dist/entities/resource.js +0 -303
  24. package/dist/entities/stateful-parameter.d.ts +0 -29
  25. package/dist/entities/stateful-parameter.js +0 -46
  26. package/dist/entities/transform-parameter.d.ts +0 -4
  27. package/dist/entities/transform-parameter.js +0 -2
  28. package/dist/pty/vitest.config.d.ts +0 -2
  29. package/dist/pty/vitest.config.js +0 -11
  30. package/dist/resource/stateful-parameter.d.ts +0 -165
  31. package/dist/resource/stateful-parameter.js +0 -94
  32. package/dist/utils/spawn-2.d.ts +0 -5
  33. package/dist/utils/spawn-2.js +0 -7
  34. package/dist/utils/spawn.d.ts +0 -29
  35. package/dist/utils/spawn.js +0 -124
@@ -4,6 +4,7 @@
4
4
  declare class CodifyCliSenderImpl {
5
5
  private readonly validateIpcMessageV2;
6
6
  requestPressKeyToContinuePrompt(message?: string): Promise<void>;
7
+ getCodifyCliCredentials(): Promise<string>;
7
8
  private sendAndWaitForResponse;
8
9
  }
9
10
  export declare const CodifyCliSender: CodifyCliSenderImpl;
@@ -17,6 +17,16 @@ class CodifyCliSenderImpl {
17
17
  }
18
18
  });
19
19
  }
20
+ async getCodifyCliCredentials() {
21
+ const data = await this.sendAndWaitForResponse({
22
+ cmd: MessageCmd.CODIFY_CREDENTIALS_REQUEST,
23
+ data: {},
24
+ });
25
+ if (typeof data.data !== 'string') {
26
+ throw new Error('Expected string back from credentials request');
27
+ }
28
+ return data.data;
29
+ }
20
30
  async sendAndWaitForResponse(message) {
21
31
  return new Promise((resolve) => {
22
32
  const requestId = nanoid(8);
@@ -24,7 +24,7 @@ export declare class ResourceController<T extends StringIndexedObject> {
24
24
  private applyModify;
25
25
  private applyDestroy;
26
26
  private validateRefreshResults;
27
- private applyTransformParameters;
27
+ private applyTransformations;
28
28
  private addDefaultValues;
29
29
  private removeDefaultValues;
30
30
  private refreshNonStatefulParameters;
@@ -33,7 +33,7 @@ export class ResourceController {
33
33
  }
34
34
  async validate(core, parameters) {
35
35
  const originalParameters = structuredClone(parameters);
36
- await this.applyTransformParameters(parameters);
36
+ await this.applyTransformations(parameters, undefined, true);
37
37
  this.addDefaultValues(parameters);
38
38
  if (this.schemaValidator) {
39
39
  // Schema validator uses pre transformation parameters
@@ -103,9 +103,9 @@ export class ResourceController {
103
103
  const originalParams = structuredClone(resource.parameters);
104
104
  const paramsToMatch = structuredClone(resourceToMatch.parameters);
105
105
  this.addDefaultValues(originalParams);
106
- await this.applyTransformParameters(originalParams);
106
+ await this.applyTransformations(originalParams);
107
107
  this.addDefaultValues(paramsToMatch);
108
- await this.applyTransformParameters(paramsToMatch);
108
+ await this.applyTransformations(paramsToMatch);
109
109
  const match = parameterMatcher(originalParams, paramsToMatch);
110
110
  if (match) {
111
111
  return resourceToMatch;
@@ -120,9 +120,9 @@ export class ResourceController {
120
120
  originalDesiredConfig: structuredClone(desired),
121
121
  };
122
122
  this.addDefaultValues(desired);
123
- await this.applyTransformParameters(desired);
123
+ await this.applyTransformations(desired);
124
124
  this.addDefaultValues(state);
125
- await this.applyTransformParameters(state);
125
+ await this.applyTransformations(state);
126
126
  // Parse data from the user supplied config
127
127
  const parsedConfig = new ConfigParser(desired, state, this.parsedSettings.statefulParameters);
128
128
  const { allParameters, allNonStatefulParameters, allStatefulParameters, } = parsedConfig;
@@ -156,7 +156,7 @@ export class ResourceController {
156
156
  }
157
157
  async planDestroy(core, parameters) {
158
158
  this.addDefaultValues(parameters);
159
- await this.applyTransformParameters(parameters);
159
+ await this.applyTransformations(parameters);
160
160
  // Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
161
161
  const parametersToRefresh = this.settings.importAndDestroy?.refreshKeys
162
162
  ? {
@@ -211,7 +211,7 @@ export class ResourceController {
211
211
  return results.filter(Boolean).flat();
212
212
  }
213
213
  this.addDefaultValues(parameters);
214
- await this.applyTransformParameters(parameters);
214
+ await this.applyTransformations(parameters);
215
215
  // Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
216
216
  const parametersToRefresh = this.getParametersToRefreshForImport(parameters, context);
217
217
  // Parse data from the user supplied config
@@ -227,7 +227,7 @@ export class ResourceController {
227
227
  const resultParametersArray = currentParametersArray
228
228
  ?.map((r, idx) => ({ ...r, ...statefulCurrentParameters[idx] }));
229
229
  for (const result of resultParametersArray) {
230
- await this.applyTransformParameters(result, { original: context.originalDesiredConfig });
230
+ await this.applyTransformations(result, { original: context.originalDesiredConfig });
231
231
  this.removeDefaultValues(result, parameters);
232
232
  }
233
233
  return resultParametersArray?.map((r) => ({ core, parameters: r }));
@@ -292,7 +292,7 @@ ${JSON.stringify(refresh, null, 2)}
292
292
  `);
293
293
  }
294
294
  }
295
- async applyTransformParameters(config, reverse) {
295
+ async applyTransformations(config, reverse, skipConfigTransformation = false) {
296
296
  if (!config) {
297
297
  return;
298
298
  }
@@ -304,7 +304,7 @@ ${JSON.stringify(refresh, null, 2)}
304
304
  ? await inputTransformation.from(config[key], reverse.original?.[key])
305
305
  : await inputTransformation.to(config[key]);
306
306
  }
307
- if (this.settings.transformation) {
307
+ if (this.settings.transformation && !skipConfigTransformation) {
308
308
  const transformed = reverse
309
309
  ? await this.settings.transformation.from({ ...config }, reverse.original)
310
310
  : await this.settings.transformation.to({ ...config });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.177",
3
+ "version": "1.0.178",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -16,7 +16,7 @@
16
16
  "dependencies": {
17
17
  "ajv": "^8.12.0",
18
18
  "ajv-formats": "^2.1.1",
19
- "codify-schemas": "1.0.76",
19
+ "codify-schemas": "1.0.77",
20
20
  "@npmcli/promise-spawn": "^7.0.1",
21
21
  "@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
22
22
  "uuid": "^10.0.0",
@@ -21,6 +21,19 @@ class CodifyCliSenderImpl {
21
21
  })
22
22
  }
23
23
 
24
+ async getCodifyCliCredentials(): Promise<string> {
25
+ const data = await this.sendAndWaitForResponse(<IpcMessageV2>{
26
+ cmd: MessageCmd.CODIFY_CREDENTIALS_REQUEST,
27
+ data: {},
28
+ })
29
+
30
+ if (typeof data.data !== 'string') {
31
+ throw new Error('Expected string back from credentials request');
32
+ }
33
+
34
+ return data.data;
35
+ }
36
+
24
37
  private async sendAndWaitForResponse(message: IpcMessageV2): Promise<IpcMessageV2> {
25
38
  return new Promise((resolve) => {
26
39
  const requestId = nanoid(8);
@@ -59,7 +59,7 @@ export class ResourceController<T extends StringIndexedObject> {
59
59
  parameters: Partial<T>,
60
60
  ): Promise<ValidateResponseData['resourceValidations'][0]> {
61
61
  const originalParameters = structuredClone(parameters);
62
- await this.applyTransformParameters(parameters);
62
+ await this.applyTransformations(parameters, undefined, true);
63
63
  this.addDefaultValues(parameters);
64
64
 
65
65
  if (this.schemaValidator) {
@@ -143,10 +143,10 @@ export class ResourceController<T extends StringIndexedObject> {
143
143
  const paramsToMatch = structuredClone(resourceToMatch.parameters) as Partial<T>;
144
144
 
145
145
  this.addDefaultValues(originalParams);
146
- await this.applyTransformParameters(originalParams);
146
+ await this.applyTransformations(originalParams);
147
147
 
148
148
  this.addDefaultValues(paramsToMatch);
149
- await this.applyTransformParameters(paramsToMatch);
149
+ await this.applyTransformations(paramsToMatch);
150
150
 
151
151
  const match = parameterMatcher(originalParams, paramsToMatch);
152
152
  if (match) {
@@ -170,10 +170,10 @@ export class ResourceController<T extends StringIndexedObject> {
170
170
  };
171
171
 
172
172
  this.addDefaultValues(desired);
173
- await this.applyTransformParameters(desired);
173
+ await this.applyTransformations(desired);
174
174
 
175
175
  this.addDefaultValues(state);
176
- await this.applyTransformParameters(state);
176
+ await this.applyTransformations(state);
177
177
 
178
178
  // Parse data from the user supplied config
179
179
  const parsedConfig = new ConfigParser(desired, state, this.parsedSettings.statefulParameters)
@@ -221,7 +221,7 @@ export class ResourceController<T extends StringIndexedObject> {
221
221
  parameters: Partial<T>
222
222
  ): Promise<Plan<T>> {
223
223
  this.addDefaultValues(parameters);
224
- await this.applyTransformParameters(parameters);
224
+ await this.applyTransformations(parameters);
225
225
 
226
226
  // Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
227
227
  const parametersToRefresh = this.settings.importAndDestroy?.refreshKeys
@@ -298,7 +298,7 @@ export class ResourceController<T extends StringIndexedObject> {
298
298
  }
299
299
 
300
300
  this.addDefaultValues(parameters);
301
- await this.applyTransformParameters(parameters);
301
+ await this.applyTransformations(parameters);
302
302
 
303
303
  // Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
304
304
  const parametersToRefresh = this.getParametersToRefreshForImport(parameters, context);
@@ -325,7 +325,7 @@ export class ResourceController<T extends StringIndexedObject> {
325
325
  ?.map((r, idx) => ({ ...r, ...statefulCurrentParameters[idx] }))
326
326
 
327
327
  for (const result of resultParametersArray) {
328
- await this.applyTransformParameters(result, { original: context.originalDesiredConfig });
328
+ await this.applyTransformations(result, { original: context.originalDesiredConfig });
329
329
  this.removeDefaultValues(result, parameters);
330
330
  }
331
331
 
@@ -408,9 +408,9 @@ ${JSON.stringify(refresh, null, 2)}
408
408
  }
409
409
  }
410
410
 
411
- private async applyTransformParameters(config: Partial<T> | null, reverse?: {
411
+ private async applyTransformations(config: Partial<T> | null, reverse?: {
412
412
  original: Partial<T> | null
413
- }): Promise<void> {
413
+ }, skipConfigTransformation = false): Promise<void> {
414
414
  if (!config) {
415
415
  return;
416
416
  }
@@ -425,7 +425,7 @@ ${JSON.stringify(refresh, null, 2)}
425
425
  : await inputTransformation.to(config[key]);
426
426
  }
427
427
 
428
- if (this.settings.transformation) {
428
+ if (this.settings.transformation && !skipConfigTransformation) {
429
429
  const transformed = reverse
430
430
  ? await this.settings.transformation.from({ ...config }, reverse.original)
431
431
  : await this.settings.transformation.to({ ...config })
@@ -1,24 +0,0 @@
1
- import { ParameterOperation, ResourceOperation, StringIndexedObject } from 'codify-schemas';
2
- import { ParameterOptions } from './plan-types.js';
3
- export interface ParameterChange<T extends StringIndexedObject> {
4
- name: keyof T & string;
5
- operation: ParameterOperation;
6
- previousValue: any | null;
7
- newValue: any | null;
8
- }
9
- export declare class ChangeSet<T extends StringIndexedObject> {
10
- operation: ResourceOperation;
11
- parameterChanges: Array<ParameterChange<T>>;
12
- constructor(operation: ResourceOperation, parameterChanges: Array<ParameterChange<T>>);
13
- get desiredParameters(): T;
14
- get currentParameters(): T;
15
- static calculateParameterChangeSet<T extends StringIndexedObject>(desired: T | null, current: T | null, options: {
16
- statefulMode: boolean;
17
- parameterOptions?: Record<keyof T, ParameterOptions>;
18
- }): ParameterChange<T>[];
19
- static combineResourceOperations(prev: ResourceOperation, next: ResourceOperation): ResourceOperation;
20
- static isSame(desired: unknown, current: unknown, options?: ParameterOptions): boolean;
21
- private static calculateStatefulModeChangeSet;
22
- private static calculateStatelessModeChangeSet;
23
- private static addDefaultValues;
24
- }
@@ -1,152 +0,0 @@
1
- import { ParameterOperation, ResourceOperation } from 'codify-schemas';
2
- export class ChangeSet {
3
- operation;
4
- parameterChanges;
5
- constructor(operation, parameterChanges) {
6
- this.operation = operation;
7
- this.parameterChanges = parameterChanges;
8
- }
9
- get desiredParameters() {
10
- return this.parameterChanges
11
- .reduce((obj, pc) => ({
12
- ...obj,
13
- [pc.name]: pc.newValue,
14
- }), {});
15
- }
16
- get currentParameters() {
17
- return this.parameterChanges
18
- .reduce((obj, pc) => ({
19
- ...obj,
20
- [pc.name]: pc.previousValue,
21
- }), {});
22
- }
23
- static calculateParameterChangeSet(desired, current, options) {
24
- if (options.statefulMode) {
25
- return ChangeSet.calculateStatefulModeChangeSet(desired, current, options.parameterOptions);
26
- }
27
- else {
28
- return ChangeSet.calculateStatelessModeChangeSet(desired, current, options.parameterOptions);
29
- }
30
- }
31
- static combineResourceOperations(prev, next) {
32
- const orderOfOperations = [
33
- ResourceOperation.NOOP,
34
- ResourceOperation.MODIFY,
35
- ResourceOperation.RECREATE,
36
- ResourceOperation.CREATE,
37
- ResourceOperation.DESTROY,
38
- ];
39
- const indexPrev = orderOfOperations.indexOf(prev);
40
- const indexNext = orderOfOperations.indexOf(next);
41
- return orderOfOperations[Math.max(indexPrev, indexNext)];
42
- }
43
- static isSame(desired, current, options) {
44
- if (options?.isEqual) {
45
- return options.isEqual(desired, current);
46
- }
47
- if (Array.isArray(desired) && Array.isArray(current)) {
48
- const sortedDesired = desired.map((x) => x).sort();
49
- const sortedCurrent = current.map((x) => x).sort();
50
- if (sortedDesired.length !== sortedCurrent.length) {
51
- return false;
52
- }
53
- if (options?.isElementEqual) {
54
- return sortedDesired.every((value, index) => options.isElementEqual(value, sortedCurrent[index]));
55
- }
56
- return JSON.stringify(sortedDesired) === JSON.stringify(sortedCurrent);
57
- }
58
- return desired === current;
59
- }
60
- static calculateStatefulModeChangeSet(desired, current, parameterOptions) {
61
- const parameterChangeSet = new Array();
62
- const _desired = Object.fromEntries(Object.entries(desired ?? {}).filter(([, v]) => v != null));
63
- const _current = Object.fromEntries(Object.entries(current ?? {}).filter(([, v]) => v != null));
64
- this.addDefaultValues(_desired, parameterOptions);
65
- for (const [k, v] of Object.entries(_current)) {
66
- if (_desired[k] == null) {
67
- parameterChangeSet.push({
68
- name: k,
69
- previousValue: v,
70
- newValue: null,
71
- operation: ParameterOperation.REMOVE,
72
- });
73
- delete _current[k];
74
- continue;
75
- }
76
- if (!ChangeSet.isSame(_desired[k], _current[k], parameterOptions?.[k])) {
77
- parameterChangeSet.push({
78
- name: k,
79
- previousValue: v,
80
- newValue: _desired[k],
81
- operation: ParameterOperation.MODIFY,
82
- });
83
- delete _current[k];
84
- delete _desired[k];
85
- continue;
86
- }
87
- parameterChangeSet.push({
88
- name: k,
89
- previousValue: v,
90
- newValue: _desired[k],
91
- operation: ParameterOperation.NOOP,
92
- });
93
- delete _current[k];
94
- delete _desired[k];
95
- }
96
- if (Object.keys(_current).length !== 0) {
97
- throw Error('Diff algorithm error');
98
- }
99
- for (const [k, v] of Object.entries(_desired)) {
100
- parameterChangeSet.push({
101
- name: k,
102
- previousValue: null,
103
- newValue: v,
104
- operation: ParameterOperation.ADD,
105
- });
106
- }
107
- return parameterChangeSet;
108
- }
109
- static calculateStatelessModeChangeSet(desired, current, parameterOptions) {
110
- const parameterChangeSet = new Array();
111
- const _desired = Object.fromEntries(Object.entries(desired ?? {}).filter(([, v]) => v != null));
112
- const _current = Object.fromEntries(Object.entries(current ?? {}).filter(([, v]) => v != null));
113
- this.addDefaultValues(_desired, parameterOptions);
114
- for (const [k, v] of Object.entries(_desired)) {
115
- if (_current[k] == null) {
116
- parameterChangeSet.push({
117
- name: k,
118
- previousValue: null,
119
- newValue: v,
120
- operation: ParameterOperation.ADD,
121
- });
122
- continue;
123
- }
124
- if (!ChangeSet.isSame(_desired[k], _current[k], parameterOptions?.[k])) {
125
- parameterChangeSet.push({
126
- name: k,
127
- previousValue: _current[k],
128
- newValue: _desired[k],
129
- operation: ParameterOperation.MODIFY,
130
- });
131
- continue;
132
- }
133
- parameterChangeSet.push({
134
- name: k,
135
- previousValue: v,
136
- newValue: v,
137
- operation: ParameterOperation.NOOP,
138
- });
139
- }
140
- return parameterChangeSet;
141
- }
142
- static addDefaultValues(obj, options) {
143
- Object.entries(options ?? {})
144
- .filter(([, option]) => option.default !== undefined)
145
- .map(([name, option]) => [name, option.default])
146
- .forEach(([key, defaultValue]) => {
147
- if (obj[key] === undefined) {
148
- obj[key] = defaultValue;
149
- }
150
- });
151
- }
152
- }
@@ -1,4 +0,0 @@
1
- export declare class SudoError extends Error {
2
- command: string;
3
- constructor(command: string);
4
- }
@@ -1,7 +0,0 @@
1
- export class SudoError extends Error {
2
- command;
3
- constructor(command) {
4
- super();
5
- this.command = command;
6
- }
7
- }
@@ -1,25 +0,0 @@
1
- import { Plan } from './plan.js';
2
- import { StringIndexedObject } from 'codify-schemas';
3
- export interface ParameterOptions {
4
- modifyOnChange?: boolean;
5
- isEqual?: (desired: any, current: any) => boolean;
6
- isElementEqual?: (desired: any, current: any) => boolean;
7
- default?: unknown;
8
- isStatefulParameter?: boolean;
9
- }
10
- export interface PlanOptions<T> {
11
- statefulMode: boolean;
12
- parameterOptions?: Record<keyof T, ParameterOptions>;
13
- }
14
- export interface CreatePlan<T extends StringIndexedObject> extends Plan<T> {
15
- desiredConfig: T;
16
- currentConfig: null;
17
- }
18
- export interface DestroyPlan<T extends StringIndexedObject> extends Plan<T> {
19
- desiredConfig: null;
20
- currentConfig: T;
21
- }
22
- export interface ModifyPlan<T extends StringIndexedObject> extends Plan<T> {
23
- desiredConfig: T;
24
- currentConfig: T;
25
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,15 +0,0 @@
1
- import { ChangeSet } from './change-set.js';
2
- import { ApplyRequestData, PlanResponseData, ResourceConfig, StringIndexedObject } from 'codify-schemas';
3
- import { PlanOptions } from './plan-types.js';
4
- export declare class Plan<T extends StringIndexedObject> {
5
- id: string;
6
- changeSet: ChangeSet<T>;
7
- resourceMetadata: ResourceConfig;
8
- constructor(id: string, changeSet: ChangeSet<T>, resourceMetadata: ResourceConfig);
9
- static create<T extends StringIndexedObject>(desiredParameters: Partial<T> | null, currentParameters: Partial<T> | null, resourceMetadata: ResourceConfig, options: PlanOptions<T>): Plan<T>;
10
- getResourceType(): string;
11
- static fromResponse<T extends ResourceConfig>(data: ApplyRequestData['plan'], defaultValues?: Partial<Record<keyof T, unknown>>): Plan<T>;
12
- get desiredConfig(): T | null;
13
- get currentConfig(): T | null;
14
- toResponse(): PlanResponseData;
15
- }
@@ -1,127 +0,0 @@
1
- import { ChangeSet } from './change-set.js';
2
- import { ParameterOperation, ResourceOperation, } from 'codify-schemas';
3
- import { randomUUID } from 'crypto';
4
- export class Plan {
5
- id;
6
- changeSet;
7
- resourceMetadata;
8
- constructor(id, changeSet, resourceMetadata) {
9
- this.id = id;
10
- this.changeSet = changeSet;
11
- this.resourceMetadata = resourceMetadata;
12
- }
13
- static create(desiredParameters, currentParameters, resourceMetadata, options) {
14
- const parameterOptions = options.parameterOptions ?? {};
15
- const statefulParameterNames = new Set([...Object.entries(parameterOptions)]
16
- .filter(([k, v]) => v.isStatefulParameter)
17
- .map(([k, v]) => k));
18
- const parameterChangeSet = ChangeSet.calculateParameterChangeSet(desiredParameters, currentParameters, { statefulMode: options.statefulMode, parameterOptions });
19
- let resourceOperation;
20
- if (!currentParameters && desiredParameters) {
21
- resourceOperation = ResourceOperation.CREATE;
22
- }
23
- else if (currentParameters && !desiredParameters) {
24
- resourceOperation = ResourceOperation.DESTROY;
25
- }
26
- else {
27
- resourceOperation = parameterChangeSet
28
- .filter((change) => change.operation !== ParameterOperation.NOOP)
29
- .reduce((operation, curr) => {
30
- let newOperation;
31
- if (statefulParameterNames.has(curr.name)) {
32
- newOperation = ResourceOperation.MODIFY;
33
- }
34
- else if (parameterOptions[curr.name]?.modifyOnChange) {
35
- newOperation = parameterOptions[curr.name].modifyOnChange ? ResourceOperation.MODIFY : ResourceOperation.RECREATE;
36
- }
37
- else {
38
- newOperation = ResourceOperation.RECREATE;
39
- }
40
- return ChangeSet.combineResourceOperations(operation, newOperation);
41
- }, ResourceOperation.NOOP);
42
- }
43
- return new Plan(randomUUID(), new ChangeSet(resourceOperation, parameterChangeSet), resourceMetadata);
44
- }
45
- getResourceType() {
46
- return this.resourceMetadata.type;
47
- }
48
- static fromResponse(data, defaultValues) {
49
- if (!data) {
50
- throw new Error('Data is empty');
51
- }
52
- addDefaultValues();
53
- return new Plan(randomUUID(), new ChangeSet(data.operation, data.parameters), {
54
- type: data.resourceType,
55
- name: data.resourceName,
56
- });
57
- function addDefaultValues() {
58
- Object.entries(defaultValues ?? {})
59
- .forEach(([key, defaultValue]) => {
60
- const configValueExists = data
61
- .parameters
62
- .some((p) => p.name === key);
63
- if (configValueExists) {
64
- return;
65
- }
66
- switch (data.operation) {
67
- case ResourceOperation.CREATE: {
68
- data.parameters.push({
69
- name: key,
70
- operation: ParameterOperation.ADD,
71
- previousValue: null,
72
- newValue: defaultValue,
73
- });
74
- break;
75
- }
76
- case ResourceOperation.DESTROY: {
77
- data.parameters.push({
78
- name: key,
79
- operation: ParameterOperation.REMOVE,
80
- previousValue: defaultValue,
81
- newValue: null,
82
- });
83
- break;
84
- }
85
- case ResourceOperation.MODIFY:
86
- case ResourceOperation.RECREATE:
87
- case ResourceOperation.NOOP: {
88
- data.parameters.push({
89
- name: key,
90
- operation: ParameterOperation.NOOP,
91
- previousValue: defaultValue,
92
- newValue: defaultValue,
93
- });
94
- break;
95
- }
96
- }
97
- });
98
- }
99
- }
100
- get desiredConfig() {
101
- if (this.changeSet.operation === ResourceOperation.DESTROY) {
102
- return null;
103
- }
104
- return {
105
- ...this.resourceMetadata,
106
- ...this.changeSet.desiredParameters,
107
- };
108
- }
109
- get currentConfig() {
110
- if (this.changeSet.operation === ResourceOperation.CREATE) {
111
- return null;
112
- }
113
- return {
114
- ...this.resourceMetadata,
115
- ...this.changeSet.currentParameters,
116
- };
117
- }
118
- toResponse() {
119
- return {
120
- planId: this.id,
121
- operation: this.changeSet.operation,
122
- resourceName: this.resourceMetadata.name,
123
- resourceType: this.resourceMetadata.type,
124
- parameters: this.changeSet.parameterChanges,
125
- };
126
- }
127
- }
@@ -1,16 +0,0 @@
1
- import { ApplyRequestData, InitializeResponseData, PlanRequestData, PlanResponseData, ResourceConfig, ValidateRequestData, ValidateResponseData } from 'codify-schemas';
2
- import { Plan } from './plan.js';
3
- import { Resource } from './resource.js';
4
- export declare class Plugin {
5
- name: string;
6
- resources: Map<string, Resource<ResourceConfig>>;
7
- planStorage: Map<string, Plan<any>>;
8
- static create(name: string, resources: Resource<any>[]): Plugin;
9
- constructor(name: string, resources: Map<string, Resource<ResourceConfig>>);
10
- initialize(): Promise<InitializeResponseData>;
11
- validate(data: ValidateRequestData): Promise<ValidateResponseData>;
12
- plan(data: PlanRequestData): Promise<PlanResponseData>;
13
- apply(data: ApplyRequestData): Promise<void>;
14
- private resolvePlan;
15
- protected crossValidateResources(configs: ResourceConfig[]): Promise<void>;
16
- }