Skip to content

Commit

Permalink
Fix resource_bigquery_dataset ID validation (#10027) (#17449)
Browse files Browse the repository at this point in the history
* fix resource_bigquery_dataset ID validation

* skip the new validation tests for VCR

* update dataset IDs to be valid in terraform-google-conversion example files

* update more dataset IDs in the CAI files

[upstream:01aba8f193c130c5a3b64d03c1687d81543c8bac]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Feb 28, 2024
1 parent ae7d9d2 commit 519615c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/10027.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
bigquery: fixed dataset ID validation
```
2 changes: 1 addition & 1 deletion google/services/bigquery/resource_bigquery_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"google.golang.org/api/googleapi"
)

const datasetIdRegexp = `[0-9A-Za-z_]+`
const datasetIdRegexp = `^[0-9A-Za-z_]+$`

func validateDatasetId(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
Expand Down
43 changes: 43 additions & 0 deletions google/services/bigquery/resource_bigquery_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package bigquery_test

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -328,6 +330,47 @@ func TestAccBigQueryDataset_storageBillModel(t *testing.T) {
})
}

func TestAccBigQueryDataset_invalidCharacterInID(t *testing.T) {
t.Parallel()
// Not an acceptance test.
acctest.SkipIfVcr(t)

datasetID := fmt.Sprintf("tf_test_%s-with-hyphens", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryDatasetDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryDataset(datasetID),
ExpectError: regexp.MustCompile("must contain only letters.+numbers.+or underscores.+"),
},
},
})
}

func TestAccBigQueryDataset_invalidLongID(t *testing.T) {
t.Parallel()
// Not an acceptance test.
acctest.SkipIfVcr(t)

datasetSuffix := acctest.RandString(t, 10)
datasetID := fmt.Sprintf("tf_test_%s", strings.Repeat(datasetSuffix, 200))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryDatasetDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryDataset(datasetID),
ExpectError: regexp.MustCompile(".+cannot be greater than 1,024 characters"),
},
},
})
}

func testAccAddTable(t *testing.T, datasetID string, tableID string) resource.TestCheckFunc {
// Not actually a check, but adds a table independently of terraform
return func(s *terraform.State) error {
Expand Down

0 comments on commit 519615c

Please sign in to comment.