|
| 1 | +package cloudrunv2_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-provider-google/google/acctest" |
| 12 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 13 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccCloudRunV2Service_cloudrunv2ServiceFunctionExample_update(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + context := map[string]interface{}{ |
| 20 | + "zip_path": "./test-fixtures/function-source.zip", |
| 21 | + "random_suffix": acctest.RandString(t, 10), |
| 22 | + } |
| 23 | + |
| 24 | + acctest.VcrTest(t, resource.TestCase{ |
| 25 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 26 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 27 | + CheckDestroy: testAccCheckCloudRunV2ServiceDestroyProducer(t), |
| 28 | + Steps: []resource.TestStep{ |
| 29 | + { |
| 30 | + Config: testAccCloudRunV2Service_cloudrunv2ServiceFunctionExample_full(context), |
| 31 | + }, |
| 32 | + { |
| 33 | + ResourceName: "google_cloud_run_v2_service.default", |
| 34 | + ImportState: true, |
| 35 | + ImportStateVerify: true, |
| 36 | + ImportStateVerifyIgnore: []string{"annotations", "deletion_protection", "labels", "location", "name", "terraform_labels"}, |
| 37 | + }, |
| 38 | + { |
| 39 | + Config: testAccCloudRunV2Service_cloudrunv2ServiceFunctionExample_update(context), |
| 40 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 41 | + PreApply: []plancheck.PlanCheck{ |
| 42 | + plancheck.ExpectResourceAction("google_cloud_run_v2_service.default", plancheck.ResourceActionUpdate), |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + ResourceName: "google_cloud_run_v2_service.default", |
| 48 | + ImportState: true, |
| 49 | + ImportStateVerify: true, |
| 50 | + ImportStateVerifyIgnore: []string{"annotations", "deletion_protection", "labels", "location", "name", "terraform_labels"}, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func testAccCloudRunV2Service_cloudrunv2ServiceFunctionExample_full(context map[string]interface{}) string { |
| 57 | + return acctest.Nprintf(` |
| 58 | +resource "google_cloud_run_v2_service" "default" { |
| 59 | + name = "tf-test-cloudrun-service%{random_suffix}" |
| 60 | + location = "us-central1" |
| 61 | + deletion_protection = false |
| 62 | + ingress = "INGRESS_TRAFFIC_ALL" |
| 63 | +
|
| 64 | + template { |
| 65 | + containers { |
| 66 | + image = "us-docker.pkg.dev/cloudrun/container/hello" |
| 67 | + } |
| 68 | + } |
| 69 | + build_config { |
| 70 | + source_location = "gs://${google_storage_bucket.bucket.name}/${google_storage_bucket_object.object.name}" |
| 71 | + function_target = "helloHttp" |
| 72 | + image_uri = "us-docker.pkg.dev/cloudrun/container/hello" |
| 73 | + base_image = "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/nodejs22" |
| 74 | + enable_automatic_updates = true |
| 75 | + worker_pool = "worker-pool" |
| 76 | + environment_variables = { |
| 77 | + FOO_KEY = "FOO_VALUE" |
| 78 | + BAR_KEY = "BAR_VALUE" |
| 79 | + } |
| 80 | + service_account = google_service_account.cloudbuild_service_account.id |
| 81 | + } |
| 82 | + depends_on = [ |
| 83 | + google_project_iam_member.act_as, |
| 84 | + google_project_iam_member.logs_writer |
| 85 | + ] |
| 86 | +} |
| 87 | +
|
| 88 | +data "google_project" "project" { |
| 89 | +} |
| 90 | +
|
| 91 | +resource "google_storage_bucket" "bucket" { |
| 92 | + name = "${data.google_project.project.project_id}-tf-test-gcf-source%{random_suffix}" # Every bucket name must be globally unique |
| 93 | + location = "US" |
| 94 | + uniform_bucket_level_access = true |
| 95 | +} |
| 96 | +
|
| 97 | +resource "google_storage_bucket_object" "object" { |
| 98 | + name = "function-source.zip" |
| 99 | + bucket = google_storage_bucket.bucket.name |
| 100 | + source = "%{zip_path}" # Add path to the zipped function source code |
| 101 | +} |
| 102 | +
|
| 103 | +resource "google_service_account" "cloudbuild_service_account" { |
| 104 | + account_id = "tf-test-build-sa%{random_suffix}" |
| 105 | +} |
| 106 | +
|
| 107 | +resource "google_project_iam_member" "act_as" { |
| 108 | + project = data.google_project.project.project_id |
| 109 | + role = "roles/iam.serviceAccountUser" |
| 110 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 111 | +} |
| 112 | +
|
| 113 | +resource "google_project_iam_member" "logs_writer" { |
| 114 | + project = data.google_project.project.project_id |
| 115 | + role = "roles/logging.logWriter" |
| 116 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 117 | +} |
| 118 | +`, context) |
| 119 | +} |
| 120 | + |
| 121 | +func testAccCloudRunV2Service_cloudrunv2ServiceFunctionExample_update(context map[string]interface{}) string { |
| 122 | + return acctest.Nprintf(` |
| 123 | +resource "google_cloud_run_v2_service" "default" { |
| 124 | + name = "tf-test-cloudrun-service%{random_suffix}" |
| 125 | + location = "us-central1" |
| 126 | + deletion_protection = false |
| 127 | + ingress = "INGRESS_TRAFFIC_ALL" |
| 128 | +
|
| 129 | + template { |
| 130 | + containers { |
| 131 | + image = "us-docker.pkg.dev/cloudrun/container/hello" |
| 132 | + } |
| 133 | + } |
| 134 | + build_config { |
| 135 | + source_location = "gs://${google_storage_bucket.bucket.name}/${google_storage_bucket_object.object.name}" |
| 136 | + function_target = "helloHttp" |
| 137 | + image_uri = "gcr.io/cloudrun/hello:latest" |
| 138 | + base_image = "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/nodejs20" |
| 139 | + enable_automatic_updates = false |
| 140 | + worker_pool = "worker-pool-2" |
| 141 | + environment_variables = { |
| 142 | + FOO_KEY_FOO = "FOO_VALUE_FOO" |
| 143 | + BAR_KEY_BAR = "BAR_VALUE_BAR" |
| 144 | + } |
| 145 | + service_account = google_service_account.cloudbuild_service_account.id |
| 146 | + } |
| 147 | + depends_on = [ |
| 148 | + google_project_iam_member.act_as, |
| 149 | + google_project_iam_member.logs_writer |
| 150 | + ] |
| 151 | +} |
| 152 | +
|
| 153 | +data "google_project" "project" { |
| 154 | +} |
| 155 | +
|
| 156 | +resource "google_storage_bucket" "bucket" { |
| 157 | + name = "${data.google_project.project.project_id}-tf-test-gcf-source%{random_suffix}" # Every bucket name must be globally unique |
| 158 | + location = "US" |
| 159 | + uniform_bucket_level_access = true |
| 160 | +} |
| 161 | +
|
| 162 | +resource "google_storage_bucket_object" "object" { |
| 163 | + name = "function-source-updated.zip" |
| 164 | + bucket = google_storage_bucket.bucket.name |
| 165 | + source = "%{zip_path}" # Add path to the zipped function source code |
| 166 | +} |
| 167 | +
|
| 168 | +resource "google_service_account" "cloudbuild_service_account" { |
| 169 | + account_id = "tf-test-build-sa-updated%{random_suffix}" |
| 170 | +} |
| 171 | +
|
| 172 | +resource "google_project_iam_member" "act_as" { |
| 173 | + project = data.google_project.project.project_id |
| 174 | + role = "roles/iam.serviceAccountUser" |
| 175 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 176 | +} |
| 177 | +
|
| 178 | +resource "google_project_iam_member" "logs_writer" { |
| 179 | + project = data.google_project.project.project_id |
| 180 | + role = "roles/logging.logWriter" |
| 181 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 182 | +} |
| 183 | +`, context) |
| 184 | +} |
0 commit comments