netbox-community / go-netbox

The official Go API client for Netbox IPAM and DCIM service.
Other
192 stars 141 forks source link

Wrong type definition for interface link_peers #151

Closed thunderstorm99 closed 6 months ago

thunderstorm99 commented 1 year ago

We're using go-netbox (github.com/netbox-community/go-netbox/v3 v3.4.5) in our go program. We are trying to list Interfaces using the function Dcim.DcimInterfacesList.

Our code to do this is:

func (nb netboxapi) getInterfacesFromDevice(deviceID int64) error {
    // create a string to be used on API call for the deviceID
    devID := fmt.Sprintf("%d", deviceID)

    // call API to get Interfaces for this device
    list, err := nb.Dcim.DcimInterfacesList(&dcim.DcimInterfacesListParams{
        Context:  context.Background(),
        DeviceID: &devID,
    }, nil)

    if err != nil {
        return fmt.Errorf("can't get Interfaces from Device %d, error is:\n\t-> %s", deviceID, err)
    }

    ... more code ...
    return nil
}

The error is the following:

json: cannot unmarshal object into Go struct field Interface.results.link_peers of type string

I looked at the API by querying our Netbox instance at api/dcim/interfaces/?device_id=2345 which returns the following (truncated to only show link_peers)

...
"link_peers": [
  {
    "id": 1234,
    "url": "https://abc.def.ghi/api/dcim/interfaces/1234/",
    "display": "ge-0/0/8",
    "device": {
      "id": 2345,
      "url": "https://netbox-test.ps-intern.de/api/dcim/devices/2345/",
      "display": "xxxxxxxx",
      "name": "xxxxxxxx"
    },  
    "name": "ge-0/0/8",
    "cable": 3456,
    "_occupied": true
  }
]
...

Looking at the code for go-netbox it looks like the expected return value is of type []*string https://github.com/netbox-community/go-netbox/blob/3237c9d5509bfd63491ad2635b6bd6f0ed5313bb/netbox/models/interface.go#L127

This does not seem to coincide with the actual json response coming from netbox (tested both on netbox 3.3.10, as well as 3.4.5). The source code for netbox also doesn't seem to use a []*string at that location https://github.com/netbox-community/netbox/blob/85f40bcbe073ab8025a1d8afe7ffed08c002d7b8/netbox/dcim/models/device_components.py#L169-L174

thunderstorm99 commented 1 year ago

I just found out, the same is true for Interface.results.connected_endpoints https://github.com/netbox-community/go-netbox/blob/3237c9d5509bfd63491ad2635b6bd6f0ed5313bb/netbox/models/interface.go#L58

cardboardpig commented 1 year ago

@thunderstorm99

Just ran into the same thing and it seems like an issue with the netbox api spec more so than the go client, given that the spec has the following for link_peers and connected_endpoints:

                "link_peers": {
                    "description": "\nReturn the appropriate serializer for the link termination model.\n",
                    "type": "array",
                    "items": {
                        "type": "string",
                        "x-nullable": true
                    },
                    "readOnly": true
                },
                "connected_endpoints": {
                    "description": "\nReturn the appropriate serializer for the type of connected object.\n",
                    "type": "array",
                    "items": {
                        "type": "string",
                        "x-nullable": true
                    },
                    "readOnly": true
                },

Probably can only really be fixed in the main netbox project if they are prepared to document the object types rather than what they have done.

thunderstorm99 commented 7 months ago

@cardboardpig can you link to the file from the netbox repo that contains said lines?

v0ctor commented 6 months ago

A new alpha version has been released with a different software to generate the library, so hopefully this bug has been resolved.

Please feel free to test it and to provide feedback.

olekgo commented 3 months ago

I've tested the latest version and although the error changed, the problem remains. Opened a new issue https://github.com/netbox-community/go-netbox/issues/170