Skip to content

Commit

Permalink
Don't rely on bigquery table id anymore (#2760)
Browse files Browse the repository at this point in the history
Merged PR #2760.
  • Loading branch information
rileykarson authored and modular-magician committed Dec 2, 2019
1 parent 0fa4461 commit dbd2c1a
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions third_party/terraform/resources/resource_bigquery_table.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func resourceBigQueryTable() *schema.Resource {
Delete: resourceBigQueryTableDelete,
Update: resourceBigQueryTableUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceBigQueryTableImport,
},
Schema: map[string]*schema.Schema{
// TableId: [Required] The ID of the table. The ID must contain only
Expand Down Expand Up @@ -488,17 +488,20 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {

log.Printf("[INFO] Reading BigQuery table: %s", d.Id())

id, err := parseBigQueryTableId(d.Id())
project, err := getProject(d, config)
if err != nil {
return err
}

res, err := config.clientBigQuery.Tables.Get(id.Project, id.DatasetId, id.TableId).Do()
datasetID := d.Get("dataset_id").(string)
tableID := d.Get("table_id").(string)

res, err := config.clientBigQuery.Tables.Get(project, datasetID, tableID).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("BigQuery table %q", id.TableId))
return handleNotFoundError(err, d, fmt.Sprintf("BigQuery table %q", tableID))
}

d.Set("project", id.Project)
d.Set("project", project)
d.Set("description", res.Description)
d.Set("expiration_time", res.ExpirationTime)
d.Set("friendly_name", res.FriendlyName)
Expand Down Expand Up @@ -561,12 +564,15 @@ func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error

log.Printf("[INFO] Updating BigQuery table: %s", d.Id())

id, err := parseBigQueryTableId(d.Id())
project, err := getProject(d, config)
if err != nil {
return err
}

if _, err = config.clientBigQuery.Tables.Update(id.Project, id.DatasetId, id.TableId, table).Do(); err != nil {
datasetID := d.Get("dataset_id").(string)
tableID := d.Get("table_id").(string)

if _, err = config.clientBigQuery.Tables.Update(project, datasetID, tableID, table).Do(); err != nil {
return err
}

Expand All @@ -578,12 +584,15 @@ func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error

log.Printf("[INFO] Deleting BigQuery table: %s", d.Id())

id, err := parseBigQueryTableId(d.Id())
project, err := getProject(d, config)
if err != nil {
return err
}

if err := config.clientBigQuery.Tables.Delete(id.Project, id.DatasetId, id.TableId).Do(); err != nil {
datasetID := d.Get("dataset_id").(string)
tableID := d.Get("table_id").(string)

if err := config.clientBigQuery.Tables.Delete(project, datasetID, tableID).Do(); err != nil {
return err
}

Expand Down Expand Up @@ -841,20 +850,22 @@ func flattenView(vd *bigquery.ViewDefinition) []map[string]interface{} {
return []map[string]interface{}{result}
}

type bigQueryTableId struct {
Project, DatasetId, TableId string
}
func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/datasets/(?P<dataset_id>[^/]+)/tables/(?P<table_id>[^/]+)",
"(?P<project>[^/]+)/(?P<dataset_id>[^/]+)/(?P<table_id>[^/]+)",
"(?P<dataset_id>[^/]+)/(?P<table_id>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

func parseBigQueryTableId(id string) (*bigQueryTableId, error) {
// Expected format is "projects/{{project}}/datasets/{{dataset}}/tables/{{table}}"
matchRegex := regexp.MustCompile("^projects/(.+)/datasets/(.+)/tables/(.+)$")
subMatches := matchRegex.FindStringSubmatch(id)
if subMatches == nil {
return nil, fmt.Errorf("Invalid BigQuery table specifier. Expecting projects/{{project}}/datasets/{{dataset}}/tables/{{table}}, got %s", id)
}
return &bigQueryTableId{
Project: subMatches[1],
DatasetId: subMatches[2],
TableId: subMatches[3],
}, nil
return []*schema.ResourceData{d}, nil
}

0 comments on commit dbd2c1a

Please sign in to comment.