|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// ---------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// *** AUTO GENERATED CODE *** Type: Handwritten *** |
| 6 | +// |
| 7 | +// ---------------------------------------------------------------------------- |
| 8 | +// |
| 9 | +// This code is generated by Magic Modules using the following: |
| 10 | +// |
| 11 | +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/bigquery/data_source_google_bigquery_table_test.go |
| 12 | +// |
| 13 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 14 | +// overwritten during the next generation cycle. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +package bigquery_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "regexp" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 26 | + "github.com/hashicorp/terraform-provider-google/google/acctest" |
| 27 | + "github.com/hashicorp/terraform-provider-google/google/envvar" |
| 28 | +) |
| 29 | + |
| 30 | +func TestAccDataSourceGoogleBigqueryTable_basic(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + context := map[string]interface{}{ |
| 34 | + "random_suffix": acctest.RandString(t, 10), |
| 35 | + } |
| 36 | + |
| 37 | + expectedID := fmt.Sprintf("projects/%s/datasets/%s/tables/%s", envvar.GetTestProjectFromEnv(), fmt.Sprintf("tf_test_ds_%s", context["random_suffix"]), fmt.Sprintf("tf_test_table_%s", context["random_suffix"])) |
| 38 | + |
| 39 | + acctest.VcrTest(t, resource.TestCase{ |
| 40 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 41 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 42 | + CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t), |
| 43 | + Steps: []resource.TestStep{ |
| 44 | + { |
| 45 | + Config: testAccDataSourceGoogleBigqueryTable_basic(context), |
| 46 | + Check: resource.ComposeTestCheckFunc( |
| 47 | + resource.TestCheckResourceAttr("data.google_bigquery_table.example", "table_id", fmt.Sprintf("tf_test_table_%s", context["random_suffix"])), |
| 48 | + resource.TestCheckResourceAttr("data.google_bigquery_table.example", "dataset_id", fmt.Sprintf("tf_test_ds_%s", context["random_suffix"])), |
| 49 | + resource.TestCheckResourceAttrSet("data.google_bigquery_table.example", "schema"), |
| 50 | + resource.TestCheckResourceAttr("data.google_bigquery_table.example", "id", expectedID), |
| 51 | + resource.TestCheckResourceAttrWith("data.google_bigquery_table.example", "schema", func(schema string) error { |
| 52 | + var parsedSchema []map[string]interface{} |
| 53 | + |
| 54 | + if err := json.Unmarshal([]byte(schema), &parsedSchema); err != nil { |
| 55 | + return fmt.Errorf("failed to parse schema JSON: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + if len(parsedSchema) > 0 { |
| 59 | + if parsedSchema[0]["name"] != "name" { |
| 60 | + return fmt.Errorf("expected fields[0].name to be 'name', got '%v'", parsedSchema[0]["name"]) |
| 61 | + } |
| 62 | + if parsedSchema[0]["type"] != "STRING" { |
| 63 | + return fmt.Errorf("expected fields[0].type to be 'STRING', got '%v'", parsedSchema[0]["type"]) |
| 64 | + } |
| 65 | + if parsedSchema[0]["mode"] != "NULLABLE" { |
| 66 | + return fmt.Errorf("expected fields[0].mode to be 'NULLABLE', got '%v'", parsedSchema[0]["mode"]) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if len(parsedSchema) > 2 { |
| 71 | + if parsedSchema[2]["name"] != "address" { |
| 72 | + return fmt.Errorf("expected fields[2].name to be 'address', got '%v'", parsedSchema[2]["name"]) |
| 73 | + } |
| 74 | + if subFields, ok := parsedSchema[2]["fields"].([]interface{}); ok && len(subFields) > 1 { |
| 75 | + subField := subFields[1].(map[string]interface{}) |
| 76 | + if subField["name"] != "zip" { |
| 77 | + return fmt.Errorf("expected fields[2].fields[1].name to be 'zip', got '%v'", subField["name"]) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if len(parsedSchema) > 4 { |
| 83 | + if parsedSchema[4]["name"] != "policy_tag_test" { |
| 84 | + return fmt.Errorf("expected fields[4].name to be 'policy_tag_test', got '%v'", parsedSchema[4]["name"]) |
| 85 | + } |
| 86 | + if policyTags, ok := parsedSchema[4]["policyTags"].(map[string]interface{}); ok { |
| 87 | + if names, ok := policyTags["names"].([]interface{}); ok && len(names) > 0 { |
| 88 | + if !regexp.MustCompile("^projects/[^/]+/locations/us-central1/taxonomies/[^/]+/policyTags/[^/]+$").MatchString(names[0].(string)) { |
| 89 | + return fmt.Errorf("policy tag does not match expected pattern") |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | + }), |
| 97 | + ), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +func testAccDataSourceGoogleBigqueryTable_basic(context map[string]interface{}) string { |
| 104 | + return acctest.Nprintf(` |
| 105 | +
|
| 106 | + resource "google_data_catalog_policy_tag" "test" { |
| 107 | + taxonomy = google_data_catalog_taxonomy.test.id |
| 108 | + display_name = "Low security" |
| 109 | + description = "A policy tag normally associated with low security items" |
| 110 | + } |
| 111 | + |
| 112 | + resource "google_data_catalog_taxonomy" "test" { |
| 113 | + region = "us-central1" |
| 114 | + display_name = "taxonomy_%{random_suffix}" |
| 115 | + description = "A collection of policy tags" |
| 116 | + activated_policy_types = ["FINE_GRAINED_ACCESS_CONTROL"] |
| 117 | + } |
| 118 | +
|
| 119 | + resource "google_bigquery_dataset" "test" { |
| 120 | + dataset_id = "tf_test_ds_%{random_suffix}" |
| 121 | + friendly_name = "testing" |
| 122 | + description = "This is a test description" |
| 123 | + location = "us-central1" |
| 124 | + default_table_expiration_ms = 3600000 |
| 125 | + } |
| 126 | +
|
| 127 | + resource "google_bigquery_table" "test" { |
| 128 | + dataset_id = google_bigquery_dataset.test.dataset_id |
| 129 | + table_id = "tf_test_table_%{random_suffix}" |
| 130 | + deletion_protection = false |
| 131 | + depends_on = [google_data_catalog_policy_tag.test] |
| 132 | + schema = <<EOF |
| 133 | + [ |
| 134 | + { |
| 135 | + "name": "name", |
| 136 | + "type": "STRING", |
| 137 | + "mode": "NULLABLE" |
| 138 | + }, |
| 139 | + { |
| 140 | + "name": "age", |
| 141 | + "type": "INTEGER", |
| 142 | + "mode": "NULLABLE", |
| 143 | + "description": "Age of the person" |
| 144 | + }, |
| 145 | + { |
| 146 | + "name": "address", |
| 147 | + "type": "RECORD", |
| 148 | + "mode": "NULLABLE", |
| 149 | + "fields": [ |
| 150 | + { |
| 151 | + "name": "street", |
| 152 | + "type": "STRING", |
| 153 | + "mode": "NULLABLE" |
| 154 | + }, |
| 155 | + { |
| 156 | + "name": "zip", |
| 157 | + "type": "STRING", |
| 158 | + "mode": "NULLABLE" |
| 159 | + }, |
| 160 | + { |
| 161 | + "name": "city", |
| 162 | + "type": "STRING", |
| 163 | + "mode": "NULLABLE" |
| 164 | + } |
| 165 | + ], |
| 166 | + "description": "Address of the person" |
| 167 | + }, |
| 168 | + { |
| 169 | + "name": "phone_numbers", |
| 170 | + "type": "RECORD", |
| 171 | + "mode": "REPEATED", |
| 172 | + "fields": [ |
| 173 | + { |
| 174 | + "name": "type", |
| 175 | + "type": "STRING", |
| 176 | + "mode": "NULLABLE" |
| 177 | + }, |
| 178 | + { |
| 179 | + "name": "number", |
| 180 | + "type": "STRING", |
| 181 | + "mode": "NULLABLE" |
| 182 | + } |
| 183 | + ], |
| 184 | + "description": "Phone numbers of the person" |
| 185 | + }, |
| 186 | + { |
| 187 | + "name": "policy_tag_test", |
| 188 | + "type": "STRING", |
| 189 | + "mode": "NULLABLE", |
| 190 | + "description": "A test field with policy tags", |
| 191 | + "policyTags": { |
| 192 | + "names": [ |
| 193 | + "${google_data_catalog_policy_tag.test.id}" |
| 194 | + ] |
| 195 | + } |
| 196 | + } |
| 197 | + ] |
| 198 | + EOF |
| 199 | + } |
| 200 | +
|
| 201 | + data "google_bigquery_table" "example" { |
| 202 | + dataset_id = google_bigquery_table.test.dataset_id |
| 203 | + table_id = google_bigquery_table.test.table_id |
| 204 | + } |
| 205 | +`, context) |
| 206 | +} |
0 commit comments