Skip to content

Commit

Permalink
Don't rely on bigquery table id anymore
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
rileykarson authored and modular-magician committed Nov 27, 2019
1 parent b11c43f commit f44286a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
59 changes: 34 additions & 25 deletions google/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"log"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
Expand All @@ -20,7 +19,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 @@ -487,17 +486,19 @@ 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("description", res.Description)
d.Set("expiration_time", res.ExpirationTime)
d.Set("friendly_name", res.FriendlyName)
Expand Down Expand Up @@ -560,12 +561,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 @@ -577,12 +581,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 @@ -840,20 +847,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
}
1 change: 0 additions & 1 deletion google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

Expand Down

0 comments on commit f44286a

Please sign in to comment.