Skip to content

Commit a0c15b5

Browse files
committed
Baremetal API: Return versions supported by the API
1 parent 1574321 commit a0c15b5

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Package apiversions provides information about the versions supported by a specific Ironic API.
3+
4+
Example to list versions
5+
6+
allVersions, err := apiversions.List(client.ServiceClient()).AllPages()
7+
8+
Example to get a specific version
9+
10+
actual, err := apiversions.Get(client.ServiceClient(), "v1").Extract()
11+
12+
*/
13+
package apiversions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package apiversions
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
)
6+
7+
// List lists all the API versions available to end users.
8+
func List(client *gophercloud.ServiceClient) (r ListResult) {
9+
_, r.Err = client.Get(listURL(client), &r.Body, nil)
10+
return
11+
}
12+
13+
// Get will get a specific API version, specified by major ID.
14+
func Get(client *gophercloud.ServiceClient, v string) (r GetResult) {
15+
_, r.Err = client.Get(getURL(client, v), &r.Body, nil)
16+
return
17+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package apiversions
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
)
6+
7+
// APIVersions represents the result from getting a list of all versions available
8+
type APIVersions struct {
9+
DefaultVersion APIVersion `json:"default_version"`
10+
Versions []APIVersion `json:"versions"`
11+
}
12+
13+
// APIVersion represents an API version for Ironic
14+
type APIVersion struct {
15+
// ID is the unique identifier of the API version.
16+
ID string `json:"id"`
17+
18+
// MinVersion is the minimum microversion supported.
19+
MinVersion string `json:"min_version"`
20+
21+
// Status is the API versions status.
22+
Status string `json:"status"`
23+
24+
// Version is the maximum microversion supported.
25+
Version string `json:"version"`
26+
}
27+
28+
// GetResult represents the result of a get operation.
29+
type GetResult struct {
30+
gophercloud.Result
31+
}
32+
33+
// ListResult represents the result of a list operation.
34+
type ListResult struct {
35+
gophercloud.Result
36+
}
37+
38+
// Extract is a function that accepts a get result and extracts an API version resource.
39+
func (r GetResult) Extract() (*APIVersion, error) {
40+
var s struct {
41+
Version APIVersion `json:"version"`
42+
}
43+
44+
err := r.ExtractInto(&s)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
return &s.Version, nil
50+
}
51+
52+
// Extract is a function that accepts a list result and extracts an APIVersions resource
53+
func (r ListResult) Extract() (*APIVersions, error) {
54+
var version APIVersions
55+
56+
err := r.ExtractInto(&version)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return &version, nil
62+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package testing
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/gophercloud/gophercloud/openstack/baremetal/apiversions"
9+
th "github.com/gophercloud/gophercloud/testhelper"
10+
"github.com/gophercloud/gophercloud/testhelper/client"
11+
)
12+
13+
const IronicAPIAllVersionResponse = `
14+
{
15+
"default_version": {
16+
"status": "CURRENT",
17+
"min_version": "1.1",
18+
"version": "1.56",
19+
"id": "v1",
20+
"links": [
21+
{
22+
"href": "http://localhost:6385/v1/",
23+
"rel": "self"
24+
}
25+
]
26+
},
27+
"versions": [
28+
{
29+
"status": "CURRENT",
30+
"min_version": "1.1",
31+
"version": "1.56",
32+
"id": "v1",
33+
"links": [
34+
{
35+
"href": "http://localhost:6385/v1/",
36+
"rel": "self"
37+
}
38+
]
39+
}
40+
],
41+
"name": "OpenStack Ironic API",
42+
"description": "Ironic is an OpenStack project which aims to provision baremetal machines."
43+
}
44+
`
45+
46+
const IronicAPIVersionResponse = `
47+
{
48+
"media_types": [
49+
{
50+
"base": "application/json",
51+
"type": "application/vnd.openstack.ironic.v1+json"
52+
}
53+
],
54+
"version": {
55+
"status": "CURRENT",
56+
"min_version": "1.1",
57+
"version": "1.56",
58+
"id": "v1",
59+
"links": [
60+
{
61+
"href": "http://localhost:6385/v1/",
62+
"rel": "self"
63+
}
64+
]
65+
},
66+
"id": "v1"
67+
}
68+
`
69+
70+
var IronicAPIVersion1Result = apiversions.APIVersion{
71+
ID: "v1",
72+
Status: "CURRENT",
73+
MinVersion: "1.1",
74+
Version: "1.56",
75+
}
76+
77+
var IronicAllAPIVersionResults = apiversions.APIVersions{
78+
DefaultVersion: IronicAPIVersion1Result,
79+
Versions: []apiversions.APIVersion{
80+
IronicAPIVersion1Result,
81+
},
82+
}
83+
84+
func MockListResponse(t *testing.T) {
85+
th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
86+
th.TestMethod(t, r, "GET")
87+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
88+
89+
w.Header().Add("Content-Type", "application/json")
90+
w.WriteHeader(http.StatusOK)
91+
92+
fmt.Fprintf(w, IronicAPIAllVersionResponse)
93+
})
94+
}
95+
96+
func MockGetResponse(t *testing.T) {
97+
th.Mux.HandleFunc("/v1/", func(w http.ResponseWriter, r *http.Request) {
98+
th.TestMethod(t, r, "GET")
99+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
100+
101+
w.Header().Add("Content-Type", "application/json")
102+
w.WriteHeader(http.StatusOK)
103+
104+
fmt.Fprintf(w, IronicAPIVersionResponse)
105+
})
106+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package testing
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gophercloud/gophercloud/openstack/baremetal/apiversions"
7+
th "github.com/gophercloud/gophercloud/testhelper"
8+
"github.com/gophercloud/gophercloud/testhelper/client"
9+
)
10+
11+
func TestListAPIVersions(t *testing.T) {
12+
th.SetupHTTP()
13+
defer th.TeardownHTTP()
14+
15+
MockListResponse(t)
16+
17+
actual, err := apiversions.List(client.ServiceClient()).Extract()
18+
th.AssertNoErr(t, err)
19+
20+
th.AssertDeepEquals(t, IronicAllAPIVersionResults, *actual)
21+
}
22+
23+
func TestGetAPIVersion(t *testing.T) {
24+
th.SetupHTTP()
25+
defer th.TeardownHTTP()
26+
27+
MockGetResponse(t)
28+
29+
actual, err := apiversions.Get(client.ServiceClient(), "v1").Extract()
30+
th.AssertNoErr(t, err)
31+
32+
th.AssertDeepEquals(t, IronicAPIVersion1Result, *actual)
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package apiversions
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
)
6+
7+
func getURL(c *gophercloud.ServiceClient, version string) string {
8+
return c.ServiceURL(version)
9+
}
10+
11+
func listURL(c *gophercloud.ServiceClient) string {
12+
return c.ServiceURL()
13+
}

0 commit comments

Comments
 (0)