Skip to content

Commit 9ad376c

Browse files
committed
feat: Upgraded to latest schema which separates core and parameters
1 parent 91a26eb commit 9ad376c

6 files changed

Lines changed: 48 additions & 72 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"ajv": "^8.12.0",
1313
"ajv-formats": "^3.0.1",
1414
"chalk": "^5.3.0",
15-
"codify-schemas": "^1.0.60",
15+
"codify-schemas": "^1.0.61",
1616
"debug": "^4.3.4",
1717
"ink": "^5",
1818
"ink-form": "^2.0.1",

src/entities/plan-request.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/entities/project.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ValidateResponseData } from 'codify-schemas';
1+
import { ValidateResponseData, PlanRequestData } from 'codify-schemas';
22

33
import { PluginValidationError, PluginValidationErrorParams, TypeNotFoundError } from '../common/errors.js';
44
import { ctx } from '../events/context.js';
@@ -7,7 +7,6 @@ import { DependencyMap } from '../plugins/plugin-manager.js';
77
import { DependencyGraphResolver } from '../utils/dependency-graph-resolver.js';
88
import { groupBy } from '../utils/index.js';
99
import { ConfigBlock, ConfigType } from './config.js';
10-
import { PlanRequest } from './plan-request.js';
1110
import { ProjectConfig } from './project-config.js';
1211
import { ResourceConfig } from './resource-config.js';
1312

@@ -19,7 +18,7 @@ export class Project {
1918
path: string;
2019

2120
sourceMaps?: SourceMapCache;
22-
planRequestsCache?: Map<string, PlanRequest>
21+
planRequestsCache?: Map<string, PlanRequestData>
2322

2423
isDestroyProject = false;
2524

@@ -68,22 +67,32 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
6867
return this;
6968
}
7069

