Move API call to function and implement keys

Move all API calls to a new common function called call_api to
facilitate easier future changes. Use this function to implement API key
handling via request header value as well as integrate the request URI
generation and debug output handling.

Closes #65
This commit is contained in:
Joshua Boniface 2020-01-08 19:34:24 -05:00
parent 10d892c698
commit 6e5065511c
7 changed files with 298 additions and 756 deletions

View File

@ -24,33 +24,14 @@ import re
import json import json
import time import time
import math import math
import requests
import cli_lib.ansiprint as ansiprint import cli_lib.ansiprint as ansiprint
from cli_lib.common import call_api
def debug_output(config, request_uri, response):
if config['debug']:
import click
click.echo('API endpoint: {}'.format(request_uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
# #
# Supplemental functions # Supplemental functions
# #
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
# Format byte sizes to/from human-readable units # Format byte sizes to/from human-readable units
byte_unit_matrix = { byte_unit_matrix = {
'B': 1, 'B': 1,
@ -126,12 +107,7 @@ def ceph_status(config):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/storage/ceph/status') response = call_api(config, 'get', '/storage/ceph/status')
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -146,12 +122,7 @@ def ceph_util(config):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/storage/ceph/utilization') response = call_api(config, 'get', '/storage/ceph/utilization')
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -177,12 +148,7 @@ def ceph_osd_info(config, osd):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/storage/ceph/osd/{osd}'.format(osd=osd)) response = call_api(config, 'get', '/storage/ceph/osd/{osd}'.format(osd=osd))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -201,13 +167,7 @@ def ceph_osd_list(config, limit):
if limit: if limit:
params['limit'] = limit params['limit'] = limit
request_uri = get_request_uri(config, '/storage/ceph/osd') response = call_api(config, 'get', '/storage/ceph/osd', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -222,17 +182,12 @@ def ceph_osd_add(config, node, device, weight):
API arguments: node={node}, device={device}, weight={weight} API arguments: node={node}, device={device}, weight={weight}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/osd') params = {
response = requests.post(
request_uri,
params={
'node': node, 'node': node,
'device': device, 'device': device,
'weight': weight 'weight': weight
} }
) response = call_api(config, 'post', '/storage/ceph/osd', params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -249,15 +204,10 @@ def ceph_osd_remove(config, osdid):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/osd/{osdid}'.format(osdid=osdid)) params = {
response = requests.delete(
request_uri,
params={
'yes-i-really-mean-it': 'yes' 'yes-i-really-mean-it': 'yes'
} }
) response = call_api(config, 'delete', '/storage/ceph/osd/{osdid}'.format(osdid=osdid), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -274,15 +224,10 @@ def ceph_osd_state(config, osdid, state):
API arguments: state={state} API arguments: state={state}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/osd/{osdid}/state'.format(osdid=osdid)) params = {
response = requests.post(
request_uri,
params={
'state': state 'state': state
} }
) response = call_api(config, 'post', '/storage/ceph/osd/{osdid}/state'.format(osdid=osdid), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -299,16 +244,11 @@ def ceph_osd_option(config, option, action):
API arguments: option={option}, action={action} API arguments: option={option}, action={action}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/option') params = {
response = requests.post(
request_uri,
params={
'option': option, 'option': option,
'action': action 'action': action
} }
) response = call_api(config, 'post', '/storage/ceph/option', params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -588,12 +528,7 @@ def ceph_pool_info(config, pool):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/storage/ceph/pool/{pool}'.format(pool=pool)) response = call_api(config, 'get', '/storage/ceph/pool/{pool}'.format(pool=pool))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -612,13 +547,7 @@ def ceph_pool_list(config, limit):
if limit: if limit:
params['limit'] = limit params['limit'] = limit
request_uri = get_request_uri(config, '/storage/ceph/pool') response = call_api(config, 'get', '/storage/ceph/pool', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -633,17 +562,12 @@ def ceph_pool_add(config, pool, pgs, replcfg):
API arguments: pool={pool}, pgs={pgs}, replcfg={replcfg} API arguments: pool={pool}, pgs={pgs}, replcfg={replcfg}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/pool') params = {
response = requests.post(
request_uri,
params={
'pool': pool, 'pool': pool,
'pgs': pgs, 'pgs': pgs,
'replcfg': replcfg 'replcfg': replcfg
} }
) response = call_api(config, 'post', '/storage/ceph/pool', params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -660,15 +584,10 @@ def ceph_pool_remove(config, pool):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/pool/{pool}'.format(pool=pool)) params = {
response = requests.delete(
request_uri,
params={
'yes-i-really-mean-it': 'yes' 'yes-i-really-mean-it': 'yes'
} }
) response = call_api(config, 'delete', '/storage/ceph/pool/{pool}'.format(pool=pool), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -886,12 +805,7 @@ def ceph_volume_info(config, pool, volume):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/storage/ceph/volume/{pool}/{volume}'.format(volume=volume, pool=pool)) response = call_api(config, 'get', '/storage/ceph/volume/{pool}/{volume}'.format(volume=volume, pool=pool))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -912,13 +826,7 @@ def ceph_volume_list(config, limit, pool):
if pool: if pool:
params['pool'] = pool params['pool'] = pool
request_uri = get_request_uri(config, '/storage/ceph/volume') response = call_api(config, 'get', '/storage/ceph/volume', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -933,17 +841,12 @@ def ceph_volume_add(config, pool, volume, size):
API arguments: volume={volume}, pool={pool}, size={size} API arguments: volume={volume}, pool={pool}, size={size}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/volume') params = {
response = requests.post(
request_uri,
params={
'volume': volume, 'volume': volume,
'pool': pool, 'pool': pool,
'size': size 'size': size
} }
) response = call_api(config, 'post', '/storage/ceph/volume', params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -960,12 +863,7 @@ def ceph_volume_remove(config, pool, volume):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/volume/{pool}/{volume}'.format(volume=volume, pool=pool)) response = call_api(config, 'delete', '/storage/ceph/volume/{pool}/{volume}'.format(volume=volume, pool=pool))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -989,13 +887,7 @@ def ceph_volume_modify(config, pool, volume, new_name=None, new_size=None):
if new_size: if new_size:
params['new_size'] = new_size params['new_size'] = new_size
request_uri = get_request_uri(config, '/storage/ceph/volume/{pool}/{volume}'.format(volume=volume, pool=pool)) response = call_api(config, 'put', '/storage/ceph/volume/{pool}/{volume}'.format(volume=volume, pool=pool), params=params)
response = requests.put(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -1012,15 +904,10 @@ def ceph_volume_clone(config, pool, volume, new_volume):
API arguments: new_volume={new_volume API arguments: new_volume={new_volume
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/volume/{pool}/{volume}/clone'.format(volume=volume, pool=pool)) params = {
response = requests.post(
request_uri,
params={
'new_volume': new_volume 'new_volume': new_volume
} }
) response = call_api(config, 'post', '/storage/ceph/volume/{pool}/{volume}/clone'.format(volume=volume, pool=pool), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -1155,12 +1042,7 @@ def ceph_snapshot_info(config, pool, volume, snapshot):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/storage/ceph/snapshot/{pool}/{volume}/{snapshot}'.format(snapshot=snapshot, volume=volume, pool=pool)) response = call_api(config, 'get', '/storage/ceph/snapshot/{pool}/{volume}/{snapshot}'.format(snapshot=snapshot, volume=volume, pool=pool))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -1183,13 +1065,7 @@ def ceph_snapshot_list(config, limit, volume, pool):
if pool: if pool:
params['pool'] = pool params['pool'] = pool
request_uri = get_request_uri(config, '/storage/ceph/snapshot') response = call_api(config, 'get', '/storage/ceph/snapshot', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -1204,17 +1080,12 @@ def ceph_snapshot_add(config, pool, volume, snapshot):
API arguments: snapshot={snapshot}, volume={volume}, pool={pool} API arguments: snapshot={snapshot}, volume={volume}, pool={pool}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/snapshot') params = {
response = requests.post(
request_uri,
params={
'snapshot': snapshot, 'snapshot': snapshot,
'volume': volume, 'volume': volume,
'pool': pool 'pool': pool
} }
) response = call_api(config, 'post', '/storage/ceph/snapshot', params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -1231,12 +1102,7 @@ def ceph_snapshot_remove(config, pool, volume, snapshot):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/storage/ceph/snapshot/{pool}/{volume}/{snapshot}'.format(snapshot=snapshot, volume=volume, pool=pool)) response = call_api(config, 'delete', '/storage/ceph/snapshot/{pool}/{volume}/{snapshot}'.format(snapshot=snapshot, volume=volume, pool=pool))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -1258,13 +1124,7 @@ def ceph_snapshot_modify(config, pool, volume, snapshot, new_name=None):
if new_name: if new_name:
params['new_name'] = new_name params['new_name'] = new_name
request_uri = get_request_uri(config, '/storage/ceph/snapshot/{pool}/{volume}/{snapshot}'.format(snapshot=snapshot, volume=volume, pool=pool)) response = call_api(config, 'put', '/storage/ceph/snapshot/{pool}/{volume}/{snapshot}'.format(snapshot=snapshot, volume=volume, pool=pool), params=params)
response = requests.put(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True

View File

@ -21,28 +21,9 @@
############################################################################### ###############################################################################
import json import json
import requests
import cli_lib.ansiprint as ansiprint import cli_lib.ansiprint as ansiprint
from cli_lib.common import call_api
def debug_output(config, request_uri, response):
if config['debug']:
import click
click.echo('API endpoint: POST {}'.format(request_uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
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
def initialize(config): def initialize(config):
""" """
@ -52,12 +33,7 @@ def initialize(config):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/initialize') response = call_api(config, 'get', '/initialize')
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -74,12 +50,7 @@ def get_info(config):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/status') response = call_api(config, 'get', '/status')
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()

View File

@ -0,0 +1,82 @@
#!/usr/bin/env python3
# common.py - PVC CLI client function library, Common functions
# Part of the Parallel Virtual Cluster (PVC) system
#
# Copyright (C) 2018-2019 Joshua M. Boniface <joshua@boniface.me>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
###############################################################################
import requests
import click
def call_api(config, operation, request_uri, params=None, data=None):
# Craft the URI
uri = '{}://{}{}{}'.format(
config['api_scheme'],
config['api_host'],
config['api_prefix'],
request_uri
)
# Craft the authentication header if required
if config['api_key']:
headers = {'X-Api-Key': config['api_key']}
else:
headers = None
# Determine the request type and hit the API
if operation == 'get':
response = requests.get(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'post':
response = requests.post(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'put':
response = requests.put(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'patch':
response = requests.patch(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'delete':
response = requests.delete(
uri,
headers=headers,
params=params,
data=data
)
# Display debug output
if config['debug']:
click.echo('API endpoint: {}'.format(uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
# Return the response object
return response

View File

@ -22,28 +22,9 @@
import difflib import difflib
import colorama import colorama
import requests
import cli_lib.ansiprint as ansiprint import cli_lib.ansiprint as ansiprint
from cli_lib.common import call_api
def debug_output(config, request_uri, response):
if config['debug']:
import click
click.echo('API endpoint: POST {}'.format(request_uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
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
def isValidMAC(macaddr): def isValidMAC(macaddr):
allowed = re.compile(r""" allowed = re.compile(r"""
@ -82,12 +63,7 @@ def net_info(config, net):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/network/{net}'.format(net=net)) response = call_api(config, 'get', '/network/{net}'.format(net=net))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -106,13 +82,7 @@ def net_list(config, limit):
if limit: if limit:
params['limit'] = limit params['limit'] = limit
request_uri = get_request_uri(config, '/network') response = call_api(config, 'get', '/network', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -127,10 +97,7 @@ def net_add(config, vni, description, nettype, domain, name_servers, ip4_network
API arguments: lots API arguments: lots
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/network') params = {
response = requests.post(
request_uri,
params={
'vni': vni, 'vni': vni,
'description': description, 'description': description,
'nettype': nettype, 'nettype': nettype,
@ -144,9 +111,7 @@ def net_add(config, vni, description, nettype, domain, name_servers, ip4_network
'dhcp4_start': dhcp4_start, 'dhcp4_start': dhcp4_start,
'dhcp4_end': dhcp4_end 'dhcp4_end': dhcp4_end
} }
) response = call_api(config, 'post', '/network', params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -163,7 +128,6 @@ def net_modify(config, net, description, domain, name_servers, ip4_network, ip4_
API arguments: lots API arguments: lots
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/network/{net}'.format(net=net))
params = dict() params = dict()
if description is not None: if description is not None:
params['description'] = description params['description'] = description
@ -186,12 +150,7 @@ def net_modify(config, net, description, domain, name_servers, ip4_network, ip4_
if dhcp4_end is not None: if dhcp4_end is not None:
params['dhcp4_end'] = dhcp4_end params['dhcp4_end'] = dhcp4_end
response = requests.put( response = call_api(config, 'put', '/network/{net}'.format(net=net), params=params)
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -208,12 +167,7 @@ def net_remove(config, net):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/network/{net}'.format(net=net)) response = call_api(config, 'delete', '/network/{net}'.format(net=net))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -233,12 +187,7 @@ def net_dhcp_info(config, net, mac):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/network/{net}/lease/{mac}'.format(net=net, mac=mac)) response = call_api(config, 'get', '/network/{net}/lease/{mac}'.format(net=net, mac=mac))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -259,13 +208,7 @@ def net_dhcp_list(config, net, limit, only_static=False):
if only_static: if only_static:
params['static'] = True params['static'] = True
request_uri = get_request_uri(config, '/network/{net}/lease'.format(net=net)) response = call_api(config, 'get', '/network/{net}/lease'.format(net=net), params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -280,17 +223,12 @@ def net_dhcp_add(config, net, ipaddr, macaddr, hostname):
API arguments: macaddress=macaddr, ipaddress=ipaddr, hostname=hostname API arguments: macaddress=macaddr, ipaddress=ipaddr, hostname=hostname
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/network/{net}/lease'.format(net=net)) params = {
response = requests.post(
request_uri,
params={
'macaddress': macaddr, 'macaddress': macaddr,
'ipaddress': ipaddr, 'ipaddress': ipaddr,
'hostname': hostname 'hostname': hostname
} }
) response = call_api(config, 'post', '/network/{net}/lease'.format(net=net), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -307,12 +245,7 @@ def net_dhcp_remove(config, net, mac):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/network/{net}/lease/{mac}'.format(net=net, mac=mac)) response = call_api(config, 'delete', '/network/{net}/lease/{mac}'.format(net=net, mac=mac))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -332,12 +265,7 @@ def net_acl_info(config, net, description):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/network/{net}/acl/{description}'.format(net=net, description=description)) response = call_api(config, 'get', '/network/{net}/acl/{description}'.format(net=net, description=description))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -358,13 +286,7 @@ def net_acl_list(config, net, limit, direction):
if direction is not None: if direction is not None:
params['direction'] = direction params['direction'] = direction
request_uri = get_request_uri(config, '/network/{net}/acl'.format(net=net)) response = call_api(config, 'get', '/network/{net}/acl'.format(net=net), params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -386,13 +308,7 @@ def net_acl_add(config, net, direction, description, rule, order):
if order is not None: if order is not None:
params['order'] = order params['order'] = order
request_uri = get_request_uri(config, '/network/{net}/acl'.format(net=net)) response = call_api(config, 'post', '/network/{net}/acl'.format(net=net), params=params)
response = requests.post(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -409,12 +325,7 @@ def net_acl_remove(config, net, description):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/network/{net}/acl/{description}'.format(net=net, description=description)) response = call_api(config, 'delete', '/network/{net}/acl/{description}'.format(net=net, description=description))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True

View File

@ -20,28 +20,8 @@
# #
############################################################################### ###############################################################################
import requests
import cli_lib.ansiprint as ansiprint import cli_lib.ansiprint as ansiprint
from cli_lib.common import call_api
def debug_output(config, request_uri, response):
if config['debug']:
import click
click.echo('API endpoint: POST {}'.format(request_uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
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 # Primary functions
@ -54,15 +34,10 @@ def node_coordinator_state(config, node, action):
API arguments: action={action} API arguments: action={action}
API schema: {"message": "{data}"} API schema: {"message": "{data}"}
""" """
request_uri = get_request_uri(config, '/node/{node}/coordinator-state'.format(node=node))
response = requests.post(
request_uri,
params={ params={
'state': action 'state': action
} }
) response = call_api(config, 'post', '/node/{node}/coordinator-state'.format(node=node), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -79,16 +54,11 @@ def node_domain_state(config, node, action, wait):
API arguments: action={action}, wait={wait} API arguments: action={action}, wait={wait}
API schema: {"message": "{data}"} API schema: {"message": "{data}"}
""" """
request_uri = get_request_uri(config, '/node/{node}/domain-state'.format(node=node))
response = requests.post(
request_uri,
params={ params={
'state': action, 'state': action,
'wait': wait 'wait': wait
} }
) response = call_api(config, 'post', '/node/{node}/domain-state'.format(node=node), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -105,12 +75,7 @@ def node_info(config, node):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/node/{node}'.format(node=node)) response = call_api(config, 'get', '/node/{node}'.format(node=node))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -125,20 +90,11 @@ def node_list(config, limit):
API arguments: limit={limit} API arguments: limit={limit}
API schema: [{json_data_object},{json_data_object},etc.] API schema: [{json_data_object},{json_data_object},etc.]
""" """
params = dict()
if limit: if limit:
params = { params['limit'] = limit
'limit': limit
}
else:
params = {}
request_uri = get_request_uri(config, '/node') response = call_api(config, 'get', '/node', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()

View File

@ -23,28 +23,9 @@
import time import time
import re import re
import subprocess import subprocess
import requests
import cli_lib.ansiprint as ansiprint import cli_lib.ansiprint as ansiprint
from cli_lib.common import call_api
def debug_output(config, request_uri, response):
if config['debug']:
import click
click.echo('API endpoint: POST {}'.format(request_uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
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 # Primary functions
@ -57,12 +38,7 @@ def template_info(config, template, template_type):
API arguments: API arguments:
API schema: {json_template_object} API schema: {json_template_object}
""" """
request_uri = get_request_uri(config, '/provisioner/template/{template_type}/{template}'.format(template_type=template_type, template=template)) response = call_api(config, 'get', '/provisioner/template/{template_type}/{template}'.format(template_type=template_type, template=template))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -82,15 +58,9 @@ def template_list(config, limit, template_type=None):
params['limit'] = limit params['limit'] = limit
if template_type is not None: if template_type is not None:
request_uri = get_request_uri(config, '/provisioner/template/{template_type}'.format(template_type=template_type)) response = call_api(config, 'get', '/provisioner/template/{template_type}'.format(template_type=template_type), params=params)
else: else:
request_uri = get_request_uri(config, '/provisioner/template') response = call_api(config, 'get', '/provisioner/template', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -105,13 +75,7 @@ def template_add(config, params, template_type=None):
API_arguments: args API_arguments: args
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/template/{template_type}'.format(template_type=template_type)) response = call_api(config, 'post', '/provisioner/template/{template_type}'.format(template_type=template_type), params=params)
response = requests.post(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -128,12 +92,7 @@ def template_remove(config, name, template_type=None):
API_arguments: API_arguments:
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/template/{template_type}/{name}'.format(template_type=template_type, name=name)) response = call_api(config, 'delete', '/provisioner/template/{template_type}/{name}'.format(template_type=template_type, name=name))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -150,13 +109,7 @@ def template_element_add(config, name, element_id, params, element_type=None, te
API_arguments: args API_arguments: args
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/template/{template_type}/{name}/{element_type}/{element_id}'.format(template_type=template_type, name=name, element_type=element_type, element_id=element_id)) response = call_api(config, 'post', '/provisioner/template/{template_type}/{name}/{element_type}/{element_id}'.format(template_type=template_type, name=name, element_type=element_type, element_id=element_id), params=params)
response = requests.post(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -173,12 +126,7 @@ def template_element_remove(config, name, element_id, element_type=None, templat
API_arguments: API_arguments:
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/template/{template_type}/{name}/{element_type}/{element_id}'.format(template_type=template_type, name=name, element_type=element_type, element_id=element_id)) response = call_api(config, 'delete', '/provisioner/template/{template_type}/{name}/{element_type}/{element_id}'.format(template_type=template_type, name=name, element_type=element_type, element_id=element_id))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -195,12 +143,7 @@ def userdata_info(config, userdata):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/provisioner/userdata/{userdata}'.format(userdata=userdata)) response = call_api(config, 'get', '/provisioner/userdata/{userdata}'.format(userdata=userdata))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json()[0] return True, response.json()[0]
@ -219,13 +162,7 @@ def userdata_list(config, limit):
if limit: if limit:
params['limit'] = limit params['limit'] = limit
request_uri = get_request_uri(config, '/provisioner/userdata') response = call_api(config, 'get', '/provisioner/userdata', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -243,18 +180,13 @@ def userdata_add(config, params):
name = params.get('name') name = params.get('name')
userdata_data = params.get('data') userdata_data = params.get('data')
request_uri = get_request_uri(config, '/provisioner/userdata') params = {
response = requests.post(
request_uri,
params={
'name': name 'name': name
}, }
data={ data = {
'data': userdata_data 'data': userdata_data
} }
) response = call_api(config, 'post', '/provisioner/userdata', params=params, data=data)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -273,18 +205,13 @@ def userdata_modify(config, name, params):
""" """
userdata_data = params.get('data') userdata_data = params.get('data')
request_uri = get_request_uri(config, '/provisioner/userdata/{name}'.format(name=name)) params = {
response = requests.put(
request_uri,
params={
'name': name 'name': name
}, }
data={ data = {
'data': userdata_data 'data': userdata_data
} }
) response = call_api(config, 'put', '/provisioner/userdata/{name}'.format(name=name), params=params, data=data)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -301,12 +228,7 @@ def userdata_remove(config, name):
API_arguments: API_arguments:
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/userdata/{name}'.format(name=name)) response = call_api(config, 'delete', '/provisioner/userdata/{name}'.format(name=name))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -323,12 +245,7 @@ def script_info(config, script):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/provisioner/script/{script}'.format(script=script)) response = call_api(config, 'get', '/provisioner/script/{script}'.format(script=script))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json()[0] return True, response.json()[0]
@ -347,13 +264,7 @@ def script_list(config, limit):
if limit: if limit:
params['limit'] = limit params['limit'] = limit
request_uri = get_request_uri(config, '/provisioner/script') response = call_api(config, 'get', '/provisioner/script', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -371,18 +282,13 @@ def script_add(config, params):
name = params.get('name') name = params.get('name')
script_data = params.get('data') script_data = params.get('data')
request_uri = get_request_uri(config, '/provisioner/script') params = {
response = requests.post(
request_uri,
params={
'name': name 'name': name
}, }
data={ data = {
'data': script_data 'data': script_data
} }
) response = call_api(config, 'post', '/provisioner/script', params=params, data=data)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -401,18 +307,13 @@ def script_modify(config, name, params):
""" """
script_data = params.get('data') script_data = params.get('data')
request_uri = get_request_uri(config, '/provisioner/script/{name}'.format(name=name)) params = {
response = requests.put(
request_uri,
params={
'name': name 'name': name
}, }
data={ data = {
'data': script_data 'data': script_data
} }
) response = call_api(config, 'put', '/provisioner/script/{name}'.format(name=name), params=params, data=data)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -429,12 +330,7 @@ def script_remove(config, name):
API_arguments: API_arguments:
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/script/{name}'.format(name=name)) response = call_api(config, 'delete', '/provisioner/script/{name}'.format(name=name))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -451,12 +347,7 @@ def profile_info(config, profile):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/provisioner/profile/{profile}'.format(profile=profile)) response = call_api(config, 'get', '/provisioner/profile/{profile}'.format(profile=profile))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json()[0] return True, response.json()[0]
@ -475,13 +366,7 @@ def profile_list(config, limit):
if limit: if limit:
params['limit'] = limit params['limit'] = limit
request_uri = get_request_uri(config, '/provisioner/profile') response = call_api(config, 'get', '/provisioner/profile', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -496,13 +381,7 @@ def profile_add(config, params):
API_arguments: args API_arguments: args
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/profile') response = call_api(config, 'post', '/provisioner/profile', params=params)
response = requests.post(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -519,13 +398,7 @@ def profile_modify(config, name, params):
API_arguments: args API_arguments: args
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/profile/{name}'.format(name=name)) response = call_api(config, 'put', '/provisioner/profile/{name}'.format(name=name), params=params)
response = requests.put(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -542,12 +415,7 @@ def profile_remove(config, name):
API_arguments: API_arguments:
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/profile/{name}'.format(name=name)) response = call_api(config, 'delete', '/provisioner/profile/{name}'.format(name=name))
response = requests.delete(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True
@ -564,16 +432,11 @@ def vm_create(config, name, profile, wait_flag):
API_arguments: name={name}, profile={profile} API_arguments: name={name}, profile={profile}
API schema: {message} API schema: {message}
""" """
request_uri = get_request_uri(config, '/provisioner/create') params = {
response = requests.post(
request_uri,
params={
'name': name, 'name': name,
'profile': profile 'profile': profile
} }
) response = call_api(config, 'post', '/provisioner/create', params=params)
debug_output(config, request_uri, response)
if response.status_code == 202: if response.status_code == 202:
retvalue = True retvalue = True
@ -596,12 +459,7 @@ def task_status(config, task_id, is_watching=False):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/provisioner/status/{task_id}'.format(task_id=task_id)) response = call_api(config, 'get', '/provisioner/status/{task_id}'.format(task_id=task_id))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retvalue = True retvalue = True

View File

@ -23,31 +23,12 @@
import time import time
import re import re
import subprocess import subprocess
import requests
from collections import deque from collections import deque
import cli_lib.ansiprint as ansiprint import cli_lib.ansiprint as ansiprint
import cli_lib.ceph as ceph import cli_lib.ceph as ceph
from cli_lib.common import call_api
def debug_output(config, request_uri, response):
if config['debug']:
import click
click.echo('API endpoint: POST {}'.format(request_uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
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 # Primary functions
@ -60,12 +41,7 @@ def vm_info(config, vm):
API arguments: API arguments:
API schema: {json_data_object} API schema: {json_data_object}
""" """
request_uri = get_request_uri(config, '/vm/{vm}'.format(vm=vm)) response = call_api(config, 'get', '/vm/{vm}'.format(vm=vm))
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -88,13 +64,7 @@ def vm_list(config, limit, target_node, target_state):
if target_state: if target_state:
params['state'] = target_state params['state'] = target_state
request_uri = get_request_uri(config, '/vm') response = call_api(config, 'get', '/vm', params=params)
response = requests.get(
request_uri,
params=params
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
return True, response.json() return True, response.json()
@ -109,21 +79,16 @@ def vm_define(config, xml, node, node_limit, node_selector, node_autostart):
API arguments: xml={xml}, node={node}, limit={node_limit}, selector={node_selector}, autostart={node_autostart} API arguments: xml={xml}, node={node}, limit={node_limit}, selector={node_selector}, autostart={node_autostart}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/vm') params = {
response = requests.post(
request_uri,
params={
'node': node, 'node': node,
'limit': node_limit, 'limit': node_limit,
'selector': node_selector, 'selector': node_selector,
'autostart': node_autostart 'autostart': node_autostart
}, }
data={ data = {
'xml': xml 'xml': xml
} }
) response = call_api(config, 'post', '/vm', params=params, data=data)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -140,16 +105,13 @@ def vm_modify(config, vm, xml, restart):
API arguments: xml={xml}, restart={restart} API arguments: xml={xml}, restart={restart}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}'.format(vm=vm)) params = {
response = requests.put(
request_uri,
params={
'xml': xml,
'restart': restart 'restart': restart
} }
) data = {
'xml': xml
debug_output(config, request_uri, response) }
response = call_api(config, 'put', '/vm/{vm}'.format(vm=vm), params=params, data=data)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -166,14 +128,7 @@ def vm_metadata(config, vm, node_limit, node_selector, node_autostart):
API arguments: limit={node_limit}, selector={node_selector}, autostart={node_autostart} API arguments: limit={node_limit}, selector={node_selector}, autostart={node_autostart}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}/meta'.format(vm=vm)) response = call_api(config, 'get', '/vm/{vm}/meta'.format(vm=vm))
# Get the existing metadata so we can perform a fully dynamic update
response = requests.get(
request_uri
)
debug_output(config, request_uri, response)
metadata = response.json() metadata = response.json()
@ -191,17 +146,12 @@ def vm_metadata(config, vm, node_limit, node_selector, node_autostart):
metadata['node_autostart'] = node_autostart metadata['node_autostart'] = node_autostart
# Write the new metadata # Write the new metadata
print(metadata['node_limit'])
response = requests.post(
request_uri,
params={ params={
'limit': metadata['node_limit'], 'limit': metadata['node_limit'],
'selector': metadata['node_selector'], 'selector': metadata['node_selector'],
'autostart': metadata['node_autostart'] 'autostart': metadata['node_autostart']
} }
) response = call_api(config, 'post', '/vm/{vm}/meta'.format(vm=vm), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -218,15 +168,10 @@ def vm_remove(config, vm, delete_disks=False):
API arguments: delete_disks={delete_disks} API arguments: delete_disks={delete_disks}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}'.format(vm=vm))
response = requests.delete(
request_uri,
params={ params={
'delete_disks': delete_disks 'delete_disks': delete_disks
} }
) response = call_api(config, 'delete', '/vm/{vm}'.format(vm=vm), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -243,15 +188,10 @@ def vm_state(config, vm, target_state):
API arguments: state={state} API arguments: state={state}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}/state'.format(vm=vm))
response = requests.post(
request_uri,
params={ params={
'state': target_state, 'state': target_state,
} }
) response = call_api(config, 'post', '/vm/{vm}/state'.format(vm=vm), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -268,17 +208,12 @@ def vm_node(config, vm, target_node, action, force=False):
API arguments: node={target_node}, action={action}, force={force} API arguments: node={target_node}, action={action}, force={force}
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}/node'.format(vm=vm))
response = requests.post(
request_uri,
params={ params={
'node': target_node, 'node': target_node,
'action': action, 'action': action,
'force': force 'force': force
} }
) response = call_api(config, 'post', '/vm/{vm}/node'.format(vm=vm), params=params)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -295,12 +230,7 @@ def vm_locks(config, vm):
API arguments: API arguments:
API schema: {"message":"{data}"} API schema: {"message":"{data}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}/locks'.format(vm=vm)) response = call_api(config, 'post', '/vm/{vm}/locks'.format(vm=vm))
response = requests.post(
request_uri
)
debug_output(config, request_uri, response)
if response.status_code == 200: if response.status_code == 200:
retstatus = True retstatus = True
@ -317,13 +247,10 @@ def view_console_log(config, vm, lines=100):
API arguments: lines={lines} API arguments: lines={lines}
API schema: {"name":"{vmname}","data":"{console_log}"} API schema: {"name":"{vmname}","data":"{console_log}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}/console'.format(vm=vm)) params = {
response = requests.get( 'lines': lines
request_uri, }
params={'lines': lines} response = call_api(config, 'get', '/vm/{vm}/console'.format(vm=vm), params=params)
)
debug_output(config, request_uri, response)
if response.status_code != 200: if response.status_code != 200:
return False, response.json()['message'] return False, response.json()['message']
@ -344,15 +271,10 @@ def follow_console_log(config, vm, lines=10):
API arguments: lines={lines} API arguments: lines={lines}
API schema: {"name":"{vmname}","data":"{console_log}"} API schema: {"name":"{vmname}","data":"{console_log}"}
""" """
request_uri = get_request_uri(config, '/vm/{vm}/console'.format(vm=vm)) params = {
response = requests.get( 'lines': lines
request_uri, }
params={'lines': lines} response = call_api(config, 'get', '/vm/{vm}/console'.format(vm=vm), params=params)
)
debug_output(config, request_uri, response)
console_log = response.json()['data']
# Shrink the log buffer to length lines # Shrink the log buffer to length lines
shrunk_log = console_log.split('\n')[-lines:] shrunk_log = console_log.split('\n')[-lines:]
@ -363,19 +285,7 @@ def follow_console_log(config, vm, lines=10):
while True: while True:
# Grab the next line set # Grab the next line set
# Get the (initial) data from the API response = call_api(config, 'get', '/vm/{vm}/console'.format(vm=vm), params=params)
response = requests.get(
'{}://{}{}{}'.format(
config['api_scheme'],
config['api_host'],
config['api_prefix'],
'/vm/{}/console'.format(vm)
),
params={'lines': lines}
)
debug_output(config, request_uri, response)
new_console_log = response.json()['data'] new_console_log = response.json()['data']
# Split the new and old log strings into constitutent lines # Split the new and old log strings into constitutent lines
old_console_loglines = console_log.split('\n') old_console_loglines = console_log.split('\n')
@ -480,10 +390,7 @@ def format_info(config, domain_information, long_output):
else: else:
net_vni = re.sub('br', '', net['source']) net_vni = re.sub('br', '', net['source'])
request_uri = get_request_uri(config, '/network/{net}'.format(net=net_vni)) response = call_api(config, 'get', '/network/{net}'.format(net=net_vni))
response = requests.get(
request_uri
)
if response.status_code != 200 and net_vni != 'cluster': if response.status_code != 200 and net_vni != 'cluster':
net_list.append(ansiprint.red() + net_vni + ansiprint.end() + ' [invalid]') net_list.append(ansiprint.red() + net_vni + ansiprint.end() + ' [invalid]')
else: else:
@ -632,10 +539,7 @@ def format_list(config, vm_list, raw):
vm_net_colour = '' vm_net_colour = ''
for net_vni in raw_net_list: for net_vni in raw_net_list:
if not net_vni in valid_net_list: if not net_vni in valid_net_list:
request_uri = get_request_uri(config, '/network/{net}'.format(net=net_vni)) response = call_api(config, 'get', '/network/{net}'.format(net=net_vni))
response = requests.get(
request_uri
)
if response.status_code != 200 and net_vni != 'cluster': if response.status_code != 200 and net_vni != 'cluster':
vm_net_colour = ansiprint.red() vm_net_colour = ansiprint.red()
else: else: