Commit 5ca2a4ad authored by Vegard Vesterheim's avatar Vegard Vesterheim
Browse files

misc fixups, add --room CLI argument

parent 9d10447e
......@@ -11,6 +11,7 @@ import os
from deepdiff import DeepDiff
import collections.abc
import copy
import urllib.parse
# FIXME: why do I need this
sys.path.append(os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
......@@ -19,14 +20,14 @@ from kindnavsync.errors import retry_on_timeout
from kindnavsync.navapi import NAVAPI, ObjectNotFound, ClientError
# FIXME
# NAV_API_URL = "https://uninavn.uninett.no/api/1"
NAV_API_URL = "http://localhost/api/1"
# NAV_API_URL = "http://localhost/api/1"
NAV_API_URL = "https://uninavn.uninett.no/api/1"
NAV_API_TIMEOUT = 10.0
# FIXME
#KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json?navn=tromso'
KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json'
# KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json'
KIND_SERVICE_URL = 'https://kind.uninett.no/api/nettinstallasjoner.json'
DEFAULT_ORGANIZATION = "uninett"
DEFAULT_LOCATION = "norge"
......@@ -35,7 +36,8 @@ LOG = logging.getLogger("nettinst2room")
def main():
args = parse_args()
nettinst = get_kind_data()
room = args.room
nettinst = get_kind_data(args.room)
api_url=args.api_url
api_token=args.api_token
......@@ -64,23 +66,28 @@ def main():
## "location": "norge"
## }
LOG.debug ("Creating rom for: %s", ni['navn'])
LOG.debug ("Creating room for: %s", ni['navn'])
room = dict(id=ni['navn'], location=DEFAULT_LOCATION)
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'])
LOG.error ("Error in creating room for %s: ", ni['navn'])
LOG.error ("Error is : %s", e)
continue
except Exception as e:
LOG.debug ("Error: ", e)
# Update room attributes
LOG.error ("Error in fetching room for %s: ", ni['navn'])
LOG.error ("Error: %s", e)
continue
# Creating a deep copy can make the patch bigger than needed...
patch = copy.deepcopy(room)
org = {}
# Update room attributes
org['location'] = DEFAULT_LOCATION
# FIXME, add more into description
# FIXME, maybe 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:
......@@ -96,7 +103,7 @@ def main():
'%.12f' % round(ni['lat'], 12),
'%.12f' % round(ni['lon'], 12)
]
# Merge data from KIND into existing entry
merge_dict (patch, org)
if DeepDiff (patch, room, ignore_order=True):
LOG.debug ("Update needed")
......@@ -120,16 +127,23 @@ def merge_dict(d, updates):
d[k] = v
return d
def get_kind_data(room):
url = KIND_SERVICE_URL
if room:
url = KIND_SERVICE_URL+'?navn='+urllib.parse.quote(room)
LOG.debug("Fetching data from: %s", url)
return requests.get(url).json()
def get_kind_data():
LOG.debug("Henter data fra %s", KIND_SERVICE_URL)
return requests.get(KIND_SERVICE_URL).json()
def parse_args():
parser = argparse.ArgumentParser(
description="Synchronizes rooms in NAV with those from Kind"
)
parser.add_argument(
"--room",
"-r",
help="Name of single room to sync. (All rooms otherwise)",
)
parser.add_argument(
"--api-url",
"-a",
......@@ -156,5 +170,6 @@ def parse_args():
return args
if __name__ == "__main__":
main()
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