Adding ListAPIVersions operation
diff --git a/openstack/networking/v2/networks/requests.go b/openstack/networking/v2/networks/requests.go
index b74608b..da97f56 100644
--- a/openstack/networking/v2/networks/requests.go
+++ b/openstack/networking/v2/networks/requests.go
@@ -1,5 +1,10 @@
package networks
+import (
+ "github.com/racker/perigee"
+ "github.com/rackspace/gophercloud"
+)
+
// User-defined options sent to the API when creating or updating a network.
type NetworkOpts struct {
// The administrative state of the network, which is up (true) or down (false).
@@ -15,3 +20,19 @@
// own. You cannot change this value through authorization policies.
TenantID string `json:"tenant_id"`
}
+
+func APIVersions(c *gophercloud.ServiceClient) (*APIVersionsList, error) {
+ url := APIVersionsURL(c)
+
+ var resp APIVersionsList
+ _, err := perigee.Request("GET", url, perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ Results: &resp,
+ OkCodes: []int{200},
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return &resp, nil
+}
diff --git a/openstack/networking/v2/networks/requests_test.go b/openstack/networking/v2/networks/requests_test.go
index 083ca59..fe11a93 100644
--- a/openstack/networking/v2/networks/requests_test.go
+++ b/openstack/networking/v2/networks/requests_test.go
@@ -1,8 +1,13 @@
package networks
import (
+ "fmt"
+ "net/http"
+ "reflect"
+ "testing"
+
"github.com/rackspace/gophercloud"
- "github.com/rackspace/gophercloud/testhelper"
+ th "github.com/rackspace/gophercloud/testhelper"
)
const TokenID = "123"
@@ -12,6 +17,57 @@
Provider: &gophercloud.ProviderClient{
TokenID: TokenID,
},
- Endpoint: testhelper.Endpoint(),
+ Endpoint: th.Endpoint(),
+ }
+}
+
+func TestListAPIVersions(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/", 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, `
+{
+ "versions": [
+ {
+ "status": "CURRENT",
+ "id": "v2.0",
+ "links": [
+ {
+ "href": "http://23.253.228.211:9696/v2.0",
+ "rel": "self"
+ }
+ ]
+ }
+ ]
+}`)
+ })
+
+ c := ServiceClient()
+
+ res, err := APIVersions(c)
+ if err != nil {
+ t.Fatalf("Error listing API versions: %v", err)
+ }
+
+ coll, err := gophercloud.AllPages(res)
+
+ actual := ToAPIVersions(coll)
+
+ expected := []APIVersion{
+ APIVersion{
+ Status: "CURRENT",
+ ID: "v2.0",
+ },
+ }
+
+ if !reflect.DeepEqual(expected, actual) {
+ t.Errorf("Expected %#v, got %#v", expected, actual)
}
}
diff --git a/openstack/networking/v2/networks/results.go b/openstack/networking/v2/networks/results.go
index dddeed7..eabf6bc 100644
--- a/openstack/networking/v2/networks/results.go
+++ b/openstack/networking/v2/networks/results.go
@@ -1,5 +1,12 @@
package networks
+import (
+ "fmt"
+
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+)
+
// A Network represents a a virtual layer-2 broadcast domain.
type Network struct {
// Id is the unique identifier for the network.
@@ -25,3 +32,51 @@
// ProviderSegmentationId is the provider network identifier (such as the vlan id).
ProviderSegmentationId string `json:"provider:segmentation_id"`
}
+
+type APIVersion struct {
+ Status string
+ ID string
+}
+
+type APIVersionsList struct {
+ gophercloud.PaginationLinks `json:"links"`
+ Client *gophercloud.ServiceClient
+ APIVersions []APIVersion `json:"versions"`
+}
+
+func (list APIVersionsList) Pager() gophercloud.Pager {
+ return gophercloud.NewLinkPager(list)
+}
+
+func (list APIVersionsList) Concat(other gophercloud.Collection) gophercloud.Collection {
+ return APIVersionsList{
+ Client: list.Client,
+ APIVersions: append(list.APIVersions, ToAPIVersions(other)...),
+ }
+}
+
+func (list APIVersionsList) Service() *gophercloud.ServiceClient {
+ return list.Client
+}
+
+func (list APIVersionsList) Links() gophercloud.PaginationLinks {
+ return list.PaginationLinks
+}
+
+func (list APIVersionsList) Interpret(json interface{}) (gophercloud.LinkCollection, error) {
+ mapped, ok := json.(map[string]interface{})
+ if !ok {
+ return nil, fmt.Errorf("Unexpected JSON response: %#v", json)
+ }
+
+ var result APIVersionsList
+ err := mapstructure.Decode(mapped, &result)
+ if err != nil {
+ return nil, err
+ }
+ return result, nil
+}
+
+func ToAPIVersions(results gophercloud.Collection) []APIVersion {
+ return results.(*APIVersionsList).APIVersions
+}
diff --git a/openstack/networking/v2/networks/results_test.go b/openstack/networking/v2/networks/results_test.go
deleted file mode 100644
index e69de29..0000000
--- a/openstack/networking/v2/networks/results_test.go
+++ /dev/null
diff --git a/openstack/networking/v2/networks/urls.go b/openstack/networking/v2/networks/urls.go
index e69de29..012e486 100644
--- a/openstack/networking/v2/networks/urls.go
+++ b/openstack/networking/v2/networks/urls.go
@@ -0,0 +1,9 @@
+package networks
+
+import (
+ "github.com/rackspace/gophercloud"
+)
+
+func APIVersionsURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL("")
+}
diff --git a/openstack/networking/v2/networks/urls_test.go b/openstack/networking/v2/networks/urls_test.go
index e69de29..87ae69e 100644
--- a/openstack/networking/v2/networks/urls_test.go
+++ b/openstack/networking/v2/networks/urls_test.go
@@ -0,0 +1,18 @@
+package networks
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud"
+)
+
+const Endpoint = "http://localhost:57909/v3/"
+
+func TestAPIVersionsURL(t *testing.T) {
+ client := gophercloud.ServiceClient{Endpoint: Endpoint}
+ actual := APIVersionsURL(&client)
+ expected := Endpoint
+ if expected != actual {
+ t.Errorf("[%s] does not match expected [%s]", actual, expected)
+ }
+}