Skip to content

Commit 3b8d835

Browse files
ntk148vKien Nguyen
authored andcommitted
Implement Hypervisor request options
Signed-off-by: Kien Nguyen <kiennt2609@gmail.com>
1 parent 69f51f2 commit 3b8d835

File tree

4 files changed

+230
-6
lines changed

4 files changed

+230
-6
lines changed

openstack/compute/v2/extensions/hypervisors/requests.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,52 @@ import (
55
"github.com/gophercloud/gophercloud/pagination"
66
)
77

8+
// ListOptsBuilder allows extensions to add additional parameters to the
9+
// List request.
10+
type ListOptsBuilder interface {
11+
ToHypervisorListQuery() (string, error)
12+
}
13+
14+
// ListOpts allows the filtering and sorting of paginated collections through
15+
// the API. Filtering is achieved by passing in struct field values that map to
16+
// the server attributes you want to see returned. Marker and Limit are used
17+
// for pagination.
18+
type ListOpts struct {
19+
// Limit is an integer value for the limit of values to return.
20+
// This requires microversion 2.33 or later.
21+
Limit *int `q:"limit"`
22+
23+
// Marker is the ID of the last-seen item as a UUID.
24+
// This requires microversion 2.53 or later.
25+
Marker *string `q:"marker"`
26+
27+
// HypervisorHostnamePattern is the hypervisor hostname or a portion of it.
28+
// This requires microversion 2.53 or later
29+
HypervisorHostnamePattern *string `q:"hypervisor_hostname_pattern"`
30+
31+
// WithServers is a bool to include all servers which belong to each hypervisor
32+
// This requires microversion 2.53 or later
33+
WithServers *bool `q:"with_servers"`
34+
}
35+
36+
// ToHypervisorListQuery formats a ListOpts into a query string.
37+
func (opts ListOpts) ToHypervisorListQuery() (string, error) {
38+
q, err := gophercloud.BuildQueryString(opts)
39+
return q.String(), err
40+
}
41+
842
// List makes a request against the API to list hypervisors.
9-
func List(client *gophercloud.ServiceClient) pagination.Pager {
10-
return pagination.NewPager(client, hypervisorsListDetailURL(client), func(r pagination.PageResult) pagination.Page {
43+
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
44+
url := hypervisorsListDetailURL(client)
45+
if opts != nil {
46+
query, err := opts.ToHypervisorListQuery()
47+
if err != nil {
48+
return pagination.Pager{Err: err}
49+
}
50+
url += query
51+
}
52+
53+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
1154
return HypervisorPage{pagination.SinglePageBase(r)}
1255
})
1356
}

openstack/compute/v2/extensions/hypervisors/results.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func (r *Service) UnmarshalJSON(b []byte) error {
6262
return nil
6363
}
6464

65+
// Server represents an instance running on the hypervisor
66+
type Server struct {
67+
Name string `json:"name"`
68+
UUID string `json:"uuid"`
69+
}
70+
6571
// Hypervisor represents a hypervisor in the OpenStack cloud.
6672
type Hypervisor struct {
6773
// A structure that contains cpu information like arch, model, vendor,
@@ -123,6 +129,10 @@ type Hypervisor struct {
123129
// Service is the service this hypervisor represents.
124130
Service Service `json:"service"`
125131

132+
// Servers is a list of Server object.
133+
// The requires microversion 2.53 or later.
134+
Servers []*Server `json:"servers"`
135+
126136
// VCPUs is the total number of vcpus on the hypervisor.
127137
VCPUs int `json:"vcpus"`
128138

openstack/compute/v2/extensions/hypervisors/testing/fixtures.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,98 @@ const HypervisorListBody = `
157157
]
158158
}`
159159

160+
// HypervisorListWithParametersBody represents a raw hypervisor list result with Pike+ release.
161+
const HypervisorListWithParametersBody = `
162+
{
163+
"hypervisors": [
164+
{
165+
"cpu_info": {
166+
"arch": "x86_64",
167+
"model": "Nehalem",
168+
"vendor": "Intel",
169+
"features": [
170+
"pge",
171+
"clflush"
172+
],
173+
"topology": {
174+
"cores": 1,
175+
"threads": 1,
176+
"sockets": 4
177+
}
178+
},
179+
"current_workload": 0,
180+
"status": "enabled",
181+
"state": "up",
182+
"disk_available_least": 0,
183+
"host_ip": "1.1.1.1",
184+
"free_disk_gb": 1028,
185+
"free_ram_mb": 7680,
186+
"hypervisor_hostname": "fake-mini",
187+
"hypervisor_type": "fake",
188+
"hypervisor_version": 2002000,
189+
"id": "c48f6247-abe4-4a24-824e-ea39e108874f",
190+
"local_gb": 1028,
191+
"local_gb_used": 0,
192+
"memory_mb": 8192,
193+
"memory_mb_used": 512,
194+
"running_vms": 0,
195+
"service": {
196+
"host": "e6a37ee802d74863ab8b91ade8f12a67",
197+
"id": "9c2566e7-7a54-4777-a1ae-c2662f0c407c",
198+
"disabled_reason": null
199+
},
200+
"servers": [
201+
{
202+
"name": "instance-00000001",
203+
"uuid": "c42acc8d-eab3-4e4d-9d90-01b0791328f4"
204+
},
205+
{
206+
"name": "instance-00000002",
207+
"uuid": "8aaf2941-b774-41fc-921b-20c4757cc359"
208+
}
209+
],
210+
"vcpus": 1,
211+
"vcpus_used": 0
212+
},
213+
{
214+
"cpu_info": "{\"arch\": \"x86_64\", \"model\": \"Nehalem\", \"vendor\": \"Intel\", \"features\": [\"pge\", \"clflush\"], \"topology\": {\"cores\": 1, \"threads\": 1, \"sockets\": 4}}",
215+
"current_workload": 0,
216+
"status": "enabled",
217+
"state": "up",
218+
"disk_available_least": 0,
219+
"host_ip": "1.1.1.1",
220+
"free_disk_gb": 1028,
221+
"free_ram_mb": 7680,
222+
"hypervisor_hostname": "fake-mini",
223+
"hypervisor_type": "fake",
224+
"hypervisor_version": 2.002e+06,
225+
"id": "c48f6247-abe4-4a24-824e-ea39e108874f",
226+
"local_gb": 1028,
227+
"local_gb_used": 0,
228+
"memory_mb": 8192,
229+
"memory_mb_used": 512,
230+
"running_vms": 0,
231+
"servers": [
232+
{
233+
"name": "instance-00000001",
234+
"uuid": "c42acc8d-eab3-4e4d-9d90-01b0791328f4"
235+
},
236+
{
237+
"name": "instance-00000002",
238+
"uuid": "8aaf2941-b774-41fc-921b-20c4757cc359"
239+
}
240+
],
241+
"service": {
242+
"host": "e6a37ee802d74863ab8b91ade8f12a67",
243+
"id": "9c2566e7-7a54-4777-a1ae-c2662f0c407c",
244+
"disabled_reason": null
245+
},
246+
"vcpus": 1,
247+
"vcpus_used": 0
248+
}
249+
]
250+
}`
251+
160252
const HypervisorsStatisticsBody = `
161253
{
162254
"hypervisor_statistics": {
@@ -349,6 +441,56 @@ var (
349441
VCPUsUsed: 0,
350442
}
351443

444+
HypervisorFakeWithParameters = hypervisors.Hypervisor{
445+
CPUInfo: hypervisors.CPUInfo{
446+
Arch: "x86_64",
447+
Model: "Nehalem",
448+
Vendor: "Intel",
449+
Features: []string{
450+
"pge",
451+
"clflush",
452+
},
453+
Topology: hypervisors.Topology{
454+
Cores: 1,
455+
Threads: 1,
456+
Sockets: 4,
457+
},
458+
},
459+
CurrentWorkload: 0,
460+
Status: "enabled",
461+
State: "up",
462+
DiskAvailableLeast: 0,
463+
HostIP: "1.1.1.1",
464+
FreeDiskGB: 1028,
465+
FreeRamMB: 7680,
466+
HypervisorHostname: "fake-mini",
467+
HypervisorType: "fake",
468+
HypervisorVersion: 2002000,
469+
ID: "c48f6247-abe4-4a24-824e-ea39e108874f",
470+
LocalGB: 1028,
471+
LocalGBUsed: 0,
472+
MemoryMB: 8192,
473+
MemoryMBUsed: 512,
474+
RunningVMs: 0,
475+
Service: hypervisors.Service{
476+
Host: "e6a37ee802d74863ab8b91ade8f12a67",
477+
ID: "9c2566e7-7a54-4777-a1ae-c2662f0c407c",
478+
DisabledReason: "",
479+
},
480+
Servers: []*hypervisors.Server{
481+
{
482+
Name: "instance-00000001",
483+
UUID: "c42acc8d-eab3-4e4d-9d90-01b0791328f4",
484+
},
485+
{
486+
Name: "instance-00000002",
487+
UUID: "8aaf2941-b774-41fc-921b-20c4757cc359",
488+
},
489+
},
490+
VCPUs: 1,
491+
VCPUsUsed: 0,
492+
}
493+
352494
HypervisorEmptyCPUInfo = hypervisors.Hypervisor{
353495
CurrentWorkload: 0,
354496
Status: "enabled",
@@ -429,6 +571,19 @@ func HandleHypervisorListSuccessfully(t *testing.T) {
429571
})
430572
}
431573

574+
func HandleHypervisorListWithParametersSuccessfully(t *testing.T) {
575+
testhelper.Mux.HandleFunc("/os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
576+
testhelper.TestMethod(t, r, "GET")
577+
testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
578+
testhelper.TestFormValues(t, r, map[string]string{
579+
"with_servers": "true",
580+
})
581+
582+
w.Header().Add("Content-Type", "application/json")
583+
fmt.Fprintf(w, HypervisorListWithParametersBody)
584+
})
585+
}
586+
432587
func HandleHypervisorGetSuccessfully(t *testing.T) {
433588
testhelper.Mux.HandleFunc("/os-hypervisors/"+HypervisorFake.ID, func(w http.ResponseWriter, r *http.Request) {
434589
testhelper.TestMethod(t, r, "GET")

openstack/compute/v2/extensions/hypervisors/testing/requests_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ func TestListHypervisorsPre253(t *testing.T) {
1515
HandleHypervisorListPre253Successfully(t)
1616

1717
pages := 0
18-
err := hypervisors.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
18+
err := hypervisors.List(client.ServiceClient(),
19+
hypervisors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
1920
pages++
2021

2122
actual, err := hypervisors.ExtractHypervisors(page)
@@ -44,7 +45,7 @@ func TestListAllHypervisorsPre253(t *testing.T) {
4445
defer testhelper.TeardownHTTP()
4546
HandleHypervisorListPre253Successfully(t)
4647

47-
allPages, err := hypervisors.List(client.ServiceClient()).AllPages()
48+
allPages, err := hypervisors.List(client.ServiceClient(), hypervisors.ListOpts{}).AllPages()
4849
testhelper.AssertNoErr(t, err)
4950
actual, err := hypervisors.ExtractHypervisors(allPages)
5051
testhelper.AssertNoErr(t, err)
@@ -58,7 +59,8 @@ func TestListHypervisors(t *testing.T) {
5859
HandleHypervisorListSuccessfully(t)
5960

6061
pages := 0
61-
err := hypervisors.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
62+
err := hypervisors.List(client.ServiceClient(),
63+
hypervisors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
6264
pages++
6365

6466
actual, err := hypervisors.ExtractHypervisors(page)
@@ -87,14 +89,28 @@ func TestListAllHypervisors(t *testing.T) {
8789
defer testhelper.TeardownHTTP()
8890
HandleHypervisorListSuccessfully(t)
8991

90-
allPages, err := hypervisors.List(client.ServiceClient()).AllPages()
92+
allPages, err := hypervisors.List(client.ServiceClient(), hypervisors.ListOpts{}).AllPages()
9193
testhelper.AssertNoErr(t, err)
9294
actual, err := hypervisors.ExtractHypervisors(allPages)
9395
testhelper.AssertNoErr(t, err)
9496
testhelper.CheckDeepEquals(t, HypervisorFake, actual[0])
9597
testhelper.CheckDeepEquals(t, HypervisorFake, actual[1])
9698
}
9799

100+
func TestListAllHypervisorsWithParameters(t *testing.T) {
101+
testhelper.SetupHTTP()
102+
defer testhelper.TeardownHTTP()
103+
HandleHypervisorListWithParametersSuccessfully(t)
104+
105+
with_servers := true
106+
allPages, err := hypervisors.List(client.ServiceClient(), hypervisors.ListOpts{WithServers: &with_servers}).AllPages()
107+
testhelper.AssertNoErr(t, err)
108+
actual, err := hypervisors.ExtractHypervisors(allPages)
109+
testhelper.AssertNoErr(t, err)
110+
testhelper.CheckDeepEquals(t, HypervisorFakeWithParameters, actual[0])
111+
testhelper.CheckDeepEquals(t, HypervisorFakeWithParameters, actual[1])
112+
}
113+
98114
func TestHypervisorsStatistics(t *testing.T) {
99115
testhelper.SetupHTTP()
100116
defer testhelper.TeardownHTTP()

0 commit comments

Comments
 (0)