Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/vpc_dhcp_options_association - add disappears tests + prevent error when vpc is deleted out of terraform #14367

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions aws/resource_aws_vpc_dhcp_options_association.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -49,33 +50,31 @@ func resourceAwsVpcDhcpOptionsAssociationImport(d *schema.ResourceData, meta int
if err = d.Set("dhcp_options_id", vpc.DhcpOptionsId); err != nil {
return nil, err
}
d.SetId(*vpc.DhcpOptionsId + "-" + *vpc.VpcId)
d.SetId(fmt.Sprintf("%s-%s", aws.StringValue(vpc.DhcpOptionsId), aws.StringValue(vpc.VpcId)))
return []*schema.ResourceData{d}, nil
}

func resourceAwsVpcDhcpOptionsAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

log.Printf(
"[INFO] Creating DHCP Options association: %s => %s",
d.Get("vpc_id").(string),
d.Get("dhcp_options_id").(string))
vpcId := d.Get("vpc_id").(string)
optsID := d.Get("dhcp_options_id").(string)

optsID := aws.String(d.Get("dhcp_options_id").(string))
vpcID := aws.String(d.Get("vpc_id").(string))
log.Printf("[INFO] Creating DHCP Options association: %s => %s", vpcId, optsID)

if _, err := conn.AssociateDhcpOptions(&ec2.AssociateDhcpOptionsInput{
DhcpOptionsId: optsID,
VpcId: vpcID,
DhcpOptionsId: aws.String(optsID),
VpcId: aws.String(vpcId),
}); err != nil {
return err
}

// Set the ID and return
d.SetId(*optsID + "-" + *vpcID)
log.Printf("[INFO] Association ID: %s", d.Id())
d.SetId(fmt.Sprintf("%s-%s", optsID, vpcId))

return nil
log.Printf("[INFO] VPC DHCP Association ID: %s", d.Id())

return resourceAwsVpcDhcpOptionsAssociationRead(d, meta)
}

func resourceAwsVpcDhcpOptionsAssociationRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -92,11 +91,15 @@ func resourceAwsVpcDhcpOptionsAssociationRead(d *schema.ResourceData, meta inter
}

vpc := vpcRaw.(*ec2.Vpc)
if *vpc.VpcId != d.Get("vpc_id") || *vpc.DhcpOptionsId != d.Get("dhcp_options_id") {
if aws.StringValue(vpc.VpcId) != d.Get("vpc_id") ||
aws.StringValue(vpc.DhcpOptionsId) != d.Get("dhcp_options_id") {
log.Printf("[INFO] It seems the DHCP Options association is gone. Deleting reference from Graph...")
d.SetId("")
}

d.Set("vpc_id", vpc.VpcId)
d.Set("dhcp_options_id", vpc.DhcpOptionsId)

return nil
}

Expand All @@ -116,5 +119,9 @@ func resourceAwsVpcDhcpOptionsAssociationDelete(d *schema.ResourceData, meta int
VpcId: aws.String(d.Get("vpc_id").(string)),
})

if isAWSErr(err, "InvalidVpcID.NotFound", "") {
return nil
}

return err
}
102 changes: 77 additions & 25 deletions aws/resource_aws_vpc_dhcp_options_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
Expand All @@ -12,7 +13,7 @@ import (
func TestAccAWSDHCPOptionsAssociation_basic(t *testing.T) {
var v ec2.Vpc
var d ec2.DhcpOptions
resourceName := "aws_vpc_dhcp_options_association.foo"
resourceName := "aws_vpc_dhcp_options_association.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -22,9 +23,9 @@ func TestAccAWSDHCPOptionsAssociation_basic(t *testing.T) {
{
Config: testAccDHCPOptionsAssociationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.foo", &d),
testAccCheckVpcExists("aws_vpc.foo", &v),
testAccCheckDHCPOptionsAssociationExist("aws_vpc_dhcp_options_association.foo", &v),
testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.test", &d),
testAccCheckVpcExists("aws_vpc.test", &v),
testAccCheckDHCPOptionsAssociationExist(resourceName, &v),
),
},
{
Expand All @@ -37,6 +38,54 @@ func TestAccAWSDHCPOptionsAssociation_basic(t *testing.T) {
})
}

