netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
543 stars 167 forks source link

Fix custom_filed KeyError traceback #518

Closed Kani999 closed 1 year ago

Kani999 commented 1 year ago
srfwx commented 1 year ago

We need it for Objects and Multiple Objects custom fields (although it needs to be adapted too)

Kani999 commented 1 year ago

Okay, so it should return something like.

Object: inventory-item Custom Fileds:

Response

{
   "id":5023,
   "url":"https://NETBOX-INSTANCE/api/dcim/inventory-items/5023/",
   "display":"0/6/1",
   "device":61,
   "parent":"None",
   "name":"0/6/1",
   "label":"",
   "role":5,
   "manufacturer":"None",
   "part_id":"",
   "asset_tag":"None",
   "discovered":true,
   "description":"",
   "component_type":"None",
   "component_id":"None",
   "component":"None",
   "tags":[

   ],
   "custom_fields":{
      "test_json_custom_field":{
         "x":1,
         "y":2
      },
      "test_list_custom_field":[
         "x",
         "y"
      ],
      "test_multiple_objects_custom_field":[
         {
            "id":1,
            "url":"https://NETBOX-INSTANCE/api/dcim/devices/1/",
            "display":"DEV1",
            "name":"DEV1"
         },
         {
            "id":119,
            "url":"https://NETBOX-INSTANCE/api/dcim/devices/119/",
            "display":"DEV2",
            "name":"DEV2"
         },
      ],
      "abra_discovered":false,
      "asset_numbers":"T5060",
      "inventarizace":{
         "x":1,
         "y":2
      },
      "inventory_monitor_active":true,
      "inventory_monitor_last_probe":"2022-11-23",
      "inventory_monitor_probe":{
         "id":1648,
         "url":"https://NETBOX-INSTANCE/api/plugins/inventory-monitor/probes/1648/",
         "display":"Object",
         "name":"0/6/1",
         "time":"2022-11-23T01:16:47.492262Z"
      },
      "inventory_monitor_probed":true
   },
   "created":"2022-09-06T10:03:36.248425Z",
   "last_updated":"2022-11-23T08:46:57.907296Z",
   "_depth":0
}

Or it just should return the ids

{
   "id":5023,
   "url":"https://NETBOX_URL/api/dcim/inventory-items/5023/",
   "display":"0/6/1",
   "device":61,
   "parent":"None",
   "tags":[

   ],
   "custom_fields":{
      "test_json_custom_field":{
         "x":1,
         "y":2
      },
      "test_list_custom_field":[
         "x",
         "y"
      ],
      "test_multiple_objects_custom_field":[
         1,
         119,
         252
      ],
      "abra_discovered":false,
      "asset_numbers":"T5060",
      "inventarizace":{
         "x":1,
         "y":2
      },
      "inventory_monitor_active":true,
      "inventory_monitor_last_probe":"2022-11-23",
      "inventory_monitor_probe":1648,
      "inventory_monitor_probed":true
   },
   "created":"2022-09-06T10:03:36.248425Z",
   "last_updated":"2022-11-23T08:46:57.907296Z",
   "_depth":0
}

Uploaded version

michael-salo commented 1 year ago

Is there any chance this pull request can be reviewed and merged into an upstream version of pynetbox? We're hitting the same bug when trying to create netbox_prefix via ansible with custom_fields.

arthanson commented 1 year ago

Will take a look into it, probably Wed or Thursday before I can get to it.