github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_vpc_dhcp_options_association_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/service/ec2" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccAWSDHCPOptionsAssociation_basic(t *testing.T) { 13 var v ec2.Vpc 14 var d ec2.DhcpOptions 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckDHCPOptionsAssociationDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccDHCPOptionsAssociationConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.foo", &d), 25 testAccCheckVpcExists("aws_vpc.foo", &v), 26 testAccCheckDHCPOptionsAssociationExist("aws_vpc_dhcp_options_association.foo", &v), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckDHCPOptionsAssociationDestroy(s *terraform.State) error { 34 conn := testAccProvider.Meta().(*AWSClient).ec2conn 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "aws_vpc_dhcp_options_association" { 38 continue 39 } 40 41 // Try to find the VPC associated to the DHCP Options set 42 vpcs, err := findVPCsByDHCPOptionsID(conn, rs.Primary.Attributes["dhcp_options_id"]) 43 if err != nil { 44 return err 45 } 46 47 if len(vpcs) > 0 { 48 return fmt.Errorf("DHCP Options association is still associated to %d VPCs.", len(vpcs)) 49 } 50 } 51 52 return nil 53 } 54 55 func testAccCheckDHCPOptionsAssociationExist(n string, vpc *ec2.Vpc) resource.TestCheckFunc { 56 return func(s *terraform.State) error { 57 rs, ok := s.RootModule().Resources[n] 58 if !ok { 59 return fmt.Errorf("Not found: %s", n) 60 } 61 62 if rs.Primary.ID == "" { 63 return fmt.Errorf("No DHCP Options Set association ID is set") 64 } 65 66 if *vpc.DhcpOptionsId != rs.Primary.Attributes["dhcp_options_id"] { 67 return fmt.Errorf("VPC %s does not have DHCP Options Set %s associated", *vpc.VpcId, rs.Primary.Attributes["dhcp_options_id"]) 68 } 69 70 if *vpc.VpcId != rs.Primary.Attributes["vpc_id"] { 71 return fmt.Errorf("DHCP Options Set %s is not associated with VPC %s", rs.Primary.Attributes["dhcp_options_id"], *vpc.VpcId) 72 } 73 74 return nil 75 } 76 } 77 78 const testAccDHCPOptionsAssociationConfig = ` 79 resource "aws_vpc" "foo" { 80 cidr_block = "10.1.0.0/16" 81 } 82 83 resource "aws_vpc_dhcp_options" "foo" { 84 domain_name = "service.consul" 85 domain_name_servers = ["127.0.0.1", "10.0.0.2"] 86 ntp_servers = ["127.0.0.1"] 87 netbios_name_servers = ["127.0.0.1"] 88 netbios_node_type = 2 89 90 tags { 91 Name = "foo" 92 } 93 } 94 95 resource "aws_vpc_dhcp_options_association" "foo" { 96 vpc_id = "${aws_vpc.foo.id}" 97 dhcp_options_id = "${aws_vpc_dhcp_options.foo.id}" 98 } 99 `