71-
getPlanRequest(id: string): PlanRequest | undefined {
70+
getPlanRequest(id: string): PlanRequestData | undefined {
7271
// One time build a cache for plan requests to make it more efficient
7372
if (!this.planRequestsCache) {
74-
const { resourceConfigs } = this
75-
const stateOnlyConfigs = this.stateConfigs?.filter((s) =>
73+
const { resourceConfigs, stateConfigs } = this
74+
const stateOnlyConfigs = stateConfigs?.filter((s) =>
7675
!resourceConfigs.some((r) => r.id === s.id)
7776
)
7877

7978
const inputRequests = [
80-
...this.resourceConfigs.map((r) => [
81-
r.id, new PlanRequest(
82-
this.isStateful(), r, this.stateConfigs?.find((r) => r.id)
83-
)
79+
...resourceConfigs.map((r) => [
80+
r.id,
81+
<PlanRequestData>{
82+
isStateful: this.isStateful(),
83+
core: r.toJson().core,
84+
desired: r.toJson().parameters,
85+
state: stateConfigs?.find((r) => r.id)?.parameters
86+
}
8487
] as const),
8588
...(stateOnlyConfigs?.map((s) => [
86-
s.id, new PlanRequest(this.isStateful(), undefined, s)
89+
s.id,
90+
<PlanRequestData>{
91+
isStateful: this.isStateful(),
92+
core: s.toJson().core,
93+
desired: undefined,
94+
state: s.toJson().parameters,
95+
}
8796
] as const) ?? [])
8897
]
8998

src/entities/resource-config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ResourceConfig as SchemaResourceConfig } from 'codify-schemas';
1+
import { ResourceJson, ResourceConfig as SchemaResourceConfig } from 'codify-schemas';
2+
23
import { ConfigBlock, ConfigType } from './config.js';
34

45
/** Resource JSON supported format
@@ -46,6 +47,16 @@ export class ResourceConfig implements ConfigBlock {
4647
return this.name ? `${this.type}.${this.name}` : this.type;
4748
}
4849

50+
toJson(): ResourceJson {
51+
return {
52+
core: {
53+
type: this.type,
54+
name: this.name,
55+
},
56+
parameters: this.parameters ?? {},
57+
}
58+
}
59+
4960
isSame(type: string, name?: string): boolean {
5061
const externalId = name ? `${type}.${name}` : type;
5162
return externalId === this.id;

src/plugins/plugin-manager.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import {
22
GetResourceInfoResponseData,
33
ImportResponseData,
4-
ResourceOperation,
5-
ResourceConfig as SchemaResourceConfig,
64
ValidateResponseData,
75
} from 'codify-schemas';
86

97
import { InternalError } from '../common/errors.js';
108
import { Plan, ResourcePlan } from '../entities/plan.js';
11-
import { PlanRequest } from '../entities/plan-request.js';
129
import { Project } from '../entities/project.js';
10+
import { ResourceConfig } from '../entities/resource-config.js';
1311
import { SubProcessName, ctx } from '../events/context.js';
14-
import { prettyFormatResourcePlan } from '../ui/plan-pretty-printer.js';
1512
import { groupBy } from '../utils/index.js';
1613
import { Plugin } from './plugin.js';
1714
import { PluginResolver } from './resolver.js';
@@ -69,7 +66,7 @@ export class PluginManager {
6966
return plugin.getResourceInfo(type);
7067
}
7168

72-
async importResource(config: SchemaResourceConfig): Promise<ImportResponseData> {
69+
async importResource(config: ResourceConfig): Promise<ImportResponseData> {
7370
const pluginName = this.resourceToPluginMapping.get(config.type);
7471
if (!pluginName) {
7572
throw new Error(`Unable to find plugin for resource: ${config.type}`);
@@ -80,7 +77,7 @@ export class PluginManager {
8077
throw new Error(`Unable to find plugin for resource ${config.type}`);
8178
}
8279

83-
return plugin.import(config);
80+
return plugin.import(config.toJson());
8481
}
8582

8683
async getPlan(project: Project): Promise<Plan> {
@@ -89,9 +86,9 @@ export class PluginManager {
8986
project.evaluationOrder!.map(async (id) => {
9087
const planRequest = project.getPlanRequest(id)!;
9188

92-
const pluginName = this.resourceToPluginMapping.get(planRequest.type);
89+
const pluginName = this.resourceToPluginMapping.get(planRequest.core.type);
9390
if (!pluginName) {
94-
throw new InternalError(`Unable to determine plugin for validated resource: ${planRequest.id}`);
91+
throw new InternalError(`Unable to determine plugin for validated resource: ${planRequest.core.type}`);
9592
}
9693

9794
const planResult = await this.plugins.get(pluginName)!.plan(planRequest);

src/plugins/plugin.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import {
55
ImportResponseDataSchema,
66
InitializeResponseData,
77
InitializeResponseDataSchema,
8+
PlanRequestData,
89
PlanResponseData,
910
PlanResponseDataSchema,
10-
ResourceConfig as SchemaResourceConfig,
11+
ResourceJson,
1112
ValidateResponseData,
1213
ValidateResponseDataSchema,
1314
} from 'codify-schemas';
1415

1516
import { ResourcePlan } from '../entities/plan.js';
16-
import { PlanRequest } from '../entities/plan-request.js';
1717
import { ResourceConfig } from '../entities/resource-config.js';
1818
import { ajv } from '../utils/ajv.js';
1919
import { PluginProcess } from './plugin-process.js';
@@ -55,8 +55,8 @@ export class Plugin {
5555
}
5656

5757
async validate(configs: ResourceConfig[]): Promise<ValidateResponseData> {
58-
const rawConfigs = configs.map((c) => c.raw);
59-
const result = await this.process!.sendMessageForResult('validate', { configs: rawConfigs });
58+
const jsonConfigs = configs.map((c) => c.toJson());
59+
const result = await this.process!.sendMessageForResult('validate', { configs: jsonConfigs });
6060

6161
if (!result.isSuccessful()) {
6262
throw new Error(`Initialize error for plugin: "${this.name}" \n\n` + result.data);
@@ -83,11 +83,11 @@ export class Plugin {
8383
return result.data;
8484
}
8585

86-
async import(config: SchemaResourceConfig): Promise<ImportResponseData> {
87-
const result = await this.process!.sendMessageForResult('import', { config });
86+
async import(config: ResourceJson): Promise<ImportResponseData> {
87+
const result = await this.process!.sendMessageForResult('import', config);
8888

8989
if (!result.isSuccessful()) {
90-
throw new Error(`Unable import resource ${config.type} with plugin: "${this.name}" \n\n` + result.data);
90+
throw new Error(`Unable import resource ${config.core.type} with plugin: "${this.name}" \n\n` + result.data);
9191
}
9292

9393
if (!this.validateImportResponse(result.data)) {
@@ -97,18 +97,14 @@ export class Plugin {
9797
return result.data;
9898
}
9999

100-
async plan(request: PlanRequest): Promise<ResourcePlan> {
100+
async plan(request: PlanRequestData): Promise<ResourcePlan> {
101101
const result = await this.process!.sendMessageForResult(
102102
'plan',
103-
{
104-
desired: request.desired,
105-
state: request.state,
106-
isStateful: request.isStateful
107-
}
103+
request
108104
);
109105

110106
if (!result.isSuccessful()) {
111-
throw new Error(`Plan error for plugin: "${this.name}", resource: "${request.type}" \n\n` + result.data);
107+
throw new Error(`Plan error for plugin: "${this.name}", resource: "${request.core.type}" \n\n` + result.data);
112108
}
113109

114110
if (!this.validatePlanResponse(result.data)) {

0 commit comments

Comments
 (0)