Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions databricks_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
20 changes: 20 additions & 0 deletions databricks_cli/configure/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions databricks_cli/profiles/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
37 changes: 37 additions & 0 deletions databricks_cli/profiles/cli.py
Original file line number Diff line number Diff line change
@@ -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')
22 changes: 22 additions & 0 deletions tests/profiles/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
60 changes: 60 additions & 0 deletions tests/profiles/test_cli.py
Original file line number Diff line number Diff line change
@@ -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