|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// Copyright (c) HashiCorp, Inc. |
| 4 | +// SPDX-License-Identifier: MPL-2.0 |
| 5 | +package storage_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-provider-google/google/acctest" |
| 13 | + "github.com/hashicorp/terraform-provider-google/google/envvar" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccDataSourceGoogleStorageBuckets_basic(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + static_prefix := "tf-bucket-test" |
| 20 | + random_suffix := acctest.RandString(t, 10) |
| 21 | + |
| 22 | + context := map[string]interface{}{ |
| 23 | + "billing_account": envvar.GetTestBillingAccountFromEnv(t), |
| 24 | + "bucket1": static_prefix + "-1-" + random_suffix, |
| 25 | + "bucket2": static_prefix + "-2-" + random_suffix, |
| 26 | + "project_id": static_prefix + "-" + random_suffix, |
| 27 | + "organization": envvar.GetTestOrgFromEnv(t), |
| 28 | + } |
| 29 | + |
| 30 | + acctest.VcrTest(t, resource.TestCase{ |
| 31 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 32 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 33 | + Steps: []resource.TestStep{ |
| 34 | + { |
| 35 | + Config: testAccCheckGoogleStorageBucketsConfig(context), |
| 36 | + Check: resource.ComposeTestCheckFunc( |
| 37 | + // Test schema |
| 38 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.0.location"), |
| 39 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.0.name"), |
| 40 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.0.self_link"), |
| 41 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.0.storage_class"), |
| 42 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.1.location"), |
| 43 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.1.name"), |
| 44 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.1.self_link"), |
| 45 | + resource.TestCheckResourceAttrSet("data.google_storage_buckets.all", "buckets.1.storage_class"), |
| 46 | + // Test content |
| 47 | + resource.TestCheckResourceAttr("data.google_storage_buckets.all", "project", context["project_id"].(string)), |
| 48 | + resource.TestCheckResourceAttr("data.google_storage_buckets.all", "buckets.0.name", context["bucket1"].(string)), |
| 49 | + resource.TestCheckResourceAttr("data.google_storage_buckets.all", "buckets.1.name", context["bucket2"].(string)), |
| 50 | + // Test with project |
| 51 | + resource.TestCheckResourceAttr("data.google_storage_buckets.one", "buckets.0.name", context["bucket1"].(string)), |
| 52 | + // Test prefix |
| 53 | + resource.TestCheckResourceAttr("data.google_storage_buckets.two", "buckets.0.name", context["bucket2"].(string)), |
| 54 | + ), |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func testAccCheckGoogleStorageBucketsConfig(context map[string]interface{}) string { |
| 61 | + return fmt.Sprintf(` |
| 62 | +locals { |
| 63 | + billing_account = "%s" |
| 64 | + bucket_one = "%s" |
| 65 | + bucket_two = "%s" |
| 66 | + organization = "%s" |
| 67 | + project_id = "%s" |
| 68 | +} |
| 69 | +
|
| 70 | +resource "google_project" "acceptance" { |
| 71 | + name = local.project_id |
| 72 | + project_id = local.project_id |
| 73 | + org_id = local.organization |
| 74 | + billing_account = local.billing_account |
| 75 | +} |
| 76 | +
|
| 77 | +resource "google_storage_bucket" "one" { |
| 78 | + force_destroy = true |
| 79 | + location = "EU" |
| 80 | + name = local.bucket_one |
| 81 | + project = google_project.acceptance.project_id |
| 82 | + uniform_bucket_level_access = true |
| 83 | +} |
| 84 | +
|
| 85 | +resource "google_storage_bucket" "two" { |
| 86 | + force_destroy = true |
| 87 | + location = "EU" |
| 88 | + name = local.bucket_two |
| 89 | + project = google_project.acceptance.project_id |
| 90 | + uniform_bucket_level_access = true |
| 91 | +} |
| 92 | +
|
| 93 | +data "google_storage_buckets" "all" { |
| 94 | + project = google_project.acceptance.project_id |
| 95 | + |
| 96 | + depends_on = [ |
| 97 | + google_storage_bucket.one, |
| 98 | + google_storage_bucket.two, |
| 99 | + ] |
| 100 | +} |
| 101 | +
|
| 102 | +data "google_storage_buckets" "one" { |
| 103 | + prefix = "tf-bucket-test-1" |
| 104 | + project = google_project.acceptance.project_id |
| 105 | +
|
| 106 | + depends_on = [ |
| 107 | + google_storage_bucket.one, |
| 108 | + ] |
| 109 | +} |
| 110 | +
|
| 111 | +data "google_storage_buckets" "two" { |
| 112 | + prefix = "tf-bucket-test-2" |
| 113 | + project = google_project.acceptance.project_id |
| 114 | +
|
| 115 | + depends_on = [ |
| 116 | + google_storage_bucket.two, |
| 117 | + ] |
| 118 | +}`, |
| 119 | + context["billing_account"].(string), |
| 120 | + context["bucket1"].(string), |
| 121 | + context["bucket2"].(string), |
| 122 | + context["organization"].(string), |
| 123 | + context["project_id"].(string), |
| 124 | + ) |
| 125 | +} |
0 commit comments