Implement getting Node states via API

This commit is contained in:
Joshua Boniface 2019-07-28 23:31:59 -04:00
parent 47c72d9b68
commit a3c96764e0
2 changed files with 79 additions and 2 deletions

View File

@ -52,6 +52,78 @@ def node_list(limit=None):
pvc_common.stopZKConnection(zk_conn) pvc_common.stopZKConnection(zk_conn)
return flask.jsonify(retdata), retcode 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): def node_secondary(node):
""" """
Take NODE out of primary router mode. Take NODE out of primary router mode.

View File

@ -157,12 +157,17 @@ def api_node_element(node):
# Same as specifying /node?limit=NODE # Same as specifying /node?limit=NODE
return pvcapi.node_list(node) return pvcapi.node_list(node)
@api.route('/api/v1/node/<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/<node>/coordinator-state', methods=['GET', 'POST']) @api.route('/api/v1/node/<node>/coordinator-state', methods=['GET', 'POST'])
@authenticator @authenticator
def api_node_coordinator_state(node): def api_node_coordinator_state(node):
if flask.request.method == 'GET': if flask.request.method == 'GET':
return "Test", 200 return pvcapi.node_coordinator_state(node)
if flask.request.method == 'POST': if flask.request.method == 'POST':
if not 'coordinator-state' in flask.request.values: if not 'coordinator-state' in flask.request.values:
@ -178,7 +183,7 @@ def api_node_coordinator_state(node):
@authenticator @authenticator
def api_node_domain_state(node): def api_node_domain_state(node):
if flask.request.method == 'GET': if flask.request.method == 'GET':
return "Test", 200 return pvcapi.node_domain_state(node)
if flask.request.method == 'POST': if flask.request.method == 'POST':
if not 'domain-state' in flask.request.values: if not 'domain-state' in flask.request.values: