Skip to content
Snippets Groups Projects
Commit 46de1f35 authored by Robert Latta's avatar Robert Latta
Browse files

added IMS LG implementation with its own routes

parent 7caddcb7
No related branches found
No related tags found
No related merge requests found
......@@ -43,6 +43,13 @@ def create_app():
app.config['INVENTORY_PROVIDER_CONFIG'] = inventory_provider_config
# IMS based routes
from inventory_provider.routes import ims_lg
app.register_blueprint(ims_lg.routes, url_prefix='/ims-lg')
# end of IMS based routes
from inventory_provider.routes import default
app.register_blueprint(default.routes, url_prefix='/')
......
......@@ -68,6 +68,16 @@ CONFIG_SCHEMA = {
"api": {"type": "string"},
"username": {"type": "string"},
"password": {"type": "string"}
},
"otrs-export": {
"username": {"type": "string"},
"private-key": {"type": "string"},
"destination": {"type": "string"}
},
"ims": {
"api": {"type": "string"},
"username": {"type": "string"},
"password": {"type": "string"}
}
},
"oneOf": [
......@@ -77,7 +87,8 @@ CONFIG_SCHEMA = {
"ssh",
"redis",
"redis-databases",
"junosspace"]
"junosspace",
"ims"]
},
{
"required": [
......@@ -85,7 +96,8 @@ CONFIG_SCHEMA = {
"ssh",
"sentinel",
"redis-databases",
"junosspace"]
"junosspace",
"ims"]
}
],
"additionalProperties": False
......
import json
from flask import Blueprint, jsonify, Response
from inventory_provider.routes import common
routes = Blueprint("ims-lg-query-routes", __name__)
ACCESS_PUBLIC = 'public'
ACCESS_INTERNAL = 'all'
@routes.after_request
def after_request(resp):
return common.after_request(resp)
@routes.route("/routers/<string:access>", methods=['GET', 'POST'])
@common.require_accepts_json
def routers(access):
if access not in {ACCESS_INTERNAL, ACCESS_PUBLIC}:
return Response(
response='unknown access level',
status=404,
mimetype='text/html')
redis = common.get_current_redis()
def _visible(router):
if access == ACCESS_INTERNAL:
return True
return router['type'] == 'CORE'
def _routers():
i = 0
for k in redis.scan_iter(f'ims:lg:*'):
rtr = redis.get(k.decode('utf-8')).decode('utf-8')
rtr = json.loads(rtr)
i += 1
if _visible(rtr):
yield rtr
cache_key = f'classifier-cache:ims-lg:{access}'
result = redis.get(cache_key)
if result:
result = json.loads(result.decode('utf-8'))
else:
result = list(_routers())
# cache this data for the next call
redis.set(cache_key, json.dumps(result).encode('utf-8'))
if not result:
return Response(
response=f'no routers found for access level {access}',
status=404,
mimetype="text/html")
return jsonify(result)
......@@ -7,7 +7,7 @@ from lxml import etree
from inventory_provider import juniper
from inventory_provider.routes import common
from inventory_provider.tasks import worker
from inventory_provider.tasks import worker, ims_worker
from inventory_provider.tasks import common as worker_common
routes = Blueprint("inventory-data-testing-support-routes", __name__)
......@@ -19,6 +19,16 @@ def flushdb():
return Response('OK')
# IMS routes
@routes.route("update-lg-routers-ims", methods=['GET', 'POST'])
def update_lg_routers_ims():
ims_worker.update_lg_routers_ims.delay()
return Response('OK')
# End of IMS routes
@routes.route("update-interfaces-to-services", methods=['GET', 'POST'])
def update_interfaces_to_services():
worker.update_interfaces_to_services.delay()
......
import csv
import json
import logging
import subprocess
import tempfile
from datetime import datetime
from pathlib import Path
from inventory_provider.db import ims_data
from inventory_provider.db.ims import IMS
from inventory_provider import environment
from inventory_provider.tasks.app import app
from inventory_provider.tasks.common import get_next_redis
from inventory_provider.tasks.worker import InventoryTask
environment.setup_logging()
logger = logging.getLogger(__name__)
@app.task(base=InventoryTask, bind=True)
def update_lg_routers_ims(self):
logger.debug('>>> update_lg_routers_ims - MOVED')
r = get_next_redis(InventoryTask.config)
for k in r.scan_iter('ims:lg:*'):
r.delete(k)
c = InventoryTask.config["ims"]
ds = IMS(c['api'], c['username'], c['password'])
for router in ims_data.lookup_lg_routers(ds):
r.set(f'ims:lg:{router["equipment name"]}', json.dumps(router))
logger.debug('<<< update_lg_routers_ims')
......@@ -46,7 +46,12 @@ def data_config_filename():
"api": "bogus-url",
"username": "bogus-username",
"password": "bogus-password"
}
},
"ims": {
"api": "dummy",
"username": "dummy",
"password": "dummy"
}
}
f.write(json.dumps(config).encode('utf-8'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment