codify-plugin-lib 1.0.90 → 1.0.92
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.
- package/dist/messages/handlers.js +6 -1
- package/dist/plugin/plugin.d.ts +2 -1
- package/dist/plugin/plugin.js +12 -0
- package/dist/resource/resource-controller.d.ts +2 -0
- package/dist/resource/resource-controller.js +29 -0
- package/package.json +2 -2
- package/src/messages/handlers.ts +6 -1
- package/src/plugin/plugin.ts +17 -0
- package/src/resource/resource-controller.ts +42 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Ajv } from 'ajv';
|
|
2
2
|
import addFormats from 'ajv-formats';
|
|
3
|
-
import { ApplyRequestDataSchema, ApplyResponseDataSchema, GetResourceInfoRequestDataSchema, GetResourceInfoResponseDataSchema, InitializeRequestDataSchema, InitializeResponseDataSchema, IpcMessageSchema, MessageStatus, PlanRequestDataSchema, PlanResponseDataSchema, ResourceSchema, ValidateRequestDataSchema, ValidateResponseDataSchema } from 'codify-schemas';
|
|
3
|
+
import { ApplyRequestDataSchema, ApplyResponseDataSchema, GetResourceInfoRequestDataSchema, GetResourceInfoResponseDataSchema, ImportRequestDataSchema, ImportResponseDataSchema, InitializeRequestDataSchema, InitializeResponseDataSchema, IpcMessageSchema, MessageStatus, PlanRequestDataSchema, PlanResponseDataSchema, ResourceSchema, ValidateRequestDataSchema, ValidateResponseDataSchema } from 'codify-schemas';
|
|
4
4
|
import { SudoError } from '../errors.js';
|
|
5
5
|
const SupportedRequests = {
|
|
6
6
|
'initialize': {
|
|
@@ -18,6 +18,11 @@ const SupportedRequests = {
|
|
|
18
18
|
requestValidator: GetResourceInfoRequestDataSchema,
|
|
19
19
|
responseValidator: GetResourceInfoResponseDataSchema
|
|
20
20
|
},
|
|
21
|
+
'import': {
|
|
22
|
+
handler: async (plugin, data) => plugin.import(data),
|
|
23
|
+
requestValidator: ImportRequestDataSchema,
|
|
24
|
+
responseValidator: ImportResponseDataSchema
|
|
25
|
+
},
|
|
21
26
|
'plan': {
|
|
22
27
|
handler: async (plugin, data) => plugin.plan(data),
|
|
23
28
|
requestValidator: PlanRequestDataSchema,
|
package/dist/plugin/plugin.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApplyRequestData, GetResourceInfoRequestData, GetResourceInfoResponseData, InitializeResponseData, PlanRequestData, PlanResponseData, ResourceConfig, ValidateRequestData, ValidateResponseData } from 'codify-schemas';
|
|
1
|
+
import { ApplyRequestData, GetResourceInfoRequestData, GetResourceInfoResponseData, ImportRequestData, ImportResponseData, InitializeResponseData, PlanRequestData, PlanResponseData, ResourceConfig, ValidateRequestData, ValidateResponseData } from 'codify-schemas';
|
|
2
2
|
import { Plan } from '../plan/plan.js';
|
|
3
3
|
import { Resource } from '../resource/resource.js';
|
|
4
4
|
import { ResourceController } from '../resource/resource-controller.js';
|
|
@@ -10,6 +10,7 @@ export declare class Plugin {
|
|
|
10
10
|
static create(name: string, resources: Resource<any>[]): Plugin;
|
|
11
11
|
initialize(): Promise<InitializeResponseData>;
|
|
12
12
|
getResourceInfo(data: GetResourceInfoRequestData): Promise<GetResourceInfoResponseData>;
|
|
13
|
+
import(data: ImportRequestData): Promise<ImportResponseData>;
|
|
13
14
|
validate(data: ValidateRequestData): Promise<ValidateResponseData>;
|
|
14
15
|
plan(data: PlanRequestData): Promise<PlanResponseData>;
|
|
15
16
|
apply(data: ApplyRequestData): Promise<void>;
|
package/dist/plugin/plugin.js
CHANGED
|
@@ -39,6 +39,18 @@ export class Plugin {
|
|
|
39
39
|
schema: resource.settings.schema
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
+
async import(data) {
|
|
43
|
+
if (!this.resourceControllers.has(data.config.type)) {
|
|
44
|
+
throw new Error(`Cannot get info for resource ${data.config.type}, resource doesn't exist`);
|
|
45
|
+
}
|
|
46
|
+
const result = await this.resourceControllers
|
|
47
|
+
.get(data.config.type)
|
|
48
|
+
?.import(data.config);
|
|
49
|
+
return {
|
|
50
|
+
request: data.config,
|
|
51
|
+
result: result ?? [],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
42
54
|
async validate(data) {
|
|
43
55
|
const validationResults = [];
|
|
44
56
|
for (const config of data.configs) {
|
|
@@ -17,6 +17,7 @@ export declare class ResourceController<T extends StringIndexedObject> {
|
|
|
17
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
|
+
import(config: Partial<T> & ResourceConfig): Promise<(Partial<T> & ResourceConfig)[] | null>;
|
|
20
21
|
private applyCreate;
|
|
21
22
|
private applyModify;
|
|
22
23
|
private applyDestroy;
|
|
@@ -27,4 +28,5 @@ export declare class ResourceController<T extends StringIndexedObject> {
|
|
|
27
28
|
private refreshStatefulParameters;
|
|
28
29
|
private validatePlanInputs;
|
|
29
30
|
private getSortedStatefulParameterChanges;
|
|
31
|
+
private getAllParameterKeys;
|
|
30
32
|
}
|
|
@@ -130,6 +130,30 @@ export class ResourceController {
|
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
|
+
async import(config) {
|
|
134
|
+
this.addDefaultValues(config);
|
|
135
|
+
await this.applyTransformParameters(config);
|
|
136
|
+
// Try to refresh as many parameters as possible here
|
|
137
|
+
const parametersToRefresh = {
|
|
138
|
+
...Object.fromEntries(this.getAllParameterKeys().map((k) => [k, null])),
|
|
139
|
+
...config,
|
|
140
|
+
};
|
|
141
|
+
// Parse data from the user supplied config
|
|
142
|
+
const parsedConfig = new ConfigParser(parametersToRefresh, null, this.parsedSettings.statefulParameters);
|
|
143
|
+
const { allNonStatefulParameters, allStatefulParameters, coreParameters, } = parsedConfig;
|
|
144
|
+
const currentParametersArray = await this.refreshNonStatefulParameters(allNonStatefulParameters);
|
|
145
|
+
if (currentParametersArray === null
|
|
146
|
+
|| currentParametersArray === undefined
|
|
147
|
+
|| this.settings.allowMultiple // Stateful parameters are not supported currently if allowMultiple is true
|
|
148
|
+
|| currentParametersArray.length === 0
|
|
149
|
+
|| currentParametersArray.filter(Boolean).length === 0) {
|
|
150
|
+
return currentParametersArray
|
|
151
|
+
?.map((r) => ({ ...coreParameters, ...r }))
|
|
152
|
+
?? null;
|
|
153
|
+
}
|
|
154
|
+
const statefulCurrentParameters = await this.refreshStatefulParameters(allStatefulParameters, parametersToRefresh);
|
|
155
|
+
return [{ ...coreParameters, ...currentParametersArray[0], ...statefulCurrentParameters }];
|
|
156
|
+
}
|
|
133
157
|
async applyCreate(plan) {
|
|
134
158
|
await this.resource.create(plan);
|
|
135
159
|
const statefulParameterChanges = this.getSortedStatefulParameterChanges(plan.changeSet.parameterChanges);
|
|
@@ -253,4 +277,9 @@ ${JSON.stringify(refresh, null, 2)}
|
|
|
253
277
|
.filter((pc) => this.parsedSettings.statefulParameters.has(pc.name))
|
|
254
278
|
.sort((a, b) => this.parsedSettings.statefulParameterOrder.get(a.name) - this.parsedSettings.statefulParameterOrder.get(b.name));
|
|
255
279
|
}
|
|
280
|
+
getAllParameterKeys() {
|
|
281
|
+
return this.settings.schema
|
|
282
|
+
? Object.keys(this.settings.schema?.properties)
|
|
283
|
+
: Object.keys(this.parsedSettings.parameterSettings);
|
|
284
|
+
}
|
|
256
285
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codify-plugin-lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.92",
|
|
4
4
|
"description": "Library plugin library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"ajv": "^8.12.0",
|
|
16
16
|
"ajv-formats": "^2.1.1",
|
|
17
|
-
"codify-schemas": "1.0.
|
|
17
|
+
"codify-schemas": "1.0.51",
|
|
18
18
|
"@npmcli/promise-spawn": "^7.0.1",
|
|
19
19
|
"uuid": "^10.0.0"
|
|
20
20
|
},
|
package/src/messages/handlers.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
ApplyRequestDataSchema,
|
|
5
5
|
ApplyResponseDataSchema,
|
|
6
6
|
GetResourceInfoRequestDataSchema,
|
|
7
|
-
GetResourceInfoResponseDataSchema,
|
|
7
|
+
GetResourceInfoResponseDataSchema, ImportRequestDataSchema, ImportResponseDataSchema,
|
|
8
8
|
InitializeRequestDataSchema,
|
|
9
9
|
InitializeResponseDataSchema,
|
|
10
10
|
IpcMessage,
|
|
@@ -36,6 +36,11 @@ const SupportedRequests: Record<string, { handler: (plugin: Plugin, data: any) =
|
|
|
36
36
|
requestValidator: GetResourceInfoRequestDataSchema,
|
|
37
37
|
responseValidator: GetResourceInfoResponseDataSchema
|
|
38
38
|
},
|
|
39
|
+
'import': {
|
|
40
|
+
handler: async (plugin: Plugin, data: any) => plugin.import(data),
|
|
41
|
+
requestValidator: ImportRequestDataSchema,
|
|
42
|
+
responseValidator: ImportResponseDataSchema
|
|
43
|
+
},
|
|
39
44
|
'plan': {
|
|
40
45
|
handler: async (plugin: Plugin, data: any) => plugin.plan(data),
|
|
41
46
|
requestValidator: PlanRequestDataSchema,
|
package/src/plugin/plugin.ts
CHANGED
|
@@ -2,6 +2,8 @@ import {
|
|
|
2
2
|
ApplyRequestData,
|
|
3
3
|
GetResourceInfoRequestData,
|
|
4
4
|
GetResourceInfoResponseData,
|
|
5
|
+
ImportRequestData,
|
|
6
|
+
ImportResponseData,
|
|
5
7
|
InitializeResponseData,
|
|
6
8
|
PlanRequestData,
|
|
7
9
|
PlanResponseData,
|
|
@@ -64,6 +66,21 @@ export class Plugin {
|
|
|
64
66
|
}
|
|
65
67
|
}
|
|
66
68
|
|
|
69
|
+
async import(data: ImportRequestData): Promise<ImportResponseData> {
|
|
70
|
+
if (!this.resourceControllers.has(data.config.type)) {
|
|
71
|
+
throw new Error(`Cannot get info for resource ${data.config.type}, resource doesn't exist`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const result = await this.resourceControllers
|
|
75
|
+
.get(data.config.type!)
|
|
76
|
+
?.import(data.config);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
request: data.config,
|
|
80
|
+
result: result ?? [],
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
67
84
|
async validate(data: ValidateRequestData): Promise<ValidateResponseData> {
|
|
68
85
|
const validationResults = [];
|
|
69
86
|
for (const config of data.configs) {
|
|
@@ -185,6 +185,43 @@ export class ResourceController<T extends StringIndexedObject> {
|
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
async import(config: Partial<T> & ResourceConfig): Promise<(Partial<T> & ResourceConfig)[] | null> {
|
|
189
|
+
this.addDefaultValues(config);
|
|
190
|
+
await this.applyTransformParameters(config);
|
|
191
|
+
|
|
192
|
+
// Try to refresh as many parameters as possible here
|
|
193
|
+
const parametersToRefresh = {
|
|
194
|
+
...Object.fromEntries(
|
|
195
|
+
this.getAllParameterKeys().map((k) => [k, null])
|
|
196
|
+
),
|
|
197
|
+
...config,
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// Parse data from the user supplied config
|
|
201
|
+
const parsedConfig = new ConfigParser(parametersToRefresh, null, this.parsedSettings.statefulParameters)
|
|
202
|
+
const {
|
|
203
|
+
allNonStatefulParameters,
|
|
204
|
+
allStatefulParameters,
|
|
205
|
+
coreParameters,
|
|
206
|
+
} = parsedConfig;
|
|
207
|
+
|
|
208
|
+
const currentParametersArray = await this.refreshNonStatefulParameters(allNonStatefulParameters);
|
|
209
|
+
|
|
210
|
+
if (currentParametersArray === null
|
|
211
|
+
|| currentParametersArray === undefined
|
|
212
|
+
|| this.settings.allowMultiple // Stateful parameters are not supported currently if allowMultiple is true
|
|
213
|
+
|| currentParametersArray.length === 0
|
|
214
|
+
|| currentParametersArray.filter(Boolean).length === 0
|
|
215
|
+
) {
|
|
216
|
+
return currentParametersArray
|
|
217
|
+
?.map((r) => ({ ...coreParameters, ...r }))
|
|
218
|
+
?? null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const statefulCurrentParameters = await this.refreshStatefulParameters(allStatefulParameters, parametersToRefresh);
|
|
222
|
+
return [{ ...coreParameters, ...currentParametersArray[0], ...statefulCurrentParameters }];
|
|
223
|
+
}
|
|
224
|
+
|
|
188
225
|
private async applyCreate(plan: Plan<T>): Promise<void> {
|
|
189
226
|
await this.resource.create(plan as CreatePlan<T>);
|
|
190
227
|
|
|
@@ -349,5 +386,10 @@ ${JSON.stringify(refresh, null, 2)}
|
|
|
349
386
|
)
|
|
350
387
|
}
|
|
351
388
|
|
|
389
|
+
private getAllParameterKeys(): string[] {
|
|
390
|
+
return this.settings.schema
|
|
391
|
+
? Object.keys((this.settings.schema as any)?.properties)
|
|
392
|
+
: Object.keys(this.parsedSettings.parameterSettings);
|
|
393
|
+
}
|
|
352
394
|
}
|
|
353
395
|
|