-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vpnaas: Create VPN service #760
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
68aec77
Added create method for vpn services
1eacf5a
Added urls to urls file
c14e678
Added results file for create method
16586dd
Created unit test for vpn service creation
9c6e4ca
Added missing extract method to results file
e204816
Made parameter in create API compliant
19cfa7c
Made flavorId parameter optional, fixed unit tests
7cbe100
Removed space in struct definition
9df9d98
removed comma in struct
fe9da82
formatting
1cf2dbb
added documentation file doc.go
d4b3c20
added descriptive comments to fields
acfd9fa
renamed ToVPNServiceCreateMap to ToServiceCreateMap
644a052
removed faulty description of subnet ID
97f146c
removed field project_id
2c9df10
Added acceptance test for vpn service creation
325cf51
Removed unnecessary print statement
3ee1884
Formatting
ede2826
formatting doc.go file
c8c578c
Changed example in doc.go to verified working example. Also edited so…
c529c58
Fixed inline formatting for results.go
d5115ce
Added comparison to created object at the end
2dc358c
Changed individual attribute comparisons in unit test to struct compa…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
acceptance/openstack/networking/v2/extensions/vpnaas/service_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// +build acceptance networking fwaas | ||
|
||
package vpnaas | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gophercloud/gophercloud/acceptance/clients" | ||
layer3 "github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2/extensions/layer3" | ||
"github.com/gophercloud/gophercloud/acceptance/tools" | ||
) | ||
|
||
func TestServiceCRUD(t *testing.T) { | ||
client, err := clients.NewNetworkV2Client() | ||
if err != nil { | ||
t.Fatalf("Unable to create a network client: %v", err) | ||
} | ||
|
||
router, err := layer3.CreateExternalRouter(t, client) | ||
if err != nil { | ||
t.Fatalf("Unable to create router: %v", err) | ||
} | ||
defer layer3.DeleteRouter(t, client, router.ID) | ||
|
||
service, err := CreateService(t, client, router.ID) | ||
if err != nil { | ||
t.Fatalf("Unable to create service: %v", err) | ||
} | ||
|
||
tools.PrintResource(t, service) | ||
} |
32 changes: 32 additions & 0 deletions
32
acceptance/openstack/networking/v2/extensions/vpnaas/vpnaas.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package vpnaas | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
"github.com/gophercloud/gophercloud/acceptance/tools" | ||
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/services" | ||
) | ||
|
||
// CreateService will create a Service with a random name and a specified router ID | ||
// An error will be returned if the service could not be created. | ||
func CreateService(t *testing.T, client *gophercloud.ServiceClient, routerID string) (*services.Service, error) { | ||
serviceName := tools.RandomString("TESTACC-", 8) | ||
|
||
t.Logf("Attempting to create service %s", serviceName) | ||
|
||
iTrue := true | ||
createOpts := services.CreateOpts{ | ||
Name: serviceName, | ||
AdminStateUp: &iTrue, | ||
RouterID: routerID, | ||
} | ||
service, err := services.Create(client, createOpts).Extract() | ||
if err != nil { | ||
return service, err | ||
} | ||
|
||
t.Logf("Successfully created service %s", serviceName) | ||
|
||
return service, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
Package services allows management and retrieval of VPN services in the | ||
OpenStack Networking Service. | ||
|
||
|
||
Example to Create a Service | ||
|
||
createOpts := services.CreateOpts{ | ||
Name: "vpnservice1", | ||
Description: "A service", | ||
RouterID: "2512e759-e8d7-4eea-a0af-4a85927a2e59", | ||
AdminStateUp: gophercloud.Enabled, | ||
} | ||
|
||
service, err := services.Create(networkClient, createOpts).Extract() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
*/ | ||
package services |
52 changes: 52 additions & 0 deletions
52
openstack/networking/v2/extensions/vpnaas/services/requests.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package services | ||
|
||
import "github.com/gophercloud/gophercloud" | ||
|
||
// CreateOptsBuilder allows extensions to add additional parameters to the | ||
// Create request. | ||
type CreateOptsBuilder interface { | ||
ToServiceCreateMap() (map[string]interface{}, error) | ||
} | ||
|
||
// CreateOpts contains all the values needed to create a new VPN service | ||
type CreateOpts struct { | ||
// TenantID specifies a tenant to own the VPN service. The caller must have | ||
// an admin role in order to set this. Otherwise, this field is left unset | ||
// and the caller will be the owner. | ||
TenantID string `json:"tenant_id,omitempty"` | ||
|
||
// SubnetID is the ID of the subnet. | ||
SubnetID string `json:"subnet_id,omitempty"` | ||
|
||
// RouterID is the ID of the router. | ||
RouterID string `json:"router_id" required:"true"` | ||
|
||
// Description is the human readable description of the service. | ||
Description string `json:"description,omitempty"` | ||
|
||
// AdminStateUp is the administrative state of the resource, which is up (true) or down (false). | ||
AdminStateUp *bool `json:"admin_state_up"` | ||
|
||
// Name is the human readable name of the service. | ||
Name string `json:"name,omitempty"` | ||
|
||
// The ID of the flavor. | ||
FlavorID string `json:"flavor_id,omitempty"` | ||
} | ||
|
||
// ToServiceCreateMap casts a CreateOpts struct to a map. | ||
func (opts CreateOpts) ToServiceCreateMap() (map[string]interface{}, error) { | ||
return gophercloud.BuildRequestBody(opts, "vpnservice") | ||
} | ||
|
||
// Create accepts a CreateOpts struct and uses the values to create a new | ||
// VPN service. | ||
func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { | ||
b, err := opts.ToServiceCreateMap() | ||
if err != nil { | ||
r.Err = err | ||
return | ||
} | ||
_, r.Err = c.Post(rootURL(c), b, &r.Body, nil) | ||
return | ||
} |
62 changes: 62 additions & 0 deletions
62
openstack/networking/v2/extensions/vpnaas/services/results.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package services | ||
|
||
import ( | ||
"github.com/gophercloud/gophercloud" | ||
) | ||
|
||
// Service is a VPN Service | ||
type Service struct { | ||
// TenantID is the ID of the project. | ||
TenantID string `json:"tenant_id"` | ||
|
||
// SubnetID is the ID of the subnet. | ||
SubnetID string `json:"subnet_id"` | ||
|
||
// RouterID is the ID of the router. | ||
RouterID string `json:"router_id"` | ||
|
||
// Description is a human-readable description for the resource. | ||
// Default is an empty string | ||
Description string `json:"description"` | ||
|
||
// AdminStateUp is the administrative state of the resource, which is up (true) or down (false). | ||
AdminStateUp bool `json:"admin_state_up"` | ||
|
||
// Name is the human readable name of the service. | ||
Name string `json:"name"` | ||
|
||
// Status indicates whether IPsec VPN service is currently operational. | ||
// Values are ACTIVE, DOWN, BUILD, ERROR, PENDING_CREATE, PENDING_UPDATE, or PENDING_DELETE. | ||
Status string `json:"status"` | ||
|
||
// ID is the unique ID of the VPN service. | ||
ID string `json:"id"` | ||
|
||
// ExternalV6IP is the read-only external (public) IPv6 address that is used for the VPN service. | ||
ExternalV6IP string `json:"external_v6_ip"` | ||
|
||
// ExternalV4IP is the read-only external (public) IPv4 address that is used for the VPN service. | ||
ExternalV4IP string `json:"external_v4_ip"` | ||
|
||
// FlavorID is the ID of the flavor. | ||
FlavorID string `json:"flavor_id"` | ||
} | ||
|
||
type commonResult struct { | ||
gophercloud.Result | ||
} | ||
|
||
// Extract is a function that accepts a result and extracts a VPN service. | ||
func (r commonResult) Extract() (*Service, error) { | ||
var s struct { | ||
Service *Service `json:"vpnservice"` | ||
} | ||
err := r.ExtractInto(&s) | ||
return s.Service, err | ||
} | ||
|
||
// CreateResult represents the result of a create operation. Call its Extract | ||
// method to interpret it as a Service. | ||
type CreateResult struct { | ||
commonResult | ||
} |
77 changes: 77 additions & 0 deletions
77
openstack/networking/v2/extensions/vpnaas/services/testing/requests_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package testing | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" | ||
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/services" | ||
th "github.com/gophercloud/gophercloud/testhelper" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
th.SetupHTTP() | ||
defer th.TeardownHTTP() | ||
|
||
th.Mux.HandleFunc("/v2.0/vpn/vpnservices", func(w http.ResponseWriter, r *http.Request) { | ||
th.TestMethod(t, r, "POST") | ||
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) | ||
th.TestHeader(t, r, "Content-Type", "application/json") | ||
th.TestHeader(t, r, "Accept", "application/json") | ||
th.TestJSONRequest(t, r, ` | ||
{ | ||
"vpnservice": { | ||
"router_id": "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa", | ||
"name": "vpn", | ||
"admin_state_up": true, | ||
"description": "OpenStack VPN service", | ||
"tenant_id": "10039663455a446d8ba2cbb058b0f578" | ||
} | ||
} `) | ||
|
||
w.Header().Add("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusCreated) | ||
|
||
fmt.Fprintf(w, ` | ||
{ | ||
"vpnservice": { | ||
"router_id": "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa", | ||
"status": "PENDING_CREATE", | ||
"name": "vpn", | ||
"external_v6_ip": "2001:db8::1", | ||
"admin_state_up": true, | ||
"subnet_id": null, | ||
"tenant_id": "10039663455a446d8ba2cbb058b0f578", | ||
"external_v4_ip": "172.32.1.11", | ||
"id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", | ||
"description": "OpenStack VPN service" | ||
} | ||
} | ||
`) | ||
}) | ||
|
||
options := services.CreateOpts{ | ||
TenantID: "10039663455a446d8ba2cbb058b0f578", | ||
Name: "vpn", | ||
Description: "OpenStack VPN service", | ||
AdminStateUp: gophercloud.Enabled, | ||
RouterID: "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa", | ||
} | ||
actual, err := services.Create(fake.ServiceClient(), options).Extract() | ||
th.AssertNoErr(t, err) | ||
expected := services.Service{ | ||
RouterID: "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa", | ||
Status: "PENDING_CREATE", | ||
Name: "vpn", | ||
ExternalV6IP: "2001:db8::1", | ||
AdminStateUp: true, | ||
SubnetID: "", | ||
TenantID: "10039663455a446d8ba2cbb058b0f578", | ||
ExternalV4IP: "172.32.1.11", | ||
ID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", | ||
Description: "OpenStack VPN service", | ||
} | ||
th.AssertDeepEquals(t, expected, *actual) | ||
} |
16 changes: 16 additions & 0 deletions
16
openstack/networking/v2/extensions/vpnaas/services/urls.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package services | ||
|
||
import "github.com/gophercloud/gophercloud" | ||
|
||
const ( | ||
rootPath = "vpn" | ||
resourcePath = "vpnservices" | ||
) | ||
|
||
func rootURL(c *gophercloud.ServiceClient) string { | ||
return c.ServiceURL(rootPath, resourcePath) | ||
} | ||
|
||
func resourceURL(c *gophercloud.ServiceClient, id string) string { | ||
return c.ServiceURL(rootPath, resourcePath, id) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc nit: these fields should have comments to describe what they are.