Skip to content

Commit a3205a9

Browse files
Handle bigquery_reservation_capacity_commitment creation when capacity_commitment_id is unspecified (#9293) (#16320)
* handle bigquery_reservation_capacity_commitment creation when capacity_commitment_id is unspecified * revert change to import_format * fix indentation * add custom import * add custom import file [upstream:d4cafef987e2817b2c047be7d995535cadd9485b] Signed-off-by: Modular Magician <[email protected]>
1 parent 734f83c commit a3205a9

5 files changed

+76
-25
lines changed

.changelog/9293.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
bigqueryreservation: fixed bug of incorrect resource recreation when `capacity_commitment_id` is unspecified in resource `bigquery_reservation_capacity_commitment`
3+
```

google/services/bigqueryreservation/resource_bigquery_capacity_commitment.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Examples: US, EU, asia-northeast1. The default value is US.`,
104104
"renewal_plan": {
105105
Type: schema.TypeString,
106106
Optional: true,
107-
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.`,
107+
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.`,
108108
},
109109
"commitment_end_time": {
110110
Type: schema.TypeString,
@@ -206,7 +206,7 @@ func resourceBigqueryReservationCapacityCommitmentCreate(d *schema.ResourceData,
206206
}
207207

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

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

317-
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
317+
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}")
318318
if err != nil {
319319
return err
320320
}
@@ -379,7 +379,7 @@ func resourceBigqueryReservationCapacityCommitmentDelete(d *schema.ResourceData,
379379
}
380380
billingProject = project
381381

382-
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
382+
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}")
383383
if err != nil {
384384
return err
385385
}
@@ -419,8 +419,13 @@ func resourceBigqueryReservationCapacityCommitmentImport(d *schema.ResourceData,
419419
return nil, err
420420
}
421421

422+
// Set name based on the components
423+
if err := d.Set("name", "projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}"); err != nil {
424+
return nil, fmt.Errorf("Error setting name: %s", err)
425+
}
426+
422427
// Replace import id for the resource id
423-
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
428+
id, err := tpgresource.ReplaceVars(d, config, d.Get("name").(string))
424429
if err != nil {
425430
return nil, fmt.Errorf("Error constructing id: %s", err)
426431
}

google/services/bigqueryreservation/resource_bigquery_capacity_commitment_generated_test.go

+55-8
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,66 @@ func TestAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCom
6262
func testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentBasicExample(context map[string]interface{}) string {
6363
return acctest.Nprintf(`
6464
resource "google_bigquery_capacity_commitment" "commitment" {
65-
capacity_commitment_id = "capacity-tf-test%{random_suffix}"
65+
capacity_commitment_id = "capacity-tf-test%{random_suffix}"
6666
67-
location = "us-west2"
68-
slot_count = 100
69-
plan = "FLEX_FLAT_RATE"
70-
edition = "ENTERPRISE"
67+
location = "us-west2"
68+
slot_count = 100
69+
plan = "FLEX_FLAT_RATE"
70+
edition = "ENTERPRISE"
7171
}
7272
7373
resource "time_sleep" "wait_61_seconds" {
7474
depends_on = [google_bigquery_capacity_commitment.commitment]
75-
75+
7676
# Only needed for CI tests to be able to tear down the commitment once it's expired
77-
create_duration = "61s"
77+
create_duration = "61s"
78+
}
79+
`, context)
80+
}
81+
82+
func TestAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentNoIdExample(t *testing.T) {
83+
t.Parallel()
84+
85+
context := map[string]interface{}{
86+
"random_suffix": acctest.RandString(t, 10),
87+
}
88+
89+
acctest.VcrTest(t, resource.TestCase{
90+
PreCheck: func() { acctest.AccTestPreCheck(t) },
91+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
92+
ExternalProviders: map[string]resource.ExternalProvider{
93+
"random": {},
94+
"time": {},
95+
},
96+
CheckDestroy: testAccCheckBigqueryReservationCapacityCommitmentDestroyProducer(t),
97+
Steps: []resource.TestStep{
98+
{
99+
Config: testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentNoIdExample(context),
100+
},
101+
{
102+
ResourceName: "google_bigquery_capacity_commitment.commitment",
103+
ImportState: true,
104+
ImportStateVerify: true,
105+
ImportStateVerifyIgnore: []string{"capacity_commitment_id", "location", "enforce_single_admin_project_per_org"},
106+
},
107+
},
108+
})
109+
}
110+
111+
func testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentNoIdExample(context map[string]interface{}) string {
112+
return acctest.Nprintf(`
113+
resource "google_bigquery_capacity_commitment" "commitment" {
114+
location = "us-west2"
115+
slot_count = 100
116+
plan = "FLEX_FLAT_RATE"
117+
edition = "ENTERPRISE"
118+
}
119+
120+
resource "time_sleep" "wait_61_seconds" {
121+
depends_on = [google_bigquery_capacity_commitment.commitment]
122+
123+
# Only needed for CI tests to be able to tear down the commitment once it's expired
124+
create_duration = "61s"
78125
}
79126
`, context)
80127
}
@@ -91,7 +138,7 @@ func testAccCheckBigqueryReservationCapacityCommitmentDestroyProducer(t *testing
91138

92139
config := acctest.GoogleProviderConfig(t)
93140

94-
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}")
141+
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{BigqueryReservationBasePath}}{{name}}")
95142
if err != nil {
96143
return err
97144
}

google/services/bigqueryreservation/resource_bigquery_capacity_commitment_sweeper.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,19 @@ func testSweepBigqueryReservationCapacityCommitment(region string) error {
9696
nonPrefixCount := 0
9797
for _, ri := range rl {
9898
obj := ri.(map[string]interface{})
99-
var name string
100-
// Id detected in the delete URL, attempt to use id.
101-
if obj["id"] != nil {
102-
name = tpgresource.GetResourceNameFromSelfLink(obj["id"].(string))
103-
} else if obj["name"] != nil {
104-
name = tpgresource.GetResourceNameFromSelfLink(obj["name"].(string))
105-
} else {
106-
log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName)
99+
if obj["name"] == nil {
100+
log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName)
107101
return nil
108102
}
103+
104+
name := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string))
109105
// Skip resources that shouldn't be sweeped
110106
if !sweeper.IsSweepableTestResource(name) {
111107
nonPrefixCount++
112108
continue
113109
}
114110

115-
deleteTemplate := "https://bigqueryreservation.googleapis.com/v1/projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}"
111+
deleteTemplate := "https://bigqueryreservation.googleapis.com/v1/{{name}}"
116112
deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate)
117113
if err != nil {
118114
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)

website/docs/r/bigquery_capacity_commitment.html.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The following arguments are supported:
6363

6464
* `renewal_plan` -
6565
(Optional)
66-
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.
66+
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.
6767

6868
* `edition` -
6969
(Optional)
@@ -93,7 +93,7 @@ The following arguments are supported:
9393

9494
In addition to the arguments listed above, the following computed attributes are exported:
9595

96-
* `id` - an identifier for the resource with format `projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}`
96+
* `id` - an identifier for the resource with format `{{name}}`
9797

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

0 commit comments

Comments
 (0)