github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/cloudstack/resource_cloudstack_vpn_connection_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 TestAccCloudStackVPNConnection_basic(t *testing.T) {
    13  	var vpnConnection cloudstack.VpnConnection
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackVPNConnectionDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackVPNConnection_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackVPNConnectionExists(
    24  						"cloudstack_vpn_connection.foo-bar", &vpnConnection),
    25  					testAccCheckCloudStackVPNConnectionExists(
    26  						"cloudstack_vpn_connection.bar-foo", &vpnConnection),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccCheckCloudStackVPNConnectionExists(
    34  	n string, vpnConnection *cloudstack.VpnConnection) 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 Connection ID is set")
    43  		}
    44  
    45  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    46  		v, _, err := cs.VPN.GetVpnConnectionByID(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 Connection not found")
    54  		}
    55  
    56  		*vpnConnection = *v
    57  
    58  		return nil
    59  	}
    60  }
    61  
    62  func testAccCheckCloudStackVPNConnectionDestroy(s *terraform.State) error {
    63  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    64  
    65  	for _, rs := range s.RootModule().Resources {
    66  		if rs.Type != "cloudstack_vpn_connection" {
    67  			continue
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("No VPN Connection ID is set")
    72  		}
    73  
    74  		_, _, err := cs.VPN.GetVpnConnectionByID(rs.Primary.ID)
    75  		if err == nil {
    76  			return fmt.Errorf("VPN Connection %s still exists", rs.Primary.ID)
    77  		}
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  var testAccCloudStackVPNConnection_basic = fmt.Sprintf(`
    84  resource "cloudstack_vpc" "foo" {
    85    name = "terraform-vpc-foo"
    86    cidr = "%s"
    87    vpc_offering = "%s"
    88    zone = "%s"
    89  }
    90  
    91  resource "cloudstack_vpc" "bar" {
    92    name = "terraform-vpc-bar"
    93    cidr = "%s"
    94    vpc_offering = "%s"
    95    zone = "%s"
    96  }
    97  
    98  resource "cloudstack_vpn_gateway" "foo" {
    99    vpc_id = "${cloudstack_vpc.foo.id}"
   100  }
   101  
   102  resource "cloudstack_vpn_gateway" "bar" {
   103    vpc_id = "${cloudstack_vpc.bar.id}"
   104  }
   105  
   106  resource "cloudstack_vpn_customer_gateway" "foo" {
   107    name = "terraform-foo"
   108    cidr = "${cloudstack_vpc.foo.cidr}"
   109    esp_policy = "aes256-sha1"
   110    gateway = "${cloudstack_vpn_gateway.foo.public_ip}"
   111    ike_policy = "aes256-sha1"
   112    ipsec_psk = "terraform"
   113  }
   114  
   115  resource "cloudstack_vpn_customer_gateway" "bar" {
   116    name = "terraform-bar"
   117    cidr = "${cloudstack_vpc.bar.cidr}"
   118    esp_policy = "aes256-sha1"
   119    gateway = "${cloudstack_vpn_gateway.bar.public_ip}"
   120    ike_policy = "aes256-sha1"
   121    ipsec_psk = "terraform"
   122  }
   123  
   124  resource "cloudstack_vpn_connection" "foo-bar" {
   125    customer_gateway_id = "${cloudstack_vpn_customer_gateway.foo.id}"
   126    vpn_gateway_id = "${cloudstack_vpn_gateway.bar.id}"
   127  }
   128  
   129  resource "cloudstack_vpn_connection" "bar-foo" {
   130    customer_gateway_id = "${cloudstack_vpn_customer_gateway.bar.id}"
   131    vpn_gateway_id = "${cloudstack_vpn_gateway.foo.id}"
   132  }`,
   133  	CLOUDSTACK_VPC_CIDR_1,
   134  	CLOUDSTACK_VPC_OFFERING,
   135  	CLOUDSTACK_ZONE,
   136  	CLOUDSTACK_VPC_CIDR_2,
   137  	CLOUDSTACK_VPC_OFFERING,
   138  	CLOUDSTACK_ZONE)