Convert some node functions to API

This commit is contained in:
Joshua Boniface 2019-12-25 20:18:53 -05:00
parent a9aeb2e836
commit 3d178304f1
2 changed files with 95 additions and 17 deletions

View File

@ -20,12 +20,95 @@
# #
############################################################################### ###############################################################################
import difflib
import colorama
import click import click
import requests
import cli_lib.ansiprint as ansiprint 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): def getOutputColours(node_information):
if node_information['daemon_state'] == 'run': if node_information['daemon_state'] == 'run':
daemon_state_colour = ansiprint.green() daemon_state_colour = ansiprint.green()

View File

@ -81,9 +81,8 @@ def node_secondary(node):
Take NODE out of primary router mode. Take NODE out of primary router mode.
""" """
zk_conn = pvc_common.startZKConnection(zk_host) retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'secondary')
retcode, retmsg = pvc_node.secondary_node(zk_conn, node) cleanup(retcode, retmsg)
cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
# pvc node primary # pvc node primary
@ -97,9 +96,8 @@ def node_primary(node):
Put NODE into primary router mode. Put NODE into primary router mode.
""" """
zk_conn = pvc_common.startZKConnection(zk_host) retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'primary')
retcode, retmsg = pvc_node.primary_node(zk_conn, node) cleanup(retcode, retmsg)
cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
# pvc node flush # 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. 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.node_domain_state(config, node, 'flush', wait)
retcode, retmsg = pvc_node.flush_node(zk_conn, node, wait) cleanup(retcode, retmsg)
cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
# pvc node ready/unflush # 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. 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.node_domain_state(config, node, 'ready', wait)
retcode, retmsg = pvc_node.ready_node(zk_conn, node, wait) cleanup(retcode, retmsg)
cleanup(retcode, retmsg, zk_conn)
@click.command(name='unflush', short_help='Restore node to service.') @click.command(name='unflush', short_help='Restore node to service.')
@click.argument( @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. 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.node_domain_state(config, node, 'ready', wait)
retcode, retmsg = pvc_node.ready_node(zk_conn, node, wait) cleanup(retcode, retmsg)
cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
# pvc node info # pvc node info