Skip to content

Commit

Permalink
Merge branch 'master' of github.com:softlayer/softlayer-python
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Aug 18, 2015
2 parents 57fdef8 + ff5808d commit fc2f83c
Show file tree
Hide file tree
Showing 102 changed files with 374 additions and 357 deletions.
2 changes: 1 addition & 1 deletion SoftLayer/CLI/call_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def cli(env, service, method, parameters, _id, mask, limit, offset):
mask=mask,
limit=limit,
offset=offset)
return formatting.iter_to_table(result)
env.fout(formatting.iter_to_table(result))
2 changes: 1 addition & 1 deletion SoftLayer/CLI/cdn/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def cli(env, account_id):
table.add_row(['notes',
account.get('cdnAccountNote', formatting.blank())])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/cdn/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def cli(env, sortby):
])

table.sortby = sortby
return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/cdn/origin_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def cli(env, account_id):
origin.get('cname', formatting.blank()),
origin['originUrl']])

return table
env.fout(table)
58 changes: 58 additions & 0 deletions SoftLayer/CLI/columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
SoftLayer.CLI.columns
~~~~~~~~~~~~~~~~~~~~~~
Utilties for user-defined columns
:license: MIT, see LICENSE for more details.
"""
import click

from SoftLayer import utils

# pylint: disable=unused-argument


class ColumnFormatter(object):
"""Maps each column using a function"""
def __init__(self):
self.columns = []
self.column_funcs = []

def add_column(self, name, func):
"""Add a new column along with a formatting function."""
self.columns.append(name)
self.column_funcs.append(func)

def row(self, data):
"""Return a formatted row for the given data."""
for column in self.column_funcs:
if callable(column):
yield column(data)
else:
yield utils.lookup(data, *column)


def get_formatter(column_map):
"""This function returns a callback to use with click options.
The retuend function parses a comma-separated value and returns a new
ColumnFormatter.
:param column_map: a dictionary of the form: {col_name: function}.
"""

def validate(ctx, param, value):
"""Click validation function."""
try:
formatter = ColumnFormatter()
for column in [col.strip() for col in value.split(',')]:
if column in column_map:
formatter.add_column(column, column_map[column])
else:
formatter.add_column(column, (column,))

return formatter
except ValueError:
raise click.BadParameter('rolls need to be in format NdM')

return validate
14 changes: 12 additions & 2 deletions SoftLayer/CLI/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
from SoftLayer.CLI import formatting


def _resolve_transport(transport):
"""recursively look for transports which refer to other transports."""
nested_transport = getattr(transport, 'transport', None)
if nested_transport is not None:
return nested_transport

return _resolve_transport(nested_transport)


def get_settings_from_client(client):
"""Pull out settings from a SoftLayer.BaseClient instance.
Expand All @@ -21,9 +30,10 @@ def get_settings_from_client(client):
except AttributeError:
pass

transport = _resolve_transport(client.transport)
try:
settings['timeout'] = client.transport.transport.timeout
settings['endpoint_url'] = client.transport.transport.endpoint_url
settings['timeout'] = transport.timeout
settings['endpoint_url'] = transport.endpoint_url
except AttributeError:
pass

Expand Down
2 changes: 1 addition & 1 deletion SoftLayer/CLI/config/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def cli(env):
finally:
config_fd.close()

return "Configuration Updated Successfully"
env.fout("Configuration Updated Successfully")


def get_user_input(env):
Expand Down
2 changes: 1 addition & 1 deletion SoftLayer/CLI/config/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def cli(env):
"""Show current configuration."""

settings = config.get_settings_from_client(env.client)
return config.config_table(settings)
env.fout(config.config_table(settings))
18 changes: 6 additions & 12 deletions SoftLayer/CLI/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
# pylint: disable=too-many-public-methods, broad-except, unused-argument
# pylint: disable=redefined-builtin, super-init-not-called

# Disable the cyclic import error. This is handled by an inline import.
# pylint: disable=cyclic-import

