github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 = "${cloudstack_vpc.foo.name}" 100 } 101 102 resource "cloudstack_vpn_gateway" "bar" { 103 vpc = "${cloudstack_vpc.bar.name}" 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 customergatewayid = "${cloudstack_vpn_customer_gateway.foo.id}" 126 vpngatewayid = "${cloudstack_vpn_gateway.bar.id}" 127 } 128 129 resource "cloudstack_vpn_connection" "bar-foo" { 130 customergatewayid = "${cloudstack_vpn_customer_gateway.bar.id}" 131 vpngatewayid = "${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)