|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package cloudrunv2_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-provider-google/google/acctest" |
| 11 | + "github.com/hashicorp/terraform-provider-google/google/envvar" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccDataSourceGoogleCloudRunV2Service_basic(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + project := envvar.GetTestProjectFromEnv() |
| 18 | + |
| 19 | + name := fmt.Sprintf("tf-test-cloud-run-v2-service-%d", acctest.RandInt(t)) |
| 20 | + location := "us-central1" |
| 21 | + id := fmt.Sprintf("projects/%s/locations/%s/services/%s", project, location, name) |
| 22 | + |
| 23 | + acctest.VcrTest(t, resource.TestCase{ |
| 24 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 25 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 26 | + Steps: []resource.TestStep{ |
| 27 | + { |
| 28 | + Config: testAccDataSourceGoogleCloudRunV2Service_basic(name, location), |
| 29 | + Check: resource.ComposeTestCheckFunc( |
| 30 | + resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "id", id), |
| 31 | + resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "name", name), |
| 32 | + resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "location", location), |
| 33 | + ), |
| 34 | + }, |
| 35 | + }, |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +func testAccDataSourceGoogleCloudRunV2Service_basic(name, location string) string { |
| 40 | + return fmt.Sprintf(` |
| 41 | +resource "google_cloud_run_v2_service" "hello" { |
| 42 | + name = "%s" |
| 43 | + location = "%s" |
| 44 | + ingress = "INGRESS_TRAFFIC_ALL" |
| 45 | + |
| 46 | + template { |
| 47 | + containers { |
| 48 | + image = "us-docker.pkg.dev/cloudrun/container/hello" |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +
|
| 53 | +data "google_cloud_run_v2_service" "hello" { |
| 54 | + name = google_cloud_run_v2_service.hello.name |
| 55 | + location = google_cloud_run_v2_service.hello.location |
| 56 | +} |
| 57 | +`, name, location) |
| 58 | +} |
| 59 | + |
| 60 | +func TestAccDataSourceGoogleCloudRunV2Service_bindIAMPermission(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + project := envvar.GetTestProjectFromEnv() |
| 64 | + |
| 65 | + name := fmt.Sprintf("tf-test-cloud-run-v2-service-%d", acctest.RandInt(t)) |
| 66 | + location := "us-central1" |
| 67 | + id := fmt.Sprintf("projects/%s/locations/%s/services/%s", project, location, name) |
| 68 | + |
| 69 | + acctest.VcrTest(t, resource.TestCase{ |
| 70 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 71 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 72 | + Steps: []resource.TestStep{ |
| 73 | + { |
| 74 | + Config: testAccDataSourceGoogleCloudRunV2Service_bindIAMPermission(name, location), |
| 75 | + Check: resource.ComposeTestCheckFunc( |
| 76 | + resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "id", id), |
| 77 | + resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "name", name), |
| 78 | + resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "location", location), |
| 79 | + ), |
| 80 | + }, |
| 81 | + }, |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func testAccDataSourceGoogleCloudRunV2Service_bindIAMPermission(name, location string) string { |
| 86 | + return fmt.Sprintf(` |
| 87 | +resource "google_cloud_run_v2_service" "hello" { |
| 88 | + name = "%s" |
| 89 | + location = "%s" |
| 90 | + ingress = "INGRESS_TRAFFIC_ALL" |
| 91 | + |
| 92 | + template { |
| 93 | + containers { |
| 94 | + image = "us-docker.pkg.dev/cloudrun/container/hello" |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +
|
| 99 | +data "google_cloud_run_v2_service" "hello" { |
| 100 | + name = google_cloud_run_v2_service.hello.name |
| 101 | + location = google_cloud_run_v2_service.hello.location |
| 102 | +} |
| 103 | +
|
| 104 | +resource "google_service_account" "foo" { |
| 105 | + account_id = "foo-service-account" |
| 106 | + display_name = "foo-service-account" |
| 107 | +} |
| 108 | +
|
| 109 | +resource "google_cloud_run_v2_service_iam_binding" "foo_run_invoker" { |
| 110 | + name = data.google_cloud_run_v2_service.hello.name |
| 111 | + location = data.google_cloud_run_v2_service.hello.location |
| 112 | +
|
| 113 | + role = "roles/run.invoker" |
| 114 | + members = [ |
| 115 | + "serviceAccount:${google_service_account.foo.email}", |
| 116 | + ] |
| 117 | +} |
| 118 | +`, name, location) |
| 119 | +} |
0 commit comments