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

Support compute reservation resize #5999

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/3308.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added ability to resize `google_compute_reservation`
```
67 changes: 62 additions & 5 deletions google/resource_compute_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func resourceComputeReservation() *schema.Resource {
return &schema.Resource{
Create: resourceComputeReservationCreate,
Read: resourceComputeReservationRead,
Update: resourceComputeReservationUpdate,
Delete: resourceComputeReservationDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -37,6 +38,7 @@ func resourceComputeReservation() *schema.Resource {

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(4 * time.Minute),
Update: schema.DefaultTimeout(4 * time.Minute),
Delete: schema.DefaultTimeout(4 * time.Minute),
},

Expand All @@ -56,16 +58,15 @@ character, which cannot be a dash.`,
"specific_reservation": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Reservation for instances with specific machine shapes.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"count": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: `The number of resources that are allocated.`,
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(1),
Description: `The number of resources that are allocated.`,
},
"instance_properties": {
Type: schema.TypeList,
Expand Down Expand Up @@ -331,6 +332,55 @@ func resourceComputeReservationRead(d *schema.ResourceData, meta interface{}) er
return nil
}

func resourceComputeReservationUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

d.Partial(true)

if d.HasChange("specific_reservation") {
obj := make(map[string]interface{})

specificReservationProp, err := expandComputeReservationSpecificReservation(d.Get("specific_reservation"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("specific_reservation"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, specificReservationProp)) {
obj["specificReservation"] = specificReservationProp
}

obj, err = resourceComputeReservationUpdateEncoder(d, meta, obj)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}/resize")
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error updating Reservation %q: %s", d.Id(), err)
}

err = computeOperationWaitTime(
config, res, project, "Updating Reservation",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if err != nil {
return err
}

d.SetPartial("specific_reservation")
}

d.Partial(false)

return resourceComputeReservationRead(d, meta)
}

func resourceComputeReservationDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand Down Expand Up @@ -756,3 +806,10 @@ func expandComputeReservationZone(v interface{}, d TerraformResourceData, config
}
return f.RelativeLink(), nil
}

func resourceComputeReservationUpdateEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
newObj := make(map[string]interface{})
newObj["specificSkuCount"] = obj["specificReservation"].(map[string]interface{})["count"]

return newObj, nil
}
56 changes: 56 additions & 0 deletions google/resource_compute_reservation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

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

reservationName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeReservationDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeReservation_basic(reservationName, "2"),
},
{
ResourceName: "google_compute_reservation.reservation",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeReservation_basic(reservationName, "1"),
},
{
ResourceName: "google_compute_reservation.reservation",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeReservation_basic(reservationName, count string) string {
return fmt.Sprintf(`
resource "google_compute_reservation" "reservation" {
name = "%s"
zone = "us-central1-a"

specific_reservation {
count = %s
instance_properties {
min_cpu_platform = "Intel Cascade Lake"
machine_type = "n2-standard-2"
}
}
}
`, reservationName, count)
}
1 change: 1 addition & 0 deletions website/docs/r/compute_reservation.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ This resource provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - Default is 4 minutes.
- `update` - Default is 4 minutes.
- `delete` - Default is 4 minutes.

## Import
Expand Down