Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom_filed KeyError traceback #518

Merged
merged 2 commits into from
Jan 23, 2023

Conversation

Kani999
Copy link
Contributor

@Kani999 Kani999 commented Nov 21, 2022

No description provided.

@Kani999
Copy link
Contributor Author

Kani999 commented Nov 21, 2022

return {
k: v if not isinstance(v, dict) else v["value"] for k, v in custom_dict.items()
}
return {k: v for k, v in custom_dict.items()}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really seems like a NOOP to me. Perhaps it would be better to eliminate calls to flatten_custom altogether?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srfwx weighed in on #457 back in June providing reasoning that flatten_custom is needed, and it looks like some discussion around this is the main thing preventing merge of an appropriate fix.

@srfwx
Copy link
Contributor

srfwx commented Nov 21, 2022

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

@Kani999
Copy link
Contributor Author

Kani999 commented Nov 23, 2022

Okay, so it should return something like.

Object: inventory-item
Custom Fileds:

  • test_json_custom_field {'x': 1, 'y': 2} - JSON
  • test_list_custom_field ['x', 'y'] - MultipleSelection
  • test_multiple_objects_custom_field [Dev1, Dev2] - MultipleObjects (Device)
  • abra_discovered False - Boolean
  • asset_numbers T5060 - String
  • inventarizace {'x': 1, 'y': 2} - JSON
    inventory_monitor_last_probe 2022-11-23 - Date
    inventory_monitor_probe - Probe - Object (from plugin)

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

  • This approach has a caveats:
    • If JSON custom field has an 'id' as a key, it will return its value.

@michael-salo
Copy link

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants