From 78b0879c6db982d1dc26fcaa869afc29759d9729 Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Tue, 13 Jun 2017 12:43:18 -0700 Subject: [PATCH 1/3] Changed google_compute_instance_group_manager target_size default to 0. --- ...resource_compute_instance_group_manager.go | 35 +++++------ ...rce_compute_instance_group_manager_test.go | 63 +++++++++++++++++++ ...mpute_instance_group_manager.html.markdown | 6 +- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/google/resource_compute_instance_group_manager.go b/google/resource_compute_instance_group_manager.go index 58d435a7424..75f6795dda6 100644 --- a/google/resource_compute_instance_group_manager.go +++ b/google/resource_compute_instance_group_manager.go @@ -132,10 +132,9 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte return err } - // Get group size, default to 1 if not given - var target_size int64 = 1 + var targetSize int64 = 0 if v, ok := d.GetOk("target_size"); ok { - target_size = int64(v.(int)) + targetSize = int64(v.(int)) } // Build the parameter @@ -143,7 +142,8 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte Name: d.Get("name").(string), BaseInstanceName: d.Get("base_instance_name").(string), InstanceTemplate: d.Get("instance_template").(string), - TargetSize: target_size, + TargetSize: targetSize, + ForceSendFields: []string{"TargetSize"}, } // Set optional fields @@ -256,7 +256,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 { @@ -382,23 +381,19 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte d.SetPartial("named_port") } - // If size changes trigger a resize + // We won't ever see changes if target_size is unset 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) - } + target_size := int64(d.Get("target_size").(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) + } - // 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") diff --git a/google/resource_compute_instance_group_manager_test.go b/google/resource_compute_instance_group_manager_test.go index 22e35d1635e..e44620a0330 100644 --- a/google/resource_compute_instance_group_manager_test.go +++ b/google/resource_compute_instance_group_manager_test.go @@ -39,6 +39,32 @@ func TestAccInstanceGroupManager_basic(t *testing.T) { }) } +func TestAccInstanceGroupManager_targetSizeZero(t *testing.T) { + var manager compute.InstanceGroupManager + + template := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) + igm := 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(template, igm), + 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 @@ -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" { diff --git a/website/docs/r/compute_instance_group_manager.html.markdown b/website/docs/r/compute_instance_group_manager.html.markdown index b7d8c6cd910..d07d9cb8f8a 100644 --- a/website/docs/r/compute_instance_group_manager.html.markdown +++ b/website/docs/r/compute_instance_group_manager.html.markdown @@ -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, Default `0`) 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. * `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 From 3f26ea6425506607e383aeb2179f1272179b574d Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Fri, 16 Jun 2017 15:39:44 -0700 Subject: [PATCH 2/3] Updated variable names and docs. --- google/resource_compute_instance_group_manager.go | 8 ++++---- google/resource_compute_instance_group_manager_test.go | 6 +++--- .../docs/r/compute_instance_group_manager.html.markdown | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/google/resource_compute_instance_group_manager.go b/google/resource_compute_instance_group_manager.go index 75f6795dda6..1f56458975e 100644 --- a/google/resource_compute_instance_group_manager.go +++ b/google/resource_compute_instance_group_manager.go @@ -132,7 +132,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte return err } - var targetSize int64 = 0 + targetSize := int64(0) if v, ok := d.GetOk("target_size"); ok { targetSize = int64(v.(int)) } @@ -143,6 +143,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte BaseInstanceName: d.Get("base_instance_name").(string), InstanceTemplate: d.Get("instance_template").(string), TargetSize: targetSize, + // Force send TargetSize to allow a value of 0. ForceSendFields: []string{"TargetSize"}, } @@ -381,11 +382,10 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte d.SetPartial("named_port") } - // We won't ever see changes if target_size is unset if d.HasChange("target_size") { - target_size := int64(d.Get("target_size").(int)) + targetSize := int64(d.Get("target_size").(int)) op, err := config.clientCompute.InstanceGroupManagers.Resize( - project, d.Get("zone").(string), d.Id(), target_size).Do() + project, d.Get("zone").(string), d.Id(), targetSize).Do() if err != nil { return fmt.Errorf("Error updating InstanceGroupManager: %s", err) } diff --git a/google/resource_compute_instance_group_manager_test.go b/google/resource_compute_instance_group_manager_test.go index e44620a0330..c8a02b512a7 100644 --- a/google/resource_compute_instance_group_manager_test.go +++ b/google/resource_compute_instance_group_manager_test.go @@ -42,8 +42,8 @@ func TestAccInstanceGroupManager_basic(t *testing.T) { func TestAccInstanceGroupManager_targetSizeZero(t *testing.T) { var manager compute.InstanceGroupManager - template := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) - igm := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) + 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) }, @@ -51,7 +51,7 @@ func TestAccInstanceGroupManager_targetSizeZero(t *testing.T) { CheckDestroy: testAccCheckInstanceGroupManagerDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccInstanceGroupManager_targetSizeZero(template, igm), + Config: testAccInstanceGroupManager_targetSizeZero(templateName, igmName), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceGroupManagerExists( "google_compute_instance_group_manager.igm-basic", &manager), diff --git a/website/docs/r/compute_instance_group_manager.html.markdown b/website/docs/r/compute_instance_group_manager.html.markdown index d07d9cb8f8a..963bd0e4a8f 100644 --- a/website/docs/r/compute_instance_group_manager.html.markdown +++ b/website/docs/r/compute_instance_group_manager.html.markdown @@ -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, Default `0`) The target number of running instances for this managed +* `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. + 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 From de9b9a05b5320342f73eafd041c24c5974ec3c52 Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Fri, 16 Jun 2017 15:41:26 -0700 Subject: [PATCH 3/3] go fmt --- google/resource_compute_instance_group_manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/resource_compute_instance_group_manager.go b/google/resource_compute_instance_group_manager.go index 1f56458975e..4478521fb90 100644 --- a/google/resource_compute_instance_group_manager.go +++ b/google/resource_compute_instance_group_manager.go @@ -144,7 +144,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte InstanceTemplate: d.Get("instance_template").(string), TargetSize: targetSize, // Force send TargetSize to allow a value of 0. - ForceSendFields: []string{"TargetSize"}, + ForceSendFields: []string{"TargetSize"}, } // Set optional fields