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