@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 124 lines 4.56 KB
Raw
1 import * as config from "@distilled.cloud/aws/config-service";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import { toWireDays } from "alchemy/Util/Duration";
5 import type * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9
10 export interface RetentionConfigurationProps {
11 /**
12 * How long AWS Config retains your recorded configuration items. Accepts
13 * any `Duration.Input` (e.g. `"90 days"`, `Duration.days(365)`); converted
14 * to whole days on the wire (`RetentionPeriodInDays`). AWS accepts between
15 * 30 days and 7 years (2557 days).
16 */
17 retentionPeriod: Duration.Input;
18 }
19
20 export interface RetentionConfiguration extends Resource<
21 "AWS.Config.RetentionConfiguration",
22 RetentionConfigurationProps,
23 {
24 /** Name of the retention configuration. AWS always names it `default`. */
25 retentionConfigurationName: string;
26 /** The retention period in whole days. */
27 retentionPeriodInDays: number;
28 },
29 never,
30 Providers
31 > {}
32
33 /**
34 * The AWS Config retention configuration that controls how long AWS Config
35 * retains your recorded configuration items.
36 *
37 * AWS allows only **one** retention configuration per account per region and
38 * always names it `default` — treat this resource as an account-region
39 * singleton.
40 * ### Configuring Retention
41 * **Example:** Retain configuration items for one year
42 * ```typescript
43 * import * as Config from "@alchemy.run/aws/Config";
44 *
45 * const retention = yield* Config.RetentionConfiguration("Retention", {
46 * retentionPeriod: "365 days",
47 * });
48 * ```
49 *
50 * **Example:** Minimum retention
51 * ```typescript
52 * const retention = yield* Config.RetentionConfiguration("Retention", {
53 * retentionPeriod: "30 days",
54 * });
55 * ```
56 *
57 * @resource
58 */
59 export const RetentionConfiguration = Resource<RetentionConfiguration>(
60 "AWS.Config.RetentionConfiguration",
61 );
62
63 export const RetentionConfigurationProvider = () =>
64 Provider.effect(
65 RetentionConfiguration,
66 Effect.gen(function* () {
67 // The API names the singleton object `default`; describe with no name
68 // filter returns it (or nothing).
69 const observeRetention = config.describeRetentionConfigurations({}).pipe(
70 Effect.map((r) => (r.RetentionConfigurations ?? []).at(0)),
71 Effect.catchTag("NoSuchRetentionConfigurationException", () => Effect.undefined),
72 );
73
74 const toAttrs = (retention: config.RetentionConfiguration) => ({
75 retentionConfigurationName: retention.Name,
76 retentionPeriodInDays: retention.RetentionPeriodInDays,
77 });
78
79 return RetentionConfiguration.Provider.of({
80 stables: ["retentionConfigurationName"],
81 list: () =>
82 config.describeRetentionConfigurations.items({}).pipe(
83 Stream.runCollect,
84 Effect.map((retentions) => Array.from(retentions).map(toAttrs)),
85 ),
86 // Retention configurations are not taggable and AWS fixes the name to
87 // `default`, so there is no ownership marker — an existing retention
88 // configuration is treated as ours (singleton adoption), matching the
89 // DeliveryChannel convention.
90 read: Effect.fn(function* () {
91 const retention = yield* observeRetention;
92 return retention === undefined ? undefined : toAttrs(retention);
93 }),
94 reconcile: Effect.fn(function* ({ news, session }) {
95 const desiredDays = toWireDays(news.retentionPeriod)!;
96
97 // 1. OBSERVE — cloud state is authoritative.
98 const observed = yield* observeRetention;
99
100 // 2+3. ENSURE + SYNC — PutRetentionConfiguration is a full upsert
101 // of the account-region singleton; skip the API on no-op.
102 if (observed === undefined || observed.RetentionPeriodInDays !== desiredDays) {
103 yield* config.putRetentionConfiguration({
104 RetentionPeriodInDays: desiredDays,
105 });
106 }
107
108 const name = observed?.Name ?? "default";
109 yield* session.note(`${name} (${desiredDays} days)`);
110 return {
111 retentionConfigurationName: name,
112 retentionPeriodInDays: desiredDays,
113 };
114 }),
115 delete: Effect.fn(function* ({ output }) {
116 yield* config
117 .deleteRetentionConfiguration({
118 RetentionConfigurationName: output.retentionConfigurationName,
119 })
120 .pipe(Effect.catchTag("NoSuchRetentionConfigurationException", () => Effect.void));
121 }),
122 });
123 }),
124 );