codify-plugin-lib 1.0.9 → 1.0.11
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/entities/plugin.d.ts +2 -1
- package/dist/entities/plugin.js +10 -0
- package/dist/entities/resource.d.ts +1 -1
- package/dist/entities/resource.js +6 -15
- package/dist/messages/handlers.js +4 -1
- package/package.json +1 -1
- package/src/entities/plugin.ts +14 -1
- package/src/entities/resource.test.ts +37 -0
- package/src/entities/resource.ts +7 -9
- package/src/messages/handlers.ts +5 -2
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Resource } from './resource';
|
|
2
2
|
import { ApplyRequestData, PlanRequestData, PlanResponseData, ResourceConfig, ValidateRequestData, ValidateResponseData } from 'codify-schemas';
|
|
3
|
+
import { Plan } from './plan';
|
|
3
4
|
export declare class Plugin {
|
|
4
5
|
resources: Map<string, Resource<ResourceConfig>>;
|
|
5
|
-
planStorage: Map<string,
|
|
6
|
+
planStorage: Map<string, Plan>;
|
|
6
7
|
constructor(resources: Map<string, Resource<ResourceConfig>>);
|
|
7
8
|
onInitialize(): Promise<void>;
|
|
8
9
|
validate(data: ValidateRequestData): Promise<ValidateResponseData>;
|
package/dist/entities/plugin.js
CHANGED
|
@@ -29,6 +29,16 @@ class Plugin {
|
|
|
29
29
|
return plan.toResponse();
|
|
30
30
|
}
|
|
31
31
|
async apply(data) {
|
|
32
|
+
const { planId } = data;
|
|
33
|
+
const plan = this.planStorage.get(planId);
|
|
34
|
+
if (!plan) {
|
|
35
|
+
throw new Error(`Plan with id: ${planId} was not found`);
|
|
36
|
+
}
|
|
37
|
+
const resource = this.resources.get(plan.getResourceType());
|
|
38
|
+
if (!resource) {
|
|
39
|
+
throw new Error('Malformed plan with resource that cannot be found');
|
|
40
|
+
}
|
|
41
|
+
await resource.apply(plan);
|
|
32
42
|
}
|
|
33
43
|
async crossValidateResources(configs) { }
|
|
34
44
|
}
|
|
@@ -7,7 +7,7 @@ export declare abstract class Resource<T extends ResourceConfig> {
|
|
|
7
7
|
abstract getTypeId(): string;
|
|
8
8
|
onInitialize(): Promise<void>;
|
|
9
9
|
plan(desiredConfig: T): Promise<Plan>;
|
|
10
|
-
apply(plan: Plan): Promise<
|
|
10
|
+
apply(plan: Plan): Promise<void>;
|
|
11
11
|
abstract validate(config: ResourceConfig): Promise<boolean>;
|
|
12
12
|
abstract getCurrentConfig(): Promise<T | null>;
|
|
13
13
|
abstract calculateOperation(change: ParameterChange): ResourceOperation.MODIFY | ResourceOperation.RECREATE;
|
|
@@ -26,25 +26,16 @@ class Resource {
|
|
|
26
26
|
return plan_1.Plan.create(new change_set_1.ChangeSet(resourceOperation, parameterChangeSet), desiredConfig);
|
|
27
27
|
}
|
|
28
28
|
async apply(plan) {
|
|
29
|
-
if (plan.getResourceType()) {
|
|
30
|
-
throw new Error(
|
|
29
|
+
if (plan.getResourceType() !== this.getTypeId()) {
|
|
30
|
+
throw new Error(`Internal error: Plan set to wrong resource during apply. Expected ${this.getTypeId()} but got: ${plan.getResourceType()}`);
|
|
31
31
|
}
|
|
32
32
|
const changeSet = plan.changeSet;
|
|
33
33
|
switch (plan.changeSet.operation) {
|
|
34
|
-
case codify_schemas_1.ResourceOperation.CREATE:
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
case codify_schemas_1.ResourceOperation.
|
|
38
|
-
await this.applyModify(changeSet);
|
|
39
|
-
break;
|
|
40
|
-
case codify_schemas_1.ResourceOperation.RECREATE:
|
|
41
|
-
await this.applyRecreate(changeSet);
|
|
42
|
-
break;
|
|
43
|
-
case codify_schemas_1.ResourceOperation.DESTROY:
|
|
44
|
-
await this.applyDestroy(changeSet);
|
|
45
|
-
break;
|
|
34
|
+
case codify_schemas_1.ResourceOperation.CREATE: return this.applyCreate(changeSet);
|
|
35
|
+
case codify_schemas_1.ResourceOperation.MODIFY: return this.applyModify(changeSet);
|
|
36
|
+
case codify_schemas_1.ResourceOperation.RECREATE: return this.applyRecreate(changeSet);
|
|
37
|
+
case codify_schemas_1.ResourceOperation.DESTROY: return this.applyDestroy(changeSet);
|
|
46
38
|
}
|
|
47
|
-
return null;
|
|
48
39
|
}
|
|
49
40
|
}
|
|
50
41
|
exports.Resource = Resource;
|
|
@@ -21,7 +21,10 @@ const SupportedRequests = {
|
|
|
21
21
|
'apply': {
|
|
22
22
|
requestValidator: codify_schemas_1.ApplyRequestDataSchema,
|
|
23
23
|
responseValidator: codify_schemas_1.ApplyResponseDataSchema,
|
|
24
|
-
handler: async (plugin, data) =>
|
|
24
|
+
handler: async (plugin, data) => {
|
|
25
|
+
await plugin.apply(data);
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
25
28
|
}
|
|
26
29
|
};
|
|
27
30
|
class MessageHandler {
|
package/package.json
CHANGED
package/src/entities/plugin.ts
CHANGED
|
@@ -7,9 +7,10 @@ import {
|
|
|
7
7
|
ValidateRequestData,
|
|
8
8
|
ValidateResponseData
|
|
9
9
|
} from 'codify-schemas';
|
|
10
|
+
import { Plan } from './plan';
|
|
10
11
|
|
|
11
12
|
export class Plugin {
|
|
12
|
-
planStorage: Map<string,
|
|
13
|
+
planStorage: Map<string, Plan>;
|
|
13
14
|
|
|
14
15
|
constructor(
|
|
15
16
|
public resources: Map<string, Resource<ResourceConfig>>
|
|
@@ -46,6 +47,18 @@ export class Plugin {
|
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
async apply(data: ApplyRequestData): Promise<void> {
|
|
50
|
+
const { planId } = data;
|
|
51
|
+
const plan = this.planStorage.get(planId);
|
|
52
|
+
if (!plan) {
|
|
53
|
+
throw new Error(`Plan with id: ${planId} was not found`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const resource = this.resources.get(plan.getResourceType());
|
|
57
|
+
if (!resource) {
|
|
58
|
+
throw new Error('Malformed plan with resource that cannot be found');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
await resource.apply(plan);
|
|
49
62
|
}
|
|
50
63
|
|
|
51
64
|
protected async crossValidateResources(configs: ResourceConfig[]): Promise<void> {}
|
|
@@ -4,6 +4,7 @@ import { ResourceConfig, ResourceOperation } from 'codify-schemas';
|
|
|
4
4
|
import { ChangeSet, ParameterChange } from './change-set';
|
|
5
5
|
import { spy } from 'sinon';
|
|
6
6
|
import { expect } from 'chai';
|
|
7
|
+
import { Plan } from './plan';
|
|
7
8
|
|
|
8
9
|
class TestResource extends Resource<TestConfig> {
|
|
9
10
|
applyCreate(changeSet: ChangeSet): Promise<void> {
|
|
@@ -143,4 +144,40 @@ describe('Resource tests', () => {
|
|
|
143
144
|
expect(result.changeSet.operation).to.eq(ResourceOperation.CREATE);
|
|
144
145
|
expect(result.changeSet.parameterChanges.length).to.eq(3);
|
|
145
146
|
})
|
|
147
|
+
|
|
148
|
+
it('chooses the create apply properly', async () => {
|
|
149
|
+
const resource = new class extends TestResource {
|
|
150
|
+
getTypeId(): string {
|
|
151
|
+
return 'resource'
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const resourceSpy = spy(resource);
|
|
156
|
+
const result = await resourceSpy.apply(
|
|
157
|
+
Plan.create(
|
|
158
|
+
new ChangeSet(ResourceOperation.CREATE, []),
|
|
159
|
+
{ type: 'resource' }
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
expect(resourceSpy.applyCreate.calledOnce).to.be.true;
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('chooses the destroy apply properly', async () => {
|
|
167
|
+
const resource = new class extends TestResource {
|
|
168
|
+
getTypeId(): string {
|
|
169
|
+
return 'resource'
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const resourceSpy = spy(resource);
|
|
174
|
+
const result = await resourceSpy.apply(
|
|
175
|
+
Plan.create(
|
|
176
|
+
new ChangeSet(ResourceOperation.DESTROY, []),
|
|
177
|
+
{ type: 'resource' }
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
expect(resourceSpy.applyDestroy.calledOnce).to.be.true;
|
|
182
|
+
})
|
|
146
183
|
})
|
package/src/entities/resource.ts
CHANGED
|
@@ -42,20 +42,18 @@ export abstract class Resource<T extends ResourceConfig> {
|
|
|
42
42
|
);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
async apply(plan: Plan): Promise<
|
|
46
|
-
if (plan.getResourceType()) {
|
|
47
|
-
throw new Error(
|
|
45
|
+
async apply(plan: Plan): Promise<void> {
|
|
46
|
+
if (plan.getResourceType() !== this.getTypeId()) {
|
|
47
|
+
throw new Error(`Internal error: Plan set to wrong resource during apply. Expected ${this.getTypeId()} but got: ${plan.getResourceType()}`);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
const changeSet = plan.changeSet;
|
|
51
51
|
switch (plan.changeSet.operation) {
|
|
52
|
-
case ResourceOperation.CREATE:
|
|
53
|
-
case ResourceOperation.MODIFY:
|
|
54
|
-
case ResourceOperation.RECREATE:
|
|
55
|
-
case ResourceOperation.DESTROY:
|
|
52
|
+
case ResourceOperation.CREATE: return this.applyCreate(changeSet);
|
|
53
|
+
case ResourceOperation.MODIFY: return this.applyModify(changeSet);
|
|
54
|
+
case ResourceOperation.RECREATE: return this.applyRecreate(changeSet);
|
|
55
|
+
case ResourceOperation.DESTROY: return this.applyDestroy(changeSet);
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
return null;
|
|
59
57
|
}
|
|
60
58
|
|
|
61
59
|
abstract validate(config: ResourceConfig): Promise<boolean>;
|
package/src/messages/handlers.ts
CHANGED
|
@@ -26,8 +26,11 @@ const SupportedRequests: Record<string, { requestValidator: SchemaObject; respon
|
|
|
26
26
|
},
|
|
27
27
|
'apply': {
|
|
28
28
|
requestValidator: ApplyRequestDataSchema,
|
|
29
|
-
responseValidator: ApplyResponseDataSchema,
|
|
30
|
-
handler: async (plugin: Plugin, data: any) =>
|
|
29
|
+
responseValidator: ApplyResponseDataSchema,
|
|
30
|
+
handler: async (plugin: Plugin, data: any) => {
|
|
31
|
+
await plugin.apply(data);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
31
34
|
}
|
|
32
35
|
}
|
|
33
36
|
|