DEBUG_LOGGING_MAP = {
0: logging.CRITICAL,
1: logging.WARNING,
Expand Down Expand Up @@ -147,24 +144,21 @@ def cli(env,
proxy=proxy,
config_file=config,
)

client.transport = SoftLayer.TimingTransport(client.transport)
env.client = client

env.vars['timings'] = SoftLayer.TimingTransport(env.client.transport)
env.client.transport = env.vars['timings']


@cli.resultcallback()
@environment.pass_env
def output_result(env, result, timings=False, **kwargs):
def output_result(env, timings=False, *args, **kwargs):
"""Outputs the results returned by the CLI and also outputs timings."""

output = env.fmt(result)
if output:
env.out(output)

if timings:
if timings and env.vars.get('timings'):
timing_table = formatting.Table(['service', 'method', 'time'])

calls = env.client.transport.get_last_calls()
calls = env.vars['timings'].get_last_calls()
for call, _, duration in calls:
timing_table.add_row([call.service, call.method, duration])

Expand Down
2 changes: 1 addition & 1 deletion SoftLayer/CLI/dns/record_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ def cli(env, zone, data, record, ttl, type):
record['data']
])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/dns/zone_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def cli(env):
zone['updateDate'],
])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/dns/zone_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def cli(env, zone):

manager = SoftLayer.DNSManager(env.client)
zone_id = helpers.resolve_id(manager.resolve_ids, zone, name='zone')
return manager.dump_zone(zone_id)
env.fout(manager.dump_zone(zone_id))
5 changes: 5 additions & 0 deletions SoftLayer/CLI/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def fmt(self, output):
"""Format output based on current the environment format."""
return formatting.format_output(output, fmt=self.format)

def fout(self, output, newline=True):
"""Format the input and output to the console (stdout)."""
if output is not None:
self.out(self.fmt(output), newline=newline)

def input(self, prompt, default=None, show_default=True):
"""Provide a command prompt."""
return click.prompt(prompt, default=default, show_default=show_default)
Expand Down
5 changes: 3 additions & 2 deletions SoftLayer/CLI/firewall/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def cli(env, target, firewall_type, high_availability):
pkg = mgr.get_standard_package(target, is_virt=False)

if not pkg:
return "Unable to add firewall - Is network public enabled?"
exceptions.CLIAbort(
"Unable to add firewall - Is network public enabled?")

env.out("******************")
env.out("Product: %s" % pkg[0]['description'])
Expand All @@ -54,4 +55,4 @@ def cli(env, target, firewall_type, high_availability):
elif firewall_type == 'server':
mgr.add_standard_firewall(target, is_virt=False)

return "Firewall is being created!"
env.fout("Firewall is being created!")
2 changes: 1 addition & 1 deletion SoftLayer/CLI/firewall/cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def cli(env, identifier):
else:
raise exceptions.CLIAbort('Unknown firewall type: %s' % firewall_type)

return 'Firewall with id %s is being cancelled!' % identifier
env.fout('Firewall with id %s is being cancelled!' % identifier)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/firewall/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def cli(env, identifier):
else:
rules = mgr.get_standard_fwl_rules(firewall_id)

return get_rules_table(rules)
env.fout(get_rules_table(rules))


def get_rules_table(rules):
Expand Down
2 changes: 1 addition & 1 deletion SoftLayer/CLI/firewall/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ def cli(env, identifier):
raise exceptions.CLIAbort('Aborted.')
else:
raise exceptions.CLIAbort('Aborted.')
return 'Firewall updated!'
env.fout('Firewall updated!')
else:
raise exceptions.CLIAbort('Aborted.')
2 changes: 1 addition & 1 deletion SoftLayer/CLI/firewall/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def cli(env):
'hardwareId')
])

return table
env.fout(table)


def has_firewall_component(server):
Expand Down
2 changes: 1 addition & 1 deletion SoftLayer/CLI/globalip/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def cli(env, ipv6, test):
table.add_row([price['item']['description'], rate])

