codify-plugin-lib 1.0.9 → 1.0.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -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, any>;
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
  })
@@ -42,20 +42,18 @@ export abstract class Resource<T extends ResourceConfig> {
42
42
  );
43
43
  }
44
44
 
45
- async apply(plan: Plan): Promise<any> {
46
- if (plan.getResourceType()) {
47
- throw new Error('Internal error: Plan set to wrong resource during apply');
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: await this.applyCreate(changeSet); break;
53
- case ResourceOperation.MODIFY: await this.applyModify(changeSet); break;
54
- case ResourceOperation.RECREATE: await this.applyRecreate(changeSet); break;
55
- case ResourceOperation.DESTROY: await this.applyDestroy(changeSet); break;
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>;
@@ -26,8 +26,11 @@ const SupportedRequests: Record<string, { requestValidator: SchemaObject; respon
26
26
  },
27
27
  'apply': {
28
28
  requestValidator: ApplyRequestDataSchema,
29
- responseValidator: ApplyResponseDataSchema, // Replace with response validator
30
- handler: async (plugin: Plugin, data: any) => plugin.apply(data)
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