Commit 9d10447e authored by Vegard Vesterheim's avatar Vegard Vesterheim
Browse files

For the most part, working

parent 69f14cf4
......@@ -8,8 +8,9 @@ import argparse
import logging
import sys
import os
from deepdiff import DeepDiff
import collections.abc
import copy
# FIXME: why do I need this
sys.path.append(os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
......@@ -24,7 +25,8 @@ NAV_API_URL = "http://localhost/api/1"
NAV_API_TIMEOUT = 10.0
# FIXME
KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json?navn=oslo'
#KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json?navn=tromso'
KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json'
DEFAULT_ORGANIZATION = "uninett"
DEFAULT_LOCATION = "norge"
......@@ -39,12 +41,12 @@ def main():
nav_api = NAVAPI(url=api_url, auth_token=api_token, timeout=NAV_API_TIMEOUT)
LOG.setLevel(logging.DEBUG if args.debug else logging.INFO)
LOG.warning("Starting up:")
LOG.addHandler(logging.StreamHandler())
for ni in nettinst:
try:
room_response = nav_api.get_room(ni['navn'])
room = room_response.body
LOG.warning("Fant rom: %s", room)
LOG.debug("Fant rom: %s", room)
except ObjectNotFound:
## {
......@@ -62,43 +64,66 @@ def main():
## "location": "norge"
## }
LOG.warning ("Creating rom for: %s", ni['navn'])
LOG.debug ("Creating rom for: %s", ni['navn'])
room = dict(id=ni['navn'], location=DEFAULT_LOCATION)
nav_api.post_room(room)
try:
LOG.debug("Creating room: %s", ni['navn'])
nav_api.post_room(room)
except Exception as e:
LOG.debug ("Error in creating room for %s: ", ni['navn'])
continue
except Exception as e:
LOG.warning ("Error: ", e)
LOG.debug ("Error: ", e)
# Update room attributes
patch = update (room, {
'location': DEFAULT_LOCATION,
'position': [str(round(ni['lat'], 12)), str(round(ni['lon'], 12))],
# FIXME, add more into description
# description = ni['termineringsadresse']+"\n"+ni['rombeskrivelse'],
'description': ni['termineringsadresse'],
'data': {
patch = copy.deepcopy(room)
org = {}
org['location'] = DEFAULT_LOCATION
# FIXME, add more into description
desc_fields = ['beskrivelse','termineringsadresse']
description = '. '.join([ni[p] for p in desc_fields if p in ni and ni[p]])
if not description:
description = 'No description found'
org['description'] = description
if 'org' in ni:
org['data'] = {
'site_owner': ni['org'],
'site_owner_url': 'https://kind.uninett.no/'+ str(int(ni['org_id']))
}
})
nav_api.patch_room(room_id=ni['navn'], patch=patch)
if 'lat' in ni and 'lon' in ni:
org['position'] = [
'%.12f' % round(ni['lat'], 12),
'%.12f' % round(ni['lon'], 12)
]
merge_dict (patch, org)
if DeepDiff (patch, room, ignore_order=True):
LOG.debug ("Update needed")
LOG.debug (patch)
nav_api.patch_room(room_id=ni['navn'], patch=patch)
else:
LOG.debug ("No update needed for room: %s", ni['navn'])
# https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
def update(d, u):
patch = {}
for k, v in u.items():
def merge_dict(d, updates):
"""Merge entries from updates into d"""
for k, v in updates.items():
if isinstance(v, collections.abc.Mapping):
d[k] = update(d.get(k, {}), v)
d[k] = merge_dict(d.get(k, {}), v)
else:
if d[k] != v:
LOG.warning("Updating %s from %s to %s", k, d[k], v)
if (not k in d):
LOG.debug("Setting %s to %s", k, v)
d[k] = v
elif d[k] != v:
LOG.debug("Updating %s from %s to %s", k, d[k], v)
d[k] = v
return d
def get_kind_data():
LOG.warning("Henter data fra %s", KIND_SERVICE_URL)
LOG.debug("Henter data fra %s", KIND_SERVICE_URL)
return requests.get(KIND_SERVICE_URL).json()
def parse_args():
......
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