From 7a6b04dfa2f59efccf8de5fa169b58155609d9d5 Mon Sep 17 00:00:00 2001 From: Eric Rutherford Date: Tue, 6 Sep 2016 12:25:42 -0500 Subject: [PATCH 1/4] adding missing failed states for the NAT Gateways --- builtin/providers/aws/resource_aws_nat_gateway.go | 2 +- builtin/providers/aws/resource_aws_nat_gateway_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_nat_gateway.go b/builtin/providers/aws/resource_aws_nat_gateway.go index 4ad23ad8f..4d54152e7 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway.go +++ b/builtin/providers/aws/resource_aws_nat_gateway.go @@ -101,7 +101,7 @@ func resourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - if ngRaw == nil || strings.ToLower(state) == "deleted" { + if ngRaw == nil || strings.ToLower(state) == "deleted" || strings.ToLower(state) == "deleting" || strings.ToLower(state) == "failed" { log.Printf("[INFO] Removing %s from Terraform state as it is not found or in the deleted state.", d.Id()) d.SetId("") return nil diff --git a/builtin/providers/aws/resource_aws_nat_gateway_test.go b/builtin/providers/aws/resource_aws_nat_gateway_test.go index c4dd8b6f6..8914ff284 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway_test.go +++ b/builtin/providers/aws/resource_aws_nat_gateway_test.go @@ -44,7 +44,7 @@ func testAccCheckNatGatewayDestroy(s *terraform.State) error { NatGatewayIds: []*string{aws.String(rs.Primary.ID)}, }) if err == nil { - if len(resp.NatGateways) > 0 && strings.ToLower(*resp.NatGateways[0].State) != "deleted" { + if len(resp.NatGateways) > 0 && strings.ToLower(*resp.NatGateways[0].State) != "deleted" && strings.ToLower(*resp.NatGateways[0].State) != "deleting" && strings.ToLower(*resp.NatGateways[0].State) != "failed" { return fmt.Errorf("still exists") } From 91f6f2a143f69fe7e22050658a1ce27cc479f869 Mon Sep 17 00:00:00 2001 From: Eric Rutherford Date: Tue, 6 Sep 2016 14:54:17 -0500 Subject: [PATCH 2/4] moving to using a map to clean up the error check --- builtin/providers/aws/resource_aws_nat_gateway.go | 9 ++++++++- builtin/providers/aws/resource_aws_nat_gateway_test.go | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_nat_gateway.go b/builtin/providers/aws/resource_aws_nat_gateway.go index 4d54152e7..0457d9cdb 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway.go +++ b/builtin/providers/aws/resource_aws_nat_gateway.go @@ -101,7 +101,14 @@ func resourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - if ngRaw == nil || strings.ToLower(state) == "deleted" || strings.ToLower(state) == "deleting" || strings.ToLower(state) == "failed" { + + status := map[string]bool { + "deleted": true, + "deleting": true, + "failed": true, + } + + if ngRaw == nil || status[strings.ToLower(state)] { log.Printf("[INFO] Removing %s from Terraform state as it is not found or in the deleted state.", d.Id()) d.SetId("") return nil diff --git a/builtin/providers/aws/resource_aws_nat_gateway_test.go b/builtin/providers/aws/resource_aws_nat_gateway_test.go index 8914ff284..dfe4cb532 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway_test.go +++ b/builtin/providers/aws/resource_aws_nat_gateway_test.go @@ -43,8 +43,13 @@ func testAccCheckNatGatewayDestroy(s *terraform.State) error { resp, err := conn.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{ NatGatewayIds: []*string{aws.String(rs.Primary.ID)}, }) + status := map[string]bool { + "deleted": false, + "deleting": false, + "failed": false, + } if err == nil { - if len(resp.NatGateways) > 0 && strings.ToLower(*resp.NatGateways[0].State) != "deleted" && strings.ToLower(*resp.NatGateways[0].State) != "deleting" && strings.ToLower(*resp.NatGateways[0].State) != "failed" { + if len(resp.NatGateways) > 0 && status[strings.ToLower(*resp.NatGateways[0].State)] { return fmt.Errorf("still exists") } From 04c2d40e572e0a62ea29b0fb223d26f8a9266518 Mon Sep 17 00:00:00 2001 From: Eric Rutherford Date: Tue, 6 Sep 2016 14:56:56 -0500 Subject: [PATCH 3/4] commit after running make fmt --- builtin/providers/aws/resource_aws_nat_gateway.go | 6 +++--- builtin/providers/aws/resource_aws_nat_gateway_test.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_nat_gateway.go b/builtin/providers/aws/resource_aws_nat_gateway.go index 0457d9cdb..deabc7297 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway.go +++ b/builtin/providers/aws/resource_aws_nat_gateway.go @@ -102,10 +102,10 @@ func resourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { return err } - status := map[string]bool { - "deleted": true, + status := map[string]bool{ + "deleted": true, "deleting": true, - "failed": true, + "failed": true, } if ngRaw == nil || status[strings.ToLower(state)] { diff --git a/builtin/providers/aws/resource_aws_nat_gateway_test.go b/builtin/providers/aws/resource_aws_nat_gateway_test.go index dfe4cb532..96aac1625 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway_test.go +++ b/builtin/providers/aws/resource_aws_nat_gateway_test.go @@ -43,10 +43,10 @@ func testAccCheckNatGatewayDestroy(s *terraform.State) error { resp, err := conn.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{ NatGatewayIds: []*string{aws.String(rs.Primary.ID)}, }) - status := map[string]bool { - "deleted": false, + status := map[string]bool{ + "deleted": false, "deleting": false, - "failed": false, + "failed": false, } if err == nil { if len(resp.NatGateways) > 0 && status[strings.ToLower(*resp.NatGateways[0].State)] { From 2cca48a829b51da19b7a19a0e60f6ec4ef4e294b Mon Sep 17 00:00:00 2001 From: Eric Rutherford Date: Tue, 6 Sep 2016 20:57:10 -0500 Subject: [PATCH 4/4] switch to go way of checking for key existence so that go doesn't crash when the value doesn't exist --- builtin/providers/aws/resource_aws_nat_gateway.go | 2 +- .../providers/aws/resource_aws_nat_gateway_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/builtin/providers/aws/resource_aws_nat_gateway.go b/builtin/providers/aws/resource_aws_nat_gateway.go index deabc7297..1ec5e986e 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway.go +++ b/builtin/providers/aws/resource_aws_nat_gateway.go @@ -108,7 +108,7 @@ func resourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { "failed": true, } - if ngRaw == nil || status[strings.ToLower(state)] { + if _, ok := status[strings.ToLower(state)]; ngRaw == nil || ok { log.Printf("[INFO] Removing %s from Terraform state as it is not found or in the deleted state.", d.Id()) d.SetId("") return nil diff --git a/builtin/providers/aws/resource_aws_nat_gateway_test.go b/builtin/providers/aws/resource_aws_nat_gateway_test.go index 96aac1625..dac50527c 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway_test.go +++ b/builtin/providers/aws/resource_aws_nat_gateway_test.go @@ -43,13 +43,13 @@ func testAccCheckNatGatewayDestroy(s *terraform.State) error { resp, err := conn.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{ NatGatewayIds: []*string{aws.String(rs.Primary.ID)}, }) - status := map[string]bool{ - "deleted": false, - "deleting": false, - "failed": false, - } if err == nil { - if len(resp.NatGateways) > 0 && status[strings.ToLower(*resp.NatGateways[0].State)] { + status := map[string]bool{ + "deleted": true, + "deleting": true, + "failed": true, + } + if _, ok := status[strings.ToLower(*resp.NatGateways[0].State)]; len(resp.NatGateways) > 0 && !ok { return fmt.Errorf("still exists") }