From b57bed379a379aedb7321bf151cdca8104fc5b61 Mon Sep 17 00:00:00 2001 From: thegreatmoonmoon Date: Sun, 1 Mar 2020 01:09:00 +0100 Subject: [PATCH] introduce new profiles command group * add new function to get all profiles from config * add and register new cmd group profiles * add new test module for the cmd group --- databricks_cli/cli.py | 3 ++ databricks_cli/configure/provider.py | 20 ++++++++++ databricks_cli/profiles/__init__.py | 22 ++++++++++ databricks_cli/profiles/cli.py | 37 +++++++++++++++++ tests/profiles/__init__.py | 22 ++++++++++ tests/profiles/test_cli.py | 60 ++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+) create mode 100644 databricks_cli/profiles/__init__.py create mode 100644 databricks_cli/profiles/cli.py create mode 100644 tests/profiles/__init__.py create mode 100644 tests/profiles/test_cli.py diff --git a/databricks_cli/cli.py b/databricks_cli/cli.py index 47bd2824..b28b3b44 100644 --- a/databricks_cli/cli.py +++ b/databricks_cli/cli.py @@ -37,6 +37,7 @@ from databricks_cli.stack.cli import stack_group from databricks_cli.groups.cli import groups_group from databricks_cli.instance_pools.cli import instance_pools_group +from databricks_cli.profiles.cli import profiles_group @click.group(context_settings=CONTEXT_SETTINGS) @@ -59,6 +60,8 @@ def cli(): cli.add_command(stack_group, name='stack') cli.add_command(groups_group, name='groups') cli.add_command(instance_pools_group, name="instance-pools") +cli.add_command(profiles_group, name="profiles") + if __name__ == "__main__": cli() diff --git a/databricks_cli/configure/provider.py b/databricks_cli/configure/provider.py index d8b96aae..ee33fb18 100644 --- a/databricks_cli/configure/provider.py +++ b/databricks_cli/configure/provider.py @@ -82,6 +82,26 @@ def _overwrite_config(raw_config): os.chmod(config_path, 0o600) +def get_all_profiles(): + """ + Returns a list of all the profiles present in the databricks config file together with + the hosts and usernames the profiles are configured with. + + :return: list + """ + config = _fetch_from_fs() + ret = [] + if config[DEFAULT_SECTION]: + ret.append([DEFAULT_SECTION, + _get_option_if_exists(config, DEFAULT_SECTION, HOST), + _get_option_if_exists(config, DEFAULT_SECTION, USERNAME)]) + for section in config.sections(): + ret.append([section, + _get_option_if_exists(config, section, HOST), + _get_option_if_exists(config, section, USERNAME)]) + return ret + + def update_and_persist_config(profile, databricks_config): """ Takes a DatabricksConfig and adds the in memory contents to the persisted version of the diff --git a/databricks_cli/profiles/__init__.py b/databricks_cli/profiles/__init__.py new file mode 100644 index 00000000..b0c9feac --- /dev/null +++ b/databricks_cli/profiles/__init__.py @@ -0,0 +1,22 @@ +# Databricks CLI +# Copyright 2017 Databricks, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"), except +# that the use of services to which certain application programming +# interfaces (each, an "API") connect requires that the user first obtain +# a license for the use of the APIs from Databricks, Inc. ("Databricks"), +# by creating an account at www.databricks.com and agreeing to either (a) +# the Community Edition Terms of Service, (b) the Databricks Terms of +# Service, or (c) another written agreement between Licensee and Databricks +# for the use of the APIs. +# +# You may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/databricks_cli/profiles/cli.py b/databricks_cli/profiles/cli.py new file mode 100644 index 00000000..9fb7f059 --- /dev/null +++ b/databricks_cli/profiles/cli.py @@ -0,0 +1,37 @@ +import click +from tabulate import tabulate + + +from databricks_cli.configure.provider import get_all_profiles +from databricks_cli.utils import CONTEXT_SETTINGS +from databricks_cli.version import print_version_callback, version + + +@click.command(context_settings=CONTEXT_SETTINGS, + short_help='Lists all profiles, hosts, and usernames present in the config file.') +@click.option('--quiet', '-q', show_default=True, is_flag=True, default=False, + help='Only show profile names.') +def list_cli(quiet): + """ + Lists all profiles, hosts, and usernames present in the config file. + """ + profiles = get_all_profiles() + if quiet: + for profile in profiles: + click.echo(profile[0]) + else: + click.echo(tabulate(profiles, tablefmt='plain')) + + +@click.group(context_settings=CONTEXT_SETTINGS, + short_help='Utility to to get info on CLI profiles.') +@click.option('--version', '-v', is_flag=True, callback=print_version_callback, + expose_value=False, is_eager=True, help=version) +def profiles_group(): + """ + Utility to to get info on CLI profiles. + """ + pass + + +profiles_group.add_command(list_cli, name='list') diff --git a/tests/profiles/__init__.py b/tests/profiles/__init__.py new file mode 100644 index 00000000..b0c9feac --- /dev/null +++ b/tests/profiles/__init__.py @@ -0,0 +1,22 @@ +# Databricks CLI +# Copyright 2017 Databricks, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"), except +# that the use of services to which certain application programming +# interfaces (each, an "API") connect requires that the user first obtain +# a license for the use of the APIs from Databricks, Inc. ("Databricks"), +# by creating an account at www.databricks.com and agreeing to either (a) +# the Community Edition Terms of Service, (b) the Databricks Terms of +# Service, or (c) another written agreement between Licensee and Databricks +# for the use of the APIs. +# +# You may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/tests/profiles/test_cli.py b/tests/profiles/test_cli.py new file mode 100644 index 00000000..90ded614 --- /dev/null +++ b/tests/profiles/test_cli.py @@ -0,0 +1,60 @@ +# Databricks CLI +# Copyright 2017 Databricks, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"), except +# that the use of services to which certain application programming +# interfaces (each, an "API") connect requires that the user first obtain +# a license for the use of the APIs from Databricks, Inc. ("Databricks"), +# by creating an account at www.databricks.com and agreeing to either (a) +# the Community Edition Terms of Service, (b) the Databricks Terms of +# Service, or (c) another written agreement between Licensee and Databricks +# for the use of the APIs. +# +# You may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# pylint:disable=protected-access + +from click.testing import CliRunner +from tabulate import tabulate + +import databricks_cli.profiles.cli as cli +from tests.utils import provide_conf + + +LIST_RETURN = [ + ['DEFAULT', 'test-host'], + ['test-profile', 'test-host-2'], + [''], +] + +QUIET_LIST_RETURN = [ + 'DEFAULT', + 'test-profile', + '', +] + + +@provide_conf +def test_profiles_list(): + runner = CliRunner() + stdout = runner.invoke(cli.list_cli).stdout + stdout_lines = stdout.split('\n') + expected = tabulate(LIST_RETURN, tablefmt='plain').split('\n') + assert stdout_lines == expected + + +@provide_conf +def test_profiles_list_quiet(): + runner = CliRunner() + stdout = runner.invoke(cli.list_cli, ['--quiet']).stdout + stdout_lines = stdout.split('\n') + assert stdout_lines == QUIET_LIST_RETURN