Skip to content

Commit

Permalink
Merge pull request #72 from cobbler/fix/network-interface-reading
Browse files Browse the repository at this point in the history
Fix issue reading floats and Network Interfaces
  • Loading branch information
SchoolGuy authored Oct 11, 2024
2 parents c34bc43 + c134a22 commit 6ede036
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
30 changes: 18 additions & 12 deletions cobblerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func cobblerDataHacks(fromType, targetType reflect.Kind, data interface{}) (inte
valueStruct.RawData = data
return valueStruct, nil
case reflect.Map:
// This can be: Top-level Map, paged search results, page-info struct or an inherited struct
// This can be: Top-level Map, paged search results, page-info struct, network interface or an inherited struct
mapKeys := dataVal.MapKeys()
sort.SliceStable(mapKeys, func(i, j int) bool {
return mapKeys[i].String() < mapKeys[j].String()
Expand All @@ -324,6 +324,10 @@ func cobblerDataHacks(fromType, targetType reflect.Kind, data interface{}) (inte
// Page-Info struct
return data, nil
}
if len(mapKeys) == 23 && mapKeys[0].String() == "bonding_opts" {
// Network Interface struct
return data, nil
}
for _, key := range mapKeys {
// If the uid key is in the map then it is the top level Map
if key.String() == "uid" {
Expand All @@ -340,23 +344,25 @@ func cobblerDataHacks(fromType, targetType reflect.Kind, data interface{}) (inte
integerValue, err := convertToInt(data)
valueStruct.Data = integerValue
valueStruct.RawData = data
if err == nil {
if err != nil {
return Value[int]{}, err
}
return valueStruct, nil
case reflect.Float64:
// Float that may or may not be inherited
valueStruct := Value[float64]{}
floatValue, err := convertToFloat(data)
valueStruct.Data = floatValue
valueStruct.RawData = data
if err != nil {
return Value[float64]{}, err
}
return valueStruct, nil
case reflect.Bool:
// Bool that may or may not be inherited
valueStruct := Value[bool]{}
integerBoolean, err := convertToInt(data)
if err == nil {
return Value[bool]{}, err
}
boolValue, err := convertIntBool(integerBoolean)
valueStruct.Data = boolValue
valueStruct.Data = data.(bool)
valueStruct.RawData = data
if err == nil {
return Value[bool]{}, err
}
return valueStruct, nil
default:
return nil, fmt.Errorf("unknown type %s fromType for Inherited or Flattened Value", fromType)
Expand All @@ -380,7 +386,7 @@ func decodeCobblerItem(raw interface{}, result interface{}) (interface{}, error)
return nil, err
}

if err := decoder.Decode(raw); err != nil {
if err = decoder.Decode(raw); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion fixtures/get-interfaces-get-system-res.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
<member>
<name>ip_address</name>
<value>
<string></string>
<string>10.1.0.1</string>
</value>
</member>
<member>
Expand Down
10 changes: 9 additions & 1 deletion system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cobblerclient

import (
"fmt"
"github.com/go-test/deep"
"testing"
"time"
Expand Down Expand Up @@ -263,6 +264,9 @@ func TestGetInterfaces(t *testing.T) {
if len(interfaces) < 1 {
t.Fatal("there should be at least one interface")
}
if interfaces["default"].IPAddress != "10.1.0.1" {
t.Fatal("incorrect IP address")
}
FailOnError(t, err)
}

Expand All @@ -279,7 +283,11 @@ func TestGetInterface(t *testing.T) {
nic, err := testsys.GetInterface("default")

// Assert
if len(deep.Equal(nic, Interface{})) > 0 {
expectedInterface := NewInterface()
expectedInterface.IPAddress = "10.1.0.1"
differences := deep.Equal(nic, expectedInterface)
if len(differences) > 0 {
fmt.Println(differences)
t.Fatal("interfaces non-equal")
}
FailOnError(t, err)
Expand Down
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ func convertIntBool(integer int) (bool, error) {
return false, errors.New("integer was neither 0 nor 1")
}

func convertToFloat(float interface{}) (float64, error) {
switch v := float.(type) {
case float64:
return v, nil
case float32:
return float64(v), nil
default:
return -1, errors.New("float could not be converted")
}
}

func convertToInt(integer interface{}) (int, error) {
switch integer.(type) {
case int8:
Expand Down

0 comments on commit 6ede036

Please sign in to comment.