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, 06:00 UTC
| 1 | import * as vpclattice from "@distilled.cloud/aws/vpc-lattice"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, diffTags, hasAlchemyTags, tagRecord } from "alchemy/Tags"; |
| 8 | import { toWireSeconds } from "alchemy/Util/Duration"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import type * as Duration from "effect/Duration"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { retryOnConflict, waitUntilAbsent, waitUntilStable } from "./internal.ts"; |
| 15 | import type { ServiceNetworkAuthType } from "./ServiceNetwork.ts"; |
| 16 | |
| 17 | export class VpcLatticeServiceUnexpectedResponse extends Data.TaggedError( |
| 18 | "VpcLatticeServiceUnexpectedResponse", |
| 19 | )<{ |
| 20 | readonly message: string; |
| 21 | readonly cause?: unknown; |
| 22 | }> {} |
| 23 | |
| 24 | export interface ServiceProps { |
| 25 | /** |
| 26 | * Name of the service. If omitted, a unique name is generated. Immutable — |
| 27 | * changing it replaces the resource. |
| 28 | */ |
| 29 | name?: string; |
| 30 | /** |
| 31 | * Authorization type for the service. |
| 32 | * @default "NONE" |
| 33 | */ |
| 34 | authType?: ServiceNetworkAuthType; |
| 35 | /** |
| 36 | * Custom domain name for the service. Immutable — changing it replaces the |
| 37 | * resource. |
| 38 | */ |
| 39 | customDomainName?: string; |
| 40 | /** |
| 41 | * ARN of an ACM certificate for the custom domain (HTTPS listeners). |
| 42 | */ |
| 43 | certificateArn?: string; |
| 44 | /** |
| 45 | * Idle timeout for connections to the service, e.g. `"60 seconds"` or |
| 46 | * `Duration.minutes(1)` (a bare number is milliseconds). Rounded to whole |
| 47 | * seconds on the wire. |
| 48 | */ |
| 49 | idleTimeout?: Duration.Input; |
| 50 | /** |
| 51 | * User-defined tags to apply to the service. |
| 52 | */ |
| 53 | tags?: Record<string, string>; |
| 54 | } |
| 55 | |
| 56 | export interface Service extends Resource< |
| 57 | "AWS.VpcLattice.Service", |
| 58 | ServiceProps, |
| 59 | { |
| 60 | /** |
| 61 | * Service-assigned unique ID of the service. |
| 62 | */ |
| 63 | serviceId: string; |
| 64 | /** |
| 65 | * ARN of the service. |
| 66 | */ |
| 67 | serviceArn: string; |
| 68 | /** |
| 69 | * Physical name of the service. |
| 70 | */ |
| 71 | name: string; |
| 72 | /** |
| 73 | * Current lifecycle status (e.g. `ACTIVE`). |
| 74 | */ |
| 75 | status: string; |
| 76 | /** |
| 77 | * Lattice-generated DNS name clients resolve the service by. |
| 78 | */ |
| 79 | dnsName?: string; |
| 80 | /** |
| 81 | * Effective authorization type. |
| 82 | */ |
| 83 | authType: ServiceNetworkAuthType; |
| 84 | /** |
| 85 | * Current tags reported for the service. |
| 86 | */ |
| 87 | tags: Record<string, string>; |
| 88 | }, |
| 89 | never, |
| 90 | Providers |
| 91 | > {} |
| 92 | |
| 93 | /** |
| 94 | * An Amazon VPC Lattice service — an independently deployable unit of software |
| 95 | * (running on Lambda, ECS, EC2, or elsewhere) that is made discoverable through |
| 96 | * a service network. Cheap control-plane resource. |
| 97 | * |
| 98 | * ### Creating Services |
| 99 | * **Example:** Basic Service |
| 100 | * ```typescript |
| 101 | * const service = yield* Service("PaymentsService", {}); |
| 102 | * ``` |
| 103 | * |
| 104 | * **Example:** Service with Custom Domain |
| 105 | * ```typescript |
| 106 | * const service = yield* Service("PaymentsService", { |
| 107 | * customDomainName: "payments.internal.example.com", |
| 108 | * certificateArn: cert.certificateArn, |
| 109 | * authType: "AWS_IAM", |
| 110 | * idleTimeout: "60 seconds", |
| 111 | * }); |
| 112 | * ``` |
| 113 | * |
| 114 | * @resource |
| 115 | */ |
| 116 | export const Service = Resource<Service>("AWS.VpcLattice.Service"); |
| 117 | |
| 118 | export const ServiceProvider = () => |
| 119 | Provider.effect( |
| 120 | Service, |
| 121 | Effect.gen(function* () { |
| 122 | const toName = (id: string, props: { name?: string } = {}) => |
| 123 | props.name |
| 124 | ? Effect.succeed(props.name) |
| 125 | : createPhysicalName({ id, maxLength: 40, lowercase: true }); |
| 126 | |
| 127 | // getService only accepts an id or ARN — never a name. |
| 128 | const observe = (serviceIdentifier: string) => |
| 129 | vpclattice |
| 130 | .getService({ serviceIdentifier }) |
| 131 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 132 | |
| 133 | const findByName = (name: string) => |
| 134 | vpclattice.listServices.pages({}).pipe( |
| 135 | Stream.runCollect, |
| 136 | Effect.map((chunk) => |
| 137 | Array.from(chunk) |
| 138 | .flatMap((page) => page.items ?? []) |
| 139 | .find((s) => s.name === name), |
| 140 | ), |
| 141 | Effect.flatMap((summary) => (summary?.id ? observe(summary.id) : Effect.undefined)), |
| 142 | ); |
| 143 | |
| 144 | const syncTags = Effect.fn(function* (arn: string, desiredTags: Record<string, string>) { |
| 145 | const listed = yield* vpclattice.listTagsForResource({ |
| 146 | resourceArn: arn, |
| 147 | }); |
| 148 | const { removed, upsert } = diffTags(tagRecord(listed.tags), desiredTags); |
| 149 | if (upsert.length > 0) { |
| 150 | yield* vpclattice.tagResource({ |
| 151 | resourceArn: arn, |
| 152 | tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 153 | }); |
| 154 | } |
| 155 | if (removed.length > 0) { |
| 156 | yield* vpclattice.untagResource({ |
| 157 | resourceArn: arn, |
| 158 | tagKeys: removed, |
| 159 | }); |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | return { |
| 164 | stables: ["serviceId", "serviceArn", "name"], |
| 165 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 166 | if (!isResolved(news)) return; |
| 167 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 168 | return { action: "replace" } as const; |
| 169 | } |
| 170 | // Custom domain name is fixed at creation time. |
| 171 | if ((olds?.customDomainName ?? undefined) !== news.customDomainName) { |
| 172 | return { action: "replace" } as const; |
| 173 | } |
| 174 | }), |
| 175 | read: Effect.fn(function* ({ id, olds, output }) { |
| 176 | const service = output?.serviceId |
| 177 | ? yield* observe(output.serviceId) |
| 178 | : yield* findByName(yield* toName(id, olds ?? {})); |
| 179 | if (!service?.arn || !service.id) return undefined; |
| 180 | const listed = yield* vpclattice.listTagsForResource({ |
| 181 | resourceArn: service.arn, |
| 182 | }); |
| 183 | const attrs = { |
| 184 | serviceId: service.id, |
| 185 | serviceArn: service.arn, |
| 186 | name: service.name!, |
| 187 | status: service.status ?? "UNKNOWN", |
| 188 | dnsName: service.dnsEntry?.domainName, |
| 189 | authType: (service.authType as ServiceNetworkAuthType) ?? "NONE", |
| 190 | tags: tagRecord(listed.tags), |
| 191 | }; |
| 192 | return (yield* hasAlchemyTags(id, listed.tags)) ? attrs : Unowned(attrs); |
| 193 | }), |
| 194 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 195 | const name = yield* toName(id, news); |
| 196 | const internalTags = yield* createInternalTags(id); |
| 197 | const desiredTags = { ...internalTags, ...news.tags }; |
| 198 | const desiredAuthType = news.authType ?? "NONE"; |
| 199 | const desiredIdleTimeoutSeconds = toWireSeconds(news.idleTimeout); |
| 200 | |
| 201 | const existing = output?.serviceId |
| 202 | ? yield* observe(output.serviceId) |
| 203 | : yield* findByName(name); |
| 204 | |
| 205 | let serviceId: string; |
| 206 | let serviceArn: string; |
| 207 | if (!existing?.arn || !existing.id) { |
| 208 | const created = yield* vpclattice |
| 209 | .createService({ |
| 210 | name, |
| 211 | authType: desiredAuthType, |
| 212 | customDomainName: news.customDomainName, |
| 213 | certificateArn: news.certificateArn, |
| 214 | idleTimeoutSeconds: desiredIdleTimeoutSeconds, |
| 215 | }) |
| 216 | .pipe(Effect.catchTag("ConflictException", () => findByName(name))); |
| 217 | if (!created?.arn || !created.id) { |
| 218 | return yield* new VpcLatticeServiceUnexpectedResponse({ |
| 219 | message: `Failed to create service ${name}`, |
| 220 | }); |
| 221 | } |
| 222 | serviceId = created.id; |
| 223 | serviceArn = created.arn; |
| 224 | } else { |
| 225 | serviceId = existing.id; |
| 226 | serviceArn = existing.arn; |
| 227 | } |
| 228 | |
| 229 | // VPC Lattice services reject updates/tags while CREATE_IN_PROGRESS; |
| 230 | // wait for a stable status before syncing mutable settings. |
| 231 | const stable = yield* waitUntilStable(observe(serviceId)); |
| 232 | const needsUpdate = |
| 233 | (stable?.authType ?? "NONE") !== desiredAuthType || |
| 234 | stable?.certificateArn !== news.certificateArn || |
| 235 | stable?.idleTimeoutSeconds !== desiredIdleTimeoutSeconds; |
| 236 | if (needsUpdate) { |
| 237 | yield* vpclattice.updateService({ |
| 238 | serviceIdentifier: serviceId, |
| 239 | authType: desiredAuthType, |
| 240 | certificateArn: news.certificateArn, |
| 241 | idleTimeoutSeconds: desiredIdleTimeoutSeconds, |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | yield* syncTags(serviceArn, desiredTags); |
| 246 | |
| 247 | const final = yield* observe(serviceId); |
| 248 | yield* session.note(serviceArn); |
| 249 | return { |
| 250 | serviceId, |
| 251 | serviceArn, |
| 252 | name, |
| 253 | status: final?.status ?? stable?.status ?? "ACTIVE", |
| 254 | dnsName: final?.dnsEntry?.domainName ?? stable?.dnsEntry?.domainName, |
| 255 | authType: desiredAuthType, |
| 256 | tags: desiredTags, |
| 257 | }; |
| 258 | }), |
| 259 | list: () => |
| 260 | Effect.gen(function* () { |
| 261 | const summaries = yield* vpclattice.listServices.pages({}).pipe( |
| 262 | Stream.runCollect, |
| 263 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.items ?? [])), |
| 264 | ); |
| 265 | return yield* Effect.forEach( |
| 266 | summaries.filter( |
| 267 | (s): s is typeof s & { id: string; arn: string } => s.id != null && s.arn != null, |
| 268 | ), |
| 269 | (summary) => |
| 270 | Effect.gen(function* () { |
| 271 | const service = yield* observe(summary.id); |
| 272 | const listed = yield* vpclattice.listTagsForResource({ |
| 273 | resourceArn: summary.arn, |
| 274 | }); |
| 275 | return { |
| 276 | serviceId: summary.id, |
| 277 | serviceArn: summary.arn, |
| 278 | name: summary.name!, |
| 279 | status: summary.status ?? "UNKNOWN", |
| 280 | dnsName: summary.dnsEntry?.domainName, |
| 281 | authType: (service?.authType as ServiceNetworkAuthType) ?? "NONE", |
| 282 | tags: tagRecord(listed.tags), |
| 283 | }; |
| 284 | }), |
| 285 | { concurrency: 10 }, |
| 286 | ); |
| 287 | }), |
| 288 | delete: Effect.fn(function* ({ output }) { |
| 289 | yield* retryOnConflict( |
| 290 | vpclattice.deleteService({ serviceIdentifier: output.serviceId }), |
| 291 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 292 | yield* waitUntilAbsent(observe(output.serviceId)); |
| 293 | }), |
| 294 | }; |
| 295 | }), |
| 296 | ); |