Skip to content

Commit

Permalink
Fix: external_data_configuration.connection_id Diff Suppression (#9053
Browse files Browse the repository at this point in the history
) (#15983)

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Sep 25, 2023
1 parent e07be00 commit 486f03a
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/9053.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigquery: fix diff suppression in `external_data_configuration.connection_id`
```
17 changes: 14 additions & 3 deletions google/services/bigquery/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,21 @@ func bigQueryTableConnectionIdSuppress(name, old, new string, _ *schema.Resource
return false
}

re := regexp.MustCompile("projects/(.+)/(?:locations|regions)/(.+)/connections/(.+)")
if matches := re.FindStringSubmatch(new); matches != nil {
return old == matches[1]+"."+matches[2]+"."+matches[3]
// Old is in the dot format, and new is in the slash format.
// They represent the same connection if the project, locaition, and IDs are
// the same.
// Location should use a case-insenstive comparison.
dotRe := regexp.MustCompile(`(.+)\.(.+)\.(.+)`)
slashRe := regexp.MustCompile("projects/(.+)/(?:locations|regions)/(.+)/connections/(.+)")
dotMatches := dotRe.FindStringSubmatch(old)
slashMatches := slashRe.FindStringSubmatch(new)
if dotMatches != nil && slashMatches != nil {
sameProject := dotMatches[1] == slashMatches[1]
sameLocation := strings.EqualFold(dotMatches[2], slashMatches[2])
sameId := dotMatches[3] == slashMatches[3]
return sameProject && sameLocation && sameId
}

return false
}

Expand Down
179 changes: 179 additions & 0 deletions google/services/bigquery/resource_bigquery_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,131 @@ func TestAccBigQueryExternalDataTable_objectTable(t *testing.T) {
})
}

func TestAccBigQueryExternalDataTable_connectionIdDiff_UseNameReference(t *testing.T) {
t.Parallel()
// Setup
bucketName := acctest.TestBucketName(t)
objectName := fmt.Sprintf("tf_test_%s.csv", acctest.RandString(t, 10))
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
connectionID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

// Feature Under Test.
location := "US"
connection_id_reference := "google_bigquery_connection.test.name"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableExternalDataConfigurationConnectionID(location, connectionID, datasetID, tableID, bucketName, objectName, connection_id_reference),
},
},
})
}

func TestAccBigQueryExternalDataTable_connectionIdDiff_UseIdReference(t *testing.T) {
t.Parallel()
// Setup
bucketName := acctest.TestBucketName(t)
objectName := fmt.Sprintf("tf_test_%s.csv", acctest.RandString(t, 10))
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
connectionID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

// Feature Under Test.
location := "US"
connection_id_reference := "google_bigquery_connection.test.id"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableExternalDataConfigurationConnectionID(location, connectionID, datasetID, tableID, bucketName, objectName, connection_id_reference),
},
},
})
}

func TestAccBigQueryExternalDataTable_connectionIdDiff_UseIdReference_UsCentral1LowerCase(t *testing.T) {
t.Parallel()
// Setup
bucketName := acctest.TestBucketName(t)
objectName := fmt.Sprintf("tf_test_%s.csv", acctest.RandString(t, 10))
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
connectionID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

// Feature Under Test.
location := "us-central1"
connection_id_reference := "google_bigquery_connection.test.id"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableExternalDataConfigurationConnectionID(location, connectionID, datasetID, tableID, bucketName, objectName, connection_id_reference),
},
},
})
}

func TestAccBigQueryExternalDataTable_connectionIdDiff_UseIdReference_UsEast1(t *testing.T) {
t.Parallel()
// Setup
bucketName := acctest.TestBucketName(t)
objectName := fmt.Sprintf("tf_test_%s.csv", acctest.RandString(t, 10))
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
connectionID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

// Feature Under Test.
location := "US-EAST1"
connection_id_reference := "google_bigquery_connection.test.id"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableExternalDataConfigurationConnectionID(location, connectionID, datasetID, tableID, bucketName, objectName, connection_id_reference),
},
},
})
}

func TestAccBigQueryExternalDataTable_connectionIdDiff_UseIdReference_EuropeWest8(t *testing.T) {
t.Parallel()
// Setup
bucketName := acctest.TestBucketName(t)
objectName := fmt.Sprintf("tf_test_%s.csv", acctest.RandString(t, 10))
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
connectionID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

// Feature Under Test.
location := "EUROPE-WEST8"
connection_id_reference := "google_bigquery_connection.test.id"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableExternalDataConfigurationConnectionID(location, connectionID, datasetID, tableID, bucketName, objectName, connection_id_reference),
},
},
})
}

func TestAccBigQueryExternalDataTable_CSV(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2381,6 +2506,60 @@ resource "google_bigquery_table" "test" {
`, datasetID, bucketName, manifestName, parquetFileName, tableID)
}

func testAccBigQueryTableExternalDataConfigurationConnectionID(location, connectionID, datasetID, tableID, bucketName, objectName, connectionIdReference string) string {
return fmt.Sprintf(`
resource "google_bigquery_connection" "test" {
connection_id = "%s"
location = "%s"
cloud_resource {}
}
data "google_project" "project" {}
resource "google_project_iam_member" "test" {
role = "roles/storage.objectViewer"
project = data.google_project.project.id
member = "serviceAccount:${google_bigquery_connection.test.cloud_resource[0].service_account_id}"
}
resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
location = "%s"
}
resource "google_storage_bucket" "test" {
name = "%s"
location = "%s"
force_destroy = true
}
resource "google_storage_bucket_object" "test" {
name = "%s"
source = "./test-fixtures/test.parquet.gzip"
bucket = google_storage_bucket.test.name
}
resource "google_bigquery_table" "test" {
deletion_protection = false
table_id = "%s"
dataset_id = google_bigquery_dataset.test.dataset_id
external_data_configuration {
# Feature Under Test
connection_id = %s
autodetect = false
object_metadata = "SIMPLE"
metadata_cache_mode = "MANUAL"
source_uris = [
"gs://${google_storage_bucket.test.name}/*",
]
}
}
`, connectionID, location, datasetID, location, bucketName, location, objectName, tableID, connectionIdReference)
}

func testAccBigQueryTableFromGCSObjectTable(connectionID, datasetID, tableID, bucketName, objectName, maxStaleness string) string {
return fmt.Sprintf(`
resource "google_bigquery_connection" "test" {
Expand Down

0 comments on commit 486f03a

Please sign in to comment.