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

Fix bug with importing bq capacity commitments #14226

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
3 changes: 3 additions & 0 deletions .changelog/7621.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigquery: fixed the import logic in`google_bigquery_capacity_commitment`
```
24 changes: 16 additions & 8 deletions google/resource_bigquery_capacity_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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<project>[^ ]+) (?P<name>[^ ]+)", "(?P<name>[^ ]+)"}, d, config); err != nil {
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/capacityCommitments/(?P<capacity_commitment_id>[^/]+)",
"(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<capacity_commitment_id>[^/]+)",
"(?P<location>[^/]+)/(?P<capacity_commitment_id>[^/]+)",
}, 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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 9 additions & 5 deletions google/resource_bigquery_capacity_commitment_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions website/docs/r/bigquery_capacity_commitment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down