table.add_row(['Total monthly cost', "%.2f" % total])
return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/globalip/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ def cli(env, ip_version):
ip_address['ipAddress']['ipAddress'],
assigned,
target])
return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/image/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ def cli(env, identifier):
table.add_row(['datacenters', formatting.listing(sorted(datacenters),
separator=',')])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/image/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ def cli(env, name, note, osrefcode, uri):
table.add_row(['created', result['createDate']])
table.add_row(['guid', result['globalIdentifier']])
output.append(table)
return output
env.fout(output)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/image/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ def cli(env, public):
image.get('accountId', formatting.blank()),
])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/iscsi/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ def cli(env, identifier, password):
pass_table.add_row([result['username'], result['password']])
table.add_row(['users', pass_table])

return table
env.fout(table)
3 changes: 2 additions & 1 deletion SoftLayer/CLI/iscsi/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ def cli(env):
iscsi.get('password', formatting.blank()),
iscsi.get('serviceResourceBackendIpAddress',
formatting.blank())])
return table

env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def cli(env, identifier):
raise exceptions.CLIAbort('Aborted.')

mgr.cancel_lb(loadbal_id)
return 'Load Balancer with id %s is being cancelled!' % identifier
env.fout('Load Balancer with id %s is being cancelled!' % identifier)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def cli(env, billing_id, datacenter):
"account. Continue?"):
raise exceptions.CLIAbort('Aborted.')
mgr.add_local_lb(billing_id, datacenter=datacenter)
return "Load balancer is being created!"
env.fout("Load balancer is being created!")
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/create_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def cli(env):
'%.2f' % float(package['prices'][0]['recurringFee'])
])

return table
env.fout(table)
3 changes: 2 additions & 1 deletion SoftLayer/CLI/loadbal/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ def cli(env, identifier):
table.add_row([' Services', table3])
else:
table.add_row([' Services', 'None'])
return table

env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/group_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def cli(env, identifier, allocation, port, routing_type, routing_method):
routing_type=routing_type,
routing_method=routing_method)

return 'Load balancer service group is being added!'
env.fout('Load balancer service group is being added!')
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/group_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def cli(env, identifier):
raise exceptions.CLIAbort('Aborted.')

mgr.delete_service_group(group_id)
return 'Service group %s is being deleted!' % identifier
env.fout('Service group %s is being deleted!' % identifier)
6 changes: 4 additions & 2 deletions SoftLayer/CLI/loadbal/group_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import loadbal

import click
Expand All @@ -29,7 +30,8 @@ def cli(env, identifier, allocation, port, routing_type, routing_method):

# check if any input is provided
if not any([allocation, port, routing_type, routing_method]):
return 'At least one property is required to be changed!'
raise exceptions.CLIAbort(
'At least one property is required to be changed!')

mgr.edit_service_group(loadbal_id,
group_id,
Expand All @@ -38,4 +40,4 @@ def cli(env, identifier, allocation, port, routing_type, routing_method):
routing_type=routing_type,
routing_method=routing_method)

return 'Load balancer service group %s is being updated!' % identifier
env.fout('Load balancer service group %s is being updated!' % identifier)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/group_reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def cli(env, identifier):
loadbal_id, group_id = loadbal.parse_id(identifier)

mgr.reset_service_group(loadbal_id, group_id)
return 'Load balancer service group connections are being reset!'
env.fout('Load balancer service group connections are being reset!')
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/health_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def cli(env):
for hc_type in hc_types:
table.add_row([hc_type['id'], hc_type['name']])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ def cli(env):
lb_type
])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/routing_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def cli(env):
for routing_method in routing_methods:
table.add_row([routing_method['id'], routing_method['name']])

return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/routing_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def cli(env):
table.sortby = 'ID'
for routing_method in routing_methods:
table.add_row([routing_method['id'], routing_method['name']])
return table
env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/service_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ def cli(env, identifier, enabled, port, weight, healthcheck_type, ip_address):
port=port,
weight=weight,
hc_type=healthcheck_type)
return 'Load balancer service is being added!'
env.fout('Load balancer service is being added!')
2 changes: 1 addition & 1 deletion SoftLayer/CLI/loadbal/service_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def cli(env, identifier):
raise exceptions.CLIAbort('Aborted.')

mgr.delete_service(service_id)
return 'Load balancer service %s is being cancelled!' % service_id
env.fout('Load balancer service %s is being cancelled!' % service_id)
Loading

0 comments on commit fc2f83c

Please sign in to comment.