From 48cfe8619101e3b09037efbc68148888bf980c30 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Mon, 11 Feb 2019 16:18:47 +0100
Subject: [PATCH] moved old router list apis to /testing/*

---
 inventory_provider/routes/testing.py | 34 ++++++++++++++++++++-
 test/test_testing_routes.py          | 44 ++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/inventory_provider/routes/testing.py b/inventory_provider/routes/testing.py
index 16f1d670..1770be9d 100644
--- a/inventory_provider/routes/testing.py
+++ b/inventory_provider/routes/testing.py
@@ -1,4 +1,6 @@
-from flask import Blueprint, Response
+import json
+
+from flask import Blueprint, Response, current_app, jsonify
 from inventory_provider.routes import common
 
 routes = Blueprint("inventory-data-testing-support-routes", __name__)
@@ -8,3 +10,33 @@ routes = Blueprint("inventory-data-testing-support-routes", __name__)
 def flushdb():
     common.get_redis().flushdb()
     return Response('OK')
+
+
+@routes.route("/infinera-dna-addresses", methods=['GET', 'POST'])
+@common.require_accepts_json
+def infinera_addresses():
+    infinera_config = current_app.config[
+        "INVENTORY_PROVIDER_CONFIG"]["infinera-dna"]
+    return jsonify([dna['address'] for dna in infinera_config])
+
+
+@routes.route("/coriant-tnms-addresses", methods=['GET', 'POST'])
+@common.require_accepts_json
+def coriant_addresses():
+    coriant_config = current_app.config[
+        "INVENTORY_PROVIDER_CONFIG"]["coriant-tnms"]
+    return jsonify([tnms['address'] for tnms in coriant_config])
+
+
+@routes.route("/juniper-server-addresses", methods=['GET', 'POST'])
+@common.require_accepts_json
+def juniper_addresses():
+    # TODO: this route (and corant, infinera routes) can be removed
+    r = common.get_redis()
+    routers = []
+    for k in r.keys('junosspace:*'):
+        info = r.get(k.decode('utf-8'))
+        assert info  # sanity: value shouldn't be empty
+        info = json.loads(info.decode('utf-8'))
+        routers.append(info['address'])
+    return jsonify(routers)
diff --git a/test/test_testing_routes.py b/test/test_testing_routes.py
index 44ae19a0..c3fa0e53 100644
--- a/test/test_testing_routes.py
+++ b/test/test_testing_routes.py
@@ -1,4 +1,48 @@
+import json
+import jsonschema
+
+DEFAULT_REQUEST_HEADERS = {
+    "Content-type": "application/json",
+    "Accept": ["application/json"]
+}
+
+ROUTER_LIST_SCHEMA = {
+    "$schema": "http://json-schema.org/draft-07/schema#",
+    "type": "array",
+    "items": {"type": "string"}
+}
+
 
 def test_flushdb(client):
     rv = client.post("/testing/flushdb")
     assert rv.status_code == 200
+
+
+def test_infinera_addresses(client):
+    rv = client.post(
+        "/testing/infinera-dna-addresses",
+        headers=DEFAULT_REQUEST_HEADERS)
+    assert rv.status_code == 200
+    jsonschema.validate(
+        json.loads(rv.data.decode("utf-8")),
+        ROUTER_LIST_SCHEMA)
+
+
+def test_coriant_addresses(client):
+    rv = client.post(
+        "/testing/coriant-tnms-addresses",
+        headers=DEFAULT_REQUEST_HEADERS)
+    assert rv.status_code == 200
+    jsonschema.validate(
+        json.loads(rv.data.decode("utf-8")),
+        ROUTER_LIST_SCHEMA)
+
+
+def test_juniper_addresses(mocker, client):
+    rv = client.post(
+        "/testing/juniper-server-addresses",
+        headers=DEFAULT_REQUEST_HEADERS)
+    assert rv.status_code == 200
+    response_data = json.loads(rv.data.decode('utf-8'))
+    jsonschema.validate(response_data, ROUTER_LIST_SCHEMA)
+    assert len(response_data) > 0  # test data is not empty
-- 
GitLab