Skip to content
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

[nova] fix pagination of hypervisors.List() #3223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openstack/compute/v2/hypervisors/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pa
}

return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return HypervisorPage{pagination.SinglePageBase(r)}
return HypervisorPage{pagination.LinkedPageBase{PageResult: r}}
})
}

Expand Down
15 changes: 14 additions & 1 deletion openstack/compute/v2/hypervisors/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (r *Hypervisor) UnmarshalJSON(b []byte) error {
// HypervisorPage represents a single page of all Hypervisors from a List
// request.
type HypervisorPage struct {
pagination.SinglePageBase
pagination.LinkedPageBase
}

// IsEmpty determines whether or not a HypervisorPage is empty.
Expand All @@ -253,6 +253,19 @@ func (page HypervisorPage) IsEmpty() (bool, error) {
return len(va) == 0, err
}

// NextPageURL uses the response's embedded link reference to navigate to the
// next page of results.
func (page HypervisorPage) NextPageURL() (string, error) {
var s struct {
Links []gophercloud.Link `json:"hypervisors_links"`
}
err := page.ExtractInto(&s)
if err != nil {
return "", err
}
return gophercloud.ExtractNextURL(s.Links)
}

// ExtractHypervisors interprets a page of results as a slice of Hypervisors.
func ExtractHypervisors(p pagination.Page) ([]Hypervisor, error) {
var h struct {
Expand Down
34 changes: 29 additions & 5 deletions openstack/compute/v2/hypervisors/testing/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ const HypervisorListBodyPre253 = `
]
}`

// HypervisorListBody represents a raw hypervisor list result with Pike+ release.
const HypervisorListBody = `
// HypervisorListBodyPage1 represents page 1 of a raw hypervisor list result with Pike+ release.
const HypervisorListBodyPage1 = `
{
"hypervisors": [
{
Expand Down Expand Up @@ -127,7 +127,20 @@ const HypervisorListBody = `
},
"vcpus": 1,
"vcpus_used": 0
},
}
],
"hypervisors_links": [
{
"href": "%s/os-hypervisors/detail?marker=c48f6247-abe4-4a24-824e-ea39e108874f",
"rel": "next"
}
]
}`

// HypervisorListBodyPage2 represents page 2 of a raw hypervisor list result with Pike+ release.
const HypervisorListBodyPage2 = `
{
"hypervisors": [
{
"cpu_info": "{\"arch\": \"x86_64\", \"model\": \"Nehalem\", \"vendor\": \"Intel\", \"features\": [\"pge\", \"clflush\"], \"topology\": {\"cores\": 1, \"threads\": 1, \"sockets\": 4}}",
"current_workload": 0,
Expand Down Expand Up @@ -157,6 +170,9 @@ const HypervisorListBody = `
]
}`

// HypervisorListBodyEmpty represents an empty raw hypervisor list result, marking the end of pagination.
const HypervisorListBodyEmpty = `{ "hypervisors": [] }`

// HypervisorListWithParametersBody represents a raw hypervisor list result with Pike+ release.
const HypervisorListWithParametersBody = `
{
Expand Down Expand Up @@ -624,8 +640,16 @@ func HandleHypervisorListSuccessfully(t *testing.T) {
testhelper.TestMethod(t, r, "GET")
testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)

w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, HypervisorListBody)
switch r.URL.Query().Get("marker") {
case "":
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, HypervisorListBodyPage1, testhelper.Server.URL)
case "c48f6247-abe4-4a24-824e-ea39e108874f":
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, HypervisorListBodyPage2)
default:
http.Error(w, "unexpected marker value", http.StatusInternalServerError)
}
})
}

Expand Down
9 changes: 4 additions & 5 deletions openstack/compute/v2/hypervisors/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,18 @@ func TestListHypervisors(t *testing.T) {
return false, err
}

if len(actual) != 2 {
t.Fatalf("Expected 2 hypervisors, got %d", len(actual))
if len(actual) != 1 {
t.Fatalf("Expected 1 hypervisors on page %d, got %d", pages, len(actual))
}
testhelper.CheckDeepEquals(t, HypervisorFake, actual[0])
testhelper.CheckDeepEquals(t, HypervisorFake, actual[1])

return true, nil
})

testhelper.AssertNoErr(t, err)

if pages != 1 {
t.Errorf("Expected 1 page, saw %d", pages)
if pages != 2 {
t.Errorf("Expected 2 pages, saw %d", pages)
}
}

Expand Down
Loading