netbox-sync icon indicating copy to clipboard operation
netbox-sync copied to clipboard

Update for netbox ver 4

Open Sivolen opened this issue 9 months ago • 9 comments

Hi!

Tell me, will there be an update for netbox version 4?

Sivolen avatar May 07 '24 04:05 Sivolen

The largest issue seems to be the change from ContentType to ObjectType. We were able to get it working by replacing all instances of content_types in the files in module/ and subdirectories with object_types, and replacing line 1192 in module/netbox/object_classes.py with: for content_type in data.get("object_types", []):

Whether or not that was all required or if it fixes all problems, I don't know yet, but that got it to run for us.

jhammond-git avatar May 07 '24 15:05 jhammond-git

Hey @jhammond-git,

Thank you for the hint. Currently implementing the support for 4.0, hope I can finish it before I have to leave for holidays.

bb-Ricardo avatar May 07 '24 22:05 bb-Ricardo

Hi,

I just pushed a new commit to "development" would you be able to test and see if it's working with NetBox 3.7 and NetBox 4.0?

Thank you

bb-Ricardo avatar May 08 '24 06:05 bb-Ricardo

Thank you, I can check on Monday

Sivolen avatar May 08 '24 12:05 Sivolen

Still need to change line 1192 as noted above:

for content_type in data.get("object_types", []):

jhammond-git avatar May 08 '24 15:05 jhammond-git

Traceback (most recent call last):
  File "/opt/netbox-sync/./netbox-sync.py", line 146, in <module>
    main()
  File "/opt/netbox-sync/./netbox-sync.py", line 98, in main
    nb_handler.query_current_data(source.dependent_netbox_objects)
  File "/opt/netbox-sync/module/netbox/connection.py", line 521, in query_current_data
    self.inventory.add_object(nb_object_class, data=object_data, read_from_netbox=True)
  File "/opt/netbox-sync/module/netbox/inventory.py", line 176, in add_object
    new_object = object_type(data, read_from_netbox=read_from_netbox, inventory=self, source=source)
  File "/opt/netbox-sync/module/netbox/object_classes.py", line 1178, in __init__
    super().__init__(*args, **kwargs)
  File "/opt/netbox-sync/module/netbox/object_classes.py", line 296, in __init__
    self.update(data=data, read_from_netbox=read_from_netbox, source=source)
  File "/opt/netbox-sync/module/netbox/object_classes.py", line 1194, in update
    for object_type in data.get("object_types"):
TypeError: 'NoneType' object is not iterable

jhammond-git avatar May 08 '24 15:05 jhammond-git

Awesome that 4.0 compatibility is already on it's way! When I saw the changelogs I was exited and anxious at the same time. (Jay new UI with working Markdown hyperlinks & inline images, but SO many breaking changes).

I should be able to spin up a testing environment for this next week, and I'll keep my eyes on this issue in case further testing is required.

yaiqsa avatar May 08 '24 16:05 yaiqsa

Still need to change line 1192 as noted above:

for content_type in data.get("object_types", []):

Apparently I was wrong. Whatever happened the first run after pulling the new code didn't happen the second, so please ignore for now.

jhammond-git avatar May 08 '24 17:05 jhammond-git

I have the same error


2024-05-13 11:33:56,894 - INFO: Querying necessary objects from NetBox. This might take a while.
Traceback (most recent call last):
  File "/opt/scripts/netbox-sync/./netbox-sync.py", line 146, in <module>
    main()
  File "/opt/scripts/netbox-sync/./netbox-sync.py", line 98, in main
    nb_handler.query_current_data(source.dependent_netbox_objects)
  File "/opt/scripts/netbox-sync/module/netbox/connection.py", line 521, in query_current_data
    self.inventory.add_object(nb_object_class, data=object_data, read_from_netbox=True)
  File "/opt/scripts/netbox-sync/module/netbox/inventory.py", line 176, in add_object
    new_object = object_type(data, read_from_netbox=read_from_netbox, inventory=self, source=source)
  File "/opt/scripts/netbox-sync/module/netbox/object_classes.py", line 1178, in __init__
    super().__init__(*args, **kwargs)
  File "/opt/scripts/netbox-sync/module/netbox/object_classes.py", line 296, in __init__
    self.update(data=data, read_from_netbox=read_from_netbox, source=source)
  File "/opt/scripts/netbox-sync/module/netbox/object_classes.py", line 1194, in update
    for object_type in data.get("object_types"):
