Skip to content

Commit

Permalink
Support compute reservation resize (#3308)
Browse files Browse the repository at this point in the history
* support compute reservations resize

* remove if custom encoder

* review comments
  • Loading branch information
megan07 authored Mar 27, 2020
1 parent 399a974 commit 7b67854
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11906,6 +11906,8 @@ objects:
required: true
description: |
Reservation for instances with specific machine shapes.
update_verb: :POST
update_url: 'projects/{{project}}/zones/{{zone}}/reservations/{{name}}/resize'
properties:
- !ruby/object:Api::Type::Integer
name: 'count'
Expand All @@ -11920,6 +11922,7 @@ objects:
- !ruby/object:Api::Type::NestedObject
name: 'instanceProperties'
required: true
input: true
description: |
The instance properties for the reservation.
properties:
Expand Down
5 changes: 5 additions & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1494,11 +1494,16 @@ overrides: !ruby/object:Overrides::ResourceOverrides
primary_resource_id: "gce_reservation"
vars:
reservation_name: "gce-reservation"
custom_code: !ruby/object:Provider::Terraform::CustomCode
update_encoder: templates/terraform/update_encoder/reservation.go.erb
properties:
id: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: true
specificReservation.instanceProperties.minCpuPlatform: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
specificReservation.count: !ruby/object:Overrides::Terraform::PropertyOverride
validation: !ruby/object:Provider::Terraform::Validation
function: 'validation.IntAtLeast(1)'
Route: !ruby/object:Overrides::Terraform::ResourceOverride
examples:
- !ruby/object:Provider::Terraform::Examples
Expand Down
8 changes: 8 additions & 0 deletions templates/terraform/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ if <%= props.map { |prop| "d.HasChange(\"#{prop.name.underscore}\")" }.join ' ||
}
<% end # props.each -%>

<%# We need to decide what encoder to use here - if there's an update encoder, use that! -%>
<% if object.custom_code.update_encoder -%>
obj, err = resource<%= resource_name -%>UpdateEncoder(d, meta, obj)
if err != nil {
return err
}
<% end -%>

<% if object.mutex -%>
lockName, err := replaceVars(d, config, "<%= object.mutex -%>")
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions templates/terraform/update_encoder/reservation.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%# The license inside this block applies to this file.
# Copyright 2020 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
newObj := make(map[string]interface{})
newObj["specificSkuCount"] = obj["specificReservation"].(map[string]interface{})["count"]

return newObj, nil
56 changes: 56 additions & 0 deletions third_party/terraform/tests/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)
}

0 comments on commit 7b67854

Please sign in to comment.