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,351 @@
|
|
|
1
|
+
import type * as S3 from "@distilled.cloud/aws/s3";
|
|
2
|
+
import * as s3 from "@distilled.cloud/aws/s3";
|
|
3
|
+
import * as sqs from "@distilled.cloud/aws/sqs";
|
|
4
|
+
import {
|
|
5
|
+
Array as Arr,
|
|
6
|
+
Context,
|
|
7
|
+
Data,
|
|
8
|
+
Effect,
|
|
9
|
+
Equal,
|
|
10
|
+
Match,
|
|
11
|
+
Option,
|
|
12
|
+
Schedule,
|
|
13
|
+
Schema,
|
|
14
|
+
} from "effect";
|
|
15
|
+
|
|
16
|
+
import { annotateResourceSchema } from "../../core/model.js";
|
|
17
|
+
import { decodePendingQueueArnPlaceholder } from "./awsQueue.js";
|
|
18
|
+
|
|
19
|
+
export const BucketNotificationPropsSchema = annotateResourceSchema(
|
|
20
|
+
s3.PutBucketNotificationConfigurationRequest,
|
|
21
|
+
{
|
|
22
|
+
provider: "aws",
|
|
23
|
+
service: "s3",
|
|
24
|
+
resource: "bucket-notification",
|
|
25
|
+
operation: "create",
|
|
26
|
+
stateSecretOutputKeys: [],
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export type BucketNotificationProps = Schema.Schema.Type<
|
|
31
|
+
typeof BucketNotificationPropsSchema
|
|
32
|
+
>;
|
|
33
|
+
|
|
34
|
+
export const BucketNotificationObservedStateSchema = annotateResourceSchema(
|
|
35
|
+
s3.NotificationConfiguration,
|
|
36
|
+
{
|
|
37
|
+
provider: "aws",
|
|
38
|
+
service: "s3",
|
|
39
|
+
resource: "bucket-notification",
|
|
40
|
+
operation: "read",
|
|
41
|
+
stateSecretOutputKeys: [],
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
export type BucketNotificationObservedState = Schema.Schema.Type<
|
|
46
|
+
typeof BucketNotificationObservedStateSchema
|
|
47
|
+
>;
|
|
48
|
+
|
|
49
|
+
export type BucketNotificationState = {
|
|
50
|
+
readonly bucketName: string;
|
|
51
|
+
readonly queueArn: string;
|
|
52
|
+
readonly events: ReadonlyArray<S3.Event>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const BucketNotificationOutputsSchema =
|
|
56
|
+
s3.PutBucketNotificationConfigurationResponse;
|
|
57
|
+
|
|
58
|
+
export type BucketNotificationOutputs = Schema.Schema.Type<
|
|
59
|
+
typeof BucketNotificationOutputsSchema
|
|
60
|
+
>;
|
|
61
|
+
|
|
62
|
+
export class BucketNotificationCreateFailed extends Data.TaggedError(
|
|
63
|
+
"BucketNotificationCreateFailed",
|
|
64
|
+
)<{
|
|
65
|
+
readonly cause: S3.PutBucketNotificationConfigurationError;
|
|
66
|
+
}> {}
|
|
67
|
+
|
|
68
|
+
export class BucketNotificationReadFailed extends Data.TaggedError(
|
|
69
|
+
"BucketNotificationReadFailed",
|
|
70
|
+
)<{
|
|
71
|
+
readonly cause: S3.GetBucketNotificationConfigurationError;
|
|
72
|
+
}> {}
|
|
73
|
+
|
|
74
|
+
export class BucketNotificationQueueConfigurationMissing extends Data.TaggedError(
|
|
75
|
+
"BucketNotificationQueueConfigurationMissing",
|
|
76
|
+
)<{
|
|
77
|
+
readonly bucketName: string;
|
|
78
|
+
}> {}
|
|
79
|
+
|
|
80
|
+
export type BucketNotificationError =
|
|
81
|
+
| BucketNotificationCreateFailed
|
|
82
|
+
| BucketNotificationReadFailed
|
|
83
|
+
| BucketNotificationQueueConfigurationMissing;
|
|
84
|
+
|
|
85
|
+
const emptyNotificationConfiguration =
|
|
86
|
+
BucketNotificationObservedStateSchema.make({});
|
|
87
|
+
|
|
88
|
+
export const bucketNotificationsEqual = (
|
|
89
|
+
left: S3.QueueConfiguration | undefined,
|
|
90
|
+
right: S3.QueueConfiguration,
|
|
91
|
+
) =>
|
|
92
|
+
Option.fromUndefinedOr(left).pipe(
|
|
93
|
+
Option.match({
|
|
94
|
+
onNone: () => false,
|
|
95
|
+
onSome: (existing) =>
|
|
96
|
+
Equal.equals(existing.Id, right.Id) &&
|
|
97
|
+
Equal.equals(existing.QueueArn, right.QueueArn) &&
|
|
98
|
+
Equal.equals(existing.Events, right.Events),
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Notification policies use this adapter to resolve queue refs and change only
|
|
104
|
+
* the graph-managed bucket notification entry.
|
|
105
|
+
*/
|
|
106
|
+
export class BucketNotificationLifecycleService extends Context.Service<BucketNotificationLifecycleService>()(
|
|
107
|
+
"nomoss/providers/aws/awsBucketNotification/BucketNotificationLifecycleService",
|
|
108
|
+
{
|
|
109
|
+
make: Effect.gen(function* () {
|
|
110
|
+
const queueLookupRetryPolicy = Schedule.recurs(20);
|
|
111
|
+
const getBucketNotificationConfiguration =
|
|
112
|
+
yield* s3.getBucketNotificationConfiguration;
|
|
113
|
+
const putBucketNotificationConfiguration =
|
|
114
|
+
yield* s3.putBucketNotificationConfiguration;
|
|
115
|
+
const getQueueAttributes = yield* sqs.getQueueAttributes;
|
|
116
|
+
const getQueueUrl = yield* sqs.getQueueUrl;
|
|
117
|
+
|
|
118
|
+
const firstQueueConfiguration = (props: BucketNotificationProps) =>
|
|
119
|
+
Option.fromUndefinedOr(
|
|
120
|
+
props.NotificationConfiguration.QueueConfigurations,
|
|
121
|
+
).pipe(
|
|
122
|
+
Option.flatMap((queueConfigurations) =>
|
|
123
|
+
Option.fromUndefinedOr(queueConfigurations[0]),
|
|
124
|
+
),
|
|
125
|
+
Option.match({
|
|
126
|
+
onNone: () =>
|
|
127
|
+
Effect.fail(
|
|
128
|
+
new BucketNotificationQueueConfigurationMissing({
|
|
129
|
+
bucketName: props.Bucket,
|
|
130
|
+
}),
|
|
131
|
+
),
|
|
132
|
+
onSome: Effect.succeed,
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
const bucketNotificationState = (
|
|
136
|
+
props: BucketNotificationProps,
|
|
137
|
+
queueConfiguration: S3.QueueConfiguration,
|
|
138
|
+
): BucketNotificationState => ({
|
|
139
|
+
bucketName: props.Bucket,
|
|
140
|
+
queueArn: queueConfiguration.QueueArn,
|
|
141
|
+
events: queueConfiguration.Events,
|
|
142
|
+
});
|
|
143
|
+
const queueConfigurations = (
|
|
144
|
+
configuration: S3.NotificationConfiguration,
|
|
145
|
+
) => configuration.QueueConfigurations ?? [];
|
|
146
|
+
const managedQueueConfiguration = (
|
|
147
|
+
configuration: S3.NotificationConfiguration,
|
|
148
|
+
desired: S3.QueueConfiguration,
|
|
149
|
+
) =>
|
|
150
|
+
queueConfigurations(configuration).find(
|
|
151
|
+
(queueConfiguration) =>
|
|
152
|
+
queueConfiguration.Id === desired.Id ||
|
|
153
|
+
queueConfiguration.QueueArn === desired.QueueArn,
|
|
154
|
+
);
|
|
155
|
+
const mergedNotificationConfiguration = (
|
|
156
|
+
current: S3.NotificationConfiguration,
|
|
157
|
+
desired: S3.QueueConfiguration,
|
|
158
|
+
): S3.NotificationConfiguration => ({
|
|
159
|
+
TopicConfigurations: current.TopicConfigurations,
|
|
160
|
+
QueueConfigurations: Arr.append(
|
|
161
|
+
Arr.filter(
|
|
162
|
+
queueConfigurations(current),
|
|
163
|
+
(queueConfiguration) =>
|
|
164
|
+
queueConfiguration.Id !== desired.Id &&
|
|
165
|
+
queueConfiguration.QueueArn !== desired.QueueArn,
|
|
166
|
+
),
|
|
167
|
+
desired,
|
|
168
|
+
),
|
|
169
|
+
LambdaFunctionConfigurations: current.LambdaFunctionConfigurations,
|
|
170
|
+
EventBridgeConfiguration: current.EventBridgeConfiguration,
|
|
171
|
+
});
|
|
172
|
+
const removedNotificationConfiguration = (
|
|
173
|
+
current: S3.NotificationConfiguration,
|
|
174
|
+
desired: S3.QueueConfiguration,
|
|
175
|
+
): S3.NotificationConfiguration => ({
|
|
176
|
+
TopicConfigurations: current.TopicConfigurations,
|
|
177
|
+
QueueConfigurations: Arr.filter(
|
|
178
|
+
queueConfigurations(current),
|
|
179
|
+
(queueConfiguration) =>
|
|
180
|
+
queueConfiguration.Id !== desired.Id &&
|
|
181
|
+
queueConfiguration.QueueArn !== desired.QueueArn,
|
|
182
|
+
),
|
|
183
|
+
LambdaFunctionConfigurations: current.LambdaFunctionConfigurations,
|
|
184
|
+
EventBridgeConfiguration: current.EventBridgeConfiguration,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const readQueueArn = Effect.fn(
|
|
188
|
+
"BucketNotificationLifecycleService.readQueueArn",
|
|
189
|
+
)(function* (queueArn: string, queueUrl: string) {
|
|
190
|
+
const attributes = yield* getQueueAttributes({
|
|
191
|
+
QueueUrl: queueUrl,
|
|
192
|
+
AttributeNames: ["QueueArn"],
|
|
193
|
+
}).pipe(Effect.retry(queueLookupRetryPolicy));
|
|
194
|
+
|
|
195
|
+
return attributes.Attributes?.QueueArn ?? queueArn;
|
|
196
|
+
});
|
|
197
|
+
const resolvePendingQueueArn = Effect.fn(
|
|
198
|
+
"BucketNotificationLifecycleService.resolvePendingQueueArn",
|
|
199
|
+
)(function* (queueArn: string, queueName: string) {
|
|
200
|
+
const queueUrlOutput = yield* getQueueUrl({
|
|
201
|
+
QueueName: queueName,
|
|
202
|
+
}).pipe(Effect.retry(queueLookupRetryPolicy));
|
|
203
|
+
const queueUrlOption = Schema.decodeUnknownOption(Schema.String)(
|
|
204
|
+
queueUrlOutput.QueueUrl,
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
return yield* Match.value(queueUrlOption).pipe(
|
|
208
|
+
Match.when({ _tag: "None" }, () => Effect.succeed(queueArn)),
|
|
209
|
+
Match.when({ _tag: "Some" }, ({ value: queueUrl }) =>
|
|
210
|
+
readQueueArn(queueArn, queueUrl),
|
|
211
|
+
),
|
|
212
|
+
Match.exhaustive,
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
const resolveQueueArn = Effect.fn(
|
|
216
|
+
"BucketNotificationLifecycleService.resolveQueueArn",
|
|
217
|
+
)(function* (queueArn: string) {
|
|
218
|
+
const pendingQueueName = decodePendingQueueArnPlaceholder(queueArn);
|
|
219
|
+
|
|
220
|
+
return yield* Match.value(pendingQueueName).pipe(
|
|
221
|
+
Match.when({ _tag: "None" }, () => Effect.succeed(queueArn)),
|
|
222
|
+
Match.when({ _tag: "Some" }, ({ value: queueName }) =>
|
|
223
|
+
resolvePendingQueueArn(queueArn, queueName),
|
|
224
|
+
),
|
|
225
|
+
Match.exhaustive,
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
create: Effect.fn("BucketNotificationLifecycleService.create")(
|
|
231
|
+
function* (props: BucketNotificationProps) {
|
|
232
|
+
const desiredQueueConfiguration =
|
|
233
|
+
yield* firstQueueConfiguration(props);
|
|
234
|
+
const queueArn = yield* resolveQueueArn(
|
|
235
|
+
desiredQueueConfiguration.QueueArn,
|
|
236
|
+
);
|
|
237
|
+
const desired = s3.QueueConfiguration.make({
|
|
238
|
+
Id: desiredQueueConfiguration.Id,
|
|
239
|
+
QueueArn: queueArn,
|
|
240
|
+
Events: desiredQueueConfiguration.Events,
|
|
241
|
+
Filter: desiredQueueConfiguration.Filter,
|
|
242
|
+
});
|
|
243
|
+
const currentOption = yield* getBucketNotificationConfiguration({
|
|
244
|
+
Bucket: props.Bucket,
|
|
245
|
+
}).pipe(
|
|
246
|
+
Effect.map(Option.some),
|
|
247
|
+
Effect.catchTag("NoSuchBucket", () =>
|
|
248
|
+
Effect.succeed(Option.none<S3.NotificationConfiguration>()),
|
|
249
|
+
),
|
|
250
|
+
Effect.mapError(
|
|
251
|
+
(cause) => new BucketNotificationReadFailed({ cause }),
|
|
252
|
+
),
|
|
253
|
+
);
|
|
254
|
+
const current = Option.getOrElse(
|
|
255
|
+
currentOption,
|
|
256
|
+
() => emptyNotificationConfiguration,
|
|
257
|
+
);
|
|
258
|
+
const next = mergedNotificationConfiguration(current, desired);
|
|
259
|
+
|
|
260
|
+
yield* putBucketNotificationConfiguration({
|
|
261
|
+
Bucket: props.Bucket,
|
|
262
|
+
NotificationConfiguration: next,
|
|
263
|
+
}).pipe(
|
|
264
|
+
Effect.mapError(
|
|
265
|
+
(cause) => new BucketNotificationCreateFailed({ cause }),
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return bucketNotificationState(props, desired);
|
|
270
|
+
},
|
|
271
|
+
),
|
|
272
|
+
|
|
273
|
+
read: Effect.fn("BucketNotificationLifecycleService.read")(function* (
|
|
274
|
+
props: BucketNotificationProps,
|
|
275
|
+
) {
|
|
276
|
+
const configuration = yield* getBucketNotificationConfiguration({
|
|
277
|
+
Bucket: props.Bucket,
|
|
278
|
+
}).pipe(
|
|
279
|
+
Effect.map(Option.some),
|
|
280
|
+
Effect.catchTag("NoSuchBucket", () =>
|
|
281
|
+
Effect.succeed(Option.none<S3.NotificationConfiguration>()),
|
|
282
|
+
),
|
|
283
|
+
Effect.mapError(
|
|
284
|
+
(cause) => new BucketNotificationReadFailed({ cause }),
|
|
285
|
+
),
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
const observed = yield* Effect.fromOption(configuration).pipe(
|
|
289
|
+
Effect.catchNoSuchElement,
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
return observed;
|
|
293
|
+
}),
|
|
294
|
+
|
|
295
|
+
destroy: Effect.fn("BucketNotificationLifecycleService.destroy")(
|
|
296
|
+
function* (props: BucketNotificationProps) {
|
|
297
|
+
const desired = yield* firstQueueConfiguration(props);
|
|
298
|
+
const currentOption = yield* getBucketNotificationConfiguration({
|
|
299
|
+
Bucket: props.Bucket,
|
|
300
|
+
}).pipe(
|
|
301
|
+
Effect.map(Option.some),
|
|
302
|
+
Effect.catchTag("NoSuchBucket", () =>
|
|
303
|
+
Effect.succeed(Option.none<S3.NotificationConfiguration>()),
|
|
304
|
+
),
|
|
305
|
+
Effect.mapError(
|
|
306
|
+
(cause) => new BucketNotificationReadFailed({ cause }),
|
|
307
|
+
),
|
|
308
|
+
);
|
|
309
|
+
const clearNotification = (value: S3.NotificationConfiguration) =>
|
|
310
|
+
putBucketNotificationConfiguration({
|
|
311
|
+
Bucket: props.Bucket,
|
|
312
|
+
NotificationConfiguration: removedNotificationConfiguration(
|
|
313
|
+
value,
|
|
314
|
+
desired,
|
|
315
|
+
),
|
|
316
|
+
}).pipe(
|
|
317
|
+
Effect.mapError(
|
|
318
|
+
(cause) => new BucketNotificationCreateFailed({ cause }),
|
|
319
|
+
),
|
|
320
|
+
);
|
|
321
|
+
const destroyEffect = Match.value(currentOption).pipe(
|
|
322
|
+
Match.when(Option.isNone, () => Effect.void),
|
|
323
|
+
Match.when(Option.isSome, ({ value }) =>
|
|
324
|
+
clearNotification(value),
|
|
325
|
+
),
|
|
326
|
+
Match.exhaustive,
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
yield* destroyEffect;
|
|
330
|
+
},
|
|
331
|
+
),
|
|
332
|
+
|
|
333
|
+
resolve: Effect.fn("BucketNotificationLifecycleService.resolve")(
|
|
334
|
+
function* (props: BucketNotificationProps) {
|
|
335
|
+
const desired = yield* firstQueueConfiguration(props);
|
|
336
|
+
const queueArn = yield* resolveQueueArn(desired.QueueArn);
|
|
337
|
+
|
|
338
|
+
return s3.QueueConfiguration.make({
|
|
339
|
+
Id: desired.Id,
|
|
340
|
+
QueueArn: queueArn,
|
|
341
|
+
Events: desired.Events,
|
|
342
|
+
Filter: desired.Filter,
|
|
343
|
+
});
|
|
344
|
+
},
|
|
345
|
+
),
|
|
346
|
+
|
|
347
|
+
managedQueueConfiguration,
|
|
348
|
+
};
|
|
349
|
+
}),
|
|
350
|
+
},
|
|
351
|
+
) {}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import type * as S3 from "@distilled.cloud/aws/s3";
|
|
2
|
+
import {
|
|
3
|
+
Array as Arr,
|
|
4
|
+
Context,
|
|
5
|
+
Effect,
|
|
6
|
+
Match,
|
|
7
|
+
Option,
|
|
8
|
+
type Schema,
|
|
9
|
+
} from "effect";
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
PlanDecision,
|
|
13
|
+
type PlanDecision as PlanDecisionValue,
|
|
14
|
+
PlanRepairChange,
|
|
15
|
+
ResourceCommand,
|
|
16
|
+
ResourceCommandResult,
|
|
17
|
+
ResourceCommandUnsupported,
|
|
18
|
+
type ResourceObservation,
|
|
19
|
+
} from "../../core/lifecycle.js";
|
|
20
|
+
import { ResourceModel, type ResourceNode } from "../../core/model.js";
|
|
21
|
+
import { ResourcePolicy } from "../../core/resourcePolicy.js";
|
|
22
|
+
import {
|
|
23
|
+
BucketNotificationLifecycleService,
|
|
24
|
+
type BucketNotificationObservedState,
|
|
25
|
+
BucketNotificationObservedStateSchema,
|
|
26
|
+
BucketNotificationPropsSchema,
|
|
27
|
+
bucketNotificationsEqual,
|
|
28
|
+
} from "./awsBucketNotification.js";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The AWS resource dispatcher reaches bucket notification behavior through
|
|
32
|
+
* this policy, keeping graph commands separate from provider calls.
|
|
33
|
+
*/
|
|
34
|
+
export class BucketNotificationResourcePolicy extends Context.Service<BucketNotificationResourcePolicy>()(
|
|
35
|
+
"nomoss/providers/aws/awsBucketNotificationResourcePolicy/BucketNotificationResourcePolicy",
|
|
36
|
+
{
|
|
37
|
+
make: Effect.gen(function* () {
|
|
38
|
+
const lifecycle = yield* BucketNotificationLifecycleService;
|
|
39
|
+
const model = yield* ResourceModel;
|
|
40
|
+
const resourcePolicy = yield* ResourcePolicy;
|
|
41
|
+
|
|
42
|
+
const formatEvents = (events: ReadonlyArray<S3.Event> | undefined) =>
|
|
43
|
+
Match.value(events).pipe(
|
|
44
|
+
Match.when(undefined, () => undefined),
|
|
45
|
+
Match.orElse((defined) => defined.join(", ")),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const notificationChanges = (
|
|
49
|
+
observed: S3.QueueConfiguration | undefined,
|
|
50
|
+
desired: S3.QueueConfiguration,
|
|
51
|
+
) =>
|
|
52
|
+
Arr.filter(
|
|
53
|
+
[
|
|
54
|
+
PlanRepairChange.Updated({
|
|
55
|
+
path: ["aws", "s3", "bucket-notification", "queue", "Id"],
|
|
56
|
+
before: observed?.Id,
|
|
57
|
+
after: desired.Id,
|
|
58
|
+
}),
|
|
59
|
+
PlanRepairChange.Updated({
|
|
60
|
+
path: ["aws", "s3", "bucket-notification", "queue", "QueueArn"],
|
|
61
|
+
before: observed?.QueueArn,
|
|
62
|
+
after: desired.QueueArn,
|
|
63
|
+
}),
|
|
64
|
+
PlanRepairChange.Updated({
|
|
65
|
+
path: ["aws", "s3", "bucket-notification", "queue", "Events"],
|
|
66
|
+
before: formatEvents(observed?.Events),
|
|
67
|
+
after: formatEvents(desired.Events),
|
|
68
|
+
}),
|
|
69
|
+
],
|
|
70
|
+
(change) =>
|
|
71
|
+
Match.value(change).pipe(
|
|
72
|
+
Match.tag("Updated", ({ before, after }) => before !== after),
|
|
73
|
+
Match.orElse(() => true),
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const notificationDecision = (
|
|
78
|
+
node: ResourceNode,
|
|
79
|
+
state: BucketNotificationObservedState,
|
|
80
|
+
desired: S3.QueueConfiguration,
|
|
81
|
+
) =>
|
|
82
|
+
Match.value(
|
|
83
|
+
bucketNotificationsEqual(
|
|
84
|
+
lifecycle.managedQueueConfiguration(state, desired),
|
|
85
|
+
desired,
|
|
86
|
+
),
|
|
87
|
+
).pipe(
|
|
88
|
+
Match.when(true, () => PlanDecision.NoOp({ node })),
|
|
89
|
+
Match.orElse(() =>
|
|
90
|
+
PlanDecision.Repair({
|
|
91
|
+
node,
|
|
92
|
+
reason: "bucket notification differs from desired state",
|
|
93
|
+
changes: notificationChanges(
|
|
94
|
+
lifecycle.managedQueueConfiguration(state, desired),
|
|
95
|
+
desired,
|
|
96
|
+
),
|
|
97
|
+
}),
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const decidePresent = Effect.fn(
|
|
102
|
+
"BucketNotificationResourcePolicy.decidePresent",
|
|
103
|
+
)(function* (node: ResourceNode, observed: Schema.Json) {
|
|
104
|
+
const props = yield* model.decodeProps(
|
|
105
|
+
node,
|
|
106
|
+
BucketNotificationPropsSchema,
|
|
107
|
+
);
|
|
108
|
+
const state = yield* model.decodeJson(
|
|
109
|
+
BucketNotificationObservedStateSchema,
|
|
110
|
+
observed,
|
|
111
|
+
);
|
|
112
|
+
const desiredOption = yield* lifecycle.resolve(props).pipe(
|
|
113
|
+
Effect.map(Option.some),
|
|
114
|
+
Effect.catchCause(() => Effect.succeed(Option.none())),
|
|
115
|
+
);
|
|
116
|
+
const decision = Option.match(desiredOption, {
|
|
117
|
+
onNone: () => PlanDecision.Create({ node }),
|
|
118
|
+
onSome: (desired) => notificationDecision(node, state, desired),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return decision;
|
|
122
|
+
});
|
|
123
|
+
const read = Effect.fn("BucketNotificationResourcePolicy.read")(
|
|
124
|
+
function* (node: ResourceNode) {
|
|
125
|
+
const props = yield* model.decodeProps(
|
|
126
|
+
node,
|
|
127
|
+
BucketNotificationPropsSchema,
|
|
128
|
+
);
|
|
129
|
+
const output = yield* lifecycle.read(props);
|
|
130
|
+
const result = yield* Effect.fromOption(output).pipe(
|
|
131
|
+
Effect.flatMap((state: BucketNotificationObservedState) =>
|
|
132
|
+
Effect.map(
|
|
133
|
+
model.encodeJson(BucketNotificationObservedStateSchema, state),
|
|
134
|
+
(observed) => resourcePolicy.present(node, observed),
|
|
135
|
+
),
|
|
136
|
+
),
|
|
137
|
+
Effect.catchNoSuchElement,
|
|
138
|
+
Effect.map(Option.getOrElse(() => resourcePolicy.missing(node))),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
return result;
|
|
142
|
+
},
|
|
143
|
+
);
|
|
144
|
+
const diff = Effect.fn("BucketNotificationResourcePolicy.diff")(
|
|
145
|
+
function* (node: ResourceNode, observation: ResourceObservation) {
|
|
146
|
+
const decision = yield* Match.value(observation).pipe(
|
|
147
|
+
Match.tagsExhaustive({
|
|
148
|
+
Missing: () => Effect.succeed(PlanDecision.Create({ node })),
|
|
149
|
+
Unreadable: ({ reason }) =>
|
|
150
|
+
Effect.succeed(PlanDecision.Repair({ node, reason })),
|
|
151
|
+
Drifted: ({ reason }) =>
|
|
152
|
+
Effect.succeed(PlanDecision.Repair({ node, reason })),
|
|
153
|
+
Present: ({ observed }) => decidePresent(node, observed),
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
return ResourceCommandResult.Decided({ decision });
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
const create = Effect.fn("BucketNotificationResourcePolicy.create")(
|
|
161
|
+
function* (node: ResourceNode) {
|
|
162
|
+
const props = yield* model.decodeProps(
|
|
163
|
+
node,
|
|
164
|
+
BucketNotificationPropsSchema,
|
|
165
|
+
);
|
|
166
|
+
yield* lifecycle.create(props);
|
|
167
|
+
|
|
168
|
+
return ResourceCommandResult.Created({ node });
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
const repair = Effect.fn("BucketNotificationResourcePolicy.repair")(
|
|
172
|
+
function* (node: ResourceNode) {
|
|
173
|
+
const props = yield* model.decodeProps(
|
|
174
|
+
node,
|
|
175
|
+
BucketNotificationPropsSchema,
|
|
176
|
+
);
|
|
177
|
+
yield* lifecycle.create(props);
|
|
178
|
+
|
|
179
|
+
return ResourceCommandResult.Updated({ node });
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
const destroy = Effect.fn("BucketNotificationResourcePolicy.destroy")(
|
|
183
|
+
function* (node: ResourceNode) {
|
|
184
|
+
const props = yield* model.decodeProps(
|
|
185
|
+
node,
|
|
186
|
+
BucketNotificationPropsSchema,
|
|
187
|
+
);
|
|
188
|
+
yield* lifecycle.destroy(props);
|
|
189
|
+
|
|
190
|
+
return ResourceCommandResult.Destroyed({ node });
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
const apply = Effect.fn("BucketNotificationResourcePolicy.apply")(
|
|
194
|
+
function* (decision: PlanDecisionValue) {
|
|
195
|
+
return yield* Match.value(decision).pipe(
|
|
196
|
+
Match.tagsExhaustive({
|
|
197
|
+
Create: ({ node }) => create(node),
|
|
198
|
+
Repair: ({ node }) => repair(node),
|
|
199
|
+
Destroy: ({ node }) => destroy(node),
|
|
200
|
+
NoOp: () =>
|
|
201
|
+
Effect.fail(
|
|
202
|
+
new ResourceCommandUnsupported({
|
|
203
|
+
command: ResourceCommand.Apply({ decision }),
|
|
204
|
+
}),
|
|
205
|
+
),
|
|
206
|
+
Update: () =>
|
|
207
|
+
Effect.fail(
|
|
208
|
+
new ResourceCommandUnsupported({
|
|
209
|
+
command: ResourceCommand.Apply({ decision }),
|
|
210
|
+
}),
|
|
211
|
+
),
|
|
212
|
+
Delete: () =>
|
|
213
|
+
Effect.fail(
|
|
214
|
+
new ResourceCommandUnsupported({
|
|
215
|
+
command: ResourceCommand.Apply({ decision }),
|
|
216
|
+
}),
|
|
217
|
+
),
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
},
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
read,
|
|
225
|
+
diff,
|
|
226
|
+
create,
|
|
227
|
+
repair,
|
|
228
|
+
destroy,
|
|
229
|
+
apply,
|
|
230
|
+
execute: Effect.fn("BucketNotificationResourcePolicy.execute")(
|
|
231
|
+
function* (command: ResourceCommand) {
|
|
232
|
+
return yield* Match.value(command).pipe(
|
|
233
|
+
Match.tagsExhaustive({
|
|
234
|
+
Read: ({ node }) => read(node),
|
|
235
|
+
Diff: ({ node, observation }) => diff(node, observation),
|
|
236
|
+
Apply: ({ decision }) => apply(decision),
|
|
237
|
+
Create: ({ node }) => create(node),
|
|
238
|
+
Update: () =>
|
|
239
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
240
|
+
Delete: () =>
|
|
241
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
242
|
+
Destroy: () =>
|
|
243
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
244
|
+
}),
|
|
245
|
+
);
|
|
246
|
+
},
|
|
247
|
+
),
|
|
248
|
+
};
|
|
249
|
+
}),
|
|
250
|
+
},
|
|
251
|
+
) {}
|