|
| 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", &vpnConnection), |
| 25 | + ), |
| 26 | + }, |
| 27 | + }, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +func testAccCheckCloudStackVPNConnectionExists( |
| 32 | + n string, vpnConnection *cloudstack.VpnConnection) resource.TestCheckFunc { |
| 33 | + return func(s *terraform.State) error { |
| 34 | + rs, ok := s.RootModule().Resources[n] |
| 35 | + if !ok { |
| 36 | + return fmt.Errorf("Not found: %s", n) |
| 37 | + } |
| 38 | + |
| 39 | + if rs.Primary.ID == "" { |
| 40 | + return fmt.Errorf("No VPN Connection ID is set") |
| 41 | + } |
| 42 | + |
| 43 | + cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) |
| 44 | + v, _, err := cs.VPN.GetVpnConnectionByID(rs.Primary.ID) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + if v.Id != rs.Primary.ID { |
| 51 | + return fmt.Errorf("VPN Connection not found") |
| 52 | + } |
| 53 | + |
| 54 | + *vpnConnection = *v |
| 55 | + |
| 56 | + return nil |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func testAccCheckCloudStackVPNConnectionDestroy(s *terraform.State) error { |
| 61 | + cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) |
| 62 | + |
| 63 | + for _, rs := range s.RootModule().Resources { |
| 64 | + if rs.Type != "cloudstack_vpn_connection" { |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + if rs.Primary.ID == "" { |
| 69 | + return fmt.Errorf("No VPN Connection ID is set") |
| 70 | + } |
| 71 | + |
| 72 | + p := cs.VPN.NewDeleteVpnConnectionParams(rs.Primary.ID) |
| 73 | + _, err := cs.VPN.DeleteVpnConnection(p) |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf( |
| 77 | + "Error deleting VPN Connection (%s): %s", |
| 78 | + rs.Primary.ID, err) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +var testAccCloudStackVPNConnection_basic = fmt.Sprintf(` |
| 86 | +resource "cloudstack_vpc" "foo" { |
| 87 | + name = "terraform-vpc" |
| 88 | + display_text = "terraform-vpc-text" |
| 89 | + cidr = "%s" |
| 90 | + vpc_offering = "%s" |
| 91 | + zone = "%s" |
| 92 | +} |
| 93 | +
|
| 94 | +resource "cloudstack_vpc" "bar" { |
| 95 | + name = "terraform-vpc" |
| 96 | + display_text = "terraform-vpc-text" |
| 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.publicip}" |
| 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.publicip}" |
| 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) |
0 commit comments