From 3d178304f14248ae7449d82b002d8b6e5cd095fd Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Wed, 25 Dec 2019 20:18:53 -0500 Subject: [PATCH] Convert some node functions to API --- client-cli/cli_lib/node.py | 87 +++++++++++++++++++++++++++++++++++++- client-cli/pvc.py | 25 +++++------ 2 files changed, 95 insertions(+), 17 deletions(-) diff --git a/client-cli/cli_lib/node.py b/client-cli/cli_lib/node.py index bcc3d50d..7ee0dab0 100644 --- a/client-cli/cli_lib/node.py +++ b/client-cli/cli_lib/node.py @@ -20,12 +20,95 @@ # ############################################################################### -import difflib -import colorama import click +import requests import cli_lib.ansiprint as ansiprint +def get_request_uri(config, endpoint): + """ + Return the fully-formed URI for {endpoint} + """ + uri = '{}://{}{}{}'.format( + config['api_scheme'], + config['api_host'], + config['api_prefix'], + endpoint + ) + return uri + +# +# Primary functions +# +def node_coordinator_state(config, node, action): + """ + Set node coordinator state state (primary/secondary) + + API endpoint: POST /api/v1/node/{node}/coordinator-state + API arguments: action={action} + API schema: {"message": "{data}"} + """ + request_uri = get_request_uri(config, '/node/{node}/coordinator-state'.format(node=node)) + + response = requests.post( + request_uri, + params={'state': action} + ) + + if config['debug']: + print( + 'API endpoint: POST {}'.format(request_uri) + ) + print( + 'Response code: {}'.format(response.status_code) + ) + print( + 'Response headers: {}'.format(response.headers) + ) + + if response.status_code == 200: + retstatus = True + else: + retstatus = False + + return retstatus, response.json()['message'] + +def node_domain_state(config, node, action, wait): + """ + Set node domain state state (flush/ready) + + API endpoint: POST /api/v1/node/{node}/domain-state + API arguments: action={action}, wait={wait} + API schema: {"message": "{data}"} + """ + request_uri = get_request_uri(config, '/node/{node}/domain-state'.format(node=node)) + + response = requests.post( + request_uri, + params={'state': action, 'wait': wait} + ) + + if config['debug']: + print( + 'API endpoint: POST {}'.format(request_uri) + ) + print( + 'Response code: {}'.format(response.status_code) + ) + print( + 'Response headers: {}'.format(response.headers) + ) + + if response.status_code == 200: + retstatus = True + else: + retstatus = False + + return retstatus, response.json()['message'] + +# +# Output display functions +# def getOutputColours(node_information): if node_information['daemon_state'] == 'run': daemon_state_colour = ansiprint.green() diff --git a/client-cli/pvc.py b/client-cli/pvc.py index 7422ee6e..d4a4e566 100755 --- a/client-cli/pvc.py +++ b/client-cli/pvc.py @@ -81,9 +81,8 @@ def node_secondary(node): Take NODE out of primary router mode. """ - zk_conn = pvc_common.startZKConnection(zk_host) - retcode, retmsg = pvc_node.secondary_node(zk_conn, node) - cleanup(retcode, retmsg, zk_conn) + retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'secondary') + cleanup(retcode, retmsg) ############################################################################### # pvc node primary @@ -97,9 +96,8 @@ def node_primary(node): Put NODE into primary router mode. """ - zk_conn = pvc_common.startZKConnection(zk_host) - retcode, retmsg = pvc_node.primary_node(zk_conn, node) - cleanup(retcode, retmsg, zk_conn) + retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'primary') + cleanup(retcode, retmsg) ############################################################################### # pvc node flush @@ -117,9 +115,8 @@ def node_flush(node, wait): Take NODE out of active service and migrate away all VMs. If unspecified, defaults to this host. """ - zk_conn = pvc_common.startZKConnection(zk_host) - retcode, retmsg = pvc_node.flush_node(zk_conn, node, wait) - cleanup(retcode, retmsg, zk_conn) + retcode, retmsg = pvc_node.node_domain_state(config, node, 'flush', wait) + cleanup(retcode, retmsg) ############################################################################### # pvc node ready/unflush @@ -137,9 +134,8 @@ def node_ready(node, wait): Restore NODE to active service and migrate back all VMs. If unspecified, defaults to this host. """ - zk_conn = pvc_common.startZKConnection(zk_host) - retcode, retmsg = pvc_node.ready_node(zk_conn, node, wait) - cleanup(retcode, retmsg, zk_conn) + retcode, retmsg = pvc_node.node_domain_state(config, node, 'ready', wait) + cleanup(retcode, retmsg) @click.command(name='unflush', short_help='Restore node to service.') @click.argument( @@ -154,9 +150,8 @@ def node_unflush(node, wait): Restore NODE to active service and migrate back all VMs. If unspecified, defaults to this host. """ - zk_conn = pvc_common.startZKConnection(zk_host) - retcode, retmsg = pvc_node.ready_node(zk_conn, node, wait) - cleanup(retcode, retmsg, zk_conn) + retcode, retmsg = pvc_node.node_domain_state(config, node, 'ready', wait) + cleanup(retcode, retmsg) ############################################################################### # pvc node info