Commit b648896c authored by Morten Brekkevold's avatar Morten Brekkevold
Browse files

Add Kind URL as custom data to each Netbox

Because:
- Backlinks to Kind are useful!
parent a1937170
......@@ -28,6 +28,8 @@ NAV_BOX_URL = "https://uninav.uninett.no/ipdevinfo/{sysname}/"
NAV_API_URL = "http://localhost/api/1"
NAV_API_TIMEOUT = 10.0
NAV_KIND_URL_ATTRIBUTE = "kind_url"
DEFAULT_SNMP_COMMUNITY = "public"
DEFAULT_SNMP_VERSION = "2"
DEFAULT_MANAGEMENT_PROFILE = {
......@@ -501,6 +503,7 @@ def insert_one_into_nav(nav_api, kind_device):
roomid=kind_device.room,
organizationid=DEFAULT_ORGANIZATION,
categoryid=CATEGORY_MAP.get(kind_device.type, DEFAULT_CATEGORY),
data={NAV_KIND_URL_ATTRIBUTE: kind_device.get_absolute_url()},
)
nav_version = nav_api.get_version()
......@@ -579,9 +582,12 @@ def update_device_in_nav_if_changed(nav_api, kind_device):
"""
if not is_device_changed(kind_device):
return
data = kind_device.netbox["data"].copy()
data[NAV_KIND_URL_ATTRIBUTE] = kind_device.get_absolute_url()
patch = dict(
roomid=kind_device.room,
categoryid=CATEGORY_MAP.get(kind_device.type, DEFAULT_CATEGORY),
data=data,
)
try:
......@@ -596,18 +602,23 @@ def update_device_in_nav_if_changed(nav_api, kind_device):
def is_device_changed(kind_device):
assert kind_device.netbox
expected_category = CATEGORY_MAP.get(kind_device.type, DEFAULT_CATEGORY)
expected_url = kind_device.get_absolute_url()
diff = (
kind_device.room != kind_device.netbox["room"]["id"]
or kind_device.netbox["category"]["id"] != expected_category
or kind_device.netbox["data"].get(NAV_KIND_URL_ATTRIBUTE) != expected_url
)
if diff:
LOG.info(
"%s er forandret i Kind: room %r -> %r / category %r -> %r",
"%s er forandret i Kind: room %r -> %r / category %r -> %r / "
"kind_url %r -> %r",
kind_device.name,
kind_device.netbox["room"]["id"],
kind_device.room,
kind_device.netbox["category"]["id"],
expected_category,
kind_device.netbox["data"].get(NAV_KIND_URL_ATTRIBUTE),
expected_url,
)
return diff
......
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