Skip to content

Commit

Permalink
WIP: handle nova api version > 2.87 for hypervisor
Browse files Browse the repository at this point in the history
fixes: #3026
  • Loading branch information
samuelallan72 committed May 7, 2024
1 parent 543d9fd commit 03cd724
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions openstack/compute/v2/hypervisors/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,31 @@ func (r *Hypervisor) UnmarshalJSON(b []byte) error {

*r = Hypervisor(s.tmp)

// Newer versions return the CPU info as the correct type.
// Older versions return the CPU info as a string and need to be
// unmarshalled by the json parser.
var tmpb []byte

switch t := s.CPUInfo.(type) {
case string:
tmpb = []byte(t)
case map[string]interface{}:
tmpb, err = json.Marshal(t)
if err != nil {
return err
// cpu_info doesn't exist after api version 2.87,
// see https://docs.openstack.org/api-ref/compute/#id288
if s.CPUInfo != nil {
// api versions 2.28 to 2.87 return the CPU info as the correct type.
// api versions < 2.28 return the CPU info as a string and need to be
// unmarshalled by the json parser.
var tmpb []byte

switch t := s.CPUInfo.(type) {
case string:
tmpb = []byte(t)
case map[string]interface{}:
tmpb, err = json.Marshal(t)
if err != nil {
return err
}
default:
return fmt.Errorf("CPUInfo has unexpected type: %T", t)
}
default:
return fmt.Errorf("CPUInfo has unexpected type: %T", t)
}

if len(tmpb) != 0 {
err = json.Unmarshal(tmpb, &r.CPUInfo)
if err != nil {
return err
if len(tmpb) != 0 {
err = json.Unmarshal(tmpb, &r.CPUInfo)
if err != nil {
return err
}
}
}

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

switch t := s.FreeDiskGB.(type) {
case int:
r.FreeDiskGB = t
case float64:
r.FreeDiskGB = int(t)
default:
return fmt.Errorf("Free disk GB has unexpected type: %T", t)
// free_disk_gb doesn't exist after api version 2.87
if s.FreeDiskGB != nil {
switch t := s.FreeDiskGB.(type) {
case int:
r.FreeDiskGB = t
case float64:
r.FreeDiskGB = int(t)
default:
return fmt.Errorf("Free disk GB has unexpected type: %T", t)
}
}

switch t := s.LocalGB.(type) {
case int:
r.LocalGB = t
case float64:
r.LocalGB = int(t)
default:
return fmt.Errorf("Local GB has unexpected type: %T", t)
// local_gb doesn't exist after api version 2.87
if s.LocalGB != nil {
switch t := s.LocalGB.(type) {
case int:
r.LocalGB = t
case float64:
r.LocalGB = int(t)
default:
return fmt.Errorf("Local GB has unexpected type: %T", t)
}
}

// OpenStack Compute service returns ID in string representation since
Expand Down

0 comments on commit 03cd724

Please sign in to comment.