github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_vpn_customer_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 TestAccCloudStackVPNCustomerGateway_basic(t *testing.T) {
    13  	var vpnCustomerGateway cloudstack.VpnCustomerGateway
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackVPNCustomerGatewayDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackVPNCustomerGateway_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackVPNCustomerGatewayExists(
    24  						"cloudstack_vpn_customer_gateway.foo", &vpnCustomerGateway),
    25  					testAccCheckCloudStackVPNCustomerGatewayAttributes(&vpnCustomerGateway),
    26  					resource.TestCheckResourceAttr(
    27  						"cloudstack_vpn_customer_gateway.foo", "name", "terraform-foo"),
    28  					resource.TestCheckResourceAttr(
    29  						"cloudstack_vpn_customer_gateway.bar", "name", "terraform-bar"),
    30  					resource.TestCheckResourceAttr(
    31  						"cloudstack_vpn_customer_gateway.foo", "ike_policy", "aes256-sha1"),
    32  					resource.TestCheckResourceAttr(
    33  						"cloudstack_vpn_customer_gateway.bar", "esp_policy", "aes256-sha1"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccCloudStackVPNCustomerGateway_update(t *testing.T) {
    41  	var vpnCustomerGateway cloudstack.VpnCustomerGateway
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckCloudStackVPNCustomerGatewayDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: testAccCloudStackVPNCustomerGateway_basic,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckCloudStackVPNCustomerGatewayExists(
    52  						"cloudstack_vpn_customer_gateway.foo", &vpnCustomerGateway),
    53  					testAccCheckCloudStackVPNCustomerGatewayAttributes(&vpnCustomerGateway),
    54  					resource.TestCheckResourceAttr(
    55  						"cloudstack_vpn_customer_gateway.foo", "name", "terraform-foo"),
    56  					resource.TestCheckResourceAttr(
    57  						"cloudstack_vpn_customer_gateway.bar", "name", "terraform-bar"),
    58  					resource.TestCheckResourceAttr(
    59  						"cloudstack_vpn_customer_gateway.foo", "ike_policy", "aes256-sha1"),
    60  					resource.TestCheckResourceAttr(
    61  						"cloudstack_vpn_customer_gateway.bar", "esp_policy", "aes256-sha1"),
    62  				),
    63  			},
    64  
    65  			resource.TestStep{
    66  				Config: testAccCloudStackVPNCustomerGateway_update,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckCloudStackVPNCustomerGatewayExists(
    69  						"cloudstack_vpn_customer_gateway.foo", &vpnCustomerGateway),
    70  					testAccCheckCloudStackVPNCustomerGatewayUpdatedAttributes(&vpnCustomerGateway),
    71  					resource.TestCheckResourceAttr(
    72  						"cloudstack_vpn_customer_gateway.foo", "name", "terraform-foo-bar"),
    73  					resource.TestCheckResourceAttr(
    74  						"cloudstack_vpn_customer_gateway.bar", "name", "terraform-bar-foo"),
    75  					resource.TestCheckResourceAttr(
    76  						"cloudstack_vpn_customer_gateway.foo", "ike_policy", "3des-md5"),
    77  					resource.TestCheckResourceAttr(
    78  						"cloudstack_vpn_customer_gateway.bar", "esp_policy", "3des-md5"),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccCheckCloudStackVPNCustomerGatewayExists(
    86  	n string, vpnCustomerGateway *cloudstack.VpnCustomerGateway) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  		rs, ok := s.RootModule().Resources[n]
    89  		if !ok {
    90  			return fmt.Errorf("Not found: %s", n)
    91  		}
    92  
    93  		if rs.Primary.ID == "" {
    94  			return fmt.Errorf("No VPN CustomerGateway ID is set")
    95  		}
    96  
    97  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    98  		v, _, err := cs.VPN.GetVpnCustomerGatewayByID(rs.Primary.ID)
    99  
   100  		if err != nil {
   101  			return err
   102  		}
   103  
   104  		if v.Id != rs.Primary.ID {
   105  			return fmt.Errorf("VPN CustomerGateway not found")
   106  		}
   107  
   108  		*vpnCustomerGateway = *v
   109  
   110  		return nil
   111  	}
   112  }
   113  
   114  func testAccCheckCloudStackVPNCustomerGatewayAttributes(
   115  	vpnCustomerGateway *cloudstack.VpnCustomerGateway) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  
   118  		if vpnCustomerGateway.Esppolicy != "aes256-sha1" {
   119  			return fmt.Errorf("Bad ESP policy: %s", vpnCustomerGateway.Esppolicy)
   120  		}
   121  
   122  		if vpnCustomerGateway.Ikepolicy != "aes256-sha1" {
   123  			return fmt.Errorf("Bad IKE policy: %s", vpnCustomerGateway.Ikepolicy)
   124  		}
   125  
   126  		if vpnCustomerGateway.Ipsecpsk != "terraform" {
   127  			return fmt.Errorf("Bad IPSEC pre-shared key: %s", vpnCustomerGateway.Ipsecpsk)
   128  		}
   129  
   130  		return nil
   131  	}
   132  }
   133  
   134  func testAccCheckCloudStackVPNCustomerGatewayUpdatedAttributes(
   135  	vpnCustomerGateway *cloudstack.VpnCustomerGateway) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  
   138  		if vpnCustomerGateway.Esppolicy != "3des-md5" {
   139  			return fmt.Errorf("Bad ESP policy: %s", vpnCustomerGateway.Esppolicy)
   140  		}
   141  
   142  		if vpnCustomerGateway.Ikepolicy != "3des-md5" {
   143  			return fmt.Errorf("Bad IKE policy: %s", vpnCustomerGateway.Ikepolicy)
   144  		}
   145  
   146  		if vpnCustomerGateway.Ipsecpsk != "terraform" {
   147  			return fmt.Errorf("Bad IPSEC pre-shared key: %s", vpnCustomerGateway.Ipsecpsk)
   148  		}
   149  
   150  		return nil
   151  	}
   152  }
   153  
   154  func testAccCheckCloudStackVPNCustomerGatewayDestroy(s *terraform.State) error {
   155  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   156  
   157  	for _, rs := range s.RootModule().Resources {
   158  		if rs.Type != "cloudstack_vpn_customer_gateway" {
   159  			continue
   160  		}
   161  
   162  		if rs.Primary.ID == "" {
   163  			return fmt.Errorf("No VPN Customer Gateway ID is set")
   164  		}
   165  
   166  		_, _, err := cs.VPN.GetVpnCustomerGatewayByID(rs.Primary.ID)
   167  		if err == nil {
   168  			return fmt.Errorf("VPN Customer Gateway %s still exists", rs.Primary.ID)
   169  		}
   170  	}
   171  
   172  	return nil
   173  }
   174  
   175  var testAccCloudStackVPNCustomerGateway_basic = fmt.Sprintf(`
   176  resource "cloudstack_vpc" "foo" {
   177  	name = "terraform-vpc-foo"
   178  	cidr = "%s"
   179  	vpc_offering = "%s"
   180  	zone = "%s"
   181  }
   182  
   183  resource "cloudstack_vpc" "bar" {
   184  	name = "terraform-vpc-bar"
   185  	cidr = "%s"
   186  	vpc_offering = "%s"
   187  	zone = "%s"
   188  }
   189  
   190  resource "cloudstack_vpn_gateway" "foo" {
   191  	vpc = "${cloudstack_vpc.foo.name}"
   192  }
   193  
   194  resource "cloudstack_vpn_gateway" "bar" {
   195  	vpc = "${cloudstack_vpc.bar.name}"
   196  }
   197  
   198  resource "cloudstack_vpn_customer_gateway" "foo" {
   199  	name = "terraform-foo"
   200  	cidr = "${cloudstack_vpc.foo.cidr}"
   201  	esp_policy = "aes256-sha1"
   202  	gateway = "${cloudstack_vpn_gateway.foo.public_ip}"
   203  	ike_policy = "aes256-sha1"
   204  	ipsec_psk = "terraform"
   205  }
   206  
   207  resource "cloudstack_vpn_customer_gateway" "bar" {
   208    name = "terraform-bar"
   209    cidr = "${cloudstack_vpc.bar.cidr}"
   210    esp_policy = "aes256-sha1"
   211    gateway = "${cloudstack_vpn_gateway.bar.public_ip}"
   212    ike_policy = "aes256-sha1"
   213  	ipsec_psk = "terraform"
   214  }`,
   215  	CLOUDSTACK_VPC_CIDR_1,
   216  	CLOUDSTACK_VPC_OFFERING,
   217  	CLOUDSTACK_ZONE,
   218  	CLOUDSTACK_VPC_CIDR_2,
   219  	CLOUDSTACK_VPC_OFFERING,
   220  	CLOUDSTACK_ZONE)
   221  
   222  var testAccCloudStackVPNCustomerGateway_update = fmt.Sprintf(`
   223  resource "cloudstack_vpc" "foo" {
   224    name = "terraform-vpc-foo"
   225    cidr = "%s"
   226    vpc_offering = "%s"
   227    zone = "%s"
   228  }
   229  
   230  resource "cloudstack_vpc" "bar" {
   231    name = "terraform-vpc-bar"
   232    cidr = "%s"
   233    vpc_offering = "%s"
   234    zone = "%s"
   235  }
   236  
   237  resource "cloudstack_vpn_gateway" "foo" {
   238    vpc = "${cloudstack_vpc.foo.name}"
   239  }
   240  
   241  resource "cloudstack_vpn_gateway" "bar" {
   242    vpc = "${cloudstack_vpc.bar.name}"
   243  }
   244  
   245  resource "cloudstack_vpn_customer_gateway" "foo" {
   246    name = "terraform-foo-bar"
   247    cidr = "${cloudstack_vpc.foo.cidr}"
   248    esp_policy = "3des-md5"
   249    gateway = "${cloudstack_vpn_gateway.foo.public_ip}"
   250    ike_policy = "3des-md5"
   251    ipsec_psk = "terraform"
   252  }
   253  
   254  resource "cloudstack_vpn_customer_gateway" "bar" {
   255    name = "terraform-bar-foo"
   256    cidr = "${cloudstack_vpc.bar.cidr}"
   257    esp_policy = "3des-md5"
   258    gateway = "${cloudstack_vpn_gateway.bar.public_ip}"
   259    ike_policy = "3des-md5"
   260    ipsec_psk = "terraform"
   261  }`,
   262  	CLOUDSTACK_VPC_CIDR_1,
   263  	CLOUDSTACK_VPC_OFFERING,
   264  	CLOUDSTACK_ZONE,
   265  	CLOUDSTACK_VPC_CIDR_2,
   266  	CLOUDSTACK_VPC_OFFERING,
   267  	CLOUDSTACK_ZONE)