Skip to content

Commit de7fdb9

Browse files
committed
Updated codify schemas version to the latest. Updated to the latest plan schema.
1 parent f92dbee commit de7fdb9

3 files changed

Lines changed: 44 additions & 36 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-lib",
3-
"version": "1.0.73",
3+
"version": "1.0.75",
44
"description": "",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"ajv": "^8.12.0",
1616
"ajv-formats": "^2.1.1",
17-
"codify-schemas": "1.0.42",
17+
"codify-schemas": "1.0.44",
1818
"@npmcli/promise-spawn": "^7.0.1"
1919
},
2020
"devDependencies": {

src/entities/plugin.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Resource } from './resource.js';
21
import {
32
ApplyRequestData,
43
InitializeResponseData,
@@ -8,8 +7,10 @@ import {
87
ValidateRequestData,
98
ValidateResponseData
109
} from 'codify-schemas';
11-
import { Plan } from './plan.js';
10+
1211
import { splitUserConfig } from '../utils/utils.js';
12+
import { Plan } from './plan.js';
13+
import { Resource } from './resource.js';
1314

1415
export class Plugin {
1516
planStorage: Map<string, Plan<any>>;
@@ -37,8 +38,8 @@ export class Plugin {
3738
return {
3839
resourceDefinitions: [...this.resources.values()]
3940
.map((r) => ({
40-
type: r.typeId,
4141
dependencies: r.dependencies,
42+
type: r.typeId,
4243
}))
4344
}
4445
}
@@ -65,19 +66,25 @@ export class Plugin {
6566
}
6667

6768
async plan(data: PlanRequestData): Promise<PlanResponseData> {
68-
if (!this.resources.has(data.type)) {
69-
throw new Error(`Resource type not found: ${data.type}`);
69+
const type = data.desired?.type ?? data.state?.type
70+
71+
if (!type || !this.resources.has(type)) {
72+
throw new Error(`Resource type not found: ${type}`);
7073
}
7174

72-
const plan = await this.resources.get(data.type)!.plan(data);
75+
const plan = await this.resources.get(type)!.plan(
76+
data.desired ?? null,
77+
data.state ?? null,
78+
data.isStateful
79+
);
7380
this.planStorage.set(plan.id, plan);
7481

7582
return plan.toResponse();
7683
}
7784

7885
async apply(data: ApplyRequestData): Promise<void> {
7986
if (!data.planId && !data.plan) {
80-
throw new Error(`For applies either plan or planId must be supplied`);
87+
throw new Error('For applies either plan or planId must be supplied');
8188
}
8289

8390
const plan = this.resolvePlan(data);
@@ -91,7 +98,7 @@ export class Plugin {
9198
}
9299

93100
private resolvePlan(data: ApplyRequestData): Plan<ResourceConfig> {
94-
const { planId, plan: planRequest } = data;
101+
const { plan: planRequest, planId } = data;
95102

96103
if (planId) {
97104
if (!this.planStorage.has(planId)) {
@@ -106,7 +113,7 @@ export class Plugin {
106113
}
107114

108115
const resource = this.resources.get(planRequest.resourceType)!;
109-
return Plan.fromResponse(data.plan, resource.defaultValues);
116+
return Plan.fromResponse(planRequest, resource.defaultValues);
110117
}
111118

112119
protected async crossValidateResources(configs: ResourceConfig[]): Promise<void> {}

src/messages/handlers.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from '../entities/plugin.js';
1+
import Ajv2020, { SchemaObject, ValidateFunction } from 'ajv/dist/2020.js';
22
import addFormats from 'ajv-formats';
33
import {
44
ApplyRequestDataSchema,
@@ -14,32 +14,33 @@ import {
1414
ValidateRequestDataSchema,
1515
ValidateResponseDataSchema
1616
} from 'codify-schemas';
17-
import Ajv2020, { SchemaObject, ValidateFunction } from 'ajv/dist/2020.js';
17+
1818
import { SudoError } from '../entities/errors.js';
19+
import { Plugin } from '../entities/plugin.js';
1920

20-
const SupportedRequests: Record<string, { requestValidator: SchemaObject; responseValidator: SchemaObject; handler: (plugin: Plugin, data: any) => Promise<unknown> }> = {
21+
const SupportedRequests: Record<string, { handler: (plugin: Plugin, data: any) => Promise<unknown>; requestValidator: SchemaObject; responseValidator: SchemaObject }> = {
22+
'apply': {
23+
async handler(plugin: Plugin, data: any) {
24+
await plugin.apply(data);
25+
return null;
26+
},
27+
requestValidator: ApplyRequestDataSchema,
28+
responseValidator: ApplyResponseDataSchema
29+
},
2130
'initialize': {
31+
handler: async (plugin: Plugin) => plugin.initialize(),
2232
requestValidator: InitializeRequestDataSchema,
23-
responseValidator: InitializeResponseDataSchema,
24-
handler: async (plugin: Plugin) => plugin.initialize()
25-
},
26-
'validate': {
27-
requestValidator: ValidateRequestDataSchema,
28-
responseValidator: ValidateResponseDataSchema,
29-
handler: async (plugin: Plugin, data: any) => plugin.validate(data)
33+
responseValidator: InitializeResponseDataSchema
3034
},
3135
'plan': {
36+
handler: async (plugin: Plugin, data: any) => plugin.plan(data),
3237
requestValidator: PlanRequestDataSchema,
33-
responseValidator: PlanResponseDataSchema,
34-
handler: async (plugin: Plugin, data: any) => plugin.plan(data)
38+
responseValidator: PlanResponseDataSchema
3539
},
36-
'apply': {
37-
requestValidator: ApplyRequestDataSchema,
38-
responseValidator: ApplyResponseDataSchema,
39-
handler: async (plugin: Plugin, data: any) => {
40-
await plugin.apply(data);
41-
return null;
42-
}
40+
'validate': {
41+
handler: async (plugin: Plugin, data: any) => plugin.validate(data),
42+
requestValidator: ValidateRequestDataSchema,
43+
responseValidator: ValidateResponseDataSchema
4344
}
4445
}
4546

@@ -51,7 +52,7 @@ export class MessageHandler {
5152
private responseValidators: Map<string, ValidateFunction>;
5253

5354
constructor(plugin: Plugin) {
54-
this.ajv = new Ajv2020.default({ strict: true });
55+
this.ajv = new Ajv2020.default({ strict: true, strictRequired: false });
5556
addFormats.default(this.ajv);
5657
this.ajv.addSchema(ResourceSchema);
5758
this.plugin = plugin;
@@ -91,12 +92,12 @@ export class MessageHandler {
9192

9293
process.send!({
9394
cmd: message.cmd + '_Response',
94-
status: MessageStatus.SUCCESS,
9595
data: result,
96+
status: MessageStatus.SUCCESS,
9697
})
9798

98-
} catch (e: unknown) {
99-
this.handleErrors(message, e as Error);
99+
} catch (error: unknown) {
100+
this.handleErrors(message, error as Error);
100101
}
101102
}
102103

@@ -119,17 +120,17 @@ export class MessageHandler {
119120
if (e instanceof SudoError) {
120121
return process.send?.({
121122
cmd,
122-
status: MessageStatus.ERROR,
123123
data: `Plugin: '${this.plugin.name}'. Forbidden usage of sudo for command '${e.command}'. Please contact the plugin developer to fix this.`,
124+
status: MessageStatus.ERROR,
124125
})
125126
}
126127

127128
const isDebug = process.env.DEBUG?.includes('*') ?? false;
128129

129130
process.send?.({
130131
cmd,
131-
status: MessageStatus.ERROR,
132132
data: isDebug ? e.stack : e.message,
133+
status: MessageStatus.ERROR,
133134
})
134135
}
135136
}

0 commit comments

Comments
 (0)