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,253 @@
1
+ import { AwsServiceError } from "@tradedal/aws-client-core/model";
2
+ import {
3
+ iamOperations,
4
+ iamService,
5
+ makeIAMClient,
6
+ TagMFADeviceRequest,
7
+ UntagMFADeviceRequest,
8
+ VirtualMFADevice,
9
+ } from "@tradedal/aws-client-iam";
10
+ import {
11
+ Array as Arr,
12
+ Context,
13
+ Data,
14
+ Effect,
15
+ Option,
16
+ Schema,
17
+ String as Str,
18
+ Stream,
19
+ Struct,
20
+ } from "effect";
21
+ import {
22
+ AWSVirtualMFADeviceOutputsSchema,
23
+ AWSVirtualMFADeviceRequestSchema,
24
+ AWSVirtualMFADeviceResourceSchema,
25
+ } from "nomoss/providers/aws/iam/virtualmfadevice";
26
+ import {
27
+ ResourceModel,
28
+ type ResourceNode,
29
+ } from "../../../../../src/core/resource/model.js";
30
+ import { ResourceLifecycle } from "../../../../../src/core/resource/resourceLifecycle.js";
31
+ import { AwsResourceMapping } from "../../resourceMapping.js";
32
+ import { resourceRequest } from "../../resourceOperation.js";
33
+ import { AwsResourceTagOperations } from "../../resourceTagOperations.js";
34
+ import { resourceTiming } from "../../resourceTiming.js";
35
+
36
+ /**
37
+ * The identity schema requires the full IAM serial number. A device name
38
+ * alone cannot address the device because IAM can include a path in the
39
+ * serial number.
40
+ */
41
+ const identity = Schema.Struct({
42
+ SerialNumber: VirtualMFADevice.schema.fields.SerialNumber,
43
+ });
44
+
45
+ const checkpoint = identity.mapFields((fields) => ({
46
+ ...fields,
47
+ VirtualMfaDeviceName: Schema.String,
48
+ Path: Schema.String,
49
+ Users: Schema.Array(Schema.String),
50
+ Tags: VirtualMFADevice.schema.fields.Tags,
51
+ }));
52
+
53
+ const missing = AwsServiceError.hasCode("NoSuchEntity");
54
+
55
+ const tag = resourceRequest(
56
+ iamOperations.TagMFADevice,
57
+ Schema.Struct({
58
+ Parent: identity.fields.SerialNumber,
59
+ Tags: TagMFADeviceRequest.schema.fields.Tags,
60
+ }).pipe(Schema.encodeKeys({ Parent: "SerialNumber" })),
61
+ );
62
+
63
+ const untag = resourceRequest(
64
+ iamOperations.UntagMFADevice,
65
+ Schema.Struct({
66
+ Parent: identity.fields.SerialNumber,
67
+ TagKeys: UntagMFADeviceRequest.schema.fields.TagKeys,
68
+ }).pipe(Schema.encodeKeys({ Parent: "SerialNumber" })),
69
+ );
70
+
71
+ /**
72
+ * The deviceObservation function excludes the seed and QR code from saved
73
+ * observations. Either value can produce authentication codes, so the
74
+ * stack uses the serial number to identify the device and exposes no seed
75
+ * material as an output.
76
+ */
77
+ const deviceObservation = (
78
+ current: VirtualMFADevice,
79
+ ): typeof checkpoint.Type => {
80
+ const { SerialNumber } = current;
81
+
82
+ const resource = Str.split(SerialNumber, ":mfa/")[1];
83
+
84
+ const separator = resource.lastIndexOf("/");
85
+
86
+ const Users = Option.fromUndefinedOr(current.User).pipe(
87
+ Option.map(({ UserName }) => UserName),
88
+ Option.toArray,
89
+ );
90
+
91
+ return Struct.assign(
92
+ {
93
+ SerialNumber,
94
+ VirtualMfaDeviceName: resource.substring(separator + 1),
95
+ Path: `/${resource.substring(0, separator + 1)}`,
96
+ Users,
97
+ },
98
+ Struct.pick(current, ["Tags"]),
99
+ );
100
+ };
101
+
102
+ class VirtualMfaEnrollmentRequired extends Data.TaggedError(
103
+ "VirtualMfaEnrollmentRequired",
104
+ )<{
105
+ readonly reason: string;
106
+ }> {}
107
+
108
+ /**
109
+ * The VirtualMfaDeviceManagement service manages a virtual device apart
110
+ * from its enrollment. Supply an empty Users list. If an operator assigns
111
+ * the device to a user outside the stack, IAM requires that user assignment
112
+ * to end before the stack can delete the device.
113
+ *
114
+ * @see ../../../../../refs/aws-api-models/models/iam/service/2010-05-08/iam-2010-05-08.json (CreateVirtualMFADevice and EnableMFADevice)
115
+ */
116
+ export class VirtualMfaDeviceManagement extends Context.Service<VirtualMfaDeviceManagement>()(
117
+ "nomoss/providers/aws/management/operations/iam/virtualMfaDevice/VirtualMfaDeviceManagement",
118
+ {
119
+ make: Effect.gen(function* () {
120
+ const iam = yield* makeIAMClient;
121
+
122
+ const model = yield* ResourceModel;
123
+
124
+ const names = yield* AwsResourceMapping;
125
+
126
+ const tags = yield* AwsResourceTagOperations.make(iamService, {
127
+ tag,
128
+ untag,
129
+ });
130
+
131
+ yield* model.registerOutputsSchema(
132
+ AWSVirtualMFADeviceResourceSchema,
133
+ AWSVirtualMFADeviceOutputsSchema,
134
+ );
135
+
136
+ /**
137
+ * The prepare operation fails with VirtualMfaEnrollmentRequired if Users
138
+ * contains a name. IAM requires two consecutive authenticator codes to
139
+ * enroll a device, and the declaration cannot supply those codes.
140
+ */
141
+ const prepare = Effect.fnUntraced(function* (node: ResourceNode) {
142
+ const request = yield* model.decodeProps(
143
+ node,
144
+ AWSVirtualMFADeviceRequestSchema,
145
+ );
146
+
147
+ yield* new VirtualMfaEnrollmentRequired({
148
+ reason:
149
+ "Create the virtual device without Users and enroll it separately.",
150
+ }).pipe(
151
+ Effect.when(
152
+ Effect.succeed(Arr.isReadonlyArrayNonEmpty(request.Users)),
153
+ ),
154
+ );
155
+
156
+ return request;
157
+ });
158
+
159
+ /**
160
+ * The lookup operation searches assigned and unassigned devices. If an
161
+ * operator enrolls the saved device, the stack can still find it under the
162
+ * same serial number. Stream.runHead stops once the serial number matches;
163
+ * a page error before a match leaves the result unresolved.
164
+ *
165
+ * @see ../../../../../refs/effect/packages/effect/src/Stream.ts (filter and runHead)
166
+ */
167
+ const lookup = Effect.fnUntraced(function* ({
168
+ SerialNumber,
169
+ }: typeof identity.Type) {
170
+ const device = yield* iam
171
+ .listVirtualMFADevicesPages({ AssignmentStatus: "Any" })
172
+ .pipe(
173
+ Stream.flatMap(({ VirtualMFADevices }) =>
174
+ Stream.fromIterable(VirtualMFADevices),
175
+ ),
176
+ Stream.filter((device) => device.SerialNumber === SerialNumber),
177
+ Stream.runHead,
178
+ );
179
+
180
+ return Option.map(device, deviceObservation);
181
+ });
182
+
183
+ const lifecycle = yield* ResourceLifecycle.make(
184
+ {
185
+ identity,
186
+ lookup,
187
+ },
188
+ resourceTiming.iam,
189
+ );
190
+
191
+ /**
192
+ * The create operation returns IAM's serial number for the checkpoint.
193
+ * If the initial confirmation fails, the next apply can look for the
194
+ * same device by serial number.
195
+ */
196
+ const create = Effect.fn("VirtualMfaDevice.create")(function* (
197
+ node: ResourceNode,
198
+ ) {
199
+ const request = yield* prepare(node);
200
+
201
+ const VirtualMfaDeviceName = yield* names.resolveName(
202
+ node,
203
+ request.VirtualMfaDeviceName,
204
+ 226,
205
+ );
206
+
207
+ const input = Struct.assign(
208
+ { VirtualMFADeviceName: VirtualMfaDeviceName },
209
+ Struct.pick(request, ["Path", "Tags"]),
210
+ );
211
+
212
+ const response = yield* iam.createVirtualMFADevice(input);
213
+
214
+ return deviceObservation(response.VirtualMFADevice);
215
+ });
216
+
217
+ /**
218
+ * The update operation changes tags on the saved device. To remove a key
219
+ * the stack previously declared, omit it from the new declaration. Tags
220
+ * that exist only in IAM remain in place.
221
+ */
222
+ const update = lifecycle.observedUpdate(
223
+ (node, current) =>
224
+ Effect.all({ desired: prepare(node), previous: prepare(current) }),
225
+ Effect.fnUntraced(function* (observed, { desired, previous }) {
226
+ const accepts = yield* tags.reconcile(
227
+ observed.SerialNumber,
228
+ observed.Tags,
229
+ previous.Tags,
230
+ desired.Tags,
231
+ );
232
+
233
+ return (confirmed: typeof checkpoint.Type) => accepts(confirmed.Tags);
234
+ }),
235
+ );
236
+
237
+ return ResourceLifecycle.execute(
238
+ ResourceLifecycle.commands({
239
+ Read: (command) => lifecycle.observe(command),
240
+ Create: lifecycle.create(create, { checkpoint }),
241
+ Update: lifecycle.update(update),
242
+ Destroy: lifecycle.remove(
243
+ (saved) =>
244
+ iam
245
+ .deleteVirtualMFADevice(saved)
246
+ .pipe(Effect.catchIf(missing, () => Effect.void)),
247
+ {},
248
+ ),
249
+ }),
250
+ );
251
+ }),
252
+ },
253
+ ) {}
@@ -0,0 +1,112 @@
1
+ import { Context, Effect, Record, Schema } from "effect";
2
+ import {
3
+ ResourceModel,
4
+ type ResourceNode,
5
+ } from "../../../src/core/resource/model.js";
6
+ import {
7
+ AssociationNames,
8
+ AwsResourceAssociationOperations,
9
+ type ResourceAssociationOperations,
10
+ } from "./resourceAssociations.js";
11
+ import type { AwsLifecycleSettings } from "./resourceLifecycleRegistration.js";
12
+
13
+ /**
14
+ * The AwsAssociationConfiguration definition applies when AWS needs the
15
+ * parent object to exist before it accepts a member. For example, IAM
16
+ * requires an instance profile before it can attach a role to it.
17
+ */
18
+ export interface AwsAssociationConfiguration
19
+ extends ResourceAssociationOperations {
20
+ readonly property: string;
21
+ readonly parent: string;
22
+ readonly capacity: number;
23
+ }
24
+
25
+ /**
26
+ * The AwsResourceAssociationConfiguration service is for member properties
27
+ * of resources the stack creates and deletes. The member list stays part of
28
+ * the resource declaration, and the lifecycle applies it after AWS creates
29
+ * the parent.
30
+ */
31
+ export class AwsResourceAssociationConfiguration extends Context.Service<AwsResourceAssociationConfiguration>()(
32
+ "nomoss/providers/aws/management/resourceAssociationConfiguration/AwsResourceAssociationConfiguration",
33
+ {
34
+ make: Effect.fn("AwsResourceAssociationConfiguration.make")(function* (
35
+ settings: AwsLifecycleSettings,
36
+ definition: AwsAssociationConfiguration,
37
+ ) {
38
+ const model = yield* ResourceModel;
39
+
40
+ const operations = yield* AwsResourceAssociationOperations.make(
41
+ settings,
42
+ definition,
43
+ );
44
+
45
+ const members = Schema.decodeUnknownEffect(AssociationNames);
46
+
47
+ const desired = Schema.decodeUnknownEffect(
48
+ AssociationNames.check(Schema.isMaxLength(definition.capacity)),
49
+ );
50
+
51
+ const parent = Schema.decodeUnknownEffect(Schema.String);
52
+
53
+ /**
54
+ * The createProperties service method returns a request for the parent.
55
+ * It rejects a member list above the provider's limit first, so an invalid
56
+ * declaration cannot leave an empty parent behind.
57
+ */
58
+ const createProperties = Effect.fnUntraced(function* (
59
+ properties: Schema.JsonObject,
60
+ ) {
61
+ yield* desired(properties[definition.property]);
62
+
63
+ return Record.filter(
64
+ properties,
65
+ (_, key) => key !== definition.property,
66
+ );
67
+ });
68
+
69
+ /**
70
+ * The readSelection service method returns the member names for an apply.
71
+ * Resolve resource references before this call so the provider receives
72
+ * actual names. The provider's capacity applies to that resolved list.
73
+ */
74
+ const readSelection = Effect.fnUntraced(function* (node: ResourceNode) {
75
+ const properties = yield* model.decodeProps(node, Schema.JsonObject);
76
+
77
+ return yield* desired(properties[definition.property]);
78
+ });
79
+
80
+ /**
81
+ * The reconcile service method accepts a provider observation of the parent
82
+ * and applies the requested member changes. Pass its check a fresh observation
83
+ * before you save the new state. The check requires the provider to return
84
+ * the member list.
85
+ */
86
+ const reconcile = Effect.fnUntraced(function* (
87
+ observed: Schema.JsonObject,
88
+ previous: typeof AssociationNames.Type,
89
+ selection: typeof AssociationNames.Type,
90
+ ) {
91
+ const Parent = yield* parent(observed[definition.parent]);
92
+
93
+ const current = yield* members(observed[definition.property]);
94
+
95
+ const accepts = yield* operations.reconcile(
96
+ Parent,
97
+ current,
98
+ previous,
99
+ selection,
100
+ );
101
+
102
+ return (confirmed: Schema.JsonObject) => {
103
+ const selection = confirmed[definition.property];
104
+
105
+ return Schema.is(AssociationNames)(selection) && accepts(selection);
106
+ };
107
+ });
108
+
109
+ return { createProperties, readSelection, reconcile };
110
+ }),
111
+ },
112
+ ) {}
@@ -0,0 +1,349 @@
1
+ import { AwsClient } from "@tradedal/aws-client-core";
2
+ import { AwsServiceError } from "@tradedal/aws-client-core/model";
3
+ import {
4
+ Array as Arr,
5
+ Context,
6
+ Effect,
7
+ Match,
8
+ Option,
9
+ Record,
10
+ Schema,
11
+ Stream,
12
+ Struct,
13
+ } from "effect";
14
+ import {
15
+ ResourceCommandResult,
16
+ ResourceObservation,
17
+ } from "../../../src/core/resource/lifecycle.js";
18
+ import {
19
+ ResourceIdentityMismatch,
20
+ ResourceModel,
21
+ type ResourceNode,
22
+ type ResourceSchemaAnnotation,
23
+ } from "../../../src/core/resource/model.js";
24
+ import { ResourceLifecycle } from "../../../src/core/resource/resourceLifecycle.js";
25
+ import { ResourceStabilization } from "../../../src/core/resource/resourceStabilization.js";
26
+ import {
27
+ AssociationNames,
28
+ AwsResourceAssociationOperations,
29
+ type ResourceAssociationOperations,
30
+ } from "./resourceAssociations.js";
31
+ import type {
32
+ AwsLifecycleSettings,
33
+ ResourceOperation,
34
+ } from "./resourceLifecycleRegistration.js";
35
+ import { AwsResourceMapping } from "./resourceMapping.js";
36
+
37
+ /**
38
+ * The AssociationPage schema requires each reply to identify the parent and
39
+ * its members. It also preserves continuation fields such as IAM's Marker, so
40
+ * a member on a later page can count as already present.
41
+ */
42
+ export const AssociationPage = Schema.StructWithRest(
43
+ Schema.Struct({
44
+ ParentId: Schema.String,
45
+ Members: AssociationNames,
46
+ }),
47
+ [Schema.JsonObject],
48
+ );
49
+
50
+ const associationRequest = Schema.Struct({
51
+ Parent: Schema.String,
52
+ Members: AssociationNames,
53
+ });
54
+
55
+ const identity = Schema.Struct({
56
+ Id: Schema.String,
57
+ Parent: Schema.String,
58
+ ParentId: Schema.String,
59
+ });
60
+
61
+ const association = identity.mapFields((fields) => ({
62
+ ...fields,
63
+ Members: AssociationNames,
64
+ }));
65
+
66
+ /**
67
+ * The AwsResourceAssociationMapping interface defines which links a stack
68
+ * can manage on an object that exists independently of that declaration.
69
+ * Each page must identify the parent by its immutable ID, since the provider
70
+ * may reuse a name after deletion.
71
+ */
72
+ export interface AwsResourceAssociationMapping {
73
+ readonly schema: ResourceSchemaAnnotation;
74
+ readonly outputsSchema: Schema.Schema<unknown>;
75
+ readonly request: Schema.Decoder<typeof associationRequest.Type>;
76
+ readonly list: ResourceOperation<typeof AssociationPage.Type>;
77
+ readonly operations: ResourceAssociationOperations;
78
+ readonly observationKeys: {
79
+ readonly parent: string;
80
+ readonly members: string;
81
+ };
82
+ }
83
+
84
+ /**
85
+ * The AwsResourceAssociationRegistration service treats the selected links
86
+ * as the stack resource. For example, a group can remain in IAM after the
87
+ * stack removes the memberships named in its declaration.
88
+ */
89
+ export class AwsResourceAssociationRegistration extends Context.Service<AwsResourceAssociationRegistration>()(
90
+ "nomoss/providers/aws/management/resourceAssociationRegistration/AwsResourceAssociationRegistration",
91
+ {
92
+ make: Effect.fn("AwsResourceAssociationRegistration.make")(function* (
93
+ settings: AwsLifecycleSettings,
94
+ definition: AwsResourceAssociationMapping,
95
+ ) {
96
+ const client = yield* AwsClient;
97
+
98
+ const model = yield* ResourceModel;
99
+
100
+ const names = yield* AwsResourceMapping;
101
+
102
+ const stabilization = yield* ResourceStabilization;
103
+
104
+ yield* model.registerOutputsSchema(
105
+ definition.schema,
106
+ definition.outputsSchema,
107
+ );
108
+
109
+ const list = client.bindOperation(settings.service)(definition.list);
110
+
111
+ const listPages = client.bindOperationPages(settings.service)(
112
+ definition.list,
113
+ );
114
+
115
+ const operations = yield* AwsResourceAssociationOperations.make(
116
+ settings,
117
+ definition.operations,
118
+ );
119
+
120
+ const missing = AwsServiceError.hasCode(...settings.missingErrors);
121
+
122
+ /**
123
+ * The readParent operation requires one complete view of the parent before
124
+ * a command changes its memberships. Stream.runCollect fails if any page
125
+ * fails. If the provider returns different parent IDs across pages, the
126
+ * operation also rejects the result.
127
+ *
128
+ * @see ../../../refs/effect/packages/effect/src/Stream.ts (runCollect)
129
+ */
130
+ const readParent = Effect.fnUntraced(function* (Parent: string) {
131
+ const stream = Match.value(
132
+ Record.isEmptyRecord(definition.list.pagination),
133
+ ).pipe(
134
+ Match.when(true, () => Stream.fromEffect(list({ Parent }))),
135
+ Match.orElse(() => listPages({ Parent })),
136
+ );
137
+
138
+ const pages = yield* Stream.runCollect(stream);
139
+
140
+ const { ParentId } = yield* Effect.fromOption(Arr.head(pages));
141
+
142
+ const replaced = Arr.findFirst(
143
+ pages,
144
+ (page) => page.ParentId !== ParentId,
145
+ );
146
+
147
+ yield* replaced.pipe(
148
+ Option.map(
149
+ (page) =>
150
+ new ResourceIdentityMismatch({
151
+ property: definition.observationKeys.parent,
152
+ expected: ParentId,
153
+ actual: page.ParentId,
154
+ }),
155
+ ),
156
+ Option.map(Effect.fail),
157
+ Effect.transposeOption,
158
+ );
159
+
160
+ const Members = Arr.flatMap(pages, (page) => page.Members);
161
+ const parent: Omit<typeof association.Type, "Id"> = {
162
+ Parent,
163
+ ParentId,
164
+ Members,
165
+ };
166
+
167
+ return parent;
168
+ });
169
+
170
+ /**
171
+ * The lookup operation returns absence if the provider reports that the
172
+ * parent no longer exists. A different ID at the saved name is an identity
173
+ * conflict, so the caller cannot treat the result as absence.
174
+ */
175
+ const lookup = Effect.fnUntraced(
176
+ function* (saved: typeof identity.Type) {
177
+ const parent = yield* readParent(saved.Parent);
178
+
179
+ yield* new ResourceIdentityMismatch({
180
+ property: definition.observationKeys.parent,
181
+ expected: saved.ParentId,
182
+ actual: parent.ParentId,
183
+ }).pipe(
184
+ Effect.when(Effect.succeed(saved.ParentId !== parent.ParentId)),
185
+ );
186
+
187
+ const result: typeof association.Type = { ...parent, Id: saved.Id };
188
+
189
+ return Option.some(result);
190
+ },
191
+ Effect.catchIf(missing, () =>
192
+ Effect.succeed(Option.none<typeof association.Type>()),
193
+ ),
194
+ );
195
+
196
+ const lifecycle = yield* ResourceLifecycle.make(
197
+ { identity, lookup },
198
+ settings.timeout,
199
+ );
200
+
201
+ const readRequest = (node: ResourceNode) =>
202
+ Schema.decodeUnknownEffect(definition.request)(node.props);
203
+
204
+ /**
205
+ * The reconcileMembers operation returns a check for the next observation.
206
+ * When AWS accepts an add or remove before the change appears in a read,
207
+ * the check requires the new state before the lifecycle accepts the update.
208
+ */
209
+ const reconcileMembers = Effect.fnUntraced(function* (
210
+ current: typeof association.Type,
211
+ previous: typeof AssociationNames.Type,
212
+ desired: typeof AssociationNames.Type,
213
+ ) {
214
+ const accepts = yield* operations.reconcile(
215
+ current.Parent,
216
+ current.Members,
217
+ previous,
218
+ desired,
219
+ );
220
+
221
+ return (state: typeof association.Type) => accepts(state.Members);
222
+ });
223
+
224
+ /**
225
+ * The reserveIdentity operation captures the parent's provider ID before the
226
+ * first write. If an apply stops partway through, the next apply can use the
227
+ * checkpoint to tell the original parent from an object with the same name.
228
+ */
229
+ const reserveIdentity = Effect.fnUntraced(function* (node: ResourceNode) {
230
+ const { Parent } = yield* readRequest(node);
231
+
232
+ const parent = yield* readParent(Parent);
233
+
234
+ const Id = yield* names.resolveName(node, undefined, 128);
235
+
236
+ return { ...parent, Id };
237
+ });
238
+
239
+ /**
240
+ * The observe operation reports the association resource as absent if none
241
+ * of the declared members remain. If some remain, its result lists those
242
+ * members, even when the parent has other links as well.
243
+ */
244
+ const observe = Effect.fnUntraced(function* ({
245
+ node,
246
+ }: {
247
+ readonly node: ResourceNode;
248
+ }) {
249
+ const { Members } = yield* readRequest(node);
250
+
251
+ const saved = yield* model.decodeOutputs(node, identity);
252
+
253
+ const current = yield* lookup(saved);
254
+
255
+ const present = current.pipe(
256
+ Option.map((state) => Arr.intersection(Members, state.Members)),
257
+ Option.filter((members) => Arr.isReadonlyArrayNonEmpty(members)),
258
+ );
259
+
260
+ const observed = Option.match(present, {
261
+ onNone: () => ResourceObservation.Missing({ node }),
262
+ onSome: (members) =>
263
+ ResourceObservation.Present({
264
+ node,
265
+ observed: Struct.assign(
266
+ { Id: saved.Id },
267
+ {
268
+ [definition.observationKeys.parent]: saved.Parent,
269
+ [definition.observationKeys.members]: members,
270
+ },
271
+ ),
272
+ }),
273
+ });
274
+
275
+ return ResourceCommandResult.Observed({ observation: observed });
276
+ });
277
+
278
+ /**
279
+ * The destroy operation completes when the provider no longer lists any
280
+ * member selected by the saved declaration. Other memberships on the same
281
+ * parent can remain present.
282
+ */
283
+ const destroy = Effect.fnUntraced(function* ({
284
+ node,
285
+ }: {
286
+ readonly node: ResourceNode;
287
+ }) {
288
+ const { Members } = yield* readRequest(node);
289
+
290
+ const saved = yield* model.decodeOutputs(node, identity);
291
+
292
+ const current = yield* lookup(saved);
293
+
294
+ yield* Option.map(current, (state) =>
295
+ reconcileMembers(state, Members, []),
296
+ ).pipe(Effect.transposeOption);
297
+
298
+ yield* lookup(saved).pipe(
299
+ Effect.map(
300
+ Option.filter((state) =>
301
+ Arr.some(Members, (member) =>
302
+ Arr.contains(state.Members, member),
303
+ ),
304
+ ),
305
+ ),
306
+ stabilization.waitForAbsence(settings.timeout),
307
+ );
308
+
309
+ return ResourceCommandResult.Destroyed({ node });
310
+ });
311
+
312
+ return ResourceLifecycle.execute(
313
+ ResourceLifecycle.commands({
314
+ Read: observe,
315
+ Create: lifecycle.create(reserveIdentity, {
316
+ checkpoint: association,
317
+ configure: (node, state) =>
318
+ readRequest(node).pipe(
319
+ Effect.flatMap(({ Members }) =>
320
+ reconcileMembers(state, [], Members),
321
+ ),
322
+ ),
323
+ }),
324
+ Update: lifecycle.update(
325
+ lifecycle.observedUpdate(
326
+ (node, current) =>
327
+ Effect.all({
328
+ desired: readRequest(node),
329
+ previous: readRequest(current),
330
+ }),
331
+ (state, { desired, previous }) =>
332
+ new ResourceIdentityMismatch({
333
+ property: definition.observationKeys.parent,
334
+ expected: state.Parent,
335
+ actual: desired.Parent,
336
+ }).pipe(
337
+ Effect.when(Effect.succeed(desired.Parent !== state.Parent)),
338
+ Effect.andThen(
339
+ reconcileMembers(state, previous.Members, desired.Members),
340
+ ),
341
+ ),
342
+ ),
343
+ ),
344
+ Destroy: destroy,
345
+ }),
346
+ );
347
+ }),
348
+ },
349
+ ) {}