From f2f5d5fe86f0b67f4d8753241d24d62d379a683b Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Wed, 6 Jul 2016 18:48:49 -0700 Subject: [PATCH] Add --dashboard Opens the dashboard URL specified in `SUPERNOVA_DASHBOARD_URL` in a web browser. e.g.: If you have in your config: [dfw] SUPERNOVA_DASHBOARD_URL=https://dfw.dashboard.rackspacecloud.com/ ... snip ... Then: $ supernova --dashboard dfw would open https://dfw.dashboard.rackspacecloud.com/ in a web browser. --- .supernova | 1 + supernova/executable.py | 21 ++++++++++++++++++++- tests/test_executable.py | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/.supernova b/.supernova index b307541..e4440c4 100644 --- a/.supernova +++ b/.supernova @@ -1,5 +1,6 @@ # This is an example config for use with tests [dfw] +SUPERNOVA_DASHBOARD_URL=https://dfw.dashboard.rackspacecloud.com/ SUPERNOVA_GROUP=raxusa OS_AUTH_URL=https://identity.api.rackspacecloud.com/v2.0/ OS_AUTH_SYSTEM=rackspace diff --git a/supernova/executable.py b/supernova/executable.py index 8cb4a9d..0bc0c4f 100644 --- a/supernova/executable.py +++ b/supernova/executable.py @@ -18,6 +18,7 @@ to run """ import sys +import webbrowser import click @@ -93,6 +94,8 @@ def print_env_short_list(ctx, param, value): help="Display the least amount of output possible") @click.option('--echo', '-e', default=None, is_flag=True, help="Print the specified environment and exit") +@click.option('--dashboard', '-D', default=None, is_flag=True, + help="Open dashboard in browser for specified environment") @click.argument('environment', nargs=1) @click.argument('command', nargs=-1) @click.version_option() @@ -104,7 +107,7 @@ def print_env_short_list(ctx, param, value): help="List all configured environments in shorter format") @click.pass_context def run_supernova(ctx, executable, debug, quiet, environment, command, conf, - echo): + echo, dashboard): """ You can use supernova with many OpenStack clients and avoid the pain of managing multiple sets of environment variables. Getting started is easy @@ -154,6 +157,7 @@ def run_supernova(ctx, executable, debug, quiet, environment, command, conf, 'executable': executable, 'quiet': quiet, 'echo': echo, + 'dashboard': dashboard, } # If the user specified a single environment, we need to verify that the @@ -176,6 +180,21 @@ def run_supernova(ctx, executable, debug, quiet, environment, command, conf, click.echo('{0}={1}'.format(k, env[k])) ctx.exit(0) + if supernova_args['dashboard']: + if len(envs) > 1: + msg = ("\nCan't open dashboard for a group of environments.\n" + "Specify a single environment when using --dashboard.") + click.echo(msg) + ctx.exit(1) + url = nova_creds[envs[0]].get('SUPERNOVA_DASHBOARD_URL') + if url is None: + msg = ("\nNo SUPERNOVA_DASHBOARD_URL specified " + "for environment: %s" % envs[0]) + click.echo(msg) + ctx.exit(1) + webbrowser.open(url) + ctx.exit(0) + if len(command) == 0: msg = ("\nMissing arguments to pass to executable Run supernova " "--help for examples.\n".format(envs[0])) diff --git a/tests/test_executable.py b/tests/test_executable.py index 5c99739..0fe3e3a 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -1,3 +1,5 @@ +import webbrowser + from click.testing import CliRunner @@ -111,6 +113,29 @@ def test_broken_configuration_file(self): assert result.exit_code != 0 assert "There's an error in your configuration file" in result.output + def test_dashboard(self, monkeypatch): + def mockreturn(url): + return False + monkeypatch.setattr(webbrowser, "open", mockreturn) + runner = CliRunner() + result = runner.invoke(executable.run_supernova, + ['--dashboard', 'dfw']) + assert result.exit_code == 0 + + def test_dashboard_group(self): + runner = CliRunner() + result = runner.invoke(executable.run_supernova, + ['--dashboard', 'raxusa']) + assert result.exit_code == 1 + assert 'group of environments' in result.output + + def test_dashboard_no_url(self): + runner = CliRunner() + result = runner.invoke(executable.run_supernova, + ['--dashboard', 'hkg']) + assert result.exit_code == 1 + assert 'No SUPERNOVA_DASHBOARD_URL' in result.output + def test_echo(self): runner = CliRunner() result = runner.invoke(executable.run_supernova, ['--echo', 'hkg'])