Adding get port operation
diff --git a/openstack/networking/v2/apiversions/doc.go b/openstack/networking/v2/apiversions/doc.go
index 76bdb14..9ec7ea2 100644
--- a/openstack/networking/v2/apiversions/doc.go
+++ b/openstack/networking/v2/apiversions/doc.go
@@ -1 +1,3 @@
+// Package apiversions provides information and interaction with the different
+// API versions for the Neutron OpenStack service.
package apiversions
diff --git a/openstack/networking/v2/apiversions/requests.go b/openstack/networking/v2/apiversions/requests.go
index 31e07fd..570b9e8 100644
--- a/openstack/networking/v2/apiversions/requests.go
+++ b/openstack/networking/v2/apiversions/requests.go
@@ -5,12 +5,15 @@
"github.com/rackspace/gophercloud/pagination"
)
+// ListVersions lists all the Neutron API versions available to end-users
func ListVersions(c *gophercloud.ServiceClient) pagination.Pager {
return pagination.NewPager(c, APIVersionsURL(c), func(r pagination.LastHTTPResponse) pagination.Page {
return APIVersionPage{pagination.SinglePageBase(r)}
})
}
+// ListVersionResources lists all of the different API resources for a particular
+// API versions. Typical resources for Neutron might be: networks, subnets, etc.
func ListVersionResources(c *gophercloud.ServiceClient, v string) pagination.Pager {
return pagination.NewPager(c, APIInfoURL(c, v), func(r pagination.LastHTTPResponse) pagination.Page {
return APIVersionResourcePage{pagination.SinglePageBase(r)}
diff --git a/openstack/networking/v2/apiversions/results.go b/openstack/networking/v2/apiversions/results.go
index 9181e7e..0ab3b41 100644
--- a/openstack/networking/v2/apiversions/results.go
+++ b/openstack/networking/v2/apiversions/results.go
@@ -5,15 +5,20 @@
"github.com/rackspace/gophercloud/pagination"
)
+// APIVersion represents an API version for Neutron. It contains the status of
+// the API, and its unique ID.
type APIVersion struct {
Status string `mapstructure:"status" json:"status"`
ID string `mapstructure:"id" json:"id"`
}
+// APIVersionPage is the page returned by a pager when traversing over a
+// collection of API versions.
type APIVersionPage struct {
pagination.SinglePageBase
}
+// IsEmpty checks whether the page is empty.
func (r APIVersionPage) IsEmpty() (bool, error) {
is, err := ExtractAPIVersions(r)
if err != nil {
@@ -22,6 +27,8 @@
return len(is) == 0, nil
}
+// ExtractAPIVersion takes a collection page, extracts all of the elements,
+// and returns them a slice of APIVersion structs. It is effectively a cast.
func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) {
var resp struct {
Versions []APIVersion `mapstructure:"versions"`
@@ -35,6 +42,8 @@
return resp.Versions, nil
}
+// APIVersionResource represents a generic API resource. It contains the name
+// of the resource and its plural collection name.
type APIVersionResource struct {
Name string `mapstructure:"name" json:"name"`
Collection string `mapstructure:"collection" json:"collection"`
diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go
index f8e5620..672b904 100644
--- a/openstack/networking/v2/ports/requests.go
+++ b/openstack/networking/v2/ports/requests.go
@@ -3,6 +3,7 @@
import (
"strconv"
+ "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
"github.com/rackspace/gophercloud/pagination"
@@ -87,3 +88,18 @@
return PortPage{pagination.LinkedPageBase(r)}
})
}
+
+func Get(c *gophercloud.ServiceClient, id string) (*Port, error) {
+ var p Port
+ _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ Results: &struct {
+ Port *Port `json:"port"`
+ }{&p},
+ OkCodes: []int{200},
+ })
+ if err != nil {
+ return nil, err
+ }
+ return &p, nil
+}
diff --git a/openstack/networking/v2/ports/requests_test.go b/openstack/networking/v2/ports/requests_test.go
index 02ec71a..be52f6f 100644
--- a/openstack/networking/v2/ports/requests_test.go
+++ b/openstack/networking/v2/ports/requests_test.go
@@ -115,3 +115,73 @@
t.Errorf("Expected 1 page, got %d", count)
}
}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "port": {
+ "status": "ACTIVE",
+ "binding:host_id": "devstack",
+ "name": "",
+ "allowed_address_pairs": [],
+ "admin_state_up": true,
+ "network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
+ "tenant_id": "7e02058126cc4950b75f9970368ba177",
+ "extra_dhcp_opts": [],
+ "binding:vif_details": {
+ "port_filter": true,
+ "ovs_hybrid_plug": true
+ },
+ "binding:vif_type": "ovs",
+ "device_owner": "network:router_interface",
+ "mac_address": "fa:16:3e:23:fd:d7",
+ "binding:profile": {},
+ "binding:vnic_type": "normal",
+ "fixed_ips": [
+ {
+ "subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
+ "ip_address": "10.0.0.1"
+ }
+ ],
+ "id": "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2",
+ "security_groups": [],
+ "device_id": "5e3898d7-11be-483e-9732-b2f5eccd2b2e"
+ }
+}
+ `)
+ })
+
+ n, err := Get(ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2")
+ th.AssertNoErr(t, err)
+
+ th.AssertEquals(t, n.Status, "ACTIVE")
+ th.AssertEquals(t, n.BindingHostID, "devstack")
+ th.AssertEquals(t, n.Name, "")
+ th.AssertDeepEquals(t, n.AllowedAddressPairs, []interface{}(nil))
+ th.AssertEquals(t, n.AdminStateUp, true)
+ th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
+ th.AssertEquals(t, n.TenantID, "7e02058126cc4950b75f9970368ba177")
+ th.AssertDeepEquals(t, n.ExtraDHCPOpts, []interface{}{})
+ th.AssertDeepEquals(t, n.BindingVIFDetails, map[string]interface{}{"port_filter": true, "ovs_hybrid_plug": true})
+ th.AssertEquals(t, n.DeviceOwner, "network:router_interface")
+ th.AssertEquals(t, n.MACAddress, "fa:16:3e:23:fd:d7")
+ th.AssertDeepEquals(t, n.BindingProfile, map[string]interface{}{})
+ th.AssertEquals(t, n.BindingVNICType, "normal")
+ th.AssertDeepEquals(t, n.FixedIPs, []IP{
+ IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.1"},
+ })
+ th.AssertEquals(t, n.ID, "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2")
+ th.AssertDeepEquals(t, n.SecurityGroups, []string{})
+ th.AssertEquals(t, n.Status, "ACTIVE")
+ th.AssertEquals(t, n.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e")
+}