Commit 90abceac authored by Morten Brekkevold's avatar Morten Brekkevold

update nav on device changes.

if devices that exist both in Kind and NAV have differing rooms or equipment
types, update the NAV devices to be in sync with Kind.
parent 8124bd52
......@@ -262,7 +262,9 @@ def update_nav(kind_devices, api_url, api_token, dry_run=False):
if not dry_run:
remove_from_nav(nav_api, unmatched_netboxes)
insert_into_nav(nav_api, unmatched_kind_devices)
# TODO Update existing devices that have changed!
update_existing_in_nav(nav_api,
[device for device in kind_devices.values()
if device.netbox])
def insert_into_nav(nav_api, kind_devices):
......@@ -302,6 +304,50 @@ def insert_one_into_nav(nav_api, kind_device):
nav_api.post_netbox(netbox)
def update_existing_in_nav(nav_api, kind_devices):
LOG.debug("Sammenligner eksisterende utstyr i NAV med utstyr fra Kind")
for device in kind_devices:
update_device_in_nav_if_changed(nav_api, device)
def update_device_in_nav_if_changed(nav_api, kind_device):
"""Updates kind_device in NAV if its details have changed.
Specifically, if the room or category has changed in Kind, NAV is updated.
:type nav_api: kindnavsync.navapi.NAVAPI
:type kind_device: KindDevice
"""
if not is_device_changed(kind_device):
return
patch = dict(
roomid=kind_device.room,
categoryid=CATEGORY_MAP.get(kind_device.type, DEFAULT_CATEGORY),
)
try:
nav_api.get_room(kind_device.room)
except ObjectNotFound:
room = dict(id=kind_device.room, location=DEFAULT_LOCATION)
nav_api.post_room(room)
nav_api.patch_netbox(netbox_id=kind_device.netbox.id, patch=patch)
def is_device_changed(kind_device):
assert kind_device.netbox
expected_category = CATEGORY_MAP.get(kind_device.type, DEFAULT_CATEGORY)
diff = (kind_device.room != kind_device.netbox['room']['id'] or
kind_device.netbox['category']['id'] != expected_category)
if diff:
LOG.info("%s er forandret i Kind: room %r -> %r / category %r -> %r",
kind_device.name,
kind_device.netbox['room']['id'], kind_device.room,
kind_device.netbox['category']['id'], expected_category)
return diff
def remove_from_nav(nav_api, deleteable_boxes):
if not deleteable_boxes:
return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment