| 1 | import * as cloudtasks from "@distilled.cloud/gcp/cloudtasks_v2"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import { CreateTask, type CreateTaskRequest } from "./CreateTask.ts"; |
| 5 | import type { Queue } from "./Queue.ts"; |
| 6 | import { bindGcpHost } from "../Host.ts"; |
| 7 | import { grantFor } from "../HttpBinding.ts"; |
| 8 | |
| 9 | /** |
| 10 | * HTTP implementation of {@link CreateTask}. |
| 11 | * |
| 12 | * @layer |
| 13 | * @provides GCP.CloudTasks.CreateTask |
| 14 | */ |
| 15 | export const CreateTaskHttp = Layer.effect( |
| 16 | CreateTask, |
| 17 | Effect.gen(function* () { |
| 18 | const createTask = yield* cloudtasks.createProjectsLocationsQueuesTasks; |
| 19 | return Effect.fn(function* (queue: Queue) { |
| 20 | yield* bindGcpHost({ |
| 21 | tag: "GCP.CloudTasks.CreateTask", |
| 22 | resource: queue, |
| 23 | iam: [ |
| 24 | grantFor( |
| 25 | { role: "roles/cloudtasks.enqueuer", on: "cloudtasks.queue" }, |
| 26 | queue.name, |
| 27 | ), |
| 28 | ], |
| 29 | }); |
| 30 | const name = yield* queue.name; |
| 31 | return Effect.fn(`GCP.CloudTasks.CreateTask(${queue.LogicalId})`)( |
| 32 | function* (request: CreateTaskRequest) { |
| 33 | return yield* createTask({ |
| 34 | ...request, |
| 35 | parent: yield* name, |
| 36 | }); |
| 37 | }, |
| 38 | ); |
| 39 | }); |
| 40 | }), |
| 41 | ); |