diff --git a/.changelog/7621.txt b/.changelog/7621.txt new file mode 100644 index 00000000000..f40afa194a1 --- /dev/null +++ b/.changelog/7621.txt @@ -0,0 +1,3 @@ +```release-note:bug +bigquery: fixed the import logic in`google_bigquery_capacity_commitment` +``` diff --git a/google/resource_bigquery_capacity_commitment.go b/google/resource_bigquery_capacity_commitment.go index a72f0f60ae1..a8dca85f880 100644 --- a/google/resource_bigquery_capacity_commitment.go +++ b/google/resource_bigquery_capacity_commitment.go @@ -179,7 +179,7 @@ func resourceBigqueryReservationCapacityCommitmentCreate(d *schema.ResourceData, } // Store the ID now - id, err := replaceVars(d, config, "{{name}}") + id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}") if err != nil { return fmt.Errorf("Error constructing id: %s", err) } @@ -197,7 +197,7 @@ func resourceBigqueryReservationCapacityCommitmentRead(d *schema.ResourceData, m return err } - url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}") + url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}") if err != nil { return err } @@ -281,7 +281,7 @@ func resourceBigqueryReservationCapacityCommitmentUpdate(d *schema.ResourceData, obj["renewalPlan"] = renewalPlanProp } - url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}") + url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}") if err != nil { return err } @@ -334,7 +334,7 @@ func resourceBigqueryReservationCapacityCommitmentDelete(d *schema.ResourceData, } billingProject = project - url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}") + url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}") if err != nil { return err } @@ -357,14 +357,22 @@ func resourceBigqueryReservationCapacityCommitmentDelete(d *schema.ResourceData, } func resourceBigqueryReservationCapacityCommitmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - config := meta.(*Config) - - // current import_formats can't import fields with forward slashes in their value - if err := parseImportId([]string{"(?P[^ ]+) (?P[^ ]+)", "(?P[^ ]+)"}, d, config); err != nil { + if err := parseImportId([]string{ + "projects/(?P[^/]+)/locations/(?P[^/]+)/capacityCommitments/(?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}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + return []*schema.ResourceData{d}, nil } diff --git a/google/resource_bigquery_capacity_commitment_generated_test.go b/google/resource_bigquery_capacity_commitment_generated_test.go index b7054dff329..a3052766268 100644 --- a/google/resource_bigquery_capacity_commitment_generated_test.go +++ b/google/resource_bigquery_capacity_commitment_generated_test.go @@ -55,6 +55,8 @@ func TestAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCom func testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentBasicExample(context map[string]interface{}) string { return Nprintf(` resource "google_bigquery_capacity_commitment" "commitment" { + capacity_commitment_id = "capacity-tf-test%{random_suffix}" + location = "us-west2" slot_count = 100 plan = "FLEX_FLAT_RATE" @@ -82,7 +84,7 @@ func testAccCheckBigqueryReservationCapacityCommitmentDestroyProducer(t *testing config := GoogleProviderConfig(t) - url, err := replaceVarsForTest(config, rs, "{{BigqueryReservationBasePath}}{{name}}") + url, err := replaceVarsForTest(config, rs, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}") if err != nil { return err } diff --git a/google/resource_bigquery_capacity_commitment_sweeper_test.go b/google/resource_bigquery_capacity_commitment_sweeper_test.go index 3dbc100931d..252aebdd645 100644 --- a/google/resource_bigquery_capacity_commitment_sweeper_test.go +++ b/google/resource_bigquery_capacity_commitment_sweeper_test.go @@ -87,19 +87,23 @@ func testSweepBigqueryReservationCapacityCommitment(region string) error { nonPrefixCount := 0 for _, ri := range rl { obj := ri.(map[string]interface{}) - if obj["name"] == nil { - log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) + var name string + // Id detected in the delete URL, attempt to use id. + if obj["id"] != nil { + name = GetResourceNameFromSelfLink(obj["id"].(string)) + } else if obj["name"] != nil { + name = GetResourceNameFromSelfLink(obj["name"].(string)) + } else { + log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName) return nil } - - name := GetResourceNameFromSelfLink(obj["name"].(string)) // Skip resources that shouldn't be sweeped if !IsSweepableTestResource(name) { nonPrefixCount++ continue } - deleteTemplate := "https://bigqueryreservation.googleapis.com/v1/{{name}}" + deleteTemplate := "https://bigqueryreservation.googleapis.com/v1/projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}" deleteUrl, err := replaceVars(d, config, deleteTemplate) if err != nil { log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) diff --git a/website/docs/r/bigquery_capacity_commitment.html.markdown b/website/docs/r/bigquery_capacity_commitment.html.markdown index 2496eda50e9..1d9ee9a6d6f 100644 --- a/website/docs/r/bigquery_capacity_commitment.html.markdown +++ b/website/docs/r/bigquery_capacity_commitment.html.markdown @@ -93,7 +93,7 @@ The following arguments are supported: In addition to the arguments listed above, the following computed attributes are exported: -* `id` - an identifier for the resource with format `{{name}}` +* `id` - an identifier for the resource with format `projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}` * `name` - The resource name of the capacity commitment, e.g., projects/myproject/locations/US/capacityCommitments/123 @@ -123,7 +123,9 @@ This resource provides the following CapacityCommitment can be imported using any of these accepted formats: ``` -$ terraform import google_bigquery_capacity_commitment.default {{name}} +$ terraform import google_bigquery_capacity_commitment.default projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}} +$ terraform import google_bigquery_capacity_commitment.default {{project}}/{{location}}/{{capacity_commitment_id}} +$ terraform import google_bigquery_capacity_commitment.default {{location}}/{{capacity_commitment_id}} ``` ## User Project Overrides