nomoss 0.0.1
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/LICENSE +19 -0
- package/README.md +45 -0
- package/bin/nomoss +13 -0
- package/docs/code-documentation.md +36 -0
- package/docs/effect-native-development.md +55 -0
- package/docs/engine-design.md +93 -0
- package/docs/linting.md +88 -0
- package/docs/releasing.md +13 -0
- package/docs/resource-command-policy-stage.md +71 -0
- package/docs/resource-graph-mvp.md +31 -0
- package/docs/resource-graph-testing-approach.md +11 -0
- package/docs/resource-lifecycle-tracker.md +93 -0
- package/docs/testing.md +86 -0
- package/examples/upload-events/README.md +41 -0
- package/media/nomoss.gif +0 -0
- package/package.json +64 -0
- package/src/cli.ts +18 -0
- package/src/cliCommand.ts +186 -0
- package/src/cliRuntimeLayer.ts +49 -0
- package/src/core/index.ts +17 -0
- package/src/core/lifecycle.ts +127 -0
- package/src/core/model.ts +229 -0
- package/src/core/physicalNameStore.ts +226 -0
- package/src/core/planner.ts +645 -0
- package/src/core/resourceCommandPolicy.ts +79 -0
- package/src/core/resourceGraphBuilder.ts +167 -0
- package/src/core/resourceGraphStore.ts +288 -0
- package/src/core/resourceOutputResolver.ts +185 -0
- package/src/core/resourcePolicy.ts +77 -0
- package/src/core/resourceStackDefinition.ts +17 -0
- package/src/core/resourceStackLifecycle.ts +456 -0
- package/src/core/resourceStackOperations.ts +129 -0
- package/src/core/runtimeLayer.ts +73 -0
- package/src/core/stackWorkflowRenderer.ts +239 -0
- package/src/core/stateSecretService.ts +553 -0
- package/src/core/stateStore.ts +652 -0
- package/src/core/tracing.ts +26 -0
- package/src/index.ts +2 -0
- package/src/providers/aws/awsApply.ts +132 -0
- package/src/providers/aws/awsBucket.ts +329 -0
- package/src/providers/aws/awsBucketNotification.ts +351 -0
- package/src/providers/aws/awsBucketNotificationResourcePolicy.ts +251 -0
- package/src/providers/aws/awsBucketResourcePolicy.ts +198 -0
- package/src/providers/aws/awsProviderLayer.ts +274 -0
- package/src/providers/aws/awsQueue.ts +346 -0
- package/src/providers/aws/awsQueuePolicy.ts +419 -0
- package/src/providers/aws/awsQueuePolicyResourcePolicy.ts +215 -0
- package/src/providers/aws/awsQueueResourcePolicy.ts +270 -0
- package/src/providers/aws/awsReconciliation.ts +92 -0
- package/src/providers/aws/awsRefresh.ts +50 -0
- package/src/providers/aws/awsResourceLifecycle.ts +118 -0
- package/src/providers/aws/awsResourcePolicy.ts +83 -0
- package/src/providers/aws/awsResources.ts +274 -0
- package/src/providers/aws/awsRuntimeLayer.ts +21 -0
- package/src/providers/aws/awsSqsTransport.ts +33 -0
- package/src/providers/aws/awsStackLifecycle.ts +272 -0
- package/src/providers/aws/awsTagging.ts +186 -0
- package/src/providers/aws/constants.ts +3 -0
- package/src/providers/aws/index.ts +22 -0
- package/src/providers/aws/sampleStack.ts +65 -0
- package/src/providers/aws/stackWorkflow.ts +443 -0
- package/src/providers/index.ts +5 -0
- package/src/providers/provider.ts +5 -0
- package/src/providers/providerRuntimeLayer.ts +121 -0
- package/src/providers/resourceCommandPolicyLayer.ts +240 -0
- package/src/providers/stripe/index.ts +15 -0
- package/src/providers/stripe/stripeAccount.ts +139 -0
- package/src/providers/stripe/stripeBillingConfigurationExport.ts +180 -0
- package/src/providers/stripe/stripeBillingConfigurationExportResourcePolicy.ts +178 -0
- package/src/providers/stripe/stripeBillingPortalConfiguration.ts +153 -0
- package/src/providers/stripe/stripeBillingPortalConfigurationResourcePolicy.ts +177 -0
- package/src/providers/stripe/stripeCustomer.ts +148 -0
- package/src/providers/stripe/stripeCustomerResourcePolicy.ts +160 -0
- package/src/providers/stripe/stripePrice.ts +218 -0
- package/src/providers/stripe/stripePriceResourcePolicy.ts +151 -0
- package/src/providers/stripe/stripeProduct.ts +171 -0
- package/src/providers/stripe/stripeProductResourcePolicy.ts +161 -0
- package/src/providers/stripe/stripeResources.ts +454 -0
- package/src/providers/stripe/stripeRuntimeLayer.ts +131 -0
- package/src/providers/stripe/stripeWebhookEndpoint.ts +293 -0
- package/src/providers/stripe/stripeWebhookEndpointResourcePolicy.ts +198 -0
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Array as Arr,
|
|
3
|
+
Context,
|
|
4
|
+
DateTime,
|
|
5
|
+
Effect,
|
|
6
|
+
FileSystem,
|
|
7
|
+
Graph,
|
|
8
|
+
Layer,
|
|
9
|
+
Match,
|
|
10
|
+
Option,
|
|
11
|
+
Ref,
|
|
12
|
+
Schema,
|
|
13
|
+
} from "effect";
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
type DependencyEdge,
|
|
17
|
+
type PlanAction,
|
|
18
|
+
type ResourceNode,
|
|
19
|
+
ResourceNodeSchema,
|
|
20
|
+
} from "./model.js";
|
|
21
|
+
import type { ResourceCommandFailure } from "./resourceCommandPolicy.js";
|
|
22
|
+
import { StateSecretService } from "./stateSecretService.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Provider failures are persisted as compact diagnostics on the current
|
|
26
|
+
* lifecycle phase so a retry can show what failed without storing arbitrary
|
|
27
|
+
* provider error objects in state.
|
|
28
|
+
*/
|
|
29
|
+
export const ResourceFailureSchema = Schema.Struct({
|
|
30
|
+
errorTag: Schema.optional(Schema.NonEmptyString),
|
|
31
|
+
message: Schema.String,
|
|
32
|
+
occurredAt: Schema.NonEmptyString,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resource state stores lifecycle progress separately from `ResourceNode`.
|
|
37
|
+
* Planners read terminal nodes, while apply uses in-flight phases for retry
|
|
38
|
+
* and rollback recovery.
|
|
39
|
+
*/
|
|
40
|
+
export const ResourceStateSchema = Schema.Union([
|
|
41
|
+
Schema.TaggedStruct("Creating", {
|
|
42
|
+
node: ResourceNodeSchema,
|
|
43
|
+
startedAt: Schema.NonEmptyString,
|
|
44
|
+
lastFailure: Schema.optional(ResourceFailureSchema),
|
|
45
|
+
}),
|
|
46
|
+
Schema.TaggedStruct("Created", {
|
|
47
|
+
node: ResourceNodeSchema,
|
|
48
|
+
completedAt: Schema.NonEmptyString,
|
|
49
|
+
}),
|
|
50
|
+
Schema.TaggedStruct("Updating", {
|
|
51
|
+
node: ResourceNodeSchema,
|
|
52
|
+
previous: ResourceNodeSchema,
|
|
53
|
+
startedAt: Schema.NonEmptyString,
|
|
54
|
+
lastFailure: Schema.optional(ResourceFailureSchema),
|
|
55
|
+
}),
|
|
56
|
+
Schema.TaggedStruct("Updated", {
|
|
57
|
+
node: ResourceNodeSchema,
|
|
58
|
+
completedAt: Schema.NonEmptyString,
|
|
59
|
+
}),
|
|
60
|
+
Schema.TaggedStruct("Deleting", {
|
|
61
|
+
node: ResourceNodeSchema,
|
|
62
|
+
startedAt: Schema.NonEmptyString,
|
|
63
|
+
lastFailure: Schema.optional(ResourceFailureSchema),
|
|
64
|
+
}),
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
export const EnvironmentStateSchema = Schema.Struct({
|
|
68
|
+
schemaVersion: Schema.Literal(1),
|
|
69
|
+
stack: Schema.NonEmptyString,
|
|
70
|
+
resources: Schema.Array(ResourceStateSchema),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export type EnvironmentState = Schema.Schema.Type<
|
|
74
|
+
typeof EnvironmentStateSchema
|
|
75
|
+
>;
|
|
76
|
+
export type ResourceState = EnvironmentState["resources"][number];
|
|
77
|
+
export type ResourceFailure = NonNullable<
|
|
78
|
+
Extract<
|
|
79
|
+
ResourceState,
|
|
80
|
+
{ readonly _tag: "Creating" | "Updating" | "Deleting" }
|
|
81
|
+
>["lastFailure"]
|
|
82
|
+
>;
|
|
83
|
+
|
|
84
|
+
const completedStateTag = (state: ResourceState): Option.Option<"Updated"> =>
|
|
85
|
+
Match.value(state).pipe(
|
|
86
|
+
Match.when({ _tag: "Updated" }, () => Option.some<"Updated">("Updated")),
|
|
87
|
+
Match.orElse(() => Option.none<"Updated">()),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
export const EnvironmentStateFileSchema = Schema.fromJsonString(
|
|
91
|
+
EnvironmentStateSchema,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A stack apply can stop after one provider command completes while another
|
|
96
|
+
* resource remains in a non-terminal lifecycle phase. `ResourceStackLifecycle`
|
|
97
|
+
* records those durable facts here so the next prepare or retry gives
|
|
98
|
+
* `ResourcePlanner` and provider command policies the last applicable resource
|
|
99
|
+
* node instead of treating the interrupted stack as new. When a provider value
|
|
100
|
+
* such as Stripe's webhook signing secret is needed by a later update but must
|
|
101
|
+
* not remain in JSON, this same record path uses `StateSecretService` to retain
|
|
102
|
+
* only its state-secret reference.
|
|
103
|
+
*/
|
|
104
|
+
export class ResourceStateStore extends Context.Service<ResourceStateStore>()(
|
|
105
|
+
"nomoss/core/stateStore/ResourceStateStore",
|
|
106
|
+
{
|
|
107
|
+
make: Effect.gen(function* () {
|
|
108
|
+
const fs = yield* FileSystem.FileSystem;
|
|
109
|
+
const stateSecrets = yield* StateSecretService;
|
|
110
|
+
|
|
111
|
+
const loadResourceStates = Effect.fn(
|
|
112
|
+
"ResourceStateStore.loadResourceStates",
|
|
113
|
+
)(function* (stack: string) {
|
|
114
|
+
const filePath = `./.nomoss/state/${stack}.json`;
|
|
115
|
+
const states = yield* fs.readFileString(filePath).pipe(
|
|
116
|
+
Effect.flatMap(
|
|
117
|
+
Schema.decodeUnknownEffect(EnvironmentStateFileSchema),
|
|
118
|
+
),
|
|
119
|
+
Effect.map((state) => state.resources),
|
|
120
|
+
Effect.when(fs.exists(filePath)),
|
|
121
|
+
Effect.map(Option.getOrElse(() => [])),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return yield* stateSecrets.restoreStateSecrets(states);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const saveResourceStates = Effect.fn(
|
|
128
|
+
"ResourceStateStore.saveResourceStates",
|
|
129
|
+
)(function* (stack: string, resources: ReadonlyArray<ResourceState>) {
|
|
130
|
+
const externalizedResources = yield* stateSecrets.persistStateSecrets(
|
|
131
|
+
stack,
|
|
132
|
+
resources,
|
|
133
|
+
);
|
|
134
|
+
const state = yield* EnvironmentStateSchema.makeEffect({
|
|
135
|
+
schemaVersion: 1,
|
|
136
|
+
stack,
|
|
137
|
+
resources: externalizedResources,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const encoded = yield* Schema.encodeEffect(EnvironmentStateFileSchema)(
|
|
141
|
+
state,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
yield* fs.makeDirectory("./.nomoss/state", { recursive: true });
|
|
145
|
+
yield* fs.writeFileString(`./.nomoss/state/${stack}.json`, encoded);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
loadResourceStates,
|
|
150
|
+
|
|
151
|
+
loadResources: Effect.fn("ResourceStateStore.loadResources")(function* (
|
|
152
|
+
stack: string,
|
|
153
|
+
) {
|
|
154
|
+
const states = yield* loadResourceStates(stack);
|
|
155
|
+
const resources = Arr.flatMap(states, (state) =>
|
|
156
|
+
Match.value(state).pipe(
|
|
157
|
+
Match.when({ _tag: "Created" }, ({ node }) => [node]),
|
|
158
|
+
Match.when({ _tag: "Updating" }, ({ previous }) => [previous]),
|
|
159
|
+
Match.when({ _tag: "Updated" }, ({ node }) => [node]),
|
|
160
|
+
Match.when({ _tag: "Deleting" }, ({ node }) => [node]),
|
|
161
|
+
Match.orElse((): ReadonlyArray<ResourceNode> => []),
|
|
162
|
+
),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
return resources;
|
|
166
|
+
}),
|
|
167
|
+
|
|
168
|
+
saveResourceStates,
|
|
169
|
+
|
|
170
|
+
saveResources: Effect.fn("ResourceStateStore.saveResources")(function* (
|
|
171
|
+
stack: string,
|
|
172
|
+
resources: ReadonlyArray<ResourceNode>,
|
|
173
|
+
) {
|
|
174
|
+
const states = yield* loadResourceStates(stack);
|
|
175
|
+
const completedAt = yield* DateTime.now.pipe(
|
|
176
|
+
Effect.map(DateTime.formatIso),
|
|
177
|
+
);
|
|
178
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.map(
|
|
179
|
+
resources,
|
|
180
|
+
(node) =>
|
|
181
|
+
Option.match(
|
|
182
|
+
Option.flatMap(
|
|
183
|
+
Arr.findFirst(
|
|
184
|
+
states,
|
|
185
|
+
(state) => state.node.key.logicalId === node.key.logicalId,
|
|
186
|
+
),
|
|
187
|
+
completedStateTag,
|
|
188
|
+
),
|
|
189
|
+
{
|
|
190
|
+
onNone: () =>
|
|
191
|
+
ResourceStateSchema.make({
|
|
192
|
+
_tag: "Created",
|
|
193
|
+
node,
|
|
194
|
+
completedAt,
|
|
195
|
+
}),
|
|
196
|
+
onSome: (_tag) =>
|
|
197
|
+
ResourceStateSchema.make({
|
|
198
|
+
_tag,
|
|
199
|
+
node,
|
|
200
|
+
completedAt,
|
|
201
|
+
}),
|
|
202
|
+
},
|
|
203
|
+
),
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
207
|
+
}),
|
|
208
|
+
|
|
209
|
+
markResourceStarted: Effect.fn(
|
|
210
|
+
"ResourceStateStore.markResourceStarted",
|
|
211
|
+
)(function* (stack: string, action: PlanAction) {
|
|
212
|
+
const states = yield* loadResourceStates(stack);
|
|
213
|
+
const startedAt = yield* DateTime.now.pipe(
|
|
214
|
+
Effect.map(DateTime.formatIso),
|
|
215
|
+
);
|
|
216
|
+
const resourceState: ResourceState = Match.value(action).pipe(
|
|
217
|
+
Match.when({ _tag: "Create" }, ({ node }) =>
|
|
218
|
+
ResourceStateSchema.make({
|
|
219
|
+
_tag: "Creating",
|
|
220
|
+
node,
|
|
221
|
+
startedAt,
|
|
222
|
+
}),
|
|
223
|
+
),
|
|
224
|
+
Match.when({ _tag: "Update" }, ({ node, current }) =>
|
|
225
|
+
ResourceStateSchema.make({
|
|
226
|
+
_tag: "Updating",
|
|
227
|
+
node,
|
|
228
|
+
previous: current,
|
|
229
|
+
startedAt,
|
|
230
|
+
}),
|
|
231
|
+
),
|
|
232
|
+
Match.when({ _tag: "Delete" }, ({ node }) =>
|
|
233
|
+
ResourceStateSchema.make({
|
|
234
|
+
_tag: "Deleting",
|
|
235
|
+
node,
|
|
236
|
+
startedAt,
|
|
237
|
+
}),
|
|
238
|
+
),
|
|
239
|
+
Match.when({ _tag: "Destroy" }, ({ node }) =>
|
|
240
|
+
ResourceStateSchema.make({
|
|
241
|
+
_tag: "Deleting",
|
|
242
|
+
node,
|
|
243
|
+
startedAt,
|
|
244
|
+
}),
|
|
245
|
+
),
|
|
246
|
+
Match.exhaustive,
|
|
247
|
+
);
|
|
248
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.append(
|
|
249
|
+
Arr.filter(
|
|
250
|
+
states,
|
|
251
|
+
(state) =>
|
|
252
|
+
state.node.key.logicalId !== resourceState.node.key.logicalId,
|
|
253
|
+
),
|
|
254
|
+
resourceState,
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
258
|
+
}),
|
|
259
|
+
|
|
260
|
+
markResourceApplied: Effect.fn(
|
|
261
|
+
"ResourceStateStore.markResourceApplied",
|
|
262
|
+
)(function* (
|
|
263
|
+
stack: string,
|
|
264
|
+
node: ResourceNode,
|
|
265
|
+
stateTag: "Created" | "Updated",
|
|
266
|
+
) {
|
|
267
|
+
const states = yield* loadResourceStates(stack);
|
|
268
|
+
const completedAt = yield* DateTime.now.pipe(
|
|
269
|
+
Effect.map(DateTime.formatIso),
|
|
270
|
+
);
|
|
271
|
+
const resourceState: ResourceState = Match.value(stateTag).pipe(
|
|
272
|
+
Match.when("Created", (_tag) =>
|
|
273
|
+
ResourceStateSchema.make({
|
|
274
|
+
_tag,
|
|
275
|
+
node,
|
|
276
|
+
completedAt,
|
|
277
|
+
}),
|
|
278
|
+
),
|
|
279
|
+
Match.when("Updated", (_tag) =>
|
|
280
|
+
ResourceStateSchema.make({
|
|
281
|
+
_tag,
|
|
282
|
+
node,
|
|
283
|
+
completedAt,
|
|
284
|
+
}),
|
|
285
|
+
),
|
|
286
|
+
Match.exhaustive,
|
|
287
|
+
);
|
|
288
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.append(
|
|
289
|
+
Arr.filter(
|
|
290
|
+
states,
|
|
291
|
+
(state) => state.node.key.logicalId !== node.key.logicalId,
|
|
292
|
+
),
|
|
293
|
+
resourceState,
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
297
|
+
}),
|
|
298
|
+
|
|
299
|
+
deleteResourceState: Effect.fn(
|
|
300
|
+
"ResourceStateStore.deleteResourceState",
|
|
301
|
+
)(function* (stack: string, node: ResourceNode) {
|
|
302
|
+
const states = yield* loadResourceStates(stack);
|
|
303
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.filter(
|
|
304
|
+
states,
|
|
305
|
+
(state) => state.node.key.logicalId !== node.key.logicalId,
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
309
|
+
}),
|
|
310
|
+
|
|
311
|
+
markResourceFailure: Effect.fn(
|
|
312
|
+
"ResourceStateStore.markResourceFailure",
|
|
313
|
+
)(function* (
|
|
314
|
+
stack: string,
|
|
315
|
+
node: ResourceNode,
|
|
316
|
+
cause: ResourceCommandFailure,
|
|
317
|
+
) {
|
|
318
|
+
const states = yield* loadResourceStates(stack);
|
|
319
|
+
const occurredAt = yield* DateTime.now.pipe(
|
|
320
|
+
Effect.map(DateTime.formatIso),
|
|
321
|
+
);
|
|
322
|
+
const resourceFailure: ResourceFailure = ResourceFailureSchema.make({
|
|
323
|
+
errorTag: cause._tag,
|
|
324
|
+
message: cause.message,
|
|
325
|
+
occurredAt,
|
|
326
|
+
});
|
|
327
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.map(
|
|
328
|
+
states,
|
|
329
|
+
(state) =>
|
|
330
|
+
Match.value(state).pipe(
|
|
331
|
+
Match.when(
|
|
332
|
+
{
|
|
333
|
+
node: { key: { logicalId: node.key.logicalId } },
|
|
334
|
+
_tag: "Creating",
|
|
335
|
+
},
|
|
336
|
+
(creating) =>
|
|
337
|
+
ResourceStateSchema.make({
|
|
338
|
+
_tag: "Creating",
|
|
339
|
+
node: creating.node,
|
|
340
|
+
startedAt: creating.startedAt,
|
|
341
|
+
lastFailure: resourceFailure,
|
|
342
|
+
}),
|
|
343
|
+
),
|
|
344
|
+
Match.when(
|
|
345
|
+
{
|
|
346
|
+
node: { key: { logicalId: node.key.logicalId } },
|
|
347
|
+
_tag: "Updating",
|
|
348
|
+
},
|
|
349
|
+
(updating) =>
|
|
350
|
+
ResourceStateSchema.make({
|
|
351
|
+
_tag: "Updating",
|
|
352
|
+
node: updating.node,
|
|
353
|
+
previous: updating.previous,
|
|
354
|
+
startedAt: updating.startedAt,
|
|
355
|
+
lastFailure: resourceFailure,
|
|
356
|
+
}),
|
|
357
|
+
),
|
|
358
|
+
Match.when(
|
|
359
|
+
{
|
|
360
|
+
node: { key: { logicalId: node.key.logicalId } },
|
|
361
|
+
_tag: "Deleting",
|
|
362
|
+
},
|
|
363
|
+
(deleting) =>
|
|
364
|
+
ResourceStateSchema.make({
|
|
365
|
+
_tag: "Deleting",
|
|
366
|
+
node: deleting.node,
|
|
367
|
+
startedAt: deleting.startedAt,
|
|
368
|
+
lastFailure: resourceFailure,
|
|
369
|
+
}),
|
|
370
|
+
),
|
|
371
|
+
Match.orElse(() => state),
|
|
372
|
+
),
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
376
|
+
}),
|
|
377
|
+
|
|
378
|
+
resourcesFromGraph: (
|
|
379
|
+
graph: Graph.DirectedGraph<ResourceNode, DependencyEdge>,
|
|
380
|
+
) => Arr.fromIterable(Graph.values(Graph.topo(graph))),
|
|
381
|
+
};
|
|
382
|
+
}),
|
|
383
|
+
},
|
|
384
|
+
) {}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Resource stack lifecycle tests use the same state-store contract without
|
|
388
|
+
* writing `.nomoss` files into the checkout.
|
|
389
|
+
*/
|
|
390
|
+
export const ResourceStateStoreTestLayer = Layer.effect(
|
|
391
|
+
ResourceStateStore,
|
|
392
|
+
Effect.gen(function* () {
|
|
393
|
+
const statesByStack = yield* Ref.make(
|
|
394
|
+
new Map<string, ReadonlyArray<ResourceState>>(),
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
const loadResourceStates = Effect.fn(
|
|
398
|
+
"ResourceStateStoreTestLayer.loadResourceStates",
|
|
399
|
+
)(function* (stack: string) {
|
|
400
|
+
const resources = yield* Ref.get(statesByStack);
|
|
401
|
+
|
|
402
|
+
return resources.get(stack) ?? [];
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const saveResourceStates = Effect.fn(
|
|
406
|
+
"ResourceStateStoreTestLayer.saveResourceStates",
|
|
407
|
+
)(function* (stack: string, resources: ReadonlyArray<ResourceState>) {
|
|
408
|
+
yield* Ref.update(statesByStack, (current) => {
|
|
409
|
+
const entries: ReadonlyArray<
|
|
410
|
+
readonly [string, ReadonlyArray<ResourceState>]
|
|
411
|
+
> = Arr.append(Arr.fromIterable(current), [stack, resources]);
|
|
412
|
+
const nextStatesByStack = new Map(entries);
|
|
413
|
+
|
|
414
|
+
return nextStatesByStack;
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
return {
|
|
419
|
+
loadResourceStates,
|
|
420
|
+
|
|
421
|
+
loadResources: Effect.fn("ResourceStateStoreTestLayer.loadResources")(
|
|
422
|
+
function* (stack: string) {
|
|
423
|
+
const states = yield* loadResourceStates(stack);
|
|
424
|
+
const resources = Arr.flatMap(states, (state) =>
|
|
425
|
+
Match.value(state).pipe(
|
|
426
|
+
Match.when({ _tag: "Created" }, ({ node }) => [node]),
|
|
427
|
+
Match.when({ _tag: "Updating" }, ({ previous }) => [previous]),
|
|
428
|
+
Match.when({ _tag: "Updated" }, ({ node }) => [node]),
|
|
429
|
+
Match.when({ _tag: "Deleting" }, ({ node }) => [node]),
|
|
430
|
+
Match.orElse((): ReadonlyArray<ResourceNode> => []),
|
|
431
|
+
),
|
|
432
|
+
);
|
|
433
|
+
|
|
434
|
+
return resources;
|
|
435
|
+
},
|
|
436
|
+
),
|
|
437
|
+
|
|
438
|
+
saveResourceStates,
|
|
439
|
+
|
|
440
|
+
saveResources: Effect.fn("ResourceStateStoreTestLayer.saveResources")(
|
|
441
|
+
function* (stack: string, resources: ReadonlyArray<ResourceNode>) {
|
|
442
|
+
const states = yield* loadResourceStates(stack);
|
|
443
|
+
const completedAt = yield* DateTime.now.pipe(
|
|
444
|
+
Effect.map(DateTime.formatIso),
|
|
445
|
+
);
|
|
446
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.map(
|
|
447
|
+
resources,
|
|
448
|
+
(node) =>
|
|
449
|
+
Option.match(
|
|
450
|
+
Option.flatMap(
|
|
451
|
+
Arr.findFirst(
|
|
452
|
+
states,
|
|
453
|
+
(state) => state.node.key.logicalId === node.key.logicalId,
|
|
454
|
+
),
|
|
455
|
+
completedStateTag,
|
|
456
|
+
),
|
|
457
|
+
{
|
|
458
|
+
onNone: () =>
|
|
459
|
+
ResourceStateSchema.make({
|
|
460
|
+
_tag: "Created",
|
|
461
|
+
node,
|
|
462
|
+
completedAt,
|
|
463
|
+
}),
|
|
464
|
+
onSome: (_tag) =>
|
|
465
|
+
ResourceStateSchema.make({
|
|
466
|
+
_tag,
|
|
467
|
+
node,
|
|
468
|
+
completedAt,
|
|
469
|
+
}),
|
|
470
|
+
},
|
|
471
|
+
),
|
|
472
|
+
);
|
|
473
|
+
|
|
474
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
475
|
+
},
|
|
476
|
+
),
|
|
477
|
+
|
|
478
|
+
markResourceStarted: Effect.fn(
|
|
479
|
+
"ResourceStateStoreTestLayer.markResourceStarted",
|
|
480
|
+
)(function* (stack: string, action: PlanAction) {
|
|
481
|
+
const states = yield* loadResourceStates(stack);
|
|
482
|
+
const startedAt = yield* DateTime.now.pipe(
|
|
483
|
+
Effect.map(DateTime.formatIso),
|
|
484
|
+
);
|
|
485
|
+
const resourceState: ResourceState = Match.value(action).pipe(
|
|
486
|
+
Match.when({ _tag: "Create" }, ({ node }) =>
|
|
487
|
+
ResourceStateSchema.make({
|
|
488
|
+
_tag: "Creating",
|
|
489
|
+
node,
|
|
490
|
+
startedAt,
|
|
491
|
+
}),
|
|
492
|
+
),
|
|
493
|
+
Match.when({ _tag: "Update" }, ({ node, current }) =>
|
|
494
|
+
ResourceStateSchema.make({
|
|
495
|
+
_tag: "Updating",
|
|
496
|
+
node,
|
|
497
|
+
previous: current,
|
|
498
|
+
startedAt,
|
|
499
|
+
}),
|
|
500
|
+
),
|
|
501
|
+
Match.when({ _tag: "Delete" }, ({ node }) =>
|
|
502
|
+
ResourceStateSchema.make({
|
|
503
|
+
_tag: "Deleting",
|
|
504
|
+
node,
|
|
505
|
+
startedAt,
|
|
506
|
+
}),
|
|
507
|
+
),
|
|
508
|
+
Match.when({ _tag: "Destroy" }, ({ node }) =>
|
|
509
|
+
ResourceStateSchema.make({
|
|
510
|
+
_tag: "Deleting",
|
|
511
|
+
node,
|
|
512
|
+
startedAt,
|
|
513
|
+
}),
|
|
514
|
+
),
|
|
515
|
+
Match.exhaustive,
|
|
516
|
+
);
|
|
517
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.append(
|
|
518
|
+
Arr.filter(
|
|
519
|
+
states,
|
|
520
|
+
(state) =>
|
|
521
|
+
state.node.key.logicalId !== resourceState.node.key.logicalId,
|
|
522
|
+
),
|
|
523
|
+
resourceState,
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
527
|
+
}),
|
|
528
|
+
|
|
529
|
+
markResourceApplied: Effect.fn(
|
|
530
|
+
"ResourceStateStoreTestLayer.markResourceApplied",
|
|
531
|
+
)(function* (
|
|
532
|
+
stack: string,
|
|
533
|
+
node: ResourceNode,
|
|
534
|
+
stateTag: "Created" | "Updated",
|
|
535
|
+
) {
|
|
536
|
+
const states = yield* loadResourceStates(stack);
|
|
537
|
+
const completedAt = yield* DateTime.now.pipe(
|
|
538
|
+
Effect.map(DateTime.formatIso),
|
|
539
|
+
);
|
|
540
|
+
const resourceState: ResourceState = Match.value(stateTag).pipe(
|
|
541
|
+
Match.when("Created", (_tag) =>
|
|
542
|
+
ResourceStateSchema.make({
|
|
543
|
+
_tag,
|
|
544
|
+
node,
|
|
545
|
+
completedAt,
|
|
546
|
+
}),
|
|
547
|
+
),
|
|
548
|
+
Match.when("Updated", (_tag) =>
|
|
549
|
+
ResourceStateSchema.make({
|
|
550
|
+
_tag,
|
|
551
|
+
node,
|
|
552
|
+
completedAt,
|
|
553
|
+
}),
|
|
554
|
+
),
|
|
555
|
+
Match.exhaustive,
|
|
556
|
+
);
|
|
557
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.append(
|
|
558
|
+
Arr.filter(
|
|
559
|
+
states,
|
|
560
|
+
(state) => state.node.key.logicalId !== node.key.logicalId,
|
|
561
|
+
),
|
|
562
|
+
resourceState,
|
|
563
|
+
);
|
|
564
|
+
|
|
565
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
566
|
+
}),
|
|
567
|
+
|
|
568
|
+
deleteResourceState: Effect.fn(
|
|
569
|
+
"ResourceStateStoreTestLayer.deleteResourceState",
|
|
570
|
+
)(function* (stack: string, node: ResourceNode) {
|
|
571
|
+
const states = yield* loadResourceStates(stack);
|
|
572
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.filter(
|
|
573
|
+
states,
|
|
574
|
+
(state) => state.node.key.logicalId !== node.key.logicalId,
|
|
575
|
+
);
|
|
576
|
+
|
|
577
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
578
|
+
}),
|
|
579
|
+
|
|
580
|
+
markResourceFailure: Effect.fn(
|
|
581
|
+
"ResourceStateStoreTestLayer.markResourceFailure",
|
|
582
|
+
)(function* (
|
|
583
|
+
stack: string,
|
|
584
|
+
node: ResourceNode,
|
|
585
|
+
cause: ResourceCommandFailure,
|
|
586
|
+
) {
|
|
587
|
+
const states = yield* loadResourceStates(stack);
|
|
588
|
+
const occurredAt = yield* DateTime.now.pipe(
|
|
589
|
+
Effect.map(DateTime.formatIso),
|
|
590
|
+
);
|
|
591
|
+
const resourceFailure: ResourceFailure = ResourceFailureSchema.make({
|
|
592
|
+
errorTag: cause._tag,
|
|
593
|
+
message: cause.message,
|
|
594
|
+
occurredAt,
|
|
595
|
+
});
|
|
596
|
+
const resourceStates: ReadonlyArray<ResourceState> = Arr.map(
|
|
597
|
+
states,
|
|
598
|
+
(state) =>
|
|
599
|
+
Match.value(state).pipe(
|
|
600
|
+
Match.when(
|
|
601
|
+
{
|
|
602
|
+
node: { key: { logicalId: node.key.logicalId } },
|
|
603
|
+
_tag: "Creating",
|
|
604
|
+
},
|
|
605
|
+
(creating) =>
|
|
606
|
+
ResourceStateSchema.make({
|
|
607
|
+
_tag: "Creating",
|
|
608
|
+
node: creating.node,
|
|
609
|
+
startedAt: creating.startedAt,
|
|
610
|
+
lastFailure: resourceFailure,
|
|
611
|
+
}),
|
|
612
|
+
),
|
|
613
|
+
Match.when(
|
|
614
|
+
{
|
|
615
|
+
node: { key: { logicalId: node.key.logicalId } },
|
|
616
|
+
_tag: "Updating",
|
|
617
|
+
},
|
|
618
|
+
(updating) =>
|
|
619
|
+
ResourceStateSchema.make({
|
|
620
|
+
_tag: "Updating",
|
|
621
|
+
node: updating.node,
|
|
622
|
+
previous: updating.previous,
|
|
623
|
+
startedAt: updating.startedAt,
|
|
624
|
+
lastFailure: resourceFailure,
|
|
625
|
+
}),
|
|
626
|
+
),
|
|
627
|
+
Match.when(
|
|
628
|
+
{
|
|
629
|
+
node: { key: { logicalId: node.key.logicalId } },
|
|
630
|
+
_tag: "Deleting",
|
|
631
|
+
},
|
|
632
|
+
(deleting) =>
|
|
633
|
+
ResourceStateSchema.make({
|
|
634
|
+
_tag: "Deleting",
|
|
635
|
+
node: deleting.node,
|
|
636
|
+
startedAt: deleting.startedAt,
|
|
637
|
+
lastFailure: resourceFailure,
|
|
638
|
+
}),
|
|
639
|
+
),
|
|
640
|
+
Match.orElse(() => state),
|
|
641
|
+
),
|
|
642
|
+
);
|
|
643
|
+
|
|
644
|
+
yield* saveResourceStates(stack, resourceStates);
|
|
645
|
+
}),
|
|
646
|
+
|
|
647
|
+
resourcesFromGraph: (
|
|
648
|
+
graph: Graph.DirectedGraph<ResourceNode, DependencyEdge>,
|
|
649
|
+
) => Arr.fromIterable(Graph.values(Graph.topo(graph))),
|
|
650
|
+
};
|
|
651
|
+
}),
|
|
652
|
+
);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Config, Effect, Layer, Match } from "effect";
|
|
2
|
+
|
|
3
|
+
import { DevTools } from "effect/unstable/devtools";
|
|
4
|
+
|
|
5
|
+
const tracingConfig = Config.all({
|
|
6
|
+
devTools: Config.string("NOMOSS_EFFECT_DEVTOOLS").pipe(
|
|
7
|
+
Config.withDefault("0"),
|
|
8
|
+
),
|
|
9
|
+
logLevel: Config.string("NOMOSS_LOG_LEVEL").pipe(Config.withDefault("")),
|
|
10
|
+
url: Config.string("NOMOSS_EFFECT_DEVTOOLS_URL").pipe(
|
|
11
|
+
Config.withDefault("ws://localhost:34437"),
|
|
12
|
+
),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const NomossTracingLive = Layer.unwrap(
|
|
16
|
+
Effect.gen(function* () {
|
|
17
|
+
const config = yield* tracingConfig;
|
|
18
|
+
const devToolsEnabled =
|
|
19
|
+
config.logLevel === "DEBUG" || config.devTools === "1";
|
|
20
|
+
|
|
21
|
+
return Match.value(devToolsEnabled).pipe(
|
|
22
|
+
Match.when(true, () => DevTools.layer(config.url)),
|
|
23
|
+
Match.orElse(() => Layer.empty),
|
|
24
|
+
);
|
|
25
|
+
}),
|
|
26
|
+
);
|
package/src/index.ts
ADDED