Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59a66fb

Browse files
Merge pull request #3213 from gophercloud/bp-v2-c0c4736-5f4d249
[v2] Handle nova api version > 2.87 for hypervisor
2 parents c75258d + 91f66ee commit 59a66fb

File tree

3 files changed

+123
-33
lines changed

3 files changed

+123
-33
lines changed

openstack/compute/v2/hypervisors/results.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,31 @@ func (r *Hypervisor) UnmarshalJSON(b []byte) error {
158158

159159
*r = Hypervisor(s.tmp)
160160

161-
// Newer versions return the CPU info as the correct type.
162-
// Older versions return the CPU info as a string and need to be
163-
// unmarshalled by the json parser.
164-
var tmpb []byte
165-
166-
switch t := s.CPUInfo.(type) {
167-
case string:
168-
tmpb = []byte(t)
169-
case map[string]any:
170-
tmpb, err = json.Marshal(t)
171-
if err != nil {
172-
return err
161+
// cpu_info doesn't exist after api version 2.87,
162+
// see https://docs.openstack.org/api-ref/compute/#id288
163+
if s.CPUInfo != nil {
164+
// api versions 2.28 to 2.87 return the CPU info as the correct type.
165+
// api versions < 2.28 return the CPU info as a string and need to be
166+
// unmarshalled by the json parser.
167+
var tmpb []byte
168+
169+
switch t := s.CPUInfo.(type) {
170+
case string:
171+
tmpb = []byte(t)
172+
case map[string]any:
173+
tmpb, err = json.Marshal(t)
174+
if err != nil {
175+
return err
176+
}
177+
default:
178+
return fmt.Errorf("CPUInfo has unexpected type: %T", t)
173179
}
174-
default:
175-
return fmt.Errorf("CPUInfo has unexpected type: %T", t)
176-
}
177180

178-
if len(tmpb) != 0 {
179-
err = json.Unmarshal(tmpb, &r.CPUInfo)
180-
if err != nil {
181-
return err
181+
if len(tmpb) != 0 {
182+
err = json.Unmarshal(tmpb, &r.CPUInfo)
183+
if err != nil {
184+
return err
185+
}
182186
}
183187
}
184188

@@ -193,22 +197,28 @@ func (r *Hypervisor) UnmarshalJSON(b []byte) error {
193197
return fmt.Errorf("Hypervisor version has unexpected type: %T", t)
194198
}
195199

196-
switch t := s.FreeDiskGB.(type) {
197-
case int:
198-
r.FreeDiskGB = t
199-
case float64:
200-
r.FreeDiskGB = int(t)
201-
default:
202-
return fmt.Errorf("Free disk GB has unexpected type: %T", t)
200+
// free_disk_gb doesn't exist after api version 2.87
201+
if s.FreeDiskGB != nil {
202+
switch t := s.FreeDiskGB.(type) {
203+
case int:
204+
r.FreeDiskGB = t
205+
case float64:
206+
r.FreeDiskGB = int(t)
207+
default:
208+
return fmt.Errorf("Free disk GB has unexpected type: %T", t)
209+
}
203210
}
204211

205-
switch t := s.LocalGB.(type) {
206-
case int:
207-
r.LocalGB = t
208-
case float64:
209-
r.LocalGB = int(t)
210-
default:
211-
return fmt.Errorf("Local GB has unexpected type: %T", t)
212+
// local_gb doesn't exist after api version 2.87
213+
if s.LocalGB != nil {
214+
switch t := s.LocalGB.(type) {
215+
case int:
216+
r.LocalGB = t
217+
case float64:
218+
r.LocalGB = int(t)
219+
default:
220+
return fmt.Errorf("Local GB has unexpected type: %T", t)
221+
}
212222
}
213223

214224
// OpenStack Compute service returns ID in string representation since

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,36 @@ const HypervisorGetEmptyCPUInfoBody = `
346346
}
347347
`
348348

349+
// HypervisorAfterV287ResponseBody represents a raw hypervisor GET result with
350+
// missing cpu_info, free_disk_gb, local_gb as seen after v2.87
351+
const HypervisorAfterV287ResponseBody = `
352+
{
353+
"hypervisor":{
354+
"current_workload":0,
355+
"status":"enabled",
356+
"state":"up",
357+
"disk_available_least":0,
358+
"host_ip":"1.1.1.1",
359+
"free_ram_mb":7680,
360+
"hypervisor_hostname":"fake-mini",
361+
"hypervisor_type":"fake",
362+
"hypervisor_version":2002000,
363+
"id":"c48f6247-abe4-4a24-824e-ea39e108874f",
364+
"local_gb_used":0,
365+
"memory_mb":8192,
366+
"memory_mb_used":512,
367+
"running_vms":0,
368+
"service":{
369+
"host":"e6a37ee802d74863ab8b91ade8f12a67",
370+
"id":"9c2566e7-7a54-4777-a1ae-c2662f0c407c",
371+
"disabled_reason":null
372+
},
373+
"vcpus":1,
374+
"vcpus_used":0
375+
}
376+
}
377+
`
378+
349379
// HypervisorUptimeBody represents a raw hypervisor uptime request result with
350380
// Pike+ release.
351381
const HypervisorUptimeBody = `
@@ -492,6 +522,7 @@ var (
492522
}
493523

494524
HypervisorEmptyCPUInfo = hypervisors.Hypervisor{
525+
CPUInfo: hypervisors.CPUInfo{},
495526
CurrentWorkload: 0,
496527
Status: "enabled",
497528
State: "up",
@@ -517,6 +548,33 @@ var (
517548
VCPUsUsed: 0,
518549
}
519550

551+
HypervisorAfterV287Response = hypervisors.Hypervisor{
552+
CPUInfo: hypervisors.CPUInfo{},
553+
CurrentWorkload: 0,
554+
Status: "enabled",
555+
State: "up",
556+
DiskAvailableLeast: 0,
557+
HostIP: "1.1.1.1",
558+
FreeDiskGB: 0,
559+
FreeRamMB: 7680,
560+
HypervisorHostname: "fake-mini",
561+
HypervisorType: "fake",
562+
HypervisorVersion: 2002000,
563+
ID: "c48f6247-abe4-4a24-824e-ea39e108874f",
564+
LocalGB: 0,
565+
LocalGBUsed: 0,
566+
MemoryMB: 8192,
567+
MemoryMBUsed: 512,
568+
RunningVMs: 0,
569+
Service: hypervisors.Service{
570+
Host: "e6a37ee802d74863ab8b91ade8f12a67",
571+
ID: "9c2566e7-7a54-4777-a1ae-c2662f0c407c",
572+
DisabledReason: "",
573+
},
574+
VCPUs: 1,
575+
VCPUsUsed: 0,
576+
}
577+
520578
HypervisorsStatisticsExpected = hypervisors.Statistics{
521579
Count: 1,
522580
CurrentWorkload: 0,
@@ -604,6 +662,16 @@ func HandleHypervisorGetEmptyCPUInfoSuccessfully(t *testing.T) {
604662
})
605663
}
606664

665+
func HandleHypervisorAfterV287ResponseSuccessfully(t *testing.T) {
666+
testhelper.Mux.HandleFunc("/os-hypervisors/"+HypervisorFake.ID, func(w http.ResponseWriter, r *http.Request) {
667+
testhelper.TestMethod(t, r, "GET")
668+
testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
669+
670+
w.Header().Add("Content-Type", "application/json")
671+
fmt.Fprintf(w, HypervisorAfterV287ResponseBody)
672+
})
673+
}
674+
607675
func HandleHypervisorUptimeSuccessfully(t *testing.T) {
608676
testhelper.Mux.HandleFunc("/os-hypervisors/"+HypervisorFake.ID+"/uptime", func(w http.ResponseWriter, r *http.Request) {
609677
testhelper.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
@@ -148,6 +148,18 @@ func TestGetHypervisorEmptyCPUInfo(t *testing.T) {
148148
testhelper.CheckDeepEquals(t, &expected, actual)
149149
}
150150

151+
func TestGetHypervisorAfterV287Response(t *testing.T) {
152+
testhelper.SetupHTTP()
153+
defer testhelper.TeardownHTTP()
154+
HandleHypervisorAfterV287ResponseSuccessfully(t)
155+
156+
expected := HypervisorAfterV287Response
157+
158+
actual, err := hypervisors.Get(context.TODO(), client.ServiceClient(), expected.ID).Extract()
159+
testhelper.AssertNoErr(t, err)
160+
testhelper.CheckDeepEquals(t, &expected, actual)
161+
}
162+
151163
func TestHypervisorsUptime(t *testing.T) {
152164
testhelper.SetupHTTP()
153165
defer testhelper.TeardownHTTP()

0 commit comments

Comments
 (0)