From ce1d1f6ee380a226dbe59ab008f389c6811e2357 Mon Sep 17 00:00:00 2001 From: Konstantinos Tsanaktsidis Date: Sat, 4 May 2019 08:38:55 +1000 Subject: [PATCH] Fix invalid assumptions about Project ID's in BQ table (#3486) The bigquery table resource tries to infer the project and dataset from the table's full name. However, it does so incorrectly as it assumes that the project ID cannot have : or . characters in it, yet both are valid. --- google/resource_bigquery_table.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/google/resource_bigquery_table.go b/google/resource_bigquery_table.go index 52aa33ca..1e7874b9 100644 --- a/google/resource_bigquery_table.go +++ b/google/resource_bigquery_table.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "log" - "strings" + "regexp" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/structure" @@ -493,15 +493,16 @@ type bigQueryTableId struct { } func parseBigQueryTableId(id string) (*bigQueryTableId, error) { - parts := strings.FieldsFunc(id, func(r rune) bool { return r == ':' || r == '.' }) - - if len(parts) != 3 { + // Expected format is "PROJECT:DATASET.TABLE", but the project can itself have . and : in it. + // Those characters are not valid dataset or table components, so just split on the last two. + matchRegex := regexp.MustCompile("^(.+):([^:.]+)\\.([^:.]+)$") + subMatches := matchRegex.FindStringSubmatch(id) + if subMatches == nil { return nil, fmt.Errorf("Invalid BigQuery table specifier. Expecting {project}:{dataset-id}.{table-id}, got %s", id) } - return &bigQueryTableId{ - Project: parts[0], - DatasetId: parts[1], - TableId: parts[2], + Project: subMatches[1], + DatasetId: subMatches[2], + TableId: subMatches[3], }, nil }