diff --git a/google/resource_bigquery_table.go b/google/resource_bigquery_table.go index a0483ee4da3..818f3517dbe 100644 --- a/google/resource_bigquery_table.go +++ b/google/resource_bigquery_table.go @@ -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" @@ -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 @@ -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) @@ -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 } @@ -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 } @@ -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[^/]+)/datasets/(?P[^/]+)/tables/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)", + }, 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 } diff --git a/google/resource_sql_database_instance_test.go b/google/resource_sql_database_instance_test.go index 402fe1699bc..4c96a3d03b9 100644 --- a/google/resource_sql_database_instance_test.go +++ b/google/resource_sql_database_instance_test.go @@ -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" )