Skip to content

Commit

Permalink
Fix resource_bigquery_dataset ID validation (#10027)
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
  • Loading branch information
wj-chen authored Feb 28, 2024
1 parent 77cf5eb commit 01aba8f
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/bigquery_dataset.go.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package bigquery_test

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -326,6 +328,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
4 changes: 2 additions & 2 deletions mmv1/third_party/tgc/tests/data/example_bigquery_dataset.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test-dataset",
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test_dataset",
"asset_type": "bigquery.googleapis.com/Dataset",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
Expand All @@ -11,7 +11,7 @@
"data": {
"friendlyName": "",
"datasetReference": {
"datasetId": "test-dataset"
"datasetId": "test_dataset"
},
"labels": {
"env": "dev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ provider "google" {
}

resource "google_bigquery_dataset" "default" {
dataset_id = "test-dataset"
dataset_id = "test_dataset"
location = "EU"
default_table_expiration_ms = 3600000

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test-dataset",
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test_dataset",
"asset_type": "bigquery.googleapis.com/Dataset",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
Expand All @@ -10,7 +10,7 @@
"parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
"data": {
"datasetReference": {
"datasetId": "test-dataset"
"datasetId": "test_dataset"
},
"defaultTableExpirationMs": 3600000,
"labels": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ provider "google" {
}

resource "google_bigquery_dataset" "example_dataset" {
dataset_id = "test-dataset"
dataset_id = "test_dataset"
location = "EU"
project = "{{.Provider.project}}"
default_table_expiration_ms = 3600000
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test-dataset",
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test_dataset",
"asset_type": "bigquery.googleapis.com/Dataset",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
Expand All @@ -10,7 +10,7 @@
"parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
"data": {
"datasetReference": {
"datasetId": "test-dataset"
"datasetId": "test_dataset"
},
"defaultTableExpirationMs": 3600000,
"labels": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ provider "google" {
}

resource "google_bigquery_dataset" "example_dataset" {
dataset_id = "test-dataset"
dataset_id = "test_dataset"
location = "EU"
project = "{{.Provider.project}}"
default_table_expiration_ms = 3600000
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test-dataset",
"name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/test_dataset",
"asset_type": "bigquery.googleapis.com/Dataset",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
Expand All @@ -10,7 +10,7 @@
"parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
"data": {
"datasetReference": {
"datasetId": "test-dataset"
"datasetId": "test_dataset"
},
"defaultTableExpirationMs": 3600000,
"labels": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ provider "google" {
}

resource "google_bigquery_dataset" "example-dataset" {
dataset_id = "test-dataset"
dataset_id = "test_dataset"
location = "EU"
project = "{{.Provider.project}}"
default_table_expiration_ms = 3600000
Expand Down

0 comments on commit 01aba8f

Please sign in to comment.