-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource-controller.ts
More file actions
564 lines (469 loc) · 18.8 KB
/
resource-controller.ts
File metadata and controls
564 lines (469 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import { Ajv, ValidateFunction } from 'ajv';
import cleanDeep from 'clean-deep';
import {
ParameterOperation,
ResourceConfig,
ResourceJson,
ResourceOperation,
StringIndexedObject,
ValidateResponseData
} from 'codify-schemas';
import { ParameterChange } from '../plan/change-set.js';
import { Plan } from '../plan/plan.js';
import { CreatePlan, DestroyPlan, ModifyPlan } from '../plan/plan-types.js';
import { ConfigParser } from './config-parser.js';
import { ParsedResourceSettings } from './parsed-resource-settings.js';
import { RefreshContext, Resource } from './resource.js';
import { ResourceSettings } from './resource-settings.js';
export class ResourceController<T extends StringIndexedObject> {
readonly resource: Resource<T>
readonly settings: ResourceSettings<T>
readonly parsedSettings: ParsedResourceSettings<T>
readonly typeId: string;
readonly dependencies: string[];
protected ajv?: Ajv;
protected schemaValidator?: ValidateFunction;
constructor(
resource: Resource<T>,
) {
this.resource = resource;
this.settings = resource.getSettings();
this.typeId = this.settings.id;
this.dependencies = this.settings.dependencies ?? [];
if (this.settings.schema) {
this.ajv = new Ajv({
allErrors: true,
strict: true,
strictRequired: false,
allowUnionTypes: true
})
this.schemaValidator = this.ajv.compile(this.settings.schema);
}
this.parsedSettings = new ParsedResourceSettings<T>(this.settings);
}
async initialize(): Promise<void> {
return this.resource.initialize();
}
async validate(
core: ResourceConfig,
parameters: Partial<T>,
): Promise<ValidateResponseData['resourceValidations'][0]> {
const originalParameters = structuredClone(parameters);
await this.applyTransformations(parameters, undefined, true);
this.addDefaultValues(parameters);
if (this.schemaValidator) {
// Schema validator uses pre transformation parameters
const isValid = this.schemaValidator(
// @ts-expect-error Non esm package
cleanDeep(originalParameters, {
nullValues: true,
undefinedValues: true,
emptyArrays: false,
emptyStrings: false,
emptyObjects: false,
NaNValues: false
})
);
if (!isValid) {
return {
isValid: false,
resourceName: core.name,
resourceType: core.type,
schemaValidationErrors: this.schemaValidator?.errors ?? [],
}
}
}
let isValid = true;
let customValidationErrorMessage;
try {
await this.resource.validate(parameters);
} catch (error) {
isValid = false;
customValidationErrorMessage = (error as Error).message;
}
if (!isValid) {
return {
customValidationErrorMessage,
isValid: false,
resourceName: core.name,
resourceType: core.type,
schemaValidationErrors: this.schemaValidator?.errors ?? [],
}
}
return {
isValid: true,
resourceName: core.name,
resourceType: core.type,
schemaValidationErrors: [],
}
}
async match(resource: ResourceJson, array: Array<ResourceJson>): Promise<ResourceJson | undefined> {
if (resource.core.type !== this.typeId) {
throw new Error(`Unknown type passed into match method: ${resource.core.type} for ${this.typeId}`);
}
if (!this.parsedSettings.allowMultiple) {
return array.find((r) => r.core.type === resource.core.type)
}
const { name, type } = resource.core;
const parameterMatcher = this.parsedSettings.matcher;
for (const resourceToMatch of array) {
if (type !== resourceToMatch.core.type) {
return undefined;
}
// If the user specifies the same name for the resource and it's not auto-generated (a number) then it's the same resource
if (name === resourceToMatch.core.name
&& name
&& Number.isInteger(Number.parseInt(name, 10))
) {
return resourceToMatch;
}
const originalParams = structuredClone(resource.parameters) as Partial<T>;
const paramsToMatch = structuredClone(resourceToMatch.parameters) as Partial<T>;
this.addDefaultValues(originalParams);
await this.applyTransformations(originalParams);
this.addDefaultValues(paramsToMatch);
await this.applyTransformations(paramsToMatch);
const match = parameterMatcher(originalParams, paramsToMatch);
if (match) {
return resourceToMatch;
}
}
}
async plan(
core: ResourceConfig,
desired: Partial<T> | null,
state: Partial<T> | null,
isStateful = false,
commandType = 'plan',
): Promise<Plan<T>> {
this.validatePlanInputs(core, desired, state, isStateful);
const context: RefreshContext<T> = {
commandType: commandType as 'plan' | 'validationPlan',
isStateful,
originalDesiredConfig: structuredClone(desired),
};
this.addDefaultValues(desired);
await this.applyTransformations(desired);
this.addDefaultValues(state);
await this.applyTransformations(state);
// Parse data from the user supplied config
const parsedConfig = new ConfigParser(desired, state, this.parsedSettings.statefulParameters)
const {
allParameters,
allNonStatefulParameters,
allStatefulParameters,
} = parsedConfig;
// Refresh resource parameters. This refreshes the parameters that configure the resource itself
const currentArray = await this.refreshNonStatefulParameters(allNonStatefulParameters, context);
// Short circuit here. If the resource is non-existent, there's no point checking stateful parameters
if (currentArray === null
|| currentArray === undefined
|| currentArray.length === 0
|| currentArray.filter(Boolean).length === 0
) {
return Plan.calculate({
desired,
currentArray,
state,
core,
settings: this.parsedSettings,
isStateful,
});
}
// Refresh stateful parameters. These parameters have state external to the resource. Each variation of the
// current parameters (each array element) is passed into the stateful parameter refresh.
const statefulCurrentParameters = await this.refreshStatefulParameters(allStatefulParameters, currentArray, allParameters);
return Plan.calculate({
desired,
currentArray: currentArray.map((c, idx) => ({ ...c, ...statefulCurrentParameters[idx] })),
state,
core,
settings: this.parsedSettings,
isStateful
})
}
async planDestroy(
core: ResourceConfig,
parameters: Partial<T>
): Promise<Plan<T>> {
this.addDefaultValues(parameters);
await this.applyTransformations(parameters);
// Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
const parametersToRefresh = this.settings.importAndDestroy?.refreshKeys
? {
...Object.fromEntries(
this.settings.importAndDestroy?.refreshKeys.map((k) => [k, null])
),
...this.settings.importAndDestroy?.defaultRefreshValues,
...parameters,
}
: {
...Object.fromEntries(
this.getAllParameterKeys().map((k) => [k, null])
),
...this.settings.importAndDestroy?.defaultRefreshValues,
...parameters,
};
return this.plan(core, null, parametersToRefresh, true);
}
async apply(plan: Plan<T>): Promise<void> {
if (plan.getResourceType() !== this.typeId) {
throw new Error(`Internal error: Plan set to wrong resource during apply. Expected ${this.typeId} but got: ${plan.getResourceType()}`);
}
switch (plan.changeSet.operation) {
case ResourceOperation.CREATE: {
return this.applyCreate(plan);
}
case ResourceOperation.MODIFY: {
return this.applyModify(plan);
}
case ResourceOperation.RECREATE: {
await this.applyDestroy(plan);
return this.applyCreate(plan);
}
case ResourceOperation.DESTROY: {
return this.applyDestroy(plan);
}
}
}
async import(
core: ResourceConfig,
parameters: Partial<T>,
autoSearchAll = false,
): Promise<Array<ResourceJson> | null> {
if (this.settings.importAndDestroy?.preventImport) {
throw new Error(`Type: ${this.typeId} cannot be imported`);
}
const context: RefreshContext<T> = {
commandType: 'import',
isStateful: true,
originalDesiredConfig: structuredClone(parameters),
};
// Auto search means that no required parameters will be provided. We will try to generate it ourselves or return an
// empty array if they can't be.
if (autoSearchAll && this.settings.allowMultiple) {
if (this.settings.allowMultiple === true || !this.settings.allowMultiple.findAllParameters?.()) {
return [];
}
const parametersToImport = await this.settings.allowMultiple.findAllParameters?.();
const results = await Promise.all(parametersToImport.map((p) =>
this.import(core, p).catch(() => null))
);
return results.filter(Boolean).flat() as ResourceJson[];
}
this.addDefaultValues(parameters);
await this.applyTransformations(parameters);
// Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
const parametersToRefresh = this.getParametersToRefreshForImport(parameters, context);
// Parse data from the user supplied config
const parsedConfig = new ConfigParser(parametersToRefresh, null, this.parsedSettings.statefulParameters)
const {
allParameters,
allNonStatefulParameters,
allStatefulParameters,
} = parsedConfig;
const currentParametersArray = await this.refreshNonStatefulParameters(allNonStatefulParameters, context);
if (currentParametersArray === null
|| currentParametersArray === undefined
|| currentParametersArray.filter(Boolean).length === 0
) {
return [];
}
const statefulCurrentParameters = await this.refreshStatefulParameters(allStatefulParameters, currentParametersArray, allParameters);
const resultParametersArray = currentParametersArray
?.map((r, idx) => ({ ...r, ...statefulCurrentParameters[idx] }))
for (const result of resultParametersArray) {
await this.applyTransformations(result, { original: context.originalDesiredConfig });
this.removeDefaultValues(result, parameters);
}
return resultParametersArray?.map((r) => ({ core, parameters: r }))
}
private async applyCreate(plan: Plan<T>): Promise<void> {
await this.resource.create(plan as CreatePlan<T>);
const statefulParameterChanges = this.getSortedStatefulParameterChanges(plan.changeSet.parameterChanges)
for (const parameterChange of statefulParameterChanges) {
const statefulParameter = this.parsedSettings.statefulParameters.get(parameterChange.name)!;
await statefulParameter.add(parameterChange.newValue, plan);
}
}
private async applyModify(plan: Plan<T>): Promise<void> {
const parameterChanges = plan
.changeSet
.parameterChanges
.filter((c: ParameterChange<T>) => c.operation !== ParameterOperation.NOOP);
const statelessParameterChanges = parameterChanges
.filter((pc: ParameterChange<T>) => !this.parsedSettings.statefulParameters.has(pc.name))
for (const pc of statelessParameterChanges) {
await this.resource.modify(pc, plan as ModifyPlan<T>);
}
const statefulParameterChanges = this.getSortedStatefulParameterChanges(plan.changeSet.parameterChanges)
for (const parameterChange of statefulParameterChanges) {
const statefulParameter = this.parsedSettings.statefulParameters.get(parameterChange.name)!;
switch (parameterChange.operation) {
case ParameterOperation.ADD: {
await statefulParameter.add(parameterChange.newValue, plan);
break;
}
case ParameterOperation.MODIFY: {
await statefulParameter.modify(parameterChange.newValue, parameterChange.previousValue, plan);
break;
}
case ParameterOperation.REMOVE: {
await statefulParameter.remove(parameterChange.previousValue, plan);
break;
}
}
}
}
private async applyDestroy(plan: Plan<T>): Promise<void> {
// If this option is set (defaults to false), then stateful parameters need to be destroyed
// as well. This means that the stateful parameter wouldn't have been normally destroyed with applyDestroy()
if (this.settings.removeStatefulParametersBeforeDestroy) {
const statefulParameterChanges = this.getSortedStatefulParameterChanges(plan.changeSet.parameterChanges)
for (const parameterChange of statefulParameterChanges) {
const statefulParameter = this.parsedSettings.statefulParameters.get(parameterChange.name)!;
await statefulParameter.remove(parameterChange.previousValue, plan);
}
}
await this.resource.destroy(plan as DestroyPlan<T>);
}
private validateRefreshResults(refresh: Array<Partial<T>> | null) {
if (!refresh) {
return;
}
if (!this.settings.allowMultiple && refresh.length > 1) {
throw new Error(`Resource: ${this.settings.id}. Allow multiple was set to false but multiple refresh results were returned.
${JSON.stringify(refresh, null, 2)}
`)
}
}
private async applyTransformations(config: Partial<T> | null, reverse?: {
original: Partial<T> | null
}, skipConfigTransformation = false): Promise<void> {
if (!config) {
return;
}
for (const [key, inputTransformation] of Object.entries(this.parsedSettings.inputTransformations)) {
if (config[key] === undefined || !inputTransformation) {
continue;
}
(config as Record<string, unknown>)[key] = reverse
? await inputTransformation.from(config[key], reverse.original?.[key])
: await inputTransformation.to(config[key]);
}
if (this.settings.transformation && !skipConfigTransformation) {
const transformed = reverse
? await this.settings.transformation.from({ ...config }, reverse.original)
: await this.settings.transformation.to({ ...config })
Object.keys(config).forEach((k) => delete config[k])
Object.assign(config, transformed);
}
}
private addDefaultValues(config: Partial<T> | null): void {
if (!config) {
return;
}
for (const [key, defaultValue] of Object.entries(this.parsedSettings.defaultValues)) {
if (defaultValue !== undefined && (config[key] === undefined || config[key] === null)) {
(config as Record<string, unknown>)[key] = defaultValue;
}
}
}
private removeDefaultValues(newConfig: Partial<T> | null, originalConfig: Partial<T>): void {
if (!newConfig) {
return;
}
for (const [key, defaultValue] of Object.entries(this.parsedSettings.defaultValues)) {
if (defaultValue !== undefined && (newConfig[key] === defaultValue || originalConfig[key] === undefined || originalConfig[key] === null)) {
delete newConfig[key];
}
}
}
private async refreshNonStatefulParameters(resourceParameters: Partial<T>, context: RefreshContext<T>): Promise<Array<Partial<T>> | null> {
const result = await this.resource.refresh(resourceParameters, context);
const currentParametersArray = Array.isArray(result) || result === null
? result
: [result]
this.validateRefreshResults(currentParametersArray);
return currentParametersArray;
}
// Refresh stateful parameters
// This refreshes parameters that are stateful (they can be added, deleted separately from the resource)
private async refreshStatefulParameters(
statefulParametersConfig: Partial<T>,
currentArray: Array<Partial<T>>,
allParameters: Partial<T>
): Promise<Array<Partial<T>>> {
const result: Array<Partial<T>> = Array.from({ length: currentArray.length }, () => ({}))
const sortedEntries = Object.entries(statefulParametersConfig)
.sort(
([key1], [key2]) => this.parsedSettings.statefulParameterOrder.get(key1)! - this.parsedSettings.statefulParameterOrder.get(key2)!
)
for (const [idx, refreshedParams] of currentArray.entries()) {
await Promise.all(sortedEntries.map(async ([key, desiredValue]) => {
const statefulParameter = this.parsedSettings.statefulParameters.get(key);
if (!statefulParameter) {
throw new Error(`Stateful parameter ${key} was not found`);
}
(result[idx][key] as T[keyof T] | null) = await statefulParameter.refresh(desiredValue ?? null, { ...allParameters, ...refreshedParams })
}))
}
return result;
}
private validatePlanInputs(
core: ResourceConfig,
desired: Partial<T> | null,
current: Partial<T> | null,
isStateful: boolean,
) {
if (!core || !core.type) {
throw new Error('Core parameters type must be defined');
}
if (!desired && !current) {
throw new Error('Desired config and current config cannot both be missing')
}
if (!isStateful && !desired) {
throw new Error('Desired config must be provided in non-stateful mode')
}
}
private getSortedStatefulParameterChanges(parameterChanges: ParameterChange<T>[]) {
return parameterChanges
.filter((pc: ParameterChange<T>) => this.parsedSettings.statefulParameters.has(pc.name))
.sort((a, b) =>
this.parsedSettings.statefulParameterOrder.get(a.name)! - this.parsedSettings.statefulParameterOrder.get(b.name)!
)
}
private getAllParameterKeys(): string[] {
return this.settings.schema
? Object.keys((this.settings.schema as any)?.properties)
: Object.keys(this.parsedSettings.parameterSettings);
}
private getParametersToRefreshForImport(parameters: Partial<T>, context: RefreshContext<T>): Partial<T> {
if (this.settings.importAndDestroy?.refreshMapper) {
return this.settings.importAndDestroy?.refreshMapper(parameters, context);
}
return this.settings.importAndDestroy?.refreshKeys
? {
...Object.fromEntries(
this.settings.importAndDestroy?.refreshKeys.map((k) => [k, null])
),
...this.settings.importAndDestroy?.defaultRefreshValues,
...parameters,
...(Object.fromEntries( // If a default value was used, but it was also declared in the defaultRefreshValues, prefer the defaultRefreshValue instead
Object.entries(parameters).filter(([k, v]) =>
this.parsedSettings.defaultValues[k] !== undefined
&& v === this.parsedSettings.defaultValues[k]
&& context.originalDesiredConfig?.[k] === undefined
&& this.settings.importAndDestroy?.defaultRefreshValues?.[k] !== undefined
).map(([k]) => [k, this.settings.importAndDestroy!.defaultRefreshValues![k]])
))
}
: {
...Object.fromEntries(
this.getAllParameterKeys().map((k) => [k, null])
),
...this.settings.importAndDestroy?.defaultRefreshValues,
...parameters,
};
}
}