Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't rely on bigquery table id anymore #5012

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 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,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 @@ -560,12 +562,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 +582,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 +848,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