From 4147c1ea2b51c2c6a6e414e32518518450e81a62 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Thu, 20 Jul 2017 20:55:11 +1000 Subject: [PATCH] Suppress diffs when an empty set is specified for `aws_autoscaling_group.availability_zones` Fixes #1187. --- aws/diff_suppress_funcs.go | 10 +++++ aws/resource_aws_autoscaling_group.go | 11 +++--- aws/resource_aws_autoscaling_group_test.go | 45 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/aws/diff_suppress_funcs.go b/aws/diff_suppress_funcs.go index e8c58b81306..d891708b918 100644 --- a/aws/diff_suppress_funcs.go +++ b/aws/diff_suppress_funcs.go @@ -75,3 +75,13 @@ func suppressOpenIdURL(k, old, new string, d *schema.ResourceData) bool { return oldUrl.String() == newUrl.String() } + +func suppressAutoscalingGroupAvailabilityZoneDiffs(k, old, new string, d *schema.ResourceData) bool { + // If VPC zone identifiers are provided then there is no need to explicitly + // specify availability zones. + if _, ok := d.GetOk("vpc_zone_identifier"); ok { + return true + } + + return false +} diff --git a/aws/resource_aws_autoscaling_group.go b/aws/resource_aws_autoscaling_group.go index e53de952395..a0455cfcf8d 100644 --- a/aws/resource_aws_autoscaling_group.go +++ b/aws/resource_aws_autoscaling_group.go @@ -113,11 +113,12 @@ func resourceAwsAutoscalingGroup() *schema.Resource { }, "availability_zones": { - Type: schema.TypeSet, - Optional: true, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, + Type: schema.TypeSet, + Optional: true, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + DiffSuppressFunc: suppressAutoscalingGroupAvailabilityZoneDiffs, }, "placement_group": { diff --git a/aws/resource_aws_autoscaling_group_test.go b/aws/resource_aws_autoscaling_group_test.go index 46609211540..39b63efe97f 100644 --- a/aws/resource_aws_autoscaling_group_test.go +++ b/aws/resource_aws_autoscaling_group_test.go @@ -857,6 +857,26 @@ func TestAccAWSAutoScalingGroup_classicVpcZoneIdentifier(t *testing.T) { }) } +func TestAccAWSAutoScalingGroup_emptyAvailabilityZones(t *testing.T) { + var group autoscaling.Group + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_autoscaling_group.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoScalingGroupConfig_emptyAvailabilityZones, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.test", &group), + resource.TestCheckResourceAttr("aws_autoscaling_group.test", "availability_zones.#", "1"), + ), + }, + }, + }) +} + const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` resource "aws_launch_configuration" "foobar" { image_id = "ami-21f78e11" @@ -1826,3 +1846,28 @@ resource "aws_launch_configuration" "test" { instance_type = "t1.micro" } ` + +const testAccAWSAutoScalingGroupConfig_emptyAvailabilityZones = ` +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" +} + +resource "aws_subnet" "test" { + vpc_id = "${aws_vpc.test.id}" + cidr_block = "10.0.0.0/16" +} + +resource "aws_autoscaling_group" "test" { + min_size = 0 + max_size = 0 + + availability_zones = [] + launch_configuration = "${aws_launch_configuration.test.name}" + vpc_zone_identifier = ["${aws_subnet.test.id}"] +} + +resource "aws_launch_configuration" "test" { + image_id = "ami-21f78e11" + instance_type = "t1.micro" +} +`