github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 testAccCheckDHCPOptionsDestroy(s *terraform.State) error {
    40  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    41  
    42  	for _, rs := range s.RootModule().Resources {
    43  		if rs.Type != "aws_vpc_dhcp_options" {
    44  			continue
    45  		}
    46  
    47  		// Try to find the resource
    48  		resp, err := conn.DescribeDhcpOptions(&ec2.DescribeDhcpOptionsInput{
    49  			DhcpOptionsIds: []*string{
    50  				aws.String(rs.Primary.ID),
    51  			},
    52  		})
    53  		if err == nil {
    54  			if len(resp.DhcpOptions) > 0 {
    55  				return fmt.Errorf("still exist.")
    56  			}
    57  
    58  			return nil
    59  		}
    60  
    61  		// Verify the error is what we want
    62  		ec2err, ok := err.(awserr.Error)
    63  		if !ok {
    64  			return err
    65  		}
    66  		if ec2err.Code() != "InvalidDhcpOptionsID.NotFound" {
    67  			return err
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func testAccCheckDHCPOptionsExists(n string, d *ec2.DhcpOptions) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  		rs, ok := s.RootModule().Resources[n]
    77  		if !ok {
    78  			return fmt.Errorf("Not found: %s", n)
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No ID is set")
    83  		}
    84  
    85  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    86  		resp, err := conn.DescribeDhcpOptions(&ec2.DescribeDhcpOptionsInput{
    87  			DhcpOptionsIds: []*string{
    88  				aws.String(rs.Primary.ID),
    89  			},
    90  		})
    91  		if err != nil {
    92  			return err
    93  		}
    94  		if len(resp.DhcpOptions) == 0 {
    95  			return fmt.Errorf("DHCP Options not found")
    96  		}
    97  
    98  		*d = *resp.DhcpOptions[0]
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  const testAccDHCPOptionsConfig = `
   105  resource "aws_vpc_dhcp_options" "foo" {
   106  	domain_name = "service.consul"
   107  	domain_name_servers = ["127.0.0.1", "10.0.0.2"]
   108  	ntp_servers = ["127.0.0.1"]
   109  	netbios_name_servers = ["127.0.0.1"]
   110  	netbios_node_type = 2
   111  
   112  	tags {
   113  		Name = "foo-name"
   114  	}
   115  }
   116  `