github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/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/awslabs/aws-sdk-go/aws"
     8  	"github.com/awslabs/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccDHCPOptions(t *testing.T) {
    14  	var d ec2.DHCPOptions
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckDHCPOptionsDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccDHCPOptionsConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.foo", &d),
    25  					resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "domain_name", "service.consul"),
    26  					resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "domain_name_servers.0", "127.0.0.1"),
    27  					resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "domain_name_servers.1", "10.0.0.2"),
    28  					resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "ntp_servers.0", "127.0.0.1"),
    29  					resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "netbios_name_servers.0", "127.0.0.1"),
    30  					resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "netbios_node_type", "2"),
    31  					resource.TestCheckResourceAttr("aws_vpc_dhcp_options.foo", "tags.Name", "foo-name"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckDHCPOptionsDestroy(s *terraform.State) error {
    39  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    40  
    41  	for _, rs := range s.RootModule().Resources {
    42  		if rs.Type != "aws_vpc_dhcp_options" {
    43  			continue
    44  		}
    45  
    46  		// Try to find the resource
    47  		resp, err := conn.DescribeDHCPOptions(&ec2.DescribeDHCPOptionsInput{
    48  			DHCPOptionsIDs: []*string{
    49  				aws.String(rs.Primary.ID),
    50  			},
    51  		})
    52  		if err == nil {
    53  			if len(resp.DHCPOptions) > 0 {
    54  				return fmt.Errorf("still exist.")
    55  			}
    56  
    57  			return nil
    58  		}
    59  
    60  		// Verify the error is what we want
    61  		ec2err, ok := err.(aws.APIError)
    62  		if !ok {
    63  			return err
    64  		}
    65  		if ec2err.Code != "InvalidDhcpOptionsID.NotFound" {
    66  			return err
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccCheckDHCPOptionsExists(n string, d *ec2.DHCPOptions) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No ID is set")
    82  		}
    83  
    84  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    85  		resp, err := conn.DescribeDHCPOptions(&ec2.DescribeDHCPOptionsInput{
    86  			DHCPOptionsIDs: []*string{
    87  				aws.String(rs.Primary.ID),
    88  			},
    89  		})
    90  		if err != nil {
    91  			return err
    92  		}
    93  		if len(resp.DHCPOptions) == 0 {
    94  			return fmt.Errorf("DHCP Options not found")
    95  		}
    96  
    97  		*d = *resp.DHCPOptions[0]
    98  
    99  		return nil
   100  	}
   101  }
   102  
   103  const testAccDHCPOptionsConfig = `
   104  resource "aws_vpc_dhcp_options" "foo" {
   105  	domain_name = "service.consul"
   106  	domain_name_servers = ["127.0.0.1", "10.0.0.2"]
   107  	ntp_servers = ["127.0.0.1"]
   108  	netbios_name_servers = ["127.0.0.1"]
   109  	netbios_node_type = 2
   110  
   111  	tags {
   112  		Name = "foo-name"
   113  	}
   114  }
   115  `