Skip to content

Commit

Permalink
Fix invalid assumptions about Project ID's in BQ table (#3486)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KJTsanaktsidis authored and rileykarson committed May 3, 2019
1 parent f86a85a commit ce1d1f6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions google/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"strings"
"regexp"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
Expand Down Expand Up @@ -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
}

0 comments on commit ce1d1f6

Please sign in to comment.