Skip to content
Snippets Groups Projects
Commit 84d3395b authored by Erik Reid's avatar Erik Reid
Browse files

added msr route

parent 1e8024e9
No related branches found
No related tags found
No related merge requests found
......@@ -61,6 +61,9 @@ def create_app():
from inventory_provider.routes import lg
app.register_blueprint(lg.routes, url_prefix='/lg')
from inventory_provider.routes import msr
app.register_blueprint(msr.routes, url_prefix='/msr')
if app.config.get('ENABLE_TESTING_ROUTES', False):
from inventory_provider.routes import testing
app.register_blueprint(testing.routes, url_prefix='/testing')
......
import json
from flask import Blueprint, jsonify, Response
from inventory_provider.routes import common
routes = Blueprint("msr-query-routes", __name__)
@routes.after_request
def after_request(resp):
return common.after_request(resp)
@routes.route("/access-services", methods=['GET', 'POST'])
@common.require_accepts_json
def access_services():
redis = common.get_current_redis()
def _services():
for k in redis.scan_iter('opsdb:access_services:*'):
service = redis.get(k.decode('utf-8')).decode('utf-8')
yield json.loads(service)
cache_key = f'classifier-cache:msr:access-services'
result = redis.get(cache_key)
if result:
result = json.loads(result.decode('utf-8'))
else:
result = list(_services())
# 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 access services found',
status=404,
mimetype="text/html")
return jsonify(result)
import json
import jsonschema
import pytest
DEFAULT_REQUEST_HEADERS = {
"Content-type": "application/json",
"Accept": ["application/json"]
}
ACCESS_SERVICES_LIST_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"service": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"equipment": {"type": "string"},
"pop_name": {"type": "string"},
"other_end_equipment": {"type": "string"},
"other_end_pop_name": {"type": "string"},
"speed_value": {"type": "integer"},
"speed_unit": {"type": "string"}
},
"required": [
"id", "name",
"pop_name", "equipment",
"other_end_pop_name", "other_end_equipment",
"speed_value", "speed_unit"
],
"additionalProperties": False
}
},
"type": "array",
"items": {"$ref": "#/definitions/service"}
}
def test_access_services(client):
rv = client.get(
'/msr/access-services',
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, ACCESS_SERVICES_LIST_SCHEMA)
assert response_data # test data is non-empty
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