|
| 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 TestAccDataSourceGoogleStorageBucketObjects_basic(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + project := envvar.GetTestProjectFromEnv() |
| 20 | + bucket := "tf-bucket-object-test-" + acctest.RandString(t, 10) |
| 21 | + |
| 22 | + context := map[string]interface{}{ |
| 23 | + "bucket": bucket, |
| 24 | + "project": project, |
| 25 | + "object_0_name": "bee", |
| 26 | + "object_1_name": "fly", |
| 27 | + } |
| 28 | + |
| 29 | + acctest.VcrTest(t, resource.TestCase{ |
| 30 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 31 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 32 | + Steps: []resource.TestStep{ |
| 33 | + { |
| 34 | + Config: testAccCheckGoogleStorageBucketObjectsConfig(context), |
| 35 | + Check: resource.ComposeTestCheckFunc( |
| 36 | + // Test schema |
| 37 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.0.content_type"), |
| 38 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.0.media_link"), |
| 39 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.0.name"), |
| 40 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.0.self_link"), |
| 41 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.0.storage_class"), |
| 42 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.1.content_type"), |
| 43 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.1.media_link"), |
| 44 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.1.name"), |
| 45 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.1.self_link"), |
| 46 | + resource.TestCheckResourceAttrSet("data.google_storage_bucket_objects.my_insects", "bucket_objects.1.storage_class"), |
| 47 | + // Test content |
| 48 | + resource.TestCheckResourceAttr("data.google_storage_bucket_objects.my_insects", "bucket", context["bucket"].(string)), |
| 49 | + resource.TestCheckResourceAttr("data.google_storage_bucket_objects.my_insects", "bucket_objects.0.name", context["object_0_name"].(string)), |
| 50 | + resource.TestCheckResourceAttr("data.google_storage_bucket_objects.my_insects", "bucket_objects.1.name", context["object_1_name"].(string)), |
| 51 | + // Test match_glob |
| 52 | + resource.TestCheckResourceAttr("data.google_storage_bucket_objects.my_bee_glob", "bucket_objects.0.name", context["object_0_name"].(string)), |
| 53 | + // Test prefix |
| 54 | + resource.TestCheckResourceAttr("data.google_storage_bucket_objects.my_fly_prefix", "bucket_objects.0.name", context["object_1_name"].(string)), |
| 55 | + ), |
| 56 | + }, |
| 57 | + }, |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func testAccCheckGoogleStorageBucketObjectsConfig(context map[string]interface{}) string { |
| 62 | + return fmt.Sprintf(` |
| 63 | +resource "google_storage_bucket" "my_insect_cage" { |
| 64 | + force_destroy = true |
| 65 | + location = "EU" |
| 66 | + name = "%s" |
| 67 | + project = "%s" |
| 68 | + uniform_bucket_level_access = true |
| 69 | +} |
| 70 | +
|
| 71 | +resource "google_storage_bucket_object" "bee" { |
| 72 | + bucket = google_storage_bucket.my_insect_cage.name |
| 73 | + content = "bzzzzzt" |
| 74 | + name = "%s" |
| 75 | +} |
| 76 | +
|
| 77 | +resource "google_storage_bucket_object" "fly" { |
| 78 | + bucket = google_storage_bucket.my_insect_cage.name |
| 79 | + content = "zzzzzt" |
| 80 | + name = "%s" |
| 81 | +} |
| 82 | +
|
| 83 | +data "google_storage_bucket_objects" "my_insects" { |
| 84 | + bucket = google_storage_bucket.my_insect_cage.name |
| 85 | +
|
| 86 | + depends_on = [ |
| 87 | + google_storage_bucket_object.bee, |
| 88 | + google_storage_bucket_object.fly, |
| 89 | + ] |
| 90 | +} |
| 91 | +
|
| 92 | +data "google_storage_bucket_objects" "my_bee_glob" { |
| 93 | + bucket = google_storage_bucket.my_insect_cage.name |
| 94 | + match_glob = "b*" |
| 95 | +
|
| 96 | + depends_on = [ |
| 97 | + google_storage_bucket_object.bee, |
| 98 | + ] |
| 99 | +} |
| 100 | +
|
| 101 | +data "google_storage_bucket_objects" "my_fly_prefix" { |
| 102 | + bucket = google_storage_bucket.my_insect_cage.name |
| 103 | + prefix = "f" |
| 104 | +
|
| 105 | + depends_on = [ |
| 106 | + google_storage_bucket_object.fly, |
| 107 | + ] |
| 108 | +}`, |
| 109 | + context["bucket"].(string), |
| 110 | + context["project"].(string), |
| 111 | + context["object_0_name"].(string), |
| 112 | + context["object_1_name"].(string), |
| 113 | + ) |
| 114 | +} |
0 commit comments