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

Handle bigquery_reservation_capacity_commitment creation when capacity_commitment_id is unspecified #16320

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/9293.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigqueryreservation: fixed bug of incorrect resource recreation when `capacity_commitment_id` is unspecified in resource `bigquery_reservation_capacity_commitment`
```
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Examples: US, EU, asia-northeast1. The default value is US.`,
"renewal_plan": {
Type: schema.TypeString,
Optional: true,
Description: `The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable some commitment plans.`,
Description: `The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable for some commitment plans.`,
},
"commitment_end_time": {
Type: schema.TypeString,
Expand Down Expand Up @@ -206,7 +206,7 @@ func resourceBigqueryReservationCapacityCommitmentCreate(d *schema.ResourceData,
}

// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
id, err := tpgresource.ReplaceVars(d, config, "{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
Expand All @@ -224,7 +224,7 @@ func resourceBigqueryReservationCapacityCommitmentRead(d *schema.ResourceData, m
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func resourceBigqueryReservationCapacityCommitmentUpdate(d *schema.ResourceData,
obj["renewalPlan"] = renewalPlanProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -379,7 +379,7 @@ func resourceBigqueryReservationCapacityCommitmentDelete(d *schema.ResourceData,
}
billingProject = project

url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -419,8 +419,13 @@ func resourceBigqueryReservationCapacityCommitmentImport(d *schema.ResourceData,
return nil, err
}

// Set name based on the components
if err := d.Set("name", "projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}"); err != nil {
return nil, fmt.Errorf("Error setting name: %s", err)
}

// Replace import id for the resource id
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
id, err := tpgresource.ReplaceVars(d, config, d.Get("name").(string))
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,66 @@ func TestAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCom
func testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentBasicExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_bigquery_capacity_commitment" "commitment" {
capacity_commitment_id = "capacity-tf-test%{random_suffix}"
capacity_commitment_id = "capacity-tf-test%{random_suffix}"

location = "us-west2"
slot_count = 100
plan = "FLEX_FLAT_RATE"
edition = "ENTERPRISE"
location = "us-west2"
slot_count = 100
plan = "FLEX_FLAT_RATE"
edition = "ENTERPRISE"
}

resource "time_sleep" "wait_61_seconds" {
depends_on = [google_bigquery_capacity_commitment.commitment]

# Only needed for CI tests to be able to tear down the commitment once it's expired
create_duration = "61s"
create_duration = "61s"
}
`, context)
}

func TestAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentNoIdExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
"time": {},
},
CheckDestroy: testAccCheckBigqueryReservationCapacityCommitmentDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentNoIdExample(context),
},
{
ResourceName: "google_bigquery_capacity_commitment.commitment",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"capacity_commitment_id", "location", "enforce_single_admin_project_per_org"},
},
},
})
}

func testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentNoIdExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_bigquery_capacity_commitment" "commitment" {
location = "us-west2"
slot_count = 100
plan = "FLEX_FLAT_RATE"
edition = "ENTERPRISE"
}

resource "time_sleep" "wait_61_seconds" {
depends_on = [google_bigquery_capacity_commitment.commitment]

# Only needed for CI tests to be able to tear down the commitment once it's expired
create_duration = "61s"
}
`, context)
}
Expand All @@ -91,7 +138,7 @@ func testAccCheckBigqueryReservationCapacityCommitmentDestroyProducer(t *testing

config := acctest.GoogleProviderConfig(t)

url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{BigqueryReservationBasePath}}{{name}}")
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,19 @@ func testSweepBigqueryReservationCapacityCommitment(region string) error {
nonPrefixCount := 0
for _, ri := range rl {
obj := ri.(map[string]interface{})
var name string
// Id detected in the delete URL, attempt to use id.
if obj["id"] != nil {
name = tpgresource.GetResourceNameFromSelfLink(obj["id"].(string))
} else if obj["name"] != nil {
name = tpgresource.GetResourceNameFromSelfLink(obj["name"].(string))
} else {
log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName)
if obj["name"] == nil {
log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName)
return nil
}

name := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string))
// Skip resources that shouldn't be sweeped
if !sweeper.IsSweepableTestResource(name) {
nonPrefixCount++
continue
}

deleteTemplate := "https://bigqueryreservation.googleapis.com/v1/projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}"
deleteTemplate := "https://bigqueryreservation.googleapis.com/v1/{{name}}"
deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/bigquery_capacity_commitment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The following arguments are supported:

* `renewal_plan` -
(Optional)
The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable some commitment plans.
The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable for some commitment plans.

* `edition` -
(Optional)
Expand Down 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 `projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}`
* `id` - an identifier for the resource with format `{{name}}`

* `name` -
The resource name of the capacity commitment, e.g., projects/myproject/locations/US/capacityCommitments/123
Expand Down