nomoss 0.0.3 → 0.0.5

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.
Files changed (150) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +5 -15
  3. package/ROADMAP.md +2 -0
  4. package/docs/README.md +1 -1
  5. package/docs/application/aws-resource-coverage.md +26 -0
  6. package/docs/application/aws-resource-lifecycle-management.md +13 -23
  7. package/docs/application/cli-workflow.md +1 -1
  8. package/docs/application/engine-design.md +2 -2
  9. package/docs/application/generation-architecture.md +10 -45
  10. package/docs/application/resource-command-policy-stage.md +2 -2
  11. package/docs/application/resource-graph.md +1 -1
  12. package/docs/application/resource-lifecycle-tracker.md +1 -9
  13. package/docs/application/resource-mapping.md +4 -9
  14. package/docs/application/resource-operations.md +4 -2
  15. package/docs/application/tracing.md +2 -6
  16. package/docs/development/code-documentation.md +2 -0
  17. package/docs/development/effect-native-development.md +44 -0
  18. package/docs/development/linting.md +1 -1
  19. package/docs/development/review-code.md +22 -6
  20. package/docs/development/review-remediation-spec.md +28 -37
  21. package/docs/development/spec-writing.md +1 -1
  22. package/docs/development/testing.md +23 -9
  23. package/docs/operations/observability-sop.md +11 -17
  24. package/docs/operations/releasing.md +3 -1
  25. package/examples/README.md +3 -26
  26. package/examples/commands.ts +2 -36
  27. package/examples/queue/application.ts +8 -2
  28. package/examples/state-machine/application.ts +3 -1
  29. package/package.json +40 -46
  30. package/providers/aws/README.md +21 -0
  31. package/providers/aws/awsEnvironment.ts +114 -0
  32. package/providers/aws/generated/managementRegistry.ts +136 -0
  33. package/{src/providers/aws/resources/generated/native/apigatewayv2/api/index.ts → providers/aws/generated/resources/apigatewayv2/api.ts} +13 -50
  34. package/{src/providers/aws/resources/generated/native/apigatewayv2/integration/index.ts → providers/aws/generated/resources/apigatewayv2/integration.ts} +13 -37
  35. package/{src/providers/aws/resources/generated/native/apigatewayv2/route/index.ts → providers/aws/generated/resources/apigatewayv2/route.ts} +23 -40
  36. package/{src/providers/aws/resources/generated/native/apigatewayv2/stage/index.ts → providers/aws/generated/resources/apigatewayv2/stage.ts} +13 -38
  37. package/providers/aws/generated/resources/iam/accesskey.ts +184 -0
  38. package/{src/providers/aws/resources/generated/native/iam/group/index.ts → providers/aws/generated/resources/iam/group.ts} +13 -38
  39. package/{src/providers/aws/resources/generated/native/iam/grouppolicy/index.ts → providers/aws/generated/resources/iam/grouppolicy.ts} +29 -59
  40. package/{src/providers/aws/resources/generated/native/iam/instanceprofile/index.ts → providers/aws/generated/resources/iam/instanceprofile.ts} +13 -41
  41. package/{src/providers/aws/resources/generated/native/iam/managedpolicy/index.ts → providers/aws/generated/resources/iam/managedpolicy.ts} +13 -95
  42. package/providers/aws/generated/resources/iam/oidcprovider.ts +314 -0
  43. package/{src/providers/aws/resources/generated/native/iam/policy/index.ts → providers/aws/generated/resources/iam/policy.ts} +13 -28
  44. package/{src/providers/aws/resources/generated/native/iam/role/index.ts → providers/aws/generated/resources/iam/role.ts} +13 -47
  45. package/{src/providers/aws/resources/generated/native/iam/rolepolicy/index.ts → providers/aws/generated/resources/iam/rolepolicy.ts} +29 -59
  46. package/providers/aws/generated/resources/iam/samlprovider.ts +463 -0
  47. package/providers/aws/generated/resources/iam/servercertificate.ts +465 -0
  48. package/providers/aws/generated/resources/iam/servicelinkedrole.ts +187 -0
  49. package/{src/providers/aws/resources/generated/native/iam/user/index.ts → providers/aws/generated/resources/iam/user.ts} +13 -38
  50. package/{src/providers/aws/resources/generated/native/iam/userpolicy/index.ts → providers/aws/generated/resources/iam/userpolicy.ts} +29 -59
  51. package/{src/providers/aws/resources/generated/native/iam/usertogroupaddition/index.ts → providers/aws/generated/resources/iam/usertogroupaddition.ts} +13 -27
  52. package/providers/aws/generated/resources/iam/virtualmfadevice.ts +298 -0
  53. package/{src/providers/aws/resources/generated/native/kms/key/index.ts → providers/aws/generated/resources/kms/key.ts} +13 -41
  54. package/{src/providers/aws/resources/generated/native/lambda/function/index.ts → providers/aws/generated/resources/lambda/function.ts} +13 -63
  55. package/{src/providers/aws/resources/generated/native/lambda/permission/index.ts → providers/aws/generated/resources/lambda/permission.ts} +13 -55
  56. package/{src/providers/aws/resources/generated/native/s3/bucket/index.ts → providers/aws/generated/resources/s3/bucket.ts} +14 -104
  57. package/{src/providers/aws/resources/generated/native/sns/topic/index.ts → providers/aws/generated/resources/sns/topic.ts} +14 -29
  58. package/{src/providers/aws/resources/generated/native/sqs/queue/index.ts → providers/aws/generated/resources/sqs/queue.ts} +14 -38
  59. package/{src/providers/aws/resources/generated/native/stepfunctions/statemachine/index.ts → providers/aws/generated/resources/stepfunctions/statemachine.ts} +15 -58
  60. package/providers/aws/management/mappings/iam/group.ts +63 -0
  61. package/providers/aws/management/mappings/iam/iamIdentity.ts +41 -0
  62. package/providers/aws/management/mappings/iam/iamLifecycleSettings.ts +8 -0
  63. package/providers/aws/management/mappings/iam/inlinePolicies.ts +159 -0
  64. package/providers/aws/management/mappings/iam/instanceProfile.ts +77 -0
  65. package/providers/aws/management/mappings/iam/oidcProvider.ts +94 -0
  66. package/providers/aws/management/mappings/iam/role.ts +74 -0
  67. package/providers/aws/management/mappings/iam/samlProvider.ts +98 -0
  68. package/providers/aws/management/mappings/iam/serverCertificate.ts +72 -0
  69. package/providers/aws/management/mappings/iam/user.ts +61 -0
  70. package/providers/aws/management/mappings/iam/userToGroupAddition.ts +75 -0
  71. package/providers/aws/management/operations/iam/accessKey.ts +206 -0
  72. package/providers/aws/management/operations/iam/managedPolicy.ts +491 -0
  73. package/providers/aws/management/operations/iam/policy.ts +476 -0
  74. package/providers/aws/management/operations/iam/serviceLinkedRole.ts +172 -0
  75. package/providers/aws/management/operations/iam/virtualMfaDevice.ts +253 -0
  76. package/providers/aws/management/resourceAssociationConfiguration.ts +112 -0
  77. package/providers/aws/management/resourceAssociationRegistration.ts +349 -0
  78. package/providers/aws/management/resourceAssociations.ts +168 -0
  79. package/{src/providers → providers}/aws/management/resourceConfiguration.ts +24 -26
  80. package/providers/aws/management/resourceLifecycleRegistration.ts +540 -0
  81. package/providers/aws/management/resourceManagementModule.ts +31 -0
  82. package/providers/aws/management/resourceManagementRegistry.ts +62 -0
  83. package/providers/aws/management/resourceMapping.ts +84 -0
  84. package/providers/aws/management/resourceOperation.ts +134 -0
  85. package/providers/aws/management/resourceTagOperations.ts +108 -0
  86. package/providers/aws/management/resourceTiming.ts +4 -0
  87. package/providers/aws/runtime.ts +52 -0
  88. package/src/core/resource/resourceCommandPolicy.ts +20 -0
  89. package/src/core/resource/resourceLifecycle.ts +644 -0
  90. package/src/core/resource/resourceStabilization.ts +120 -0
  91. package/src/core/resource/resourceVersions.ts +102 -0
  92. package/src/core/stack/planner.ts +9 -0
  93. package/src/core/stack/resourceStackLifecycle.ts +36 -3
  94. package/src/core/state/resourceState.ts +6 -4
  95. package/src/core/state/stateStore.ts +57 -10
  96. package/src/providers/file/resourcePolicy.ts +13 -12
  97. package/src/providers/providerRuntimeLayer.ts +5 -2
  98. package/src/providers/resourceCommandPolicyLayer.ts +117 -37
  99. package/src/providers/stripe/management/resourcePolicy.ts +22 -11
  100. package/examples/http-echo/application.ts +0 -87
  101. package/src/providers/aws/client/awsClient.ts +0 -418
  102. package/src/providers/aws/client/awsClientConfig.ts +0 -133
  103. package/src/providers/aws/client/awsClientModel.ts +0 -119
  104. package/src/providers/aws/client/awsHttpOperation.ts +0 -153
  105. package/src/providers/aws/client/awsQueryOperation.ts +0 -149
  106. package/src/providers/aws/client/awsRequestSigner.ts +0 -115
  107. package/src/providers/aws/client/awsResponseDecoder.ts +0 -135
  108. package/src/providers/aws/client/awsRetryPolicy.ts +0 -62
  109. package/src/providers/aws/client/generated/apiGatewayV2.ts +0 -3025
  110. package/src/providers/aws/client/generated/cloudControl.ts +0 -5746
  111. package/src/providers/aws/client/generated/iam.ts +0 -10892
  112. package/src/providers/aws/client/generated/lambda.ts +0 -5403
  113. package/src/providers/aws/client/generated/sqs.ts +0 -3769
  114. package/src/providers/aws/client/generated/stepFunctions.ts +0 -1726
  115. package/src/providers/aws/client/openapi/cloudControl/CancelResourceRequest.json +0 -579
  116. package/src/providers/aws/client/openapi/cloudControl/CreateResource.json +0 -1019
  117. package/src/providers/aws/client/openapi/cloudControl/DeleteResource.json +0 -1018
  118. package/src/providers/aws/client/openapi/cloudControl/GetResource.json +0 -883
  119. package/src/providers/aws/client/openapi/cloudControl/GetResourceRequestStatus.json +0 -655
  120. package/src/providers/aws/client/openapi/cloudControl/ListResourceRequests.json +0 -566
  121. package/src/providers/aws/client/openapi/cloudControl/ListResources.json +0 -913
  122. package/src/providers/aws/client/openapi/cloudControl/UpdateResource.json +0 -1027
  123. package/src/providers/aws/cloudControl/client.ts +0 -69
  124. package/src/providers/aws/cloudControl/requestTracker.ts +0 -138
  125. package/src/providers/aws/cloudControl/resourceCommandPolicyLayer.ts +0 -63
  126. package/src/providers/aws/cloudControl/resourceLifecycle.ts +0 -479
  127. package/src/providers/aws/cloudControl/resources/generated/certificatemanager/certificate/index.ts +0 -179
  128. package/src/providers/aws/cloudControl/resources/generated/kms/key/index.ts +0 -362
  129. package/src/providers/aws/cloudControl/resources/generated/s3/bucket/index.ts +0 -2364
  130. package/src/providers/aws/cloudControl/resources/generated/sns/topic/index.ts +0 -326
  131. package/src/providers/aws/cloudControl/resources/generated/sqs/queue/index.ts +0 -307
  132. package/src/providers/aws/cloudControl/resources/generated/sqs/queueinlinepolicy/index.ts +0 -108
  133. package/src/providers/aws/cloudControl/runtime.ts +0 -127
  134. package/src/providers/aws/management/resourceMapping.ts +0 -343
  135. package/src/providers/aws/management/resourceOperation.ts +0 -57
  136. package/src/providers/aws/management/resourcePolicy.ts +0 -549
  137. package/src/providers/aws/management/resourceRegistration.ts +0 -294
  138. package/src/providers/aws/resources/apiGatewayV2/httpApi.ts +0 -327
  139. package/src/providers/aws/resources/generated/native/resourceCommands.ts +0 -152
  140. package/src/providers/aws/resources/iam/grant.ts +0 -150
  141. package/src/providers/aws/resources/iam/grantCommands.ts +0 -283
  142. package/src/providers/aws/resources/iam/policyDocument.ts +0 -30
  143. package/src/providers/aws/resources/iam/policyStatement.ts +0 -59
  144. package/src/providers/aws/resources/iam/role.ts +0 -426
  145. package/src/providers/aws/resources/iam/roleDeclaration.ts +0 -105
  146. package/src/providers/aws/resources/lambda/function.ts +0 -403
  147. package/src/providers/aws/resources/lambda/functionDeclaration.ts +0 -234
  148. package/src/providers/aws/resources/lambda/permission.ts +0 -363
  149. package/src/providers/aws/resources/stepFunctions/stateMachine.ts +0 -95
  150. package/src/providers/aws/runtime.ts +0 -70
