Skip to content

Commit 8699876

Browse files
modular-magicianEdward Sun
and
Edward Sun
authored
fixed permadiff on connectionid (#7099) (#13560)
* fixed permadiff on connectionid * add empty value check Co-authored-by: Edward Sun <[email protected]> Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Edward Sun <[email protected]>
1 parent 47cdaec commit 8699876

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

.changelog/7099.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
bigquery: fixed permadiff on `external_data_configuration.connection_id` of `google_bigquery_table`
3+
```

google/resource_bigquery_table.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log"
9+
"reflect"
910
"regexp"
1011
"sort"
1112
"strconv"
@@ -172,6 +173,23 @@ func bigQueryTableSchemaDiffSuppress(name, old, new string, _ *schema.ResourceDa
172173
return eq
173174
}
174175

176+
func bigQueryTableConnectionIdSuppress(name, old, new string, _ *schema.ResourceData) bool {
177+
// API accepts connectionId in below two formats
178+
// "{{project}}.{{location}}.{{connection_id}}" or
179+
// "projects/{{project}}/locations/{{location}}/connections/{{connection_id}}".
180+
// but always returns "{{project}}.{{location}}.{{connection_id}}"
181+
182+
if isEmptyValue(reflect.ValueOf(old)) || isEmptyValue(reflect.ValueOf(new)) {
183+
return false
184+
}
185+
186+
re := regexp.MustCompile("projects/(.+)/(?:locations|regions)/(.+)/connections/(.+)")
187+
if matches := re.FindStringSubmatch(new); matches != nil {
188+
return old == matches[1]+"."+matches[2]+"."+matches[3]
189+
}
190+
return false
191+
}
192+
175193
func bigQueryTableTypeEq(old, new string) bool {
176194
// Do case-insensitive comparison. https://github.com/hashicorp/terraform-provider-google/issues/9472
177195
oldUpper := strings.ToUpper(old)
@@ -627,9 +645,10 @@ func resourceBigQueryTable() *schema.Resource {
627645
// "{{project}}.{{location}}.{{connection_id}}" or
628646
// "projects/{{project}}/locations/{{location}}/connections/{{connection_id}}".
629647
"connection_id": {
630-
Type: schema.TypeString,
631-
Optional: true,
632-
Description: `The connection specifying the credentials to be used to read external storage, such as Azure Blob, Cloud Storage, or S3. The connectionId can have the form "{{project}}.{{location}}.{{connection_id}}" or "projects/{{project}}/locations/{{location}}/connections/{{connection_id}}".`,
648+
Type: schema.TypeString,
649+
Optional: true,
650+
DiffSuppressFunc: bigQueryTableConnectionIdSuppress,
651+
Description: `The connection specifying the credentials to be used to read external storage, such as Azure Blob, Cloud Storage, or S3. The connectionId can have the form "{{project}}.{{location}}.{{connection_id}}" or "projects/{{project}}/locations/{{location}}/connections/{{connection_id}}".`,
633652
},
634653
"reference_file_schema_uri": {
635654
Type: schema.TypeString,

google/resource_bigquery_table_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,15 @@ func TestAccBigQueryExternalDataTable_CSV_WithSchema_UpdateToConnectionID(t *tes
959959
ImportStateVerify: true,
960960
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection"},
961961
},
962+
{
963+
Config: testAccBigQueryTableFromGCSWithSchemaWithConnectionId2(datasetID, tableID, connectionID, projectID, bucketName, objectName, TEST_SIMPLE_CSV, TEST_SIMPLE_CSV_SCHEMA),
964+
},
965+
{
966+
ResourceName: "google_bigquery_table.test",
967+
ImportState: true,
968+
ImportStateVerify: true,
969+
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection"},
970+
},
962971
},
963972
})
964973
}
@@ -2151,6 +2160,59 @@ resource "google_bigquery_table" "test" {
21512160
`, datasetID, bucketName, objectName, content, connectionID, projectID, tableID, schema)
21522161
}
21532162

2163+
func testAccBigQueryTableFromGCSWithSchemaWithConnectionId2(datasetID, tableID, connectionID, projectID, bucketName, objectName, content, schema string) string {
2164+
return fmt.Sprintf(`
2165+
resource "google_bigquery_dataset" "test" {
2166+
dataset_id = "%s"
2167+
}
2168+
resource "google_storage_bucket" "test" {
2169+
name = "%s"
2170+
location = "US"
2171+
force_destroy = true
2172+
}
2173+
resource "google_storage_bucket_object" "test" {
2174+
name = "%s"
2175+
content = <<EOF
2176+
%s
2177+
EOF
2178+
bucket = google_storage_bucket.test.name
2179+
}
2180+
resource "google_bigquery_connection" "test" {
2181+
connection_id = "%s"
2182+
location = "US"
2183+
cloud_resource {}
2184+
}
2185+
locals {
2186+
connection_id_reformatted = google_bigquery_connection.test.name
2187+
}
2188+
resource "google_project_iam_member" "test" {
2189+
role = "roles/storage.objectViewer"
2190+
project = "%s"
2191+
member = "serviceAccount:${google_bigquery_connection.test.cloud_resource[0].service_account_id}"
2192+
}
2193+
resource "google_bigquery_table" "test" {
2194+
deletion_protection = false
2195+
table_id = "%s"
2196+
dataset_id = google_bigquery_dataset.test.dataset_id
2197+
schema = <<EOF
2198+
%s
2199+
EOF
2200+
external_data_configuration {
2201+
autodetect = false
2202+
connection_id = local.connection_id_reformatted
2203+
source_format = "CSV"
2204+
csv_options {
2205+
encoding = "UTF-8"
2206+
quote = ""
2207+
}
2208+
source_uris = [
2209+
"gs://${google_storage_bucket.test.name}/${google_storage_bucket_object.test.name}",
2210+
]
2211+
}
2212+
}
2213+
`, datasetID, bucketName, objectName, content, connectionID, projectID, tableID, schema)
2214+
}
2215+
21542216
func testAccBigQueryTableFromGCSWithSchema(datasetID, tableID, bucketName, objectName, content, schema string) string {
21552217
return fmt.Sprintf(`
21562218
resource "google_bigquery_dataset" "test" {

0 commit comments

Comments
 (0)