Changed google_compute_instance_group_manager target_size default to 0.

This commit is contained in:
Riley Karson 2017-06-13 12:43:18 -07:00
parent 33ae780cc5
commit 78b0879c6d
3 changed files with 81 additions and 23 deletions

View File

@ -132,10 +132,9 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
return err return err
} }
// Get group size, default to 1 if not given var targetSize int64 = 0
var target_size int64 = 1
if v, ok := d.GetOk("target_size"); ok { if v, ok := d.GetOk("target_size"); ok {
target_size = int64(v.(int)) targetSize = int64(v.(int))
} }
// Build the parameter // Build the parameter
@ -143,7 +142,8 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
Name: d.Get("name").(string), Name: d.Get("name").(string),
BaseInstanceName: d.Get("base_instance_name").(string), BaseInstanceName: d.Get("base_instance_name").(string),
InstanceTemplate: d.Get("instance_template").(string), InstanceTemplate: d.Get("instance_template").(string),
TargetSize: target_size, TargetSize: targetSize,
ForceSendFields: []string{"TargetSize"},
} }
// Set optional fields // Set optional fields
@ -256,7 +256,6 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
d.Set("named_port", flattenNamedPorts(manager.NamedPorts)) d.Set("named_port", flattenNamedPorts(manager.NamedPorts))
d.Set("fingerprint", manager.Fingerprint) d.Set("fingerprint", manager.Fingerprint)
d.Set("instance_group", manager.InstanceGroup) d.Set("instance_group", manager.InstanceGroup)
d.Set("target_size", manager.TargetSize)
d.Set("self_link", manager.SelfLink) d.Set("self_link", manager.SelfLink)
update_strategy, ok := d.GetOk("update_strategy") update_strategy, ok := d.GetOk("update_strategy")
if !ok { if !ok {
@ -382,12 +381,9 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
d.SetPartial("named_port") 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 d.HasChange("target_size") {
if v, ok := d.GetOk("target_size"); ok { target_size := int64(d.Get("target_size").(int))
// Only do anything if the new size is set
target_size := int64(v.(int))
op, err := config.clientCompute.InstanceGroupManagers.Resize( op, err := config.clientCompute.InstanceGroupManagers.Resize(
project, d.Get("zone").(string), d.Id(), target_size).Do() project, d.Get("zone").(string), d.Id(), target_size).Do()
if err != nil { if err != nil {
@ -399,7 +395,6 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
if err != nil { if err != nil {
return err return err
} }
}
d.SetPartial("target_size") d.SetPartial("target_size")
} }

View File

@ -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) { func TestAccInstanceGroupManager_update(t *testing.T) {
var manager compute.InstanceGroupManager var manager compute.InstanceGroupManager
@ -388,6 +414,43 @@ func testAccInstanceGroupManager_basic(template, target, igm1, igm2 string) stri
`, template, target, igm1, igm2) `, 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 { func testAccInstanceGroupManager_update(template, target, igm string) string {
return fmt.Sprintf(` return fmt.Sprintf(`
resource "google_compute_instance_template" "igm-update" { resource "google_compute_instance_template" "igm-update" {

View File

@ -74,9 +74,9 @@ The following arguments are supported:
restart all of the instances at once. In the future, as the GCE API matures restart all of the instances at once. In the future, as the GCE API matures
we will support `"ROLLING_UPDATE"` as well. we will support `"ROLLING_UPDATE"` as well.
* `target_size` - (Optional) If not given at creation time, this defaults to 1. * `target_size` - (Optional, Default `0`) The target number of running instances for this managed
Do not specify this if you are managing the group with an autoscaler, as instance group. This value should always be explicitly set unless this resource is attached to
this will cause fighting. an autoscaler, in which case it should never be set.
* `target_pools` - (Optional) The full URL of all target pools to which new * `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 instances in the group are added. Updating the target pools attribute does