TypeError: 'NoneType' object is not iterable

Sivolen avatar May 13 '24 00:05 Sivolen

For me it doesn't work with Netbox v3.7 - same error as @Sivolen. It works with Netbox v4.0.1 but before that I needed to delete the cache directory.

sulik76 avatar May 13 '24 05:05 sulik76

Hi @sulik76 and @Sivolen,

did you try to clean the cache folder first?

bb-Ricardo avatar May 13 '24 09:05 bb-Ricardo

Hi @bb-Ricardo,

Before I ran the script, I deleted the entire cache directory in both Netbox versions (3.7.6 and 4.0.1). For version 4.0.1 the script worked fine, but for version 3.7.6 there is this error:

2024-05-13 12:16:26,721 - DEBUG: Requesting all custom fields from NetBox {'_body_position': None, '_cookies': <RequestsCookieJar[]>, 'body': None, 'headers': {'User-Agent': 'netbox-sync/1.6.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive', 'Authorization': 'Token xxxxxxxxxxxxx', 'Content-Type': 'application/json'}, 'hooks': {'response': []}, 'method': 'GET', 'url': 'https://localhost:443/api/extras/custom-fields/?limit=200&exclude=config_context'} send: b'GET /api/extras/custom-fields/?limit=200&exclude=config_context HTTP/1.1\r\nHost: localhost\r\nUser-Agent: netbox-sync/1.6.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: /\r\nConnection: keep-alive\r\nAuthorization: Token xxxxxxxxxxxxx\r\nContent-Type: application/json\r\n\r\n' reply: 'HTTP/1.1 200 OK\r\n' header: Server: nginx/1.22.1 header: Date: Mon, 13 May 2024 10:16:26 GMT header: Content-Type: application/json header: Content-Length: 2276 header: Connection: keep-alive header: Vary: HX-Request, Cookie, origin header: Allow: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS header: X-Request-ID: 22a497c1-b7af-48a1-b985-168136518868 header: API-Version: 3.7 header: X-Content-Type-Options: nosniff header: Referrer-Policy: same-origin header: Cross-Origin-Opener-Policy: same-origin header: X-Frame-Options: SAMEORIGIN 2024-05-13 12:16:26,766 - DEBUG: https://localhost:443 "GET /api/extras/custom-fields/?limit=200&exclude=config_context HTTP/1.1" 200 2276 2024-05-13 12:16:26,766 - DEBUG2: Received HTTP Status 200. 2024-05-13 12:16:26,767 - DEBUG: Response Body: {'count': 3, 'next': None, 'previous': None, 'results': [{'choice_set': None, 'content_types': ['dcim.device'], 'created': '2023-01-27T15:30:12.579760+01:00', 'data_type': 'string', 'default': None, 'description': "vCenter 'vcenterOZI1' reported Host CPU cores", 'display': 'Physical CPU Cores', 'filter_logic': {'label': 'Loose', 'value': 'loose'}, 'group_name': '', 'id': 1, 'is_cloneable': False, 'label': 'Physical CPU Cores', 'last_updated': '2023-01-27T15:30:12.579787+01:00', 'name': 'vcsa_host_cpu_cores', 'object_type': None, 'required': False, 'search_weight': 1000, 'type': {'label': 'Text', 'value': 'text'}, 'ui_editable': {'label': 'Yes', 'value': 'yes'}, 'ui_visible': {'label': 'Always', 'value': 'always'}, 'url': 'https://localhost/api/extras/custom-fields/1/', 'validation_maximum': None, 'validation_minimum': None, 'validation_regex': '', 'weight': 100}, {'choice_set': None, 'content_types': ['dcim.device'], 'created': '2023-01-27T15:30:12.712891+01:00', 'data_type': 'string', 'default': None, 'description': "vCenter 'vcenterOZI1' reported Memory", 'display': 'Memory', 'filter_logic': {'label': 'Loose', 'value': 'loose'}, 'group_name': '', 'id': 2, 'is_cloneable': False, 'label': 'Memory', 'last_updated': '2023-01-27T15:30:12.712912+01:00', 'name': 'vcsa_host_memory', 'object_type': None, 'required': False, 'search_weight': 1000, 'type': {'label': 'Text', 'value': 'text'}, 'ui_editable': {'label': 'Yes', 'value': 'yes'}, 'ui_visible': {'label': 'Always', 'value': 'always'}, 'url': 'https://localhost/api/extras/custom-fields/2/', 'validation_maximum': None, 'validation_minimum': None, 'validation_regex': '', 'weight': 100}, {'choice_set': None, 'content_types': ['dcim.device'], 'created': '2023-02-02T09:48:50.475600+01:00', 'data_type': 'string', 'default': None, 'description': 'Support', 'display': 'Support', 'filter_logic': {'label': 'Loose', 'value': 'loose'}, 'group_name': 'Support', 'id': 3, 'is_cloneable': False, 'label': 'Support', 'last_updated': '2023-02-02T10:43:19.424703+01:00', 'name': 'Support', 'object_type': None, 'required': False, 'search_weight': 1000, 'type': {'label': 'Text', 'value': 'text'}, 'ui_editable': {'label': 'Yes', 'value': 'yes'}, 'ui_visible': {'label': 'Always', 'value': 'always'}, 'url': 'https://localhost/api/extras/custom-fields/3/', 'validation_maximum': None, 'validation_minimum': None, 'validation_regex': '', 'weight': 100}]} 2024-05-13 12:16:26,772 - DEBUG: Successfully cached 3 objects. 2024-05-13 12:16:26,772 - DEBUG: Processing 3 returned custom fields Traceback (most recent call last): File "/opt/netbox-sync/netbox-sync.py", line 146, in main() File "/opt/netbox-sync/netbox-sync.py", line 98, in main nb_handler.query_current_data(source.dependent_netbox_objects) File "/opt/netbox-sync/module/netbox/connection.py", line 521, in query_current_data self.inventory.add_object(nb_object_class, data=object_data, read_from_netbox=True) File "/opt/netbox-sync/module/netbox/inventory.py", line 176, in add_object new_object = object_type(data, read_from_netbox=read_from_netbox, inventory=self, source=source) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox-sync/module/netbox/object_classes.py", line 1178, in init super().init(*args, **kwargs) File "/opt/netbox-sync/module/netbox/object_classes.py", line 296, in init self.update(data=data, read_from_netbox=read_from_netbox, source=source) File "/opt/netbox-sync/module/netbox/object_classes.py", line 1194, in update for object_type in data.get("object_types"): TypeError: 'NoneType' object is not iterable

sulik76 avatar May 13 '24 10:05 sulik76

Hi,

I pushed another fix to handle the renamed field correctly. Can you try it out if it works now. Also deleting cache is important.

thank you

bb-Ricardo avatar May 13 '24 14:05 bb-Ricardo

Hi,

I pushed another fix to handle the renamed field correctly. Can you try it out if it works now. Also deleting cache is important.

thank you

after clearing the cache the script works correctly, after the last commit the script works just as correctly

Sivolen avatar May 13 '24 23:05 Sivolen

Hi,

Netbox 3.7.6 After pulling the script with the latest changes, it works correctly.

Upgrade Netbox from 3.7.6 to 4.0.1 The script works correctly after clearing the cache directory.

sulik76 avatar May 14 '24 03:05 sulik76

Awesome, thank you for testing and confirming.

bb-Ricardo avatar May 14 '24 04:05 bb-Ricardo

Just released https://github.com/bb-Ricardo/netbox-sync/releases/tag/v1.6.1

bb-Ricardo avatar May 14 '24 06:05 bb-Ricardo