github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/networking/v2/extensions/vpnaas/vpnaas.go (about) 1 package vpnaas 2 3 import ( 4 "testing" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/internal/acceptance/tools" 8 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/endpointgroups" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/ikepolicies" 10 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/ipsecpolicies" 11 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/services" 12 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/siteconnections" 13 th "github.com/gophercloud/gophercloud/testhelper" 14 ) 15 16 // CreateService will create a Service with a random name and a specified router ID 17 // An error will be returned if the service could not be created. 18 func CreateService(t *testing.T, client *gophercloud.ServiceClient, routerID string) (*services.Service, error) { 19 serviceName := tools.RandomString("TESTACC-", 8) 20 21 t.Logf("Attempting to create service %s", serviceName) 22 23 iTrue := true 24 createOpts := services.CreateOpts{ 25 Name: serviceName, 26 AdminStateUp: &iTrue, 27 RouterID: routerID, 28 } 29 service, err := services.Create(client, createOpts).Extract() 30 if err != nil { 31 return service, err 32 } 33 34 t.Logf("Successfully created service %s", serviceName) 35 36 th.AssertEquals(t, service.Name, serviceName) 37 38 return service, nil 39 } 40 41 // DeleteService will delete a service with a specified ID. A fatal error 42 // will occur if the delete was not successful. This works best when used as 43 // a deferred function. 44 func DeleteService(t *testing.T, client *gophercloud.ServiceClient, serviceID string) { 45 t.Logf("Attempting to delete service: %s", serviceID) 46 47 err := services.Delete(client, serviceID).ExtractErr() 48 if err != nil { 49 t.Fatalf("Unable to delete service %s: %v", serviceID, err) 50 } 51 52 t.Logf("Service deleted: %s", serviceID) 53 } 54 55 // CreateIPSecPolicy will create an IPSec Policy with a random name and given 56 // rule. An error will be returned if the rule could not be created. 57 func CreateIPSecPolicy(t *testing.T, client *gophercloud.ServiceClient) (*ipsecpolicies.Policy, error) { 58 policyName := tools.RandomString("TESTACC-", 8) 59 60 t.Logf("Attempting to create IPSec policy %s", policyName) 61 62 createOpts := ipsecpolicies.CreateOpts{ 63 Name: policyName, 64 } 65 66 policy, err := ipsecpolicies.Create(client, createOpts).Extract() 67 if err != nil { 68 return policy, err 69 } 70 71 t.Logf("Successfully created IPSec policy %s", policyName) 72 73 th.AssertEquals(t, policy.Name, policyName) 74 75 return policy, nil 76 } 77 78 // CreateIKEPolicy will create an IKE Policy with a random name and given 79 // rule. An error will be returned if the policy could not be created. 80 func CreateIKEPolicy(t *testing.T, client *gophercloud.ServiceClient) (*ikepolicies.Policy, error) { 81 policyName := tools.RandomString("TESTACC-", 8) 82 83 t.Logf("Attempting to create IKE policy %s", policyName) 84 85 createOpts := ikepolicies.CreateOpts{ 86 Name: policyName, 87 EncryptionAlgorithm: ikepolicies.EncryptionAlgorithm3DES, 88 PFS: ikepolicies.PFSGroup5, 89 } 90 91 policy, err := ikepolicies.Create(client, createOpts).Extract() 92 if err != nil { 93 return policy, err 94 } 95 96 t.Logf("Successfully created IKE policy %s", policyName) 97 98 th.AssertEquals(t, policy.Name, policyName) 99 100 return policy, nil 101 } 102 103 // DeleteIPSecPolicy will delete an IPSec policy with a specified ID. A fatal error will 104 // occur if the delete was not successful. This works best when used as a 105 // deferred function. 106 func DeleteIPSecPolicy(t *testing.T, client *gophercloud.ServiceClient, policyID string) { 107 t.Logf("Attempting to delete IPSec policy: %s", policyID) 108 109 err := ipsecpolicies.Delete(client, policyID).ExtractErr() 110 if err != nil { 111 t.Fatalf("Unable to delete IPSec policy %s: %v", policyID, err) 112 } 113 114 t.Logf("Deleted IPSec policy: %s", policyID) 115 } 116 117 // DeleteIKEPolicy will delete an IKE policy with a specified ID. A fatal error will 118 // occur if the delete was not successful. This works best when used as a 119 // deferred function. 120 func DeleteIKEPolicy(t *testing.T, client *gophercloud.ServiceClient, policyID string) { 121 t.Logf("Attempting to delete policy: %s", policyID) 122 123 err := ikepolicies.Delete(client, policyID).ExtractErr() 124 if err != nil { 125 t.Fatalf("Unable to delete IKE policy %s: %v", policyID, err) 126 } 127 128 t.Logf("Deleted IKE policy: %s", policyID) 129 } 130 131 // CreateEndpointGroup will create an endpoint group with a random name. 132 // An error will be returned if the group could not be created. 133 func CreateEndpointGroup(t *testing.T, client *gophercloud.ServiceClient) (*endpointgroups.EndpointGroup, error) { 134 groupName := tools.RandomString("TESTACC-", 8) 135 136 t.Logf("Attempting to create group %s", groupName) 137 138 createOpts := endpointgroups.CreateOpts{ 139 Name: groupName, 140 Type: endpointgroups.TypeCIDR, 141 Endpoints: []string{ 142 "10.2.0.0/24", 143 "10.3.0.0/24", 144 }, 145 } 146 group, err := endpointgroups.Create(client, createOpts).Extract() 147 if err != nil { 148 return group, err 149 } 150 151 t.Logf("Successfully created group %s", groupName) 152 153 th.AssertEquals(t, group.Name, groupName) 154 155 return group, nil 156 } 157 158 // CreateEndpointGroupWithCIDR will create an endpoint group with a random name and a specified CIDR. 159 // An error will be returned if the group could not be created. 160 func CreateEndpointGroupWithCIDR(t *testing.T, client *gophercloud.ServiceClient, cidr string) (*endpointgroups.EndpointGroup, error) { 161 groupName := tools.RandomString("TESTACC-", 8) 162 163 t.Logf("Attempting to create group %s", groupName) 164 165 createOpts := endpointgroups.CreateOpts{ 166 Name: groupName, 167 Type: endpointgroups.TypeCIDR, 168 Endpoints: []string{ 169 cidr, 170 }, 171 } 172 group, err := endpointgroups.Create(client, createOpts).Extract() 173 if err != nil { 174 return group, err 175 } 176 177 t.Logf("Successfully created group %s", groupName) 178 t.Logf("%v", group) 179 180 th.AssertEquals(t, group.Name, groupName) 181 182 return group, nil 183 } 184 185 // DeleteEndpointGroup will delete an Endpoint group with a specified ID. A fatal error will 186 // occur if the delete was not successful. This works best when used as a 187 // deferred function. 188 func DeleteEndpointGroup(t *testing.T, client *gophercloud.ServiceClient, epGroupID string) { 189 t.Logf("Attempting to delete endpoint group: %s", epGroupID) 190 191 err := endpointgroups.Delete(client, epGroupID).ExtractErr() 192 if err != nil { 193 t.Fatalf("Unable to delete endpoint group %s: %v", epGroupID, err) 194 } 195 196 t.Logf("Deleted endpoint group: %s", epGroupID) 197 198 } 199 200 // CreateEndpointGroupWithSubnet will create an endpoint group with a random name. 201 // An error will be returned if the group could not be created. 202 func CreateEndpointGroupWithSubnet(t *testing.T, client *gophercloud.ServiceClient, subnetID string) (*endpointgroups.EndpointGroup, error) { 203 groupName := tools.RandomString("TESTACC-", 8) 204 205 t.Logf("Attempting to create group %s", groupName) 206 207 createOpts := endpointgroups.CreateOpts{ 208 Name: groupName, 209 Type: endpointgroups.TypeSubnet, 210 Endpoints: []string{ 211 subnetID, 212 }, 213 } 214 group, err := endpointgroups.Create(client, createOpts).Extract() 215 if err != nil { 216 return group, err 217 } 218 219 t.Logf("Successfully created group %s", groupName) 220 221 th.AssertEquals(t, group.Name, groupName) 222 223 return group, nil 224 } 225 226 // CreateSiteConnection will create an IPSec site connection with a random name and specified 227 // IKE policy, IPSec policy, service, peer EP group and local EP Group. 228 // An error will be returned if the connection could not be created. 229 func CreateSiteConnection(t *testing.T, client *gophercloud.ServiceClient, ikepolicyID string, ipsecpolicyID string, serviceID string, peerEPGroupID string, localEPGroupID string) (*siteconnections.Connection, error) { 230 connectionName := tools.RandomString("TESTACC-", 8) 231 232 t.Logf("Attempting to create IPSec site connection %s", connectionName) 233 234 createOpts := siteconnections.CreateOpts{ 235 Name: connectionName, 236 PSK: "secret", 237 Initiator: siteconnections.InitiatorBiDirectional, 238 AdminStateUp: gophercloud.Enabled, 239 IPSecPolicyID: ipsecpolicyID, 240 PeerEPGroupID: peerEPGroupID, 241 IKEPolicyID: ikepolicyID, 242 VPNServiceID: serviceID, 243 LocalEPGroupID: localEPGroupID, 244 PeerAddress: "172.24.4.233", 245 PeerID: "172.24.4.233", 246 MTU: 1500, 247 } 248 connection, err := siteconnections.Create(client, createOpts).Extract() 249 if err != nil { 250 return connection, err 251 } 252 253 t.Logf("Successfully created IPSec Site Connection %s", connectionName) 254 255 th.AssertEquals(t, connection.Name, connectionName) 256 257 return connection, nil 258 } 259 260 // DeleteSiteConnection will delete an IPSec site connection with a specified ID. A fatal error will 261 // occur if the delete was not successful. This works best when used as a 262 // deferred function. 263 func DeleteSiteConnection(t *testing.T, client *gophercloud.ServiceClient, siteConnectionID string) { 264 t.Logf("Attempting to delete site connection: %s", siteConnectionID) 265 266 err := siteconnections.Delete(client, siteConnectionID).ExtractErr() 267 if err != nil { 268 t.Fatalf("Unable to delete site connection %s: %v", siteConnectionID, err) 269 } 270 271 t.Logf("Deleted site connection: %s", siteConnectionID) 272 }