Skip to content

Commit 47da2ac

Browse files
authored
feat: add support for zip deploys to cloudrunv2 services (beta) (#16005)
1 parent 19b6842 commit 47da2ac

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed

mmv1/products/cloudrunv2/Service.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ examples:
167167
cloud_run_service_name: 'cloudrun-iap-service'
168168
ignore_read_extra:
169169
- 'deletion_protection'
170+
- name: 'cloudrunv2_service_zip_deploy'
171+
primary_resource_id: 'default'
172+
primary_resource_name: 'fmt.Sprintf("tf-test-cloudrun-service%s", context["random_suffix"])'
173+
min_version: 'beta'
174+
vars:
175+
cloud_run_service_name: 'cloudrun-zip-service'
176+
targz_path: './test-fixtures/function-source.tar.gz'
177+
ignore_read_extra:
178+
- 'deletion_protection'
170179
virtual_fields:
171180
- name: 'deletion_protection'
172181
description: |
@@ -813,6 +822,33 @@ properties:
813822
description: |-
814823
Source code location of the image.
815824
output: true
825+
- name: 'sourceCode'
826+
type: NestedObject
827+
description: |-
828+
Location of the source.
829+
min_version: 'beta'
830+
properties:
831+
- name: 'cloudStorageSource'
832+
type: NestedObject
833+
description: |-
834+
Cloud Storage source.
835+
exactly_one_of:
836+
- 'cloud_storage_source'
837+
properties:
838+
- name: 'bucket'
839+
type: String
840+
description: |-
841+
The Cloud Storage bucket name.
842+
required: true
843+
- name: 'object'
844+
type: String
845+
description: |-
846+
The Cloud Storage object name.
847+
required: true
848+
- name: 'generation'
849+
type: String
850+
description: |-
851+
The Cloud Storage object generation. The is an int64 value. As with most Google APIs, its JSON representation will be a string instead of an integer.
816852
- name: 'volumes'
817853
type: Array
818854
description: |-
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
resource "google_storage_bucket" "sourcebucket" {
2+
provider = google-beta
3+
name = "${data.google_project.project.project_id}-tf-test-gcf-source%{random_suffix}" # Every bucket name must be globally unique
4+
location = "US"
5+
uniform_bucket_level_access = true
6+
}
7+
8+
resource "google_storage_bucket_object" "source_tar" {
9+
provider = google-beta
10+
name = "function-source.zip"
11+
bucket = google_storage_bucket.sourcebucket.name
12+
source = "./test-fixtures/cr-zip-nodejs-hello.tar.gz"
13+
}
14+
15+
resource "google_cloud_run_v2_service" "{{$.PrimaryResourceId}}" {
16+
provider = google-beta
17+
name = "{{index $.Vars "cloud_run_service_name"}}"
18+
location = "us-central1"
19+
deletion_protection = false
20+
21+
template {
22+
containers {
23+
image = "scratch"
24+
base_image_uri = "us-central1-docker.pkg.dev/serverless-runtimes/google-24-full/runtimes/nodejs24"
25+
command = ["node"]
26+
args = ["index.js"]
27+
source_code {
28+
cloud_storage_source {
29+
bucket = google_storage_bucket.sourcebucket.name
30+
object = google_storage_bucket_object.source_tar.name
31+
generation = google_storage_bucket_object.source_tar.generation
32+
}
33+
}
34+
}
35+
}
36+
}
37+
38+
data "google_project" "project" {
39+
provider = google-beta
40+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package cloudrunv2_test
2+
3+
{{ if ne $.TargetVersionName "ga" }}
4+
import (
5+
"fmt"
6+
"log"
7+
"strconv"
8+
"strings"
9+
"testing"
10+
"time"
11+
12+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
14+
"github.com/hashicorp/terraform-plugin-testing/terraform"
15+
16+
"github.com/hashicorp/terraform-provider-google/google/acctest"
17+
"github.com/hashicorp/terraform-provider-google/google/envvar"
18+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
19+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
20+
21+
"google.golang.org/api/googleapi"
22+
)
23+
24+
var (
25+
_ = fmt.Sprintf
26+
_ = log.Print
27+
_ = strconv.Atoi
28+
_ = strings.Trim
29+
_ = time.Now
30+
_ = resource.TestMain
31+
_ = terraform.NewState
32+
_ = envvar.TestEnvVar
33+
_ = tpgresource.SetLabels
34+
_ = transport_tpg.Config{}
35+
_ = googleapi.Error{}
36+
)
37+
38+
func TestAccCloudRunV2Service_cloudrunv2ServiceZipDeployExample_update(t *testing.T) {
39+
t.Parallel()
40+
41+
context := map[string]interface{}{
42+
"random_suffix": acctest.RandString(t, 10),
43+
}
44+
45+
acctest.VcrTest(t, resource.TestCase{
46+
PreCheck: func() { acctest.AccTestPreCheck(t) },
47+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
48+
CheckDestroy: testAccCheckCloudRunV2ServiceDestroyProducer(t),
49+
Steps: []resource.TestStep{
50+
{
51+
Config: testAccCloudRunV2Service_cloudrunv2ServiceZipDeployExample_basic(context),
52+
},
53+
{
54+
ResourceName: "google_cloud_run_v2_service.default",
55+
ImportState: true,
56+
ImportStateVerify: true,
57+
ImportStateVerifyIgnore: []string{"annotations", "deletion_protection", "labels", "location", "name", "terraform_labels"},
58+
},
59+
{
60+
Config: testAccCloudRunV2Service_cloudrunv2ServiceZipDeployExample_update(context),
61+
ConfigPlanChecks: resource.ConfigPlanChecks{
62+
PreApply: []plancheck.PlanCheck{
63+
plancheck.ExpectResourceAction("google_cloud_run_v2_service.default", plancheck.ResourceActionUpdate),
64+
},
65+
},
66+
},
67+
{
68+
ResourceName: "google_cloud_run_v2_service.default",
69+
ImportState: true,
70+
ImportStateVerify: true,
71+
ImportStateVerifyIgnore: []string{"annotations", "deletion_protection", "labels", "location", "name", "terraform_labels"},
72+
},
73+
},
74+
})
75+
}
76+
77+
func testAccCloudRunV2Service_cloudrunv2ServiceZipDeployExample_basic(context map[string]interface{}) string {
78+
return acctest.Nprintf(`
79+
resource "google_storage_bucket" "sourcebucket" {
80+
provider = google-beta
81+
name = "${data.google_project.project.project_id}-tf-test-gcf-source%{random_suffix}" # Every bucket name must be globally unique
82+
location = "US"
83+
uniform_bucket_level_access = true
84+
}
85+
86+
resource "google_storage_bucket_object" "source_tar" {
87+
provider = google-beta
88+
name = "function-source.zip"
89+
bucket = google_storage_bucket.sourcebucket.name
90+
source = "./test-fixtures/cr-zip-nodejs-hello.tar.gz"
91+
}
92+
93+
resource "google_cloud_run_v2_service" "default" {
94+
provider = google-beta
95+
name = "tf-test-cloudrun-zip-service%{random_suffix}"
96+
location = "us-central1"
97+
deletion_protection = false
98+
99+
template {
100+
containers {
101+
image = "scratch"
102+
base_image_uri = "us-central1-docker.pkg.dev/serverless-runtimes/google-24-full/runtimes/nodejs24"
103+
command = ["node"]
104+
args = ["index.js"]
105+
source_code {
106+
cloud_storage_source {
107+
bucket = google_storage_bucket.sourcebucket.name
108+
object = google_storage_bucket_object.source_tar.name
109+
generation = google_storage_bucket_object.source_tar.generation
110+
}
111+
}
112+
}
113+
}
114+
}
115+
116+
data "google_project" "project" {
117+
provider = google-beta
118+
}
119+
`, context)
120+
}
121+
122+
func testAccCloudRunV2Service_cloudrunv2ServiceZipDeployExample_update(context map[string]interface{}) string {
123+
return acctest.Nprintf(`
124+
resource "google_storage_bucket" "sourcebucket" {
125+
provider = google-beta
126+
name = "${data.google_project.project.project_id}-tf-test-gcf-source%{random_suffix}" # Every bucket name must be globally unique
127+
location = "US"
128+
uniform_bucket_level_access = true
129+
}
130+
131+
resource "google_storage_bucket_object" "source_tar" {
132+
provider = google-beta
133+
name = "function-source.zip"
134+
bucket = google_storage_bucket.sourcebucket.name
135+
source = "./test-fixtures/cr-zip-py-hello.tar.gz"
136+
}
137+
138+
resource "google_cloud_run_v2_service" "default" {
139+
provider = google-beta
140+
name = "tf-test-cloudrun-zip-service%{random_suffix}"
141+
location = "us-central1"
142+
deletion_protection = false
143+
144+
template {
145+
containers {
146+
image = "scratch"
147+
base_image_uri = "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/python313"
148+
command = ["python"]
149+
args = ["main.py"]
150+
source_code {
151+
cloud_storage_source {
152+
bucket = google_storage_bucket.sourcebucket.name
153+
object = google_storage_bucket_object.source_tar.name
154+
generation = google_storage_bucket_object.source_tar.generation
155+
}
156+
}
157+
}
158+
}
159+
}
160+
161+
data "google_project" "project" {
162+
provider = google-beta
163+
}
164+
`, context)
165+
}
166+
{{ end }}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)