github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_vpc_dhcp_options_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSDHCPOptions_basic(t *testing.T) { 15 var d ec2.DhcpOptions 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckDHCPOptionsDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccDHCPOptionsConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.foo", &d), 26 resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "domain_name", "service.consul"), 27 resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "domain_name_servers.0", "127.0.0.1"), 28 resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "domain_name_servers.1", "10.0.0.2"), 29 resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "ntp_servers.0", "127.0.0.1"), 30 resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "netbios_name_servers.0", "127.0.0.1"), 31 resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "netbios_node_type", "2"), 32 resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "tags.Name", "foo-name"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccAWSDHCPOptions_deleteOptions(t *testing.T) { 40 var d ec2.DhcpOptions 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckDHCPOptionsDestroy, 46 Steps: []resource.TestStep{ 47 resource.TestStep{ 48 Config: testAccDHCPOptionsConfig, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.foo", &d), 51 testAccCheckDHCPOptionsDelete("aws_vpc_dhcp_options.foo"), 52 ), 53 ExpectNonEmptyPlan: true, 54 }, 55 }, 56 }) 57 } 58 59 func testAccCheckDHCPOptionsDestroy(s *terraform.State) error { 60 conn := testAccProvider.Meta().(*AWSClient).ec2conn 61 62 for _, rs := range s.RootModule().Resources { 63 if rs.Type != "aws_vpc_dhcp_options" { 64 continue 65 } 66 67 // Try to find the resource 68 resp, err := conn.DescribeDhcpOptions(&ec2.DescribeDhcpOptionsInput{ 69 DhcpOptionsIds: []*string{ 70 aws.String(rs.Primary.ID), 71 }, 72 }) 73 if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidDhcpOptionID.NotFound" { 74 continue 75 } 76 if err == nil { 77 if len(resp.DhcpOptions) > 0 { 78 return fmt.Errorf("still exists") 79 } 80 81 return nil 82 } 83 84 // Verify the error is what we want 85 ec2err, ok := err.(awserr.Error) 86 if !ok { 87 return err 88 } 89 if ec2err.Code() != "InvalidDhcpOptionsID.NotFound" { 90 return err 91 } 92 } 93 94 return nil 95 } 96 97 func testAccCheckDHCPOptionsExists(n string, d *ec2.DhcpOptions) resource.TestCheckFunc { 98 return func(s *terraform.State) error { 99 rs, ok := s.RootModule().Resources[n] 100 if !ok { 101 return fmt.Errorf("Not found: %s", n) 102 } 103 104 if rs.Primary.ID == "" { 105 return fmt.Errorf("No ID is set") 106 } 107 108 conn := testAccProvider.Meta().(*AWSClient).ec2conn 109 resp, err := conn.DescribeDhcpOptions(&ec2.DescribeDhcpOptionsInput{ 110 DhcpOptionsIds: []*string{ 111 aws.String(rs.Primary.ID), 112 }, 113 }) 114 if err != nil { 115 return err 116 } 117 if len(resp.DhcpOptions) == 0 { 118 return fmt.Errorf("DHCP Options not found") 119 } 120 121 *d = *resp.DhcpOptions[0] 122 123 return nil 124 } 125 } 126 127 func testAccCheckDHCPOptionsDelete(n string) resource.TestCheckFunc { 128 return func(s *terraform.State) error { 129 rs, ok := s.RootModule().Resources[n] 130 if !ok { 131 return fmt.Errorf("Not found: %s", n) 132 } 133 134 if rs.Primary.ID == "" { 135 return fmt.Errorf("No ID is set") 136 } 137 138 conn := testAccProvider.Meta().(*AWSClient).ec2conn 139 _, err := conn.DeleteDhcpOptions(&ec2.DeleteDhcpOptionsInput{ 140 DhcpOptionsId: aws.String(rs.Primary.ID), 141 }) 142 143 return err 144 } 145 } 146 147 const testAccDHCPOptionsConfig = ` 148 resource "aws_vpc_dhcp_options" "foo" { 149 domain_name = "service.consul" 150 domain_name_servers = ["127.0.0.1", "10.0.0.2"] 151 ntp_servers = ["127.0.0.1"] 152 netbios_name_servers = ["127.0.0.1"] 153 netbios_node_type = 2 154 155 tags { 156 Name = "foo-name" 157 } 158 } 159 `