Skip to content

Commit

Permalink
Merge pull request #65 from rileykarson/13-instance-group-manager-tar…
Browse files Browse the repository at this point in the history
…get-size

Changed google_compute_instance_group_manager target_size default to 0.
  • Loading branch information
danawillow authored Jun 16, 2017
2 parents 003fd5b + de9b9a0 commit fc3d41e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 23 deletions.
35 changes: 15 additions & 20 deletions google/resource_compute_instance_group_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,19 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
return err
}

// Get group size, default to 1 if not given
var target_size int64 = 1
targetSize := int64(0)
if v, ok := d.GetOk("target_size"); ok {
target_size = int64(v.(int))
targetSize = int64(v.(int))
}

// Build the parameter
manager := &compute.InstanceGroupManager{
Name: d.Get("name").(string),
BaseInstanceName: d.Get("base_instance_name").(string),
InstanceTemplate: d.Get("instance_template").(string),
TargetSize: target_size,
TargetSize: targetSize,
// Force send TargetSize to allow a value of 0.
ForceSendFields: []string{"TargetSize"},
}

// Set optional fields
Expand Down Expand Up @@ -256,7 +257,6 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
d.Set("named_port", flattenNamedPorts(manager.NamedPorts))
d.Set("fingerprint", manager.Fingerprint)
d.Set("instance_group", manager.InstanceGroup)
d.Set("target_size", manager.TargetSize)
d.Set("self_link", manager.SelfLink)
update_strategy, ok := d.GetOk("update_strategy")
if !ok {
Expand Down Expand Up @@ -382,23 +382,18 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
d.SetPartial("named_port")
}

// If size changes trigger a resize
if d.HasChange("target_size") {
if v, ok := d.GetOk("target_size"); ok {
// Only do anything if the new size is set
target_size := int64(v.(int))

op, err := config.clientCompute.InstanceGroupManagers.Resize(
project, d.Get("zone").(string), d.Id(), target_size).Do()
if err != nil {
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
}
targetSize := int64(d.Get("target_size").(int))
op, err := config.clientCompute.InstanceGroupManagers.Resize(
project, d.Get("zone").(string), d.Id(), targetSize).Do()
if err != nil {
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
}

// Wait for the operation to complete
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroupManager")
if err != nil {
return err
}
// Wait for the operation to complete
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroupManager")
if err != nil {
return err
}

d.SetPartial("target_size")
Expand Down
63 changes: 63 additions & 0 deletions google/resource_compute_instance_group_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ func TestAccInstanceGroupManager_basic(t *testing.T) {
})
}

func TestAccInstanceGroupManager_targetSizeZero(t *testing.T) {
var manager compute.InstanceGroupManager

templateName := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
igmName := fmt.Sprintf("igm-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceGroupManagerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccInstanceGroupManager_targetSizeZero(templateName, igmName),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceGroupManagerExists(
"google_compute_instance_group_manager.igm-basic", &manager),
),
},
},
})

if manager.TargetSize != 0 {
t.Errorf("Expected target_size to be 0, got %d", manager.TargetSize)
}
}

func TestAccInstanceGroupManager_update(t *testing.T) {
var manager compute.InstanceGroupManager

Expand Down Expand Up @@ -388,6 +414,43 @@ func testAccInstanceGroupManager_basic(template, target, igm1, igm2 string) stri
`, template, target, igm1, igm2)
}

func testAccInstanceGroupManager_targetSizeZero(template, igm string) string {
return fmt.Sprintf(`
resource "google_compute_instance_template" "igm-basic" {
name = "%s"
machine_type = "n1-standard-1"
can_ip_forward = false
tags = ["foo", "bar"]
disk {
source_image = "debian-cloud/debian-8-jessie-v20160803"
auto_delete = true
boot = true
}
network_interface {
network = "default"
}
metadata {
foo = "bar"
}
service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}
resource "google_compute_instance_group_manager" "igm-basic" {
description = "Terraform test instance group manager"
name = "%s"
instance_template = "${google_compute_instance_template.igm-basic.self_link}"
base_instance_name = "igm-basic"
zone = "us-central1-c"
}
`, template, igm)
}

func testAccInstanceGroupManager_update(template, target, igm string) string {
return fmt.Sprintf(`
resource "google_compute_instance_template" "igm-update" {
Expand Down
6 changes: 3 additions & 3 deletions website/docs/r/compute_instance_group_manager.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ The following arguments are supported:
restart all of the instances at once. In the future, as the GCE API matures
we will support `"ROLLING_UPDATE"` as well.

* `target_size` - (Optional) If not given at creation time, this defaults to 1.
Do not specify this if you are managing the group with an autoscaler, as
this will cause fighting.
* `target_size` - (Optional) The target number of running instances for this managed
instance group. This value should always be explicitly set unless this resource is attached to
an autoscaler, in which case it should never be set. Defaults to `0`.

* `target_pools` - (Optional) The full URL of all target pools to which new
instances in the group are added. Updating the target pools attribute does
Expand Down

0 comments on commit fc3d41e

Please sign in to comment.