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

added mic/sites route

parent b5a8fd86
No related branches found
No related tags found
No related merge requests found
......@@ -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')
......
"""
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')
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):
#
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment