@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 110 lines 3.8 KB
Raw
1 import * as appconfig from "@distilled.cloud/aws/appconfig";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Build the ARN of an AppConfig application from its identity.
7 * `arn:aws:appconfig:{region}:{account}:application/{appId}`
8 */
9 export const applicationArn = (region: string, accountId: string, applicationId: string): string =>
10 `arn:aws:appconfig:${region}:${accountId}:application/${applicationId}`;
11
12 /**
13 * Build the ARN of an AppConfig environment.
14 * `arn:aws:appconfig:{region}:{account}:application/{appId}/environment/{envId}`
15 */
16 export const environmentArn = (
17 region: string,
18 accountId: string,
19 applicationId: string,
20 environmentId: string,
21 ): string =>
22 `arn:aws:appconfig:${region}:${accountId}:application/${applicationId}/environment/${environmentId}`;
23
24 /**
25 * Build the ARN of an AppConfig configuration profile.
26 * `arn:aws:appconfig:{region}:{account}:application/{appId}/configurationprofile/{id}`
27 */
28 export const configurationProfileArn = (
29 region: string,
30 accountId: string,
31 applicationId: string,
32 configurationProfileId: string,
33 ): string =>
34 `arn:aws:appconfig:${region}:${accountId}:application/${applicationId}/configurationprofile/${configurationProfileId}`;
35
36 /**
37 * Build the ARN of an AppConfig deployment strategy.
38 * `arn:aws:appconfig:{region}:{account}:deploymentstrategy/{id}`
39 */
40 export const deploymentStrategyArn = (
41 region: string,
42 accountId: string,
43 deploymentStrategyId: string,
44 ): string => `arn:aws:appconfig:${region}:${accountId}:deploymentstrategy/${deploymentStrategyId}`;
45
46 /**
47 * Data-plane resource ARN used by appconfigdata (StartConfigurationSession /
48 * GetLatestConfiguration). Note the segment is `configuration`, not
49 * `configurationprofile`.
50 * `arn:aws:appconfig:{region}:{account}:application/{appId}/environment/{envId}/configuration/{profileId}`
51 */
52 export const configurationDataArn = (
53 region: string,
54 accountId: string,
55 applicationId: string,
56 environmentId: string,
57 configurationProfileId: string,
58 ): string =>
59 `arn:aws:appconfig:${region}:${accountId}:application/${applicationId}/environment/${environmentId}/configuration/${configurationProfileId}`;
60
61 /**
62 * Build the ARN of an AppConfig extension association.
63 * `arn:aws:appconfig:{region}:{account}:extensionassociation/{id}`
64 */
65 export const extensionAssociationArn = (
66 region: string,
67 accountId: string,
68 extensionAssociationId: string,
69 ): string =>
70 `arn:aws:appconfig:${region}:${accountId}:extensionassociation/${extensionAssociationId}`;
71
72 /** Drop undefined values from an AppConfig tag map. */
73 export const toTagRecord = (
74 tags: { [key: string]: string | undefined } | undefined,
75 ): Record<string, string> =>
76 Object.fromEntries(
77 Object.entries(tags ?? {}).filter((entry): entry is [string, string] => entry[1] !== undefined),
78 );
79
80 /**
81 * Read the observed tags of an AppConfig resource. Tag reads are best-effort —
82 * a failure (e.g. a race with deletion) reports no tags.
83 */
84 export const readAppConfigTags = Effect.fn(function* (arn: string) {
85 const response = yield* appconfig
86 .listTagsForResource({ ResourceArn: arn })
87 .pipe(Effect.orElseSucceed(() => undefined));
88 return toTagRecord(response?.Tags);
89 });
90
91 /**
92 * Sync tags on an AppConfig resource: diff the OBSERVED cloud tags against the
93 * desired set and apply only the delta.
94 */
95 export const syncAppConfigTags = Effect.fn(function* (
96 arn: string,
97 desiredTags: Record<string, string>,
98 ) {
99 const observedTags = yield* readAppConfigTags(arn);
100 const { removed, upsert } = diffTags(observedTags, desiredTags);
101 if (upsert.length > 0) {
102 yield* appconfig.tagResource({
103 ResourceArn: arn,
104 Tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
105 });
106 }
107 if (removed.length > 0) {
108 yield* appconfig.untagResource({ ResourceArn: arn, TagKeys: removed });
109 }
110 });