From a3c96764e0c07de63432e26bfc466e0de88ef4d0 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Sun, 28 Jul 2019 23:31:59 -0400 Subject: [PATCH] Implement getting Node states via API --- client-api/api_lib/pvcapi.py | 72 ++++++++++++++++++++++++++++++++++++ client-api/pvc-api.py | 9 ++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/client-api/api_lib/pvcapi.py b/client-api/api_lib/pvcapi.py index 9a8d91be..91931816 100755 --- a/client-api/api_lib/pvcapi.py +++ b/client-api/api_lib/pvcapi.py @@ -52,6 +52,78 @@ def node_list(limit=None): pvc_common.stopZKConnection(zk_conn) return flask.jsonify(retdata), retcode +def node_daemon_state(node): + """ + Return the daemon state of node NODE. + """ + zk_conn = pvc_common.startZKConnection(config['coordinators']) + retflag, retdata = pvc_node.get_list(zk_conn, node, is_fuzzy=False) + if retflag: + if retdata: + retcode = 200 + retdata = { + 'name': node, + 'daemon_state': retdata[0]['daemon_state'] + } + else: + retcode = 404 + retdata = { + 'message': 'Node not found.' + } + else: + retcode = 400 + + pvc_common.stopZKConnection(zk_conn) + return flask.jsonify(retdata), retcode + +def node_coordinator_state(node): + """ + Return the coordinator state of node NODE. + """ + zk_conn = pvc_common.startZKConnection(config['coordinators']) + retflag, retdata = pvc_node.get_list(zk_conn, node, is_fuzzy=False) + if retflag: + if retdata: + retcode = 200 + retdata = { + 'name': node, + 'coordinator_state': retdata[0]['coordinator_state'] + } + else: + retcode = 404 + retdata = { + 'message': 'Node not found.' + } + else: + retcode = 400 + + pvc_common.stopZKConnection(zk_conn) + return flask.jsonify(retdata), retcode + +def node_domain_state(node): + """ + Return the domain state of node NODE. + """ + zk_conn = pvc_common.startZKConnection(config['coordinators']) + retflag, retdata = pvc_node.get_list(zk_conn, node, is_fuzzy=False) + if retflag: + if retdata: + retcode = 200 + retdata = { + 'name': node, + 'domain_state': retdata[0]['domain_state'] + } + else: + retcode = 404 + retdata = { + 'message': 'Node not found.' + } + else: + retcode = 400 + + pvc_common.stopZKConnection(zk_conn) + return flask.jsonify(retdata), retcode + def node_secondary(node): """ Take NODE out of primary router mode. diff --git a/client-api/pvc-api.py b/client-api/pvc-api.py index 4b8ddb7f..6efdc056 100755 --- a/client-api/pvc-api.py +++ b/client-api/pvc-api.py @@ -157,12 +157,17 @@ def api_node_element(node): # Same as specifying /node?limit=NODE return pvcapi.node_list(node) +@api.route('/api/v1/node//daemon-state', methods=['GET']) +@authenticator +def api_node_daemon_state(node): + if flask.request.method == 'GET': + return pvcapi.node_daemon_state(node) @api.route('/api/v1/node//coordinator-state', methods=['GET', 'POST']) @authenticator def api_node_coordinator_state(node): if flask.request.method == 'GET': - return "Test", 200 + return pvcapi.node_coordinator_state(node) if flask.request.method == 'POST': if not 'coordinator-state' in flask.request.values: @@ -178,7 +183,7 @@ def api_node_coordinator_state(node): @authenticator def api_node_domain_state(node): if flask.request.method == 'GET': - return "Test", 200 + return pvcapi.node_domain_state(node) if flask.request.method == 'POST': if not 'domain-state' in flask.request.values: