From 7866c00ea4a34d627edd7921977dde32d5136581 Mon Sep 17 00:00:00 2001 From: Robert Latta <robert.latta@geant.org> Date: Wed, 13 Apr 2022 16:04:09 +0000 Subject: [PATCH] added mic/sites route --- inventory_provider/__init__.py | 3 ++ inventory_provider/routes/mic.py | 62 ++++++++++++++++++++++++++++++++ test/test_mic_routes.py | 24 +++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 inventory_provider/routes/mic.py create mode 100644 test/test_mic_routes.py diff --git a/inventory_provider/__init__.py b/inventory_provider/__init__.py index b3ab8f8a..73970ad2 100644 --- a/inventory_provider/__init__.py +++ b/inventory_provider/__init__.py @@ -65,6 +65,9 @@ def create_app(): from inventory_provider.routes import default app.register_blueprint(default.routes, url_prefix='/') + from inventory_provider.routes import mic + app.register_blueprint(mic.routes, url_prefix='/mic') + from inventory_provider.routes import poller app.register_blueprint(poller.routes, url_prefix='/poller') diff --git a/inventory_provider/routes/mic.py b/inventory_provider/routes/mic.py new file mode 100644 index 00000000..be6e70ae --- /dev/null +++ b/inventory_provider/routes/mic.py @@ -0,0 +1,62 @@ +""" +Maintenance Impact Calculator support Endpoints +================================================= + +These endpoints are intended for use by the Maintenance Impact Calculator tool + +.. contents:: :local: + +/mic/sites +---------------------- + +.. autofunction:: inventory_provider.routes.mic.sites + + +""" +import json +import logging + +from flask import Blueprint, request, current_app, Response + +from inventory_provider.routes import common +from inventory_provider.routes.common import _ignore_cache_or_retrieve + +logger = logging.getLogger(__name__) +routes = Blueprint('mic-support-routes', __name__) + +SITES_LIST_SCHEMA = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "array", + "items": { + "type": "object", + "properties": { + "name": {"type": "string"}, + "abbreviation": {"type": "string"} + }, + "required": ["name", "abbreviation"], + "additionalProperties": False + } +} + + +@routes.route('/sites') +def get_sites(): + cache_key = 'classifier-cache:mic:sites' + r = common.get_current_redis() + result = _ignore_cache_or_retrieve(request, cache_key, r) + if not result: + def _fetch_sites(): + config = current_app.config['INVENTORY_PROVIDER_CONFIG'] + for doc in common.load_json_docs( + config_params=config, + key_pattern='ims:pop:*' + ): + yield { + 'name': doc['value']['name'], + 'abbreviation': doc['value']['abbreviation'], + } + sites = sorted(_fetch_sites(), key=lambda x: x['name']) + result = json.dumps(sites) + r.set(cache_key, result.encode('utf-8')) + + return Response(result, mimetype='application/json') diff --git a/test/test_mic_routes.py b/test/test_mic_routes.py new file mode 100644 index 00000000..06304468 --- /dev/null +++ b/test/test_mic_routes.py @@ -0,0 +1,24 @@ +import json + +import jsonschema + +from inventory_provider.routes.mic import SITES_LIST_SCHEMA + +DEFAULT_REQUEST_HEADERS = { + "Content-type": "application/json", + "Accept": ["application/json"] +} + + +def test_get_sites(client, mocked_redis): + rv = client.get( + '/mic/sites', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + response_data = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(response_data, SITES_LIST_SCHEMA) + + +# def test_get_sites_data(mocked_redis): +# -- GitLab