Skip to content

Commit f3d2128

Browse files
committed
Implement hypervisors.GetExt: Get with Query parameter
This introduces a new function GetExt in order to avoid breaking backwards compatibility. Implements #3479
1 parent f1cabac commit f3d2128

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed

openstack/compute/v2/hypervisors/requests.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,42 @@ func GetStatistics(ctx context.Context, client *gophercloud.ServiceClient) (r St
6666
return
6767
}
6868

69+
// GetOptsBuilder allows extensions to add additional parameters to the
70+
// Get request.
71+
type GetOptsBuilder interface {
72+
ToHypervisorGetQuery() (string, error)
73+
}
74+
75+
// GetOpts allows the opt-in to add the servers to the response
76+
type GetOpts struct {
77+
// WithServers is a bool to include all servers which belong to the hypervisor
78+
// This requires microversion 2.53 or later
79+
WithServers *bool `q:"with_servers"`
80+
}
81+
82+
// ToHypervisorGetQuery formats a GetOpts into a query string.
83+
func (opts GetOpts) ToHypervisorGetQuery() (string, error) {
84+
q, err := gophercloud.BuildQueryString(opts)
85+
return q.String(), err
86+
}
87+
6988
// Get makes a request against the API to get details for specific hypervisor.
7089
func Get(ctx context.Context, client *gophercloud.ServiceClient, hypervisorID string) (r HypervisorResult) {
71-
resp, err := client.Get(ctx, hypervisorsGetURL(client, hypervisorID), &r.Body, &gophercloud.RequestOpts{
90+
return GetExt(ctx, client, hypervisorID, nil)
91+
}
92+
93+
// Show makes a request against the API to get details for specific hypervisor with optional query parameters
94+
func GetExt(ctx context.Context, client *gophercloud.ServiceClient, hypervisorID string, opts GetOptsBuilder) (r HypervisorResult) {
95+
url := hypervisorsGetURL(client, hypervisorID)
96+
if opts != nil {
97+
query, err := opts.ToHypervisorGetQuery()
98+
if err != nil {
99+
return HypervisorResult{gophercloud.Result{Err: err}}
100+
}
101+
url += query
102+
}
103+
104+
resp, err := client.Get(ctx, url, &r.Body, &gophercloud.RequestOpts{
72105
OkCodes: []int{200},
73106
})
74107
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)

openstack/compute/v2/hypervisors/testing/fixtures_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,62 @@ const HypervisorGetBody = `
329329
}
330330
`
331331

332+
// HypervisorGetPost253Body represents a raw hypervisor GET result with Pike+
333+
// release with optional server list
334+
const HypervisorGetPost253Body = `
335+
{
336+
"hypervisor":{
337+
"cpu_info":{
338+
"arch":"x86_64",
339+
"model":"Nehalem",
340+
"vendor":"Intel",
341+
"features":[
342+
"pge",
343+
"clflush"
344+
],
345+
"topology":{
346+
"cores":1,
347+
"threads":1,
348+
"sockets":4
349+
}
350+
},
351+
"current_workload":0,
352+
"status":"enabled",
353+
"state":"up",
354+
"servers": [
355+
{
356+
"name": "test_server1",
357+
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
358+
},
359+
{
360+
"name": "test_server2",
361+
"uuid": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
362+
}
363+
],
364+
"disk_available_least":0,
365+
"host_ip":"1.1.1.1",
366+
"free_disk_gb":1028,
367+
"free_ram_mb":7680,
368+
"hypervisor_hostname":"fake-mini",
369+
"hypervisor_type":"fake",
370+
"hypervisor_version":2002000,
371+
"id":"c48f6247-abe4-4a24-824e-ea39e108874f",
372+
"local_gb":1028,
373+
"local_gb_used":0,
374+
"memory_mb":8192,
375+
"memory_mb_used":512,
376+
"running_vms":2,
377+
"service":{
378+
"host":"e6a37ee802d74863ab8b91ade8f12a67",
379+
"id":"9c2566e7-7a54-4777-a1ae-c2662f0c407c",
380+
"disabled_reason":null
381+
},
382+
"vcpus":1,
383+
"vcpus_used":0
384+
}
385+
}
386+
`
387+
332388
// HypervisorGetEmptyCPUInfoBody represents a raw hypervisor GET result with
333389
// no cpu_info
334390
const HypervisorGetEmptyCPUInfoBody = `
@@ -487,6 +543,56 @@ var (
487543
VCPUsUsed: 0,
488544
}
489545

546+
HypervisorFakeWithServers = hypervisors.Hypervisor{
547+
CPUInfo: hypervisors.CPUInfo{
548+
Arch: "x86_64",
549+
Model: "Nehalem",
550+
Vendor: "Intel",
551+
Features: []string{
552+
"pge",
553+
"clflush",
554+
},
555+
Topology: hypervisors.Topology{
556+
Cores: 1,
557+
Threads: 1,
558+
Sockets: 4,
559+
},
560+
},
561+
CurrentWorkload: 0,
562+
Status: "enabled",
563+
State: "up",
564+
Servers: &[]hypervisors.Server{
565+
{
566+
Name: "test_server1",
567+
UUID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
568+
},
569+
{
570+
Name: "test_server2",
571+
UUID: "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
572+
},
573+
},
574+
DiskAvailableLeast: 0,
575+
HostIP: "1.1.1.1",
576+
FreeDiskGB: 1028,
577+
FreeRamMB: 7680,
578+
HypervisorHostname: "fake-mini",
579+
HypervisorType: "fake",
580+
HypervisorVersion: 2002000,
581+
ID: "c48f6247-abe4-4a24-824e-ea39e108874f",
582+
LocalGB: 1028,
583+
LocalGBUsed: 0,
584+
MemoryMB: 8192,
585+
MemoryMBUsed: 512,
586+
RunningVMs: 2,
587+
Service: hypervisors.Service{
588+
Host: "e6a37ee802d74863ab8b91ade8f12a67",
589+
ID: "9c2566e7-7a54-4777-a1ae-c2662f0c407c",
590+
DisabledReason: "",
591+
},
592+
VCPUs: 1,
593+
VCPUsUsed: 0,
594+
}
595+
490596
HypervisorFakeWithParameters = hypervisors.Hypervisor{
491597
CPUInfo: hypervisors.CPUInfo{
492598
Arch: "x86_64",
@@ -676,6 +782,20 @@ func HandleHypervisorGetSuccessfully(t *testing.T, fakeServer th.FakeServer) {
676782
})
677783
}
678784

785+
func HandleHypervisorGetPost253Successfully(t *testing.T, fakeServer th.FakeServer) {
786+
fakeServer.Mux.HandleFunc("/os-hypervisors/"+HypervisorFake.ID, func(w http.ResponseWriter, r *http.Request) {
787+
th.TestMethod(t, r, "GET")
788+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
789+
790+
w.Header().Add("Content-Type", "application/json")
791+
if r.URL.Query().Get("with_servers") == "true" {
792+
fmt.Fprint(w, HypervisorGetPost253Body)
793+
} else {
794+
fmt.Fprint(w, HypervisorGetBody)
795+
}
796+
})
797+
}
798+
679799
func HandleHypervisorGetEmptyCPUInfoSuccessfully(t *testing.T, fakeServer th.FakeServer) {
680800
fakeServer.Mux.HandleFunc("/os-hypervisors/"+HypervisorFake.ID, func(w http.ResponseWriter, r *http.Request) {
681801
th.TestMethod(t, r, "GET")

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ func TestGetHypervisor(t *testing.T) {
135135
th.CheckDeepEquals(t, &expected, actual)
136136
}
137137

138+
func TestGetWithServersHypervisor(t *testing.T) {
139+
fakeServer := th.SetupHTTP()
140+
defer fakeServer.Teardown()
141+
HandleHypervisorGetPost253Successfully(t, fakeServer)
142+
143+
expected := HypervisorFakeWithServers
144+
withServers := true
145+
actual, err := hypervisors.GetExt(context.TODO(), client.ServiceClient(fakeServer), expected.ID, hypervisors.GetOpts{WithServers: &withServers}).Extract()
146+
th.AssertNoErr(t, err)
147+
th.CheckDeepEquals(t, &expected, actual)
148+
}
149+
138150
func TestGetHypervisorEmptyCPUInfo(t *testing.T) {
139151
fakeServer := th.SetupHTTP()
140152
defer fakeServer.Teardown()

0 commit comments

Comments
 (0)