Commit 69f14cf4 authored by Vegard Vesterheim's avatar Vegard Vesterheim
Browse files

unfinished script for syncing rooms

parent b87d22a0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Synkronisering av KIND/nettinstallasjon -> NAV/room
"""
import requests
import argparse
import logging
import sys
import os
import collections.abc
# FIXME: why do I need this
sys.path.append(os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
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_TIMEOUT = 10.0
# FIXME
KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json?navn=oslo'
DEFAULT_ORGANIZATION = "uninett"
DEFAULT_LOCATION = "norge"
LOG = logging.getLogger("nettinst2room")
def main():
args = parse_args()
nettinst = get_kind_data()
api_url=args.api_url
api_token=args.api_token
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:")
for ni in nettinst:
try:
room_response = nav_api.get_room(ni['navn'])
room = room_response.body
LOG.warning("Fant rom: %s", room)
except ObjectNotFound:
## {
## "id": "vvwashere",
## "position": [
## "59.913054035463",
## "10.720285183010"
## ],
## "description": "Observatoriegata 1b, 0254 Oslo",
## "data": {
## "tlf": "23118900",
## "site_owner": "Norsk kulturråd",
## "site_owner_url": "https://kind.uninett.no/263"
## },
## "location": "norge"
## }
LOG.warning ("Creating rom for: %s", ni['navn'])
room = dict(id=ni['navn'], location=DEFAULT_LOCATION)
nav_api.post_room(room)
except Exception as e:
LOG.warning ("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': {
'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)
# 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():
if isinstance(v, collections.abc.Mapping):
d[k] = update(d.get(k, {}), v)
else:
if d[k] != v:
LOG.warning("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)
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(
"--api-url",
"-a",
default=os.environ.get("NAV_API_URL", NAV_API_URL),
help="URL to the NAV API. Default={}".format(NAV_API_URL),
)
parser.add_argument(
"--api-token",
"-t",
default=os.environ.get("NAV_API_TOKEN"),
help="NAV API Token",
)
parser.add_argument(
"--debug", "-d", action="store_true", help="enable debug logging"
)
args = parser.parse_args()
if not args.api_token:
parser.error(
"You must supply a NAV API token using either the "
"NAV_API_TOKEN environment variable, or the --api-token "
"option"
)
return args
if __name__ == "__main__":
main()
......@@ -80,6 +80,10 @@ class NAVAPI(object):
def post_room(self, room):
return self.api.room.create(body=room)
@_translate_exceptions
def patch_room(self, room_id, patch):
return self.api.room.partial_update(room_id, body=patch)
@functools.lru_cache(1)
@_translate_exceptions
def get_version(self):
......
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