func TestAccAWSDHCPOptionsAssociation_disappears_vpc(t *testing.T) {
var v ec2.Vpc
var d ec2.DhcpOptions
resourceName := "aws_vpc_dhcp_options_association.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDHCPOptionsAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDHCPOptionsAssociationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.test", &d),
testAccCheckVpcExists("aws_vpc.test", &v),
testAccCheckDHCPOptionsAssociationExist(resourceName, &v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsVpc(), "aws_vpc.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSDHCPOptionsAssociation_disappears_dhcp(t *testing.T) {
var v ec2.Vpc
var d ec2.DhcpOptions
resourceName := "aws_vpc_dhcp_options_association.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDHCPOptionsAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDHCPOptionsAssociationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.test", &d),
testAccCheckVpcExists("aws_vpc.test", &v),
testAccCheckDHCPOptionsAssociationExist(resourceName, &v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsVpcDhcpOptions(), "aws_vpc_dhcp_options.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccDHCPOptionsAssociationVPCImportIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down Expand Up @@ -81,40 +130,43 @@ func testAccCheckDHCPOptionsAssociationExist(n string, vpc *ec2.Vpc) resource.Te
return fmt.Errorf("No DHCP Options Set association ID is set")
}

if *vpc.DhcpOptionsId != rs.Primary.Attributes["dhcp_options_id"] {
return fmt.Errorf("VPC %s does not have DHCP Options Set %s associated", *vpc.VpcId, rs.Primary.Attributes["dhcp_options_id"])
if aws.StringValue(vpc.DhcpOptionsId) != rs.Primary.Attributes["dhcp_options_id"] {
return fmt.Errorf("VPC %s does not have DHCP Options Set %s associated",
aws.StringValue(vpc.VpcId), rs.Primary.Attributes["dhcp_options_id"])
}

if *vpc.VpcId != rs.Primary.Attributes["vpc_id"] {
return fmt.Errorf("DHCP Options Set %s is not associated with VPC %s", rs.Primary.Attributes["dhcp_options_id"], *vpc.VpcId)
if aws.StringValue(vpc.VpcId) != rs.Primary.Attributes["vpc_id"] {
return fmt.Errorf("DHCP Options Set %s is not associated with VPC %s",
rs.Primary.Attributes["dhcp_options_id"], aws.StringValue(vpc.VpcId))
}

return nil
}
}

const testAccDHCPOptionsAssociationConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-vpc-dhcp-options-association"
}
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"

tags = {
Name = "terraform-testacc-vpc-dhcp-options-association"
}
}

resource "aws_vpc_dhcp_options" "foo" {
domain_name = "service.consul"
domain_name_servers = ["127.0.0.1", "10.0.0.2"]
ntp_servers = ["127.0.0.1"]
netbios_name_servers = ["127.0.0.1"]
netbios_node_type = 2
resource "aws_vpc_dhcp_options" "test" {
domain_name = "service.consul"
domain_name_servers = ["127.0.0.1", "10.0.0.2"]
ntp_servers = ["127.0.0.1"]
netbios_name_servers = ["127.0.0.1"]
netbios_node_type = 2

tags = {
Name = "foo"
}
tags = {
Name = "terraform-testacc-vpc-dhcp-options-association"
}
}

resource "aws_vpc_dhcp_options_association" "foo" {
vpc_id = "${aws_vpc.foo.id}"
dhcp_options_id = "${aws_vpc_dhcp_options.foo.id}"
resource "aws_vpc_dhcp_options_association" "test" {
vpc_id = "${aws_vpc.test.id}"
dhcp_options_id = "${aws_vpc_dhcp_options.test.id}"
}
`