| 1 | import * as grafana from "@distilled.cloud/aws/grafana"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import * as Output from "../../Output.ts"; |
| 6 | import { toWireSeconds } from "../../Util/Duration.ts"; |
| 7 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 8 | import { |
| 9 | CreateWorkspaceServiceAccountToken, |
| 10 | type CreateWorkspaceServiceAccountTokenRequest, |
| 11 | } from "./CreateWorkspaceServiceAccountToken.ts"; |
| 12 | import type { Workspace } from "./Workspace.ts"; |
| 13 | |
| 14 | /** |
| 15 | * Bespoke (not scaffold-built): the runtime request carries a |
| 16 | * `timeToLive: Duration.Input` that is converted to the wire's |
| 17 | * `secondsToLive` via the central Duration util. |
| 18 | */ |
| 19 | export const CreateWorkspaceServiceAccountTokenHttp = Layer.effect( |
| 20 | CreateWorkspaceServiceAccountToken, |
| 21 | Effect.gen(function* () { |
| 22 | const op = yield* grafana.createWorkspaceServiceAccountToken; |
| 23 | |
| 24 | return Effect.fn(function* (workspace: Workspace) { |
| 25 | const workspaceId = yield* workspace.workspaceId; |
| 26 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 27 | const host = yield* Binding.Host; |
| 28 | if (isBindingHost(host)) { |
| 29 | yield* host.bind`Allow(${host}, AWS.Grafana.CreateWorkspaceServiceAccountToken(${workspace}))`( |
| 30 | { |
| 31 | policyStatements: [ |
| 32 | { |
| 33 | Effect: "Allow", |
| 34 | Action: ["grafana:CreateWorkspaceServiceAccountToken"], |
| 35 | Resource: [Output.interpolate`${workspace.workspaceArn}`], |
| 36 | }, |
| 37 | ], |
| 38 | }, |
| 39 | ); |
| 40 | } |
| 41 | } |
| 42 | return Effect.fn( |
| 43 | `AWS.Grafana.CreateWorkspaceServiceAccountToken(${workspace.LogicalId})`, |
| 44 | )(function* (request: CreateWorkspaceServiceAccountTokenRequest) { |
| 45 | return yield* op({ |
| 46 | name: request.name, |
| 47 | serviceAccountId: request.serviceAccountId, |
| 48 | secondsToLive: toWireSeconds(request.timeToLive)!, |
| 49 | workspaceId: yield* workspaceId, |
| 50 | }); |
| 51 | }); |
| 52 | }); |
| 53 | }), |
| 54 | ); |