@@ -0,0 +1,540 @@
1
+ import { AwsClient } from "@tradedal/aws-client-core";
2
+ import {
3
+ type AwsJsonRpcPayload,
4
+ type AwsOperation,
5
+ type AwsServiceDefinition,
6
+ AwsServiceError,
7
+ } from "@tradedal/aws-client-core/model";
8
+ import {
9
+ Array as Arr,
10
+ Context,
11
+ Data,
12
+ type Duration,
13
+ Effect,
14
+ Layer,
15
+ Match,
16
+ Option,
17
+ Record,
18
+ Schema,
19
+ Struct,
20
+ } from "effect";
21
+ import {
22
+ ResourceCommand,
23
+ ResourceCommandUnsupported,
24
+ } from "../../../src/core/resource/lifecycle.js";
25
+ import {
26
+ ResourceIdentityMismatch,
27
+ ResourceModel,
28
+ type ResourceNode,
29
+ type ResourceSchemaAnnotation,
30
+ } from "../../../src/core/resource/model.js";
31
+ import { ResourceLifecycle } from "../../../src/core/resource/resourceLifecycle.js";
32
+ import { ResourceStabilization } from "../../../src/core/resource/resourceStabilization.js";
33
+ import {
34
+ providerCommandPolicyTag,
35
+ registerResourceCommands,
36
+ } from "../../../src/providers/resourceCommandPolicyLayer.js";
37
+ import {
38
+ type AwsAssociationConfiguration,
39
+ AwsResourceAssociationConfiguration,
40
+ } from "./resourceAssociationConfiguration.js";
41
+ import { AwsResourceMapping } from "./resourceMapping.js";
42
+
43
+ /**
44
+ * The ResourceOperation type allows a lifecycle definition to use the names
45
+ * from a stack declaration even when AWS expects different field names.
46
+ * The generated operation still determines which requests and replies are valid.
47
+ */
48
+ export type ResourceOperation<Output> = AwsOperation & {
49
+ readonly input: Schema.Codec<Schema.JsonObject, AwsJsonRpcPayload>;
50
+ readonly output: Schema.Codec<Output, unknown>;
51
+ };
52
+
53
+ /**
54
+ * The AwsLifecycleSettings interface specifies which AWS errors a stack may
55
+ * treat as proof that a resource is absent. Keep authorization errors out of
56
+ * that list: a denied read tells the stack nothing about whether the resource
57
+ * exists.
58
+ */
59
+ export interface AwsLifecycleSettings {
60
+ readonly service: AwsServiceDefinition;
61
+ readonly missingErrors: ReadonlyArray<string>;
62
+ readonly timeout: Duration.Input;
63
+ }
64
+
65
+ class AwsResourceCreateConflict extends Data.TaggedError(
66
+ "AwsResourceCreateConflict",
67
+ )<{
68
+ readonly identity: Schema.JsonObject;
69
+ }> {
70
+ override get message() {
71
+ return "AWS already has an object with the requested identity.";
72
+ }
73
+ }
74
+
75
+ /**
76
+ * The AwsResourceLifecycleDefinition interface requires a read that accepts
77
+ * the saved identity on its own. A stack must still be able to verify the
78
+ * object it will delete after you remove the declaration.
79
+ */
80
+ interface AwsResourceLifecycleDefinition {
81
+ readonly schema: ResourceSchemaAnnotation;
82
+ readonly outputsSchema?: Schema.Schema<unknown>;
83
+ readonly generatedName?: {
84
+ readonly property: string;
85
+ readonly maxLength: number;
86
+ };
87
+ /**
88
+ * The createMode option protects resources when AWS uses the same operation
89
+ * to create and overwrite them. For example, PutGroupPolicy would replace
90
+ * the policy document if the group already had a policy with that name.
91
+ * With requireAbsent, the stack checks the group and policy name first. If
92
+ * IAM returns the policy, the command fails with AwsResourceCreateConflict
93
+ * before it sends PutGroupPolicy.
94
+ *
95
+ * @see ../../../refs/aws-api-models/models/iam/service/2010-05-08/iam-2010-05-08.json (PutGroupPolicy)
96
+ */
97
+ readonly createMode?: "requireAbsent";
98
+ readonly create: ResourceOperation<Schema.JsonObject>;
99
+ readonly read: ResourceOperation<Schema.JsonObject>;
100
+ readonly remove: ResourceOperation<unknown>;
101
+ readonly ownership?: {
102
+ readonly identity: Schema.Decoder<Schema.JsonObject>;
103
+ readonly property: string;
104
+ };
105
+ readonly ready?: (observation: Schema.JsonObject) => boolean;
106
+ readonly retryCreate?: (error: AwsServiceError) => boolean;
107
+ }
108
+
109
+ /**
110
+ * The AwsResourceLifecycleMapping type lets a resource use separate AWS
111
+ * calls for changes to its members. Use the association definition when
112
+ * those members are part of the declared resource but require their own AWS
113
+ * requests. An IAM instance profile's role is one example.
114
+ */
115
+ export type AwsResourceLifecycleMapping = AwsResourceLifecycleDefinition &
116
+ (
117
+ | {
118
+ readonly update: ResourceOperation<unknown>;
119
+ readonly associations?: never;
120
+ }
121
+ | {
122
+ readonly associations: AwsAssociationConfiguration;
123
+ readonly update?: never;
124
+ }
125
+ );
126
+
127
+ /**
128
+ * The AwsResourceLifecycleRegistration service is for AWS resources whose
129
+ * native read can confirm a change. If AWS accepts a name that another
130
+ * object can reuse, supply the original provider ID as part of the saved
131
+ * identity. The stack can then reject a different object at that name.
132
+ *
133
+ * @see ../../../refs/effect/packages/effect/src/Context.ts (Service)
134
+ */
135
+ export class AwsResourceLifecycleRegistration extends Context.Service<AwsResourceLifecycleRegistration>()(
136
+ "nomoss/providers/aws/management/resourceLifecycleRegistration/AwsResourceLifecycleRegistration",
137
+ {
138
+ make: Effect.fn("AwsResourceLifecycleRegistration.make")(function* (
139
+ settings: AwsLifecycleSettings,
140
+ definition: AwsResourceLifecycleMapping,
141
+ ) {
142
+ const model = yield* ResourceModel;
143
+ const names = yield* AwsResourceMapping;
144
+ const client = yield* AwsClient;
145
+ const stabilization = yield* ResourceStabilization;
146
+
147
+ const associations = yield* Option.fromUndefinedOr(
148
+ definition.associations,
149
+ ).pipe(
150
+ Option.map((mapping) =>
151
+ AwsResourceAssociationConfiguration.make(settings, mapping),
152
+ ),
153
+ Effect.transposeOption,
154
+ );
155
+
156
+ yield* Option.fromUndefinedOr(definition.outputsSchema).pipe(
157
+ Option.map((outputs) =>
158
+ model.registerOutputsSchema(definition.schema, outputs),
159
+ ),
160
+ Effect.transposeOption,
161
+ );
162
+
163
+ const bindOperation = client.bindOperation(settings.service);
164
+
165
+ const ownership = Option.fromUndefinedOr(definition.ownership);
166
+ const savedIdentity =
167
+ definition.ownership?.identity ?? Schema.toType(definition.read.input);
168
+
169
+ const isMissing = AwsServiceError.hasCode(...settings.missingErrors);
170
+
171
+ const ready = (outputs: Schema.JsonObject) =>
172
+ definition.ready?.(outputs) ?? true;
173
+
174
+ /**
175
+ * The decodeAndCall function rejects extra properties when a stack command
176
+ * submits its AWS request. A typo or an incomplete request definition should
177
+ * fail the command before the stack can report success for a property AWS
178
+ * never received.
179
+ *
180
+ * @see ../client-core/awsClient.ts (AwsClient.bindOperation)
181
+ */
182
+ const decodeAndCall = <Output>(operation: ResourceOperation<Output>) => {
183
+ const call = bindOperation(operation);
184
+ const input = Schema.decodeUnknownEffect(
185
+ Schema.toType(operation.input),
186
+ {
187
+ onExcessProperty: "error",
188
+ },
189
+ );
190
+
191
+ return (properties: Schema.JsonObject) =>
192
+ input(properties).pipe(Effect.flatMap(call));
193
+ };
194
+
195
+ const createResource = decodeAndCall(definition.create);
196
+ const readResource = decodeAndCall(definition.read);
197
+ const removeResource = decodeAndCall(definition.remove);
198
+
199
+ const readIdentity = Schema.decodeUnknownEffect(
200
+ Schema.toType(definition.read.input),
201
+ );
202
+ const removeIdentity = Schema.decodeUnknownEffect(
203
+ Schema.toType(definition.remove.input),
204
+ );
205
+
206
+ /**
207
+ * The readProvider operation rejects a different object at the saved address
208
+ * when the definition supplies an immutable ID. For a child resource, the
209
+ * observation also includes any parent identity that the AWS reply leaves out.
210
+ * An inline policy, for example, needs that identity for later commands.
211
+ *
212
+ * @see ../../../src/core/resource/resourceLifecycle.ts (ResourceLifecycle.make)
213
+ * @see ../../../refs/effect/packages/effect/src/Struct.ts (assign)
214
+ */
215
+ const readProvider = Effect.fnUntraced(function* (
216
+ saved: Schema.JsonObject,
217
+ ) {
218
+ const identity = yield* readIdentity(saved);
219
+
220
+ const returned = yield* readResource(identity);
221
+
222
+ const mismatch = ownership.pipe(
223
+ Option.filter(
224
+ ({ property }) => returned[property] !== saved[property],
225
+ ),
226
+ );
227
+
228
+ yield* mismatch.pipe(
229
+ Option.map(
230
+ ({ property }) =>
231
+ new ResourceIdentityMismatch({
232
+ property,
233
+ expected: saved[property],
234
+ actual: returned[property],
235
+ }),
236
+ ),
237
+ Option.map(Effect.fail),
238
+ Effect.transposeOption,
239
+ );
240
+
241
+ const observed = Struct.assign(identity, returned);
242
+
243
+ return Option.some(observed);
244
+ });
245
+
246
+ /**
247
+ * The creationProperties operation gives an unnamed resource the same
248
+ * generated name on another apply for the same generation. If AWS already
249
+ * received the first request, the retry still addresses that name.
250
+ *
251
+ * @see ./resourceMapping.ts (AwsResourceMapping.resolveName)
252
+ * @see ../../../refs/effect/packages/effect/src/Effect.ts (transposeOption)
253
+ */
254
+ const creationProperties = Effect.fnUntraced(function* (
255
+ node: ResourceNode,
256
+ ) {
257
+ const properties = yield* model.decodeProps(node, Schema.JsonObject);
258
+
259
+ const missingName = Option.fromUndefinedOr(
260
+ definition.generatedName,
261
+ ).pipe(
262
+ Option.filter(({ property }) => !Record.has(properties, property)),
263
+ );
264
+
265
+ const named = yield* missingName.pipe(
266
+ Option.map(
267
+ Effect.fnUntraced(function* ({ property, maxLength }) {
268
+ const name = yield* names.resolveName(node, undefined, maxLength);
269
+
270
+ return Record.set(properties, property, name);
271
+ }),
272
+ ),
273
+ Effect.transposeOption,
274
+ );
275
+
276
+ return Option.getOrElse(named, () => properties);
277
+ });
278
+
279
+ /**
280
+ * The create operation supplies an identity for the first confirmation.
281
+ * An inline policy write can return an empty reply. In that case the stack
282
+ * uses the parent and policy name from the request to find the policy.
283
+ *
284
+ * @see ../../../src/core/resource/resourceStabilization.ts (retrySchedule)
285
+ * @see ../../../refs/effect/packages/effect/src/Effect.ts (retry)
286
+ */
287
+ const create = Effect.fn("AwsResourceLifecycleRegistration.create")(
288
+ function* (node: ResourceNode) {
289
+ const properties = yield* creationProperties(node);
290
+
291
+ yield* readIdentity(properties).pipe(
292
+ Effect.flatMap((identity) =>
293
+ readResource(identity).pipe(
294
+ Effect.flatMap(() =>
295
+ Effect.fail(new AwsResourceCreateConflict({ identity })),
296
+ ),
297
+ ),
298
+ ),
299
+ Effect.catchIf(isMissing, () => Effect.void),
300
+ Effect.when(
301
+ Effect.succeed(definition.createMode === "requireAbsent"),
302
+ ),
303
+ );
304
+
305
+ const request = Option.match(associations, {
306
+ onNone: () => createResource(properties),
307
+ onSome: (operations) =>
308
+ operations
309
+ .createProperties(properties)
310
+ .pipe(Effect.flatMap(createResource)),
311
+ });
312
+
313
+ // The create operation retries only AWS service errors that this
314
+ // definition recognizes. A transport failure may leave the first
315
+ // request's outcome unknown, so this step returns that failure.
316
+ const retry = Option.fromUndefinedOr(definition.retryCreate);
317
+
318
+ const submission = Option.match(retry, {
319
+ onNone: () => request,
320
+ onSome: (accepts) =>
321
+ Effect.retry(request, {
322
+ while: (failure) =>
323
+ failure._tag === "AwsServiceError" && accepts(failure),
324
+ schedule: stabilization.retrySchedule(settings.timeout),
325
+ }),
326
+ });
327
+
328
+ const created = yield* submission;
329
+
330
+ const identified = Struct.assign(properties, created);
331
+ const identity =
332
+ yield* Schema.decodeUnknownEffect(savedIdentity)(identified);
333
+
334
+ return identity;
335
+ },
336
+ );
337
+
338
+ /**
339
+ * The directUpdate operation rejects an update that removes a declared
340
+ * property. For a patch request, the absent field could tell AWS to keep its
341
+ * old value when the stack needs the provider to clear it.
342
+ *
343
+ * @see ../../../docs/application/resource-operations.md (Update a resource)
344
+ * @see ../../../refs/effect/packages/effect/src/Effect.ts (filterOrFail)
345
+ */
346
+ const directUpdate = (operation: ResourceOperation<unknown>) =>
347
+ Effect.fn("AwsResourceLifecycleRegistration.update")(function* (
348
+ node: ResourceNode,
349
+ current: ResourceNode,
350
+ ) {
351
+ const desired = yield* model.decodeProps(node, Schema.JsonObject);
352
+ const saved = yield* model.decodeProps(current, Schema.JsonObject);
353
+
354
+ const removesProperty = Record.some(
355
+ saved,
356
+ (_, property) => !Record.has(desired, property),
357
+ );
358
+
359
+ yield* Effect.succeed(removesProperty).pipe(
360
+ Effect.filterOrFail(
361
+ (removed) => !removed,
362
+ () =>
363
+ new ResourceCommandUnsupported({
364
+ command: ResourceCommand.Apply({
365
+ decision: { _tag: "Update", node, current },
366
+ }),
367
+ }),
368
+ ),
369
+ );
370
+
371
+ const equivalent = Schema.toEquivalence(Schema.Json);
372
+ const changes = Record.filter(
373
+ desired,
374
+ (value, property) =>
375
+ !Record.has(saved, property) ||
376
+ !equivalent(value, saved[property]),
377
+ );
378
+
379
+ const identity = yield* readIdentity(current.outputs);
380
+ const request = Struct.assign(changes, identity);
381
+
382
+ // The directUpdate operation compares values in the form the AWS
383
+ // read returns. For an API Gateway route, a declaration can carry
384
+ // "integrations/abc" while the observation carries the ID "abc".
385
+ // See ./resourceOperation.ts (resourceRequest and relationshipValueCodec).
386
+ const encoded = yield* Schema.encodeEffect(operation.input)(request);
387
+ const normalized = yield* Schema.decodeEffect(operation.input)(
388
+ encoded,
389
+ );
390
+
391
+ const expected = Record.filter(normalized, (_, key) =>
392
+ Record.has(changes, key),
393
+ );
394
+
395
+ // The directUpdate operation verifies the saved ID when the
396
+ // definition supplies one. IAM can reuse a group name, and the
397
+ // update request accepts only the name as its address.
398
+ yield* model
399
+ .decodeOutputs(current, savedIdentity)
400
+ .pipe(
401
+ Effect.flatMap(readProvider),
402
+ Effect.when(Effect.succeed(Option.isSome(ownership))),
403
+ );
404
+
405
+ yield* decodeAndCall(operation)(request);
406
+
407
+ const sameProps = Record.isSubrecordBy(equivalent);
408
+ const accepts = (state: Schema.JsonObject): boolean =>
409
+ ready(state) && sameProps(expected, state);
410
+
411
+ return accepts;
412
+ });
413
+
414
+ const lifecycle = yield* ResourceLifecycle.make(
415
+ {
416
+ identity: savedIdentity,
417
+ lookup: (saved) =>
418
+ readProvider(saved).pipe(
419
+ Effect.catchIf(isMissing, () => Effect.succeedNone),
420
+ ),
421
+ },
422
+ settings.timeout,
423
+ );
424
+
425
+ /**
426
+ * The remove operation accepts the service's absence error after a prior
427
+ * attempt deletes the object. The stack can proceed to its confirmation
428
+ * read and finish teardown.
429
+ */
430
+ const remove = (saved: Schema.JsonObject) =>
431
+ removeIdentity(saved).pipe(
432
+ Effect.flatMap(removeResource),
433
+ Effect.catchIf(isMissing, () => Effect.void),
434
+ );
435
+
436
+ const verification = Option.match(associations, {
437
+ onNone: () => Option.getOrUndefined(Option.map(ownership, () => ({}))),
438
+ onSome: (operations) => ({
439
+ cleanup: (observed: Schema.JsonObject, node: ResourceNode) =>
440
+ operations
441
+ .readSelection(node)
442
+ .pipe(
443
+ Effect.flatMap((previous) =>
444
+ operations.reconcile(observed, previous, []),
445
+ ),
446
+ ),
447
+ }),
448
+ });
449
+
450
+ const destroy = lifecycle.remove(remove, verification);
451
+
452
+ const updateResource = Effect.fnUntraced(function* (
453
+ node: ResourceNode,
454
+ current: ResourceNode,
455
+ ) {
456
+ return yield* Match.value(associations).pipe(
457
+ Match.tag("Some", ({ value: operations }) =>
458
+ lifecycle.observedUpdate(
459
+ (node, current) =>
460
+ Effect.all({
461
+ desired: operations.readSelection(node),
462
+ previous: operations.readSelection(current),
463
+ }),
464
+ (observed, { previous, desired }) =>
465
+ operations.reconcile(observed, previous, desired),
466
+ )(node, current),
467
+ ),
468
+ Match.tag("None", () =>
469
+ Effect.fromOption(Option.fromUndefinedOr(definition.update)).pipe(
470
+ Effect.flatMap((operation) =>
471
+ directUpdate(operation)(node, current),
472
+ ),
473
+ ),
474
+ ),
475
+ Match.exhaustive,
476
+ );
477
+ });
478
+
479
+ const update = lifecycle.update(updateResource);
480
+
481
+ const createOptions = Option.match(associations, {
482
+ onNone: () => ({
483
+ // The create checkpoint records the accepted identity even when
484
+ // the first confirmation fails. A requireAbsent retry can then
485
+ // recognize its own resource without another create request.
486
+ checkpoint: Schema.JsonObject,
487
+ matches: (_created: Schema.JsonObject, observed: Schema.JsonObject) =>
488
+ ready(observed),
489
+ }),
490
+ onSome: (operations) => ({
491
+ checkpoint: Schema.JsonObject,
492
+ configure: (node: ResourceNode, observed: Schema.JsonObject) =>
493
+ operations
494
+ .readSelection(node)
495
+ .pipe(
496
+ Effect.flatMap((desired) =>
497
+ operations.reconcile(observed, [], desired),
498
+ ),
499
+ ),
500
+ }),
501
+ });
502
+
503
+ return ResourceLifecycle.execute(
504
+ ResourceLifecycle.commands({
505
+ Read: (command) => lifecycle.observe(command),
506
+ Create: lifecycle.create(create, createOptions),
507
+ Update: update,
508
+ Destroy: destroy,
509
+ }),
510
+ );
511
+ }),
512
+ },
513
+ ) {
514
+ /**
515
+ * The layer method registers a fixed set of definitions for a caller that
516
+ * already knows which resources it needs. Layer initialization constructs
517
+ * every executor in that set, so use the lazy registry for a provider catalog.
518
+ *
519
+ * @see ../../../refs/effect/packages/effect/src/Layer.ts (effectContext and mergeAll)
520
+ */
521
+ static layer(
522
+ settings: AwsLifecycleSettings,
523
+ ...resources: AwsResourceLifecycleMapping[]
524
+ ) {
525
+ const registrations = Arr.map(resources, (resource) =>
526
+ Layer.effectContext(
527
+ AwsResourceLifecycleRegistration.make(settings, resource).pipe(
528
+ Effect.map(({ execute }) =>
529
+ registerResourceCommands(
530
+ execute,
531
+ providerCommandPolicyTag(resource.schema),
532
+ ),
533
+ ),
534
+ ),
535
+ ),
536
+ );
537
+
538
+ return Layer.mergeAll(Layer.empty, ...registrations);
539
+ }
540
+ }
@@ -0,0 +1,31 @@
1
+ import { Data, Effect } from "effect";
2
+
3
+ class AwsResourceDefinitionLoadFailed extends Data.TaggedError(
4
+ "AwsResourceDefinitionLoadFailed",
5
+ )<{
6
+ readonly resourceType: string;
7
+ readonly cause: unknown;
8
+ }> {
9
+ override get message() {
10
+ return `Could not load AWS management for ${this.resourceType}`;
11
+ }
12
+ }
13
+
14
+ /**
15
+ * The loadAwsManagementDefinition function returns an Effect that the
16
+ * registry can run when a stack command needs the saved resource type.
17
+ * If the import rejects, AwsResourceDefinitionLoadFailed identifies that
18
+ * type and carries the import error.
19
+ *
20
+ * @see ../../../refs/effect/packages/effect/src/Effect.ts (tryPromise)
21
+ */
22
+ export const loadAwsManagementDefinition = <Module, Definition>(
23
+ resourceType: string,
24
+ load: () => Promise<Module>,
25
+ select: (module: Module) => Definition,
26
+ ) =>
27
+ Effect.tryPromise({
28
+ try: load,
29
+ catch: (cause) =>
30
+ new AwsResourceDefinitionLoadFailed({ resourceType, cause }),
31
+ }).pipe(Effect.map(select));
@@ -0,0 +1,62 @@
1
+ import { AwsRetryProgress } from "@tradedal/aws-client-core";
2
+ import { Cache, Effect, Layer, Match, Option, Record, Unify } from "effect";
3
+ import { ResourceCommandProgress } from "../../../src/core/resource/resourceCommandPolicy.js";
4
+ import {
5
+ ResourceCommandPolicyResolver,
6
+ resourceCommandPolicy,
7
+ } from "../../../src/providers/resourceCommandPolicyLayer.js";
8
+ import { awsManagementLoaders } from "../generated/managementRegistry.js";
9
+
10
+ type AwsManagementLoader =
11
+ (typeof awsManagementLoaders)[keyof typeof awsManagementLoaders];
12
+
13
+ /**
14
+ * The awsResourceManagementRegistryLayer Layer resolves native AWS commands
15
+ * from the resource type in stack state. If the registry has no entry, the
16
+ * caller gets None. If an entry exists and its module fails to load, the
17
+ * caller gets an error for that type. Concurrent commands for one type share
18
+ * its initialization within the application runtime.
19
+ *
20
+ * @see ../../../refs/effect/packages/effect/src/Cache.ts (make and get)
21
+ */
22
+ export const awsResourceManagementRegistryLayer = Layer.effect(
23
+ ResourceCommandPolicyResolver,
24
+ Effect.gen(function* () {
25
+ const loaders: Readonly<Record<string, AwsManagementLoader>> =
26
+ awsManagementLoaders;
27
+
28
+ const cache = yield* Cache.make({
29
+ capacity: Record.size(loaders),
30
+ lookup: (resourceType: string) =>
31
+ Record.get(loaders, resourceType).pipe(
32
+ Option.map((load) =>
33
+ Unify.unify(load).pipe(
34
+ Effect.flatMap((definition) => Unify.unify(definition)),
35
+ Effect.map(({ execute }) =>
36
+ resourceCommandPolicy((command) =>
37
+ Effect.gen(function* () {
38
+ const progress = yield* ResourceCommandProgress;
39
+
40
+ return yield* Unify.unify(execute(command)).pipe(
41
+ Effect.provideService(AwsRetryProgress, progress),
42
+ );
43
+ }),
44
+ ),
45
+ ),
46
+ ),
47
+ ),
48
+ Effect.transposeOption,
49
+ ),
50
+ });
51
+
52
+ return {
53
+ resolve: ({ provider, service, resource, operation }) =>
54
+ Match.value(provider).pipe(
55
+ Match.when("aws", () =>
56
+ Cache.get(cache, `${service}:${resource}:${operation}`),
57
+ ),
58
+ Match.orElse(() => Effect.succeedNone),
59
+ ),
60
+ };
61
+ }),
62
+ );