1import * as transcoder from "@distilled.cloud/gcp/transcoder_v1";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { CreateJob, type CreateJobRequest } from "./CreateJob.ts";
5import { lastSegment, parentOfName } from "./internal.ts";
6import type { JobTemplate } from "./JobTemplate.ts";
7import { bindGcpHost } from "../Host.ts";
8
9/**
10 * HTTP implementation of {@link CreateJob}.
11 *
12 * @layer
13 * @provides GCP.Transcoder.CreateJob
14 */
15export const CreateJobHttp = Layer.effect(
16 CreateJob,
17 Effect.gen(function* () {
18 const createProjectsLocationsJobs =
19 yield* transcoder.createProjectsLocationsJobs;
20 return Effect.fn(function* (template: JobTemplate) {
21 yield* bindGcpHost({
22 tag: "GCP.Transcoder.CreateJob",
23 resource: template,
24 // Narrowest predefined role with transcoder.jobs.create; Transcoder
25 // has no resource-level IAM, so this is a project grant.
26 iam: [{ role: "roles/transcoder.editor" }],
27 });
28 const name = yield* template.name;
29 const location = yield* template.location;
30 return Effect.fn(`GCP.Transcoder.CreateJob(${template.LogicalId})`)(
31 function* (request?: CreateJobRequest) {
32 const templateName = yield* name;
33 const templateLocation = yield* location;
34 const templateId = lastSegment(templateName);
35 const config = request?.body?.config;
36 return yield* createProjectsLocationsJobs({
37 parent: parentOfName(templateName, templateLocation),
38 body: {
39 ...request?.body,
40 templateId:
41 request?.body?.templateId ??
42 (config === undefined ? templateId : undefined),
43 },
44 });
45 },
46 );
47 });
48 }),
49);