diff --git a/client-cli/cli_lib/ceph.py b/client-cli/cli_lib/ceph.py index b30d676c..a90ca0bf 100644 --- a/client-cli/cli_lib/ceph.py +++ b/client-cli/cli_lib/ceph.py @@ -21,7 +21,6 @@ ############################################################################### import re -import click import json import time import math @@ -29,6 +28,13 @@ import requests import cli_lib.ansiprint as ansiprint +def debug_output(config, request_uri, response): + if config['debug']: + import click.echo + 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) + # # Supplemental functions # @@ -125,10 +131,7 @@ def ceph_status(config): request_uri ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -148,10 +151,7 @@ def ceph_util(config): request_uri ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -182,10 +182,7 @@ def ceph_osd_info(config, osd): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -210,10 +207,7 @@ def ceph_osd_list(config, limit): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -238,10 +232,7 @@ def ceph_osd_add(config, node, device, weight): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -266,10 +257,7 @@ def ceph_osd_remove(config, osdid): } ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -294,10 +282,7 @@ def ceph_osd_state(config, osdid, state): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -323,10 +308,7 @@ def ceph_osd_option(config, option, action): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -611,10 +593,7 @@ def ceph_pool_info(config, pool): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -639,10 +618,7 @@ def ceph_pool_list(config, limit): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -667,10 +643,7 @@ def ceph_pool_add(config, pool, pgs, replcfg): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -695,10 +668,7 @@ def ceph_pool_remove(config, pool): } ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -921,10 +891,7 @@ def ceph_volume_info(config, pool, volume): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -951,10 +918,7 @@ def ceph_volume_list(config, limit, pool): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -979,10 +943,7 @@ def ceph_volume_add(config, pool, volume, size): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -1004,10 +965,7 @@ def ceph_volume_remove(config, pool, volume): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -1037,10 +995,7 @@ def ceph_volume_modify(config, pool, volume, new_name=None, new_size=None): params=params ) - if config['debug']: - print('API endpoint: PUT {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -1065,10 +1020,7 @@ def ceph_volume_clone(config, pool, volume, new_volume): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -1208,10 +1160,7 @@ def ceph_snapshot_info(config, pool, volume, snapshot): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -1240,10 +1189,7 @@ def ceph_snapshot_list(config, limit, volume, pool): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -1268,10 +1214,7 @@ def ceph_snapshot_add(config, pool, volume, snapshot): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -1293,10 +1236,7 @@ def ceph_snapshot_remove(config, pool, volume, snapshot): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -1324,10 +1264,7 @@ def ceph_snapshot_modify(config, pool, volume, snapshot, new_name=None): params=params ) - if config['debug']: - print('API endpoint: PUT {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True diff --git a/client-cli/cli_lib/cluster.py b/client-cli/cli_lib/cluster.py index 25bb852f..406fd6ee 100644 --- a/client-cli/cli_lib/cluster.py +++ b/client-cli/cli_lib/cluster.py @@ -20,12 +20,18 @@ # ############################################################################### -import click import json import requests import cli_lib.ansiprint as ansiprint +def debug_output(config, request_uri, response): + if config['debug']: + import click.echo + 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} @@ -51,10 +57,7 @@ def initialize(config): request_uri ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -76,10 +79,7 @@ def get_info(config): request_uri ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -88,12 +88,10 @@ def get_info(config): def format_info(cluster_information, oformat): if oformat == 'json': - print(json.dumps(cluster_information)) - return + return json.dumps(cluster_information)) if oformat == 'json-pretty': - print(json.dumps(cluster_information, indent=4)) - return + return json.dumps(cluster_information, indent=4)) # Plain formatting, i.e. human-readable if cluster_information['health'] == 'Optimal': diff --git a/client-cli/cli_lib/network.py b/client-cli/cli_lib/network.py index f9a15f7e..485e427c 100644 --- a/client-cli/cli_lib/network.py +++ b/client-cli/cli_lib/network.py @@ -22,11 +22,17 @@ import difflib import colorama -import click import requests import cli_lib.ansiprint as ansiprint +def debug_output(config, request_uri, response): + if config['debug']: + import click.echo + 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} @@ -81,10 +87,7 @@ def net_info(config, net): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -109,10 +112,7 @@ def net_list(config, limit): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -146,10 +146,7 @@ def net_add(config, vni, description, nettype, domain, name_servers, ip4_network } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -194,10 +191,7 @@ def net_modify(config, net, description, domain, name_servers, ip4_network, ip4_ params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -219,10 +213,7 @@ def net_remove(config, net): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -247,10 +238,7 @@ def net_dhcp_info(config, net, mac): request_uri ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -277,10 +265,7 @@ def net_dhcp_list(config, net, limit, only_static=False): params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -305,10 +290,7 @@ def net_dhcp_add(config, net, ipaddr, macaddr, hostname): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -330,10 +312,7 @@ def net_dhcp_remove(config, net, mac): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -358,10 +337,7 @@ def net_acl_info(config, net, description): request_uri ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -388,10 +364,7 @@ def net_acl_list(config, net, limit, direction): params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -419,10 +392,7 @@ def net_acl_add(config, net, direction, description, rule, order): params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -444,10 +414,7 @@ def net_acl_remove(config, net, description): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True diff --git a/client-cli/cli_lib/node.py b/client-cli/cli_lib/node.py index 5304bc6b..98203240 100644 --- a/client-cli/cli_lib/node.py +++ b/client-cli/cli_lib/node.py @@ -20,11 +20,17 @@ # ############################################################################### -import click import requests import cli_lib.ansiprint as ansiprint +def debug_output(config, request_uri, response): + if config['debug']: + import click.echo + 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} @@ -56,10 +62,7 @@ def node_coordinator_state(config, node, action): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -85,10 +88,7 @@ def node_domain_state(config, node, action, wait): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -110,10 +110,7 @@ def node_info(config, node): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -141,10 +138,7 @@ def node_list(config, limit): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() diff --git a/client-cli/cli_lib/provisioner.py b/client-cli/cli_lib/provisioner.py index 30251178..9a63efa4 100644 --- a/client-cli/cli_lib/provisioner.py +++ b/client-cli/cli_lib/provisioner.py @@ -23,11 +23,17 @@ import time import re import subprocess -import click import requests import cli_lib.ansiprint as ansiprint +def debug_output(config, request_uri, response): + if config['debug']: + import click.echo + 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} @@ -56,10 +62,7 @@ def template_info(config, template, template_type): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -87,10 +90,7 @@ def template_list(config, limit, template_type=None): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -111,10 +111,7 @@ def template_add(config, params, template_type=None): params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -136,10 +133,7 @@ def template_remove(config, name, template_type=None): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -162,10 +156,7 @@ def template_element_add(config, name, element_id, params, element_type=None, te params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -187,10 +178,7 @@ def template_element_remove(config, name, element_id, element_type=None, templat request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -212,10 +200,7 @@ def userdata_info(config, userdata): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json()[0] @@ -240,10 +225,7 @@ def userdata_list(config, limit): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -264,10 +246,7 @@ def userdata_add(config, params): params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -290,10 +269,7 @@ def userdata_modify(config, name, params): params=params ) - if config['debug']: - print('API endpoint: PUT {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -315,10 +291,7 @@ def userdata_remove(config, name): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -340,10 +313,7 @@ def script_info(config, script): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json()[0] @@ -368,10 +338,7 @@ def script_list(config, limit): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -392,10 +359,7 @@ def script_add(config, params): params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -418,10 +382,7 @@ def script_modify(config, name, params): params=params ) - if config['debug']: - print('API endpoint: PUT {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -443,10 +404,7 @@ def script_remove(config, name): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -468,10 +426,7 @@ def profile_info(config, profile): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json()[0] @@ -496,10 +451,7 @@ def profile_list(config, limit): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -520,10 +472,7 @@ def profile_add(config, params): params=params ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -546,10 +495,7 @@ def profile_modify(config, name, params): params=params ) - if config['debug']: - print('API endpoint: PUT {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -571,10 +517,7 @@ def profile_remove(config, name): request_uri ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True @@ -600,10 +543,7 @@ def vm_create(config, name, profile): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 202: retvalue = True @@ -627,10 +567,7 @@ def task_status(config, task_id): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retvalue = True diff --git a/client-cli/cli_lib/vm.py b/client-cli/cli_lib/vm.py index 5f4994f5..c881462f 100644 --- a/client-cli/cli_lib/vm.py +++ b/client-cli/cli_lib/vm.py @@ -23,7 +23,6 @@ import time import re import subprocess -import click import requests from collections import deque @@ -31,6 +30,13 @@ from collections import deque import cli_lib.ansiprint as ansiprint import cli_lib.ceph as ceph +def debug_output(config, request_uri, response): + if config['debug']: + import click.echo + 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} @@ -59,10 +65,7 @@ def vm_info(config, vm): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -91,10 +94,7 @@ def vm_list(config, limit, target_node, target_state): params=params ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: return True, response.json() @@ -121,10 +121,7 @@ def vm_define(config, xml, node, node_limit, node_selector, node_autostart): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -150,10 +147,7 @@ def vm_modify(config, vm, xml, restart): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -177,10 +171,7 @@ def vm_metadata(config, vm, node_limit, node_selector, node_autostart): request_uri ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) metadata = response.json() @@ -208,10 +199,7 @@ def vm_metadata(config, vm, node_limit, node_selector, node_autostart): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -236,10 +224,7 @@ def vm_remove(config, vm, delete_disks=False): } ) - if config['debug']: - print('API endpoint: DELETE {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -264,10 +249,7 @@ def vm_state(config, vm, target_state): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -294,10 +276,7 @@ def vm_node(config, vm, target_node, action, force=False): } ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -319,10 +298,7 @@ def vm_locks(config, vm): request_uri ) - if config['debug']: - print('API endpoint: POST {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) if response.status_code == 200: retstatus = True @@ -345,10 +321,7 @@ def view_console_log(config, vm, lines=100): params={'lines': lines} ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) console_log = response.json()['data'] @@ -382,10 +355,7 @@ def follow_console_log(config, vm, lines=10): params={'lines': lines} ) - if config['debug']: - print('API endpoint: GET {}'.format(request_uri)) - print('Response code: {}'.format(response.status_code)) - print('Response headers: {}'.format(response.headers)) + debug_output(config, request_uri, response) console_log = response.json()['data'] @@ -409,17 +379,7 @@ def follow_console_log(config, vm, lines=10): params={'lines': lines} ) - if config['debug']: - print( - 'Response code: {}'.format( - response.status_code - ) - ) - print( - 'Response headers: {}'.format( - response.headers - ) - ) + debug_output(config, request_uri, response) new_console_log = response.json()['data'] # Split the new and old log strings into constitutent lines