Skip to content

Commit 9cb7286

Browse files
Taliesin MillhouseTaliesin Millhouse
authored andcommitted
feat(google-cloud-run): added package
1 parent e9b5223 commit 9cb7286

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

packages/google/cloud-run/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# @terraform-cdk-constructs/google-cloud-run
2+
3+
![NPN version](https://img.shields.io/npm/v/@terraform-cdk-constructs/google-cloud-run)
4+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/strongishllama/terraform-cdk-constructs/main/LICENSE)
5+
[![Publish](https://github.com/strongishllama/terraform-cdk-constructs/actions/workflows/publish.yaml/badge.svg)](https://github.com/strongishllama/terraform-cdk-constructs/actions/workflows/publish.yaml)
6+
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/strongishllama/terraform-cdk-constructs)
7+
8+
## Installation
9+
10+
```
11+
npm install @terraform-cdk-constructs/google-cloud-run
12+
```

packages/google/cloud-run/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./service";
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"name": "@terraform-cdk-constructs/google-cloud-run",
3+
"version": "0.0.0",
4+
"description": "Terraform CDK Constructs for Google Cloud Run",
5+
"main": "index.js",
6+
"types": "index.ts",
7+
"license": "MIT",
8+
"scripts": {
9+
"build": "tsc",
10+
"compile": "tsc --pretty",
11+
"get": "cdktf get",
12+
"format": "prettier --write '**/*.ts'",
13+
"format-check": "prettier --check '**/*.ts'",
14+
"jsii:build": "jsii",
15+
"jsii:build:watch": "jsii --watch",
16+
"jsii:package": "jsii-pacmak",
17+
"synth": "cdktf synth",
18+
"test": "jest",
19+
"test:watch": "jest --watch",
20+
"upgrade": "npm i cdktf@latest cdktf-cli@latest",
21+
"upgrade:next": "npm i cdktf@next cdktf-cli@next",
22+
"watch": "tsc -w"
23+
},
24+
"engines": {
25+
"node": ">=14.0"
26+
},
27+
"dependencies": {
28+
"@terraform-cdk-constructs/google-artifact-registry-assets": "0.0.0",
29+
"@terraform-cdk-constructs/google-compute-engine": "0.0.0",
30+
"@terraform-cdk-constructs/google-iam": "0.0.0"
31+
},
32+
"devDependencies": {
33+
"@cdktf/provider-google": "^8.0.0",
34+
"@types/jest": "^29.5.1",
35+
"@types/node": "^18.15.0",
36+
"cdktf": "^0.17.0",
37+
"constructs": "^10.2.41",
38+
"jest": "^29.5.0",
39+
"jsii": "^5.1.0",
40+
"jsii-pacmak": "^1.82.0",
41+
"lerna": "^6.6.2",
42+
"prettier": "^2.8.8",
43+
"ts-jest": "^29.0.5",
44+
"ts-node": "^10.9.1",
45+
"typescript": "^4.9.5"
46+
},
47+
"peerDependencies": {
48+
"@cdktf/provider-google": "^8.0.0",
49+
"cdktf": "^0.17.0",
50+
"constructs": "^10.2.41"
51+
},
52+
"author": {
53+
"name": "Taliesin Millhouse",
54+
"url": "https://millhouse.dev"
55+
},
56+
"keywords": [
57+
"cdktf"
58+
],
59+
"repository": {
60+
"url": "https://github.com/strongishllama/terraform-cdk-constructs",
61+
"type": "git"
62+
},
63+
"stability": "experimental",
64+
"jsii": {
65+
"outdir": "dist",
66+
"versionFormat": "full",
67+
"targets": {
68+
"java": {
69+
"package": "terraform.cdk.constructs.google.cloud.run",
70+
"maven": {
71+
"groupId": "terraform-cdk-constructs",
72+
"artifactId": "google-cloud-run"
73+
}
74+
},
75+
"python": {
76+
"distName": "terraform-cdk-constructs.google-cloud-run",
77+
"module": "terraform_cdk_constructs.google_cloud_run"
78+
},
79+
"dotnet": {
80+
"namespace": "TerraformCdkConstructs",
81+
"packageId": "GoogleCloudRun",
82+
"iconUrl": "https://raw.githubusercontent.com/todo.png"
83+
},
84+
"go": {
85+
"moduleName": "github.com/strongishllama/terraform-cdk-constructs/google-cloud-run",
86+
"packageName": "google_cloud_run"
87+
}
88+
}
89+
}
90+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./service";
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)