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