|
| 1 | +import { ITerraformDependable } from "cdktf"; |
| 2 | +import { Construct } from "constructs"; |
| 3 | +import { cloudRunV2Service, cloudRunV2ServiceIamMember } from "@cdktf/provider-google"; |
| 4 | +import { Region } from "@terraform-cdk-constructs/google-compute-engine"; |
| 5 | +import { DockerImage } from "@terraform-cdk-constructs/google-artifact-registry-assets"; |
| 6 | +import { CloudRunRoles, GrantConfig, IGrantable } from "@terraform-cdk-constructs/google-iam"; |
| 7 | + |
| 8 | +export interface ServiceConfig { |
| 9 | + readonly name: string; |
| 10 | + readonly containers: ContainerConfig[]; |
| 11 | + readonly location: Region; |
| 12 | + /** |
| 13 | + * The minimum number of serving instances that this resource should have. |
| 14 | + * |
| 15 | + * @default 0 |
| 16 | + */ |
| 17 | + readonly minimumInstanceCount?: number; |
| 18 | + /** |
| 19 | + * The maximum number of serving instances that this resource should have. |
| 20 | + * |
| 21 | + * @default 100 |
| 22 | + */ |
| 23 | + readonly maximumInstanceCount?: number; |
| 24 | + /** |
| 25 | + * Allows unauthenticated invocations of the service. Use this when creating a public API or website. |
| 26 | + * |
| 27 | + * @default false |
| 28 | + */ |
| 29 | + readonly allowUnauthenticatedInvocations?: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +export class Service extends Construct { |
| 33 | + public readonly resource: cloudRunV2Service.CloudRunV2Service; |
| 34 | + private readonly location: Region; |
| 35 | + |
| 36 | + constructor(scope: Construct, id: string, config: ServiceConfig) { |
| 37 | + super(scope, id); |
| 38 | + |
| 39 | + const allowUnauthenticatedInvocations = config.allowUnauthenticatedInvocations ?? false; |
| 40 | + const minimumInstanceCount = config.minimumInstanceCount ?? 0; |
| 41 | + const maximumInstanceCount = config.maximumInstanceCount ?? 100; |
| 42 | + |
| 43 | + if (minimumInstanceCount > maximumInstanceCount) { |
| 44 | + throw new Error(`google-cloud-run.ServiceConfig.minimumInstanceCount cannot be larger than google-cloud-run.ServiceConfig.maximumInstanceCount, minimum is ${config.minimumInstanceCount}, maximum is ${config.maximumInstanceCount}`); |
| 45 | + } |
| 46 | + |
| 47 | + this.location = config.location; |
| 48 | + |
| 49 | + const containers: cloudRunV2Service.CloudRunV2ServiceTemplateContainers[] = []; |
| 50 | + const dependsOn: ITerraformDependable[] = []; |
| 51 | + |
| 52 | + config.containers.forEach((c) => { |
| 53 | + if (c.imageUri && c.imageAsset) { |
| 54 | + throw new Error("Only google-cloud-run.ContainerConfig.imageUri or google-cloud-run.ContainerConfig.imageAsset may be defined, both are."); |
| 55 | + } |
| 56 | + |
| 57 | + let container: cloudRunV2Service.CloudRunV2ServiceTemplateContainers; |
| 58 | + if (c.imageUri) { |
| 59 | + container = { |
| 60 | + image: c.imageUri, |
| 61 | + }; |
| 62 | + } else if (c.imageAsset) { |
| 63 | + container = { |
| 64 | + image: c.imageAsset.uri, |
| 65 | + }; |
| 66 | + dependsOn.push(c.imageAsset.resource); |
| 67 | + } else { |
| 68 | + throw new Error("Either google-cloud-run.ContainerConfig.imageUri or google-cloud-run.ContainerConfig.imageAsset must be defined, neither are."); |
| 69 | + } |
| 70 | + |
| 71 | + containers.push(container); |
| 72 | + }); |
| 73 | + |
| 74 | + this.resource = new cloudRunV2Service.CloudRunV2Service(this, "resource", { |
| 75 | + location: this.location, |
| 76 | + name: config.name, |
| 77 | + template: { |
| 78 | + containers: containers, |
| 79 | + scaling: { |
| 80 | + maxInstanceCount: maximumInstanceCount, |
| 81 | + minInstanceCount: minimumInstanceCount, |
| 82 | + }, |
| 83 | + }, |
| 84 | + dependsOn: dependsOn, |
| 85 | + }); |
| 86 | + |
| 87 | + if (allowUnauthenticatedInvocations) { |
| 88 | + this.grantInvoker("all-users", { |
| 89 | + grantMember: "allUsers", |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public grantInvoker(id: string, grantee: IGrantable): cloudRunV2ServiceIamMember.CloudRunV2ServiceIamMember { |
| 95 | + return this.grant(id, grantee, { |
| 96 | + id: this.resource.name, |
| 97 | + role: CloudRunRoles.INVOKER, |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + private grant(id: string, grantee: IGrantable, config: GrantConfig): cloudRunV2ServiceIamMember.CloudRunV2ServiceIamMember { |
| 102 | + return new cloudRunV2ServiceIamMember.CloudRunV2ServiceIamMember(this, id, { |
| 103 | + location: this.location, |
| 104 | + member: grantee.grantMember, |
| 105 | + name: config.id, |
| 106 | + role: config.role, |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export interface ContainerConfig { |
| 112 | + /** |
| 113 | + * An Artifact Registry or Container Registry Docker image URI. |
| 114 | + * |
| 115 | + * If provided, 'imageAsset', must be 'undefined'. |
| 116 | + */ |
| 117 | + readonly imageUri?: string; |
| 118 | + /** |
| 119 | + * A Docker image asset that is located either in Artifact Registry or Container Registry. |
| 120 | + * |
| 121 | + * If provided, 'imageUri', must be 'undefined'. |
| 122 | + */ |
| 123 | + readonly imageAsset?: DockerImage; |
| 124 | +} |
0 commit comments