github.com/mhlias/terraform@v0.6.12-0.20161118140322-a5d6410b912a/builtin/providers/aws/resource_aws_dc_virtual_interface_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/directconnect" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAwsDcVirtualInterface_basic(t *testing.T) { 16 var virtualIF directconnect.VirtualInterface 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDCVirtualInterfaceDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccDCVirtualInterfaceConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckDCVirtualInterfaceExists("aws_dc_virtual_interface.virtualinterface", &virtualIF), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckDCVirtualInterfaceDestroy(s *terraform.State) error { 34 conn := testAccProvider.Meta().(*AWSClient).dcconn 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "aws_dc_virtual_interface" { 38 continue 39 } 40 41 // Try to find the resource 42 resp, err := conn.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{ 43 VirtualInterfaceId: aws.String(rs.Primary.ID), 44 }) 45 if err == nil { 46 if len(resp.VirtualInterfaces) > 0 && strings.ToLower(*resp.VirtualInterfaces[0].VirtualInterfaceState) != "deleted" { 47 return fmt.Errorf("still exists") 48 } 49 50 return nil 51 } 52 53 _, ok := err.(awserr.Error) 54 if !ok { 55 return err 56 } 57 } 58 59 return nil 60 } 61 62 func testAccCheckDCVirtualInterfaceExists(n string, ng *directconnect.VirtualInterface) resource.TestCheckFunc { 63 return func(s *terraform.State) error { 64 rs, ok := s.RootModule().Resources[n] 65 if !ok { 66 return fmt.Errorf("Not found: %s", n) 67 } 68 69 if rs.Primary.ID == "" { 70 return fmt.Errorf("No ID is set") 71 } 72 73 conn := testAccProvider.Meta().(*AWSClient).dcconn 74 75 resp, err := conn.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{ 76 VirtualInterfaceId: aws.String(rs.Primary.ID), 77 }) 78 if err != nil { 79 return err 80 } 81 if len(resp.VirtualInterfaces) == 0 { 82 return fmt.Errorf("DCVirtualInterface not found") 83 } 84 85 *ng = *resp.VirtualInterfaces[0] 86 87 return nil 88 } 89 } 90 91 const testAccDCVirtualInterfaceConfig = ` 92 resource "aws_vpc" "vpc" { 93 cidr_block = "10.0.0.0/16" 94 } 95 96 resource "aws_vpn_gateway" "vpn_gw" { 97 vpc_id = "${aws_vpc.vpc.id}" 98 } 99 100 101 resource "aws_dc_virtual_interface" "vif" { 102 connection_id = "dxcon-xyz" 103 virtual_interface_name = "test-vif" 104 asn = "12345" 105 vlan = "1024" 106 auth_key = "super_secret_key" 107 amazon_address = "10.0.0.45/30" 108 customer_address = "10.0.0.46/30" 109 interface_type = "private" 110 virtual_gateway_id = "${aws_vpn_gateway.vpn_gw.id}" 111 } 112 `