github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/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 p := cs.VPN.NewDeleteVpnConnectionParams(rs.Primary.ID) 75 _, err := cs.VPN.DeleteVpnConnection(p) 76 77 if err != nil { 78 return fmt.Errorf( 79 "Error deleting VPN Connection (%s): %s", 80 rs.Primary.ID, err) 81 } 82 } 83 84 return nil 85 } 86 87 var testAccCloudStackVPNConnection_basic = fmt.Sprintf(` 88 resource "cloudstack_vpc" "foo" { 89 name = "terraform-vpc-foo" 90 cidr = "%s" 91 vpc_offering = "%s" 92 zone = "%s" 93 } 94 95 resource "cloudstack_vpc" "bar" { 96 name = "terraform-vpc-bar" 97 cidr = "%s" 98 vpc_offering = "%s" 99 zone = "%s" 100 } 101 102 resource "cloudstack_vpn_gateway" "foo" { 103 vpc = "${cloudstack_vpc.foo.name}" 104 } 105 106 resource "cloudstack_vpn_gateway" "bar" { 107 vpc = "${cloudstack_vpc.bar.name}" 108 } 109 110 resource "cloudstack_vpn_customer_gateway" "foo" { 111 name = "terraform-foo" 112 cidr = "${cloudstack_vpc.foo.cidr}" 113 esp_policy = "aes256-sha1" 114 gateway = "${cloudstack_vpn_gateway.foo.public_ip}" 115 ike_policy = "aes256-sha1" 116 ipsec_psk = "terraform" 117 } 118 119 resource "cloudstack_vpn_customer_gateway" "bar" { 120 name = "terraform-bar" 121 cidr = "${cloudstack_vpc.bar.cidr}" 122 esp_policy = "aes256-sha1" 123 gateway = "${cloudstack_vpn_gateway.bar.public_ip}" 124 ike_policy = "aes256-sha1" 125 ipsec_psk = "terraform" 126 } 127 128 resource "cloudstack_vpn_connection" "foo-bar" { 129 customergatewayid = "${cloudstack_vpn_customer_gateway.foo.id}" 130 vpngatewayid = "${cloudstack_vpn_gateway.bar.id}" 131 } 132 133 resource "cloudstack_vpn_connection" "bar-foo" { 134 customergatewayid = "${cloudstack_vpn_customer_gateway.bar.id}" 135 vpngatewayid = "${cloudstack_vpn_gateway.foo.id}" 136 }`, 137 CLOUDSTACK_VPC_CIDR_1, 138 CLOUDSTACK_VPC_OFFERING, 139 CLOUDSTACK_ZONE, 140 CLOUDSTACK_VPC_CIDR_2, 141 CLOUDSTACK_VPC_OFFERING, 142 CLOUDSTACK_ZONE)