github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_vpn_gateway_test.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func TestAccCloudStackVPNGateway_basic(t *testing.T) {
    13  	var vpnGateway cloudstack.VpnGateway
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackVPNGatewayDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackVPNGateway_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackVPNGatewayExists(
    24  						"cloudstack_vpn_gateway.foo", &vpnGateway),
    25  					resource.TestCheckResourceAttr(
    26  						"cloudstack_vpn_gateway.foo", "vpc", "terraform-vpc"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccCheckCloudStackVPNGatewayExists(
    34  	n string, vpnGateway *cloudstack.VpnGateway) resource.TestCheckFunc {
    35  	return func(s *terraform.State) error {
    36  		rs, ok := s.RootModule().Resources[n]
    37  		if !ok {
    38  			return fmt.Errorf("Not found: %s", n)
    39  		}
    40  
    41  		if rs.Primary.ID == "" {
    42  			return fmt.Errorf("No VPN Gateway ID is set")
    43  		}
    44  
    45  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    46  		v, _, err := cs.VPN.GetVpnGatewayByID(rs.Primary.ID)
    47  
    48  		if err != nil {
    49  			return err
    50  		}
    51  
    52  		if v.Id != rs.Primary.ID {
    53  			return fmt.Errorf("VPN Gateway not found")
    54  		}
    55  
    56  		*vpnGateway = *v
    57  
    58  		return nil
    59  	}
    60  }
    61  
    62  func testAccCheckCloudStackVPNGatewayDestroy(s *terraform.State) error {
    63  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    64  
    65  	for _, rs := range s.RootModule().Resources {
    66  		if rs.Type != "cloudstack_vpn_gateway" {
    67  			continue
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("No VPN Gateway ID is set")
    72  		}
    73  
    74  		_, _, err := cs.VPN.GetVpnGatewayByID(rs.Primary.ID)
    75  		if err == nil {
    76  			return fmt.Errorf("VPN Gateway %s still exists", rs.Primary.ID)
    77  		}
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  var testAccCloudStackVPNGateway_basic = fmt.Sprintf(`
    84  resource "cloudstack_vpc" "foo" {
    85    name = "terraform-vpc"
    86    display_text = "terraform-vpc-text"
    87    cidr = "%s"
    88    vpc_offering = "%s"
    89    zone = "%s"
    90  }
    91  
    92  resource "cloudstack_vpn_gateway" "foo" {
    93    vpc = "${cloudstack_vpc.foo.name}"
    94  }`,
    95  	CLOUDSTACK_VPC_CIDR_1,
    96  	CLOUDSTACK_VPC_OFFERING,
    97  	CLOUDSTACK_ZONE)