Example #1
0
def bind_client(req, resp, kwargs):
    req.env['sl_timehook_start_time'] = time.time()
    endpoint = cfg.CONF['softlayer']['endpoint']
    client = SoftLayer.TimedClient(endpoint_url=endpoint,
                                   proxy=cfg.CONF['softlayer']['proxy'])
    client.auth = None
    req.sl_client = client

    auth_token = req.env.get('auth', None)

    if auth_token is not None:
        client.auth = auth.get_auth(auth_token)
Example #2
0
def main(args=sys.argv[1:], env=environment.Environment()):
    """Entry point for the command-line client."""
    # Parse Top-Level Arguments
    exit_status = 0
    resolver = CommandParser(env)
    try:
        command, command_args = resolver.parse(args)

        # Set logging level
        debug_level = command_args.get('--debug')
        if debug_level:
            logger = logging.getLogger()
            handler = logging.StreamHandler()
            logger.addHandler(handler)
            logger.setLevel(DEBUG_LOGGING_MAP.get(debug_level, logging.DEBUG))

        kwargs = {
            'proxy': command_args.get('--proxy'),
            'config_file': command_args.get('--config')
        }
        if command_args.get('--timings'):
            client = SoftLayer.TimedClient(**kwargs)
        else:
            client = SoftLayer.Client(**kwargs)

        # Do the thing
        runnable = command(client=client, env=env)
        data = runnable.execute(command_args)
        if data:
            out_format = command_args.get('--format', 'table')
            if out_format not in VALID_FORMATS:
                raise exceptions.ArgumentError('Invalid format "%s"'
                                               % out_format)
            output = formatting.format_output(data, fmt=out_format)
            if output:
                env.out(output)

        if command_args.get('--timings'):
            out_format = command_args.get('--format', 'table')
            api_calls = client.get_last_calls()
            timing_table = formatting.KeyValueTable(['call', 'time'])

            for call, _, duration in api_calls:
                timing_table.add_row([call, duration])

            env.err(formatting.format_output(timing_table, fmt=out_format))

    except exceptions.InvalidCommand as ex:
        env.err(resolver.get_module_help(ex.module_name))
        if ex.command_name:
            env.err('')
            env.err(str(ex))
            exit_status = 1
    except exceptions.InvalidModule as ex:
        env.err(resolver.get_main_help())
        if ex.module_name:
            env.err('')
            env.err(str(ex))
        exit_status = 1
    except docopt.DocoptExit as ex:
        env.err(ex.usage)
        env.err(
            '\nUnknown argument(s), use -h or --help for available options')
        exit_status = 127
    except KeyboardInterrupt:
        env.out('')
        exit_status = 1
    except exceptions.CLIAbort as ex:
        env.err(str(ex.message))
        exit_status = ex.code
    except SystemExit as ex:
        exit_status = ex.code
    except SoftLayer.SoftLayerAPIError as ex:
        if 'invalid api token' in ex.faultString.lower():
            env.out("Authentication Failed: To update your credentials, use "
                    "'sl config setup'")
        else:
            env.err(str(ex))
            exit_status = 1
    except SoftLayer.SoftLayerError as ex:
        env.err(str(ex))
        exit_status = 1
    except Exception:
        import traceback
        env.err("An unexpected error has occured:")
        env.err(traceback.format_exc())
        env.err("Feel free to report this error as it is likely a bug:")
        env.err("    https://github.com/softlayer/softlayer-python/issues")
        exit_status = 1

    sys.exit(exit_status)
Example #3
0
 def set_up(self):
     self.client = SoftLayer.TimedClient(username='******',
                                         api_key='issurelywrong',
                                         endpoint_url="ENDPOINT")