Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML-30728] Added CLI support for Delta Sharing Models #662

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
176 changes: 176 additions & 0 deletions databricks_cli/unity_catalog/delta_sharing_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=too-many-lines

import functools
from json import loads as json_loads

Expand Down Expand Up @@ -359,6 +361,16 @@ def shared_table_object(name=None, comment=None, shared_as=None,
val['start_version'] = start_version
return val

def shared_model_object(name=None, comment=None):
val = {
'data_object_type': 'MODEL'
}
if name is not None:
val['name'] = name
if comment is not None:
val['comment'] = comment
return val

def create_common_shared_table_options(f):
@click.option('--table', default=None,
help='Full name of the shared table.')
Expand All @@ -377,6 +389,16 @@ def wrapper(*args, **kwargs):
f(*args, **kwargs)
return wrapper

def create_common_shared_model_options(f):
@click.option('--model', default=None,
help='Full name of the shared model.')
@click.option('--comment', default=None,
help='New comment of the shared model.')
@functools.wraps(f)
def wrapper(*args, **kwargs):
f(*args, **kwargs)
return wrapper


@click.command(context_settings=CONTEXT_SETTINGS,
short_help='Add a shared table.')
Expand Down Expand Up @@ -547,6 +569,157 @@ def api_call(d):
click.echo(mc_pretty_format(share_json))


@click.command(context_settings=CONTEXT_SETTINGS,
short_help='Add a shared model.')
@click.option('--share', required=True,
help='Name of the share to update.')
@create_common_shared_model_options
@click.option('--json-file', default=None, type=click.Path(),
help="Adds a shared model based on shared data object represented in JSON file.")
@click.option('--json', default=None, type=JsonClickType(),
help="Adds a shared model based on shared data object represented in JSON.")
@debug_option
@profile_option
@eat_exceptions
@provide_api_client
def add_share_model_cli(api_client, share, model, comment, json_file, json):
"""
Adds a shared model.

The public specification for the JSON request is in development.
"""
if (json_file is not None) or (json is not None):
def api_call(d):
if 'data_object_type' in d and d['data_object_type'] != "MODEL":
jinzhang21 marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError('Must specify data_object_type as "MODEL" '
'or not specify data_object_type at all')
UnityCatalogApi(api_client).update_share(share, {
'updates': [
{
'action': 'REMOVE',
'data_object': d,
}
]
})
json_cli_base(json_file, json, api_call)
else:
if model is None:
raise ValueError('Must specify model name when adding shared table')
data = {
'updates': [
{
'action': 'ADD',
'data_object': shared_model_object(
name=model,
comment=comment,
)
}
]
}
share_json = UnityCatalogApi(api_client).update_share(share, data)
click.echo(mc_pretty_format(share_json))


@click.command(context_settings=CONTEXT_SETTINGS,
short_help='Update a shared model.')
@click.option('--share', required=True,
help='Name of the share to update.')
@create_common_shared_model_options
@click.option('--json-file', default=None, type=click.Path(),
help="Updates the shared model to shared data object represented in JSON file.")
@click.option('--json', default=None, type=JsonClickType(),
help="Updates the shared model to shared data object represented in JSON.")
@debug_option
@profile_option
@eat_exceptions
@provide_api_client
def update_share_model_cli(api_client, share, model, comment, json_file, json):
"""
Updates a shared model.

The public specification for the JSON request is in development.
"""
if (json_file is not None) or (json is not None):
def api_call(d):
if 'data_object_type' in d and d['data_object_type'] != "MODEL":
jinzhang21 marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError('Must specify data_object_type as "MODEL" '
'or not specify data_object_type at all')
UnityCatalogApi(api_client).update_share(share, {
'updates': [
{
'action': 'UPDATE',
'data_object': d,
}
]
})
json_cli_base(json_file, json, api_call)
else:
if model is None:
raise ValueError('Must specify model name when updating shared model')
data = {
'updates': [
{
'action': 'UPDATE',
'data_object': shared_model_object(
name=model,
comment=comment,
)
}
]
}
share_json = UnityCatalogApi(api_client).update_share(share, data)
click.echo(mc_pretty_format(share_json))


@click.command(context_settings=CONTEXT_SETTINGS,
short_help='Remove a shared model.')
@click.option('--share', required=True,
help='Name of the share to update.')
@click.option('--model', required=True,
help='Full name of the model to remove from share.')
@click.option('--json-file', default=None, type=click.Path(),
help="Removes the shared model based on shared data object represented in JSON file.")
@click.option('--json', default=None, type=JsonClickType(),
help="Removes the shared model based on shared data object represented in JSON.")
@debug_option
@profile_option
@eat_exceptions
@provide_api_client
def remove_share_model_cli(api_client, share, model, json_file, json):
"""
Removes a shared model either by model name or the shared-as model name.

The public specification for the JSON request is in development.
"""
if (json_file is not None) or (json is not None):
def api_call(d):
if 'data_object_type' in d and d['data_object_type'] != "MODEL":
jinzhang21 marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError('Must specify data_object_type as "MODEL" '
'or not specify data_object_type at all')
UnityCatalogApi(api_client).update_share(share, {
'updates': [
{
'action': 'REMOVE',
'data_object': d,
}
]
})
json_cli_base(json_file, json, api_call)
else:
data = {
'updates': [
{
'action': 'REMOVE',
'data_object': shared_model_object(
name=model,
)
}
]
}
share_json = UnityCatalogApi(api_client).update_share(share, data)
click.echo(mc_pretty_format(share_json))


@click.command(context_settings=CONTEXT_SETTINGS,
short_help='Delete a share.')
@click.option('--name', required=True,
Expand Down Expand Up @@ -909,6 +1082,9 @@ def register_shares_commands(cmd_group):
shares_group.add_command(add_share_table_cli, name='add-table')
shares_group.add_command(update_share_table_cli, name='update-table')
shares_group.add_command(remove_share_table_cli, name='remove-table')
shares_group.add_command(add_share_model_cli, name='add-model')
shares_group.add_command(update_share_model_cli, name='update-model')
shares_group.add_command(remove_share_model_cli, name='remove-model')
shares_group.add_command(delete_share_cli, name='delete')
shares_group.add_command(list_share_permissions_cli, name='list-permissions')
shares_group.add_command(update_share_permissions_cli, name='update-permissions')
Expand Down
80 changes: 80 additions & 0 deletions tests/unity_catalog/test_delta_sharing_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,86 @@ def test_remove_share_table_cli_asserts_error_if_both_specified(api_mock):
)
assert not api_mock.update_share.called


@provide_conf
def test_add_share_model_cli(api_mock, echo_mock):
api_mock.update_share.return_value = SHARE
runner = CliRunner()
runner.invoke(
delta_sharing_cli.add_share_model_cli,
args=[
'--share', SHARE_NAME,
'--model', 'add.model',
'--comment', 'add.comment'
])
expected_data = {
'updates': [
{
'action': 'ADD',
'data_object': {
'data_object_type': 'MODEL',
'name': 'add.model',
'comment': 'add.comment'
}
}
]
}
api_mock.update_share.assert_called_once_with(SHARE_NAME, expected_data)
echo_mock.assert_called_once_with(mc_pretty_format(SHARE))


@provide_conf
def test_update_share_model_cli(api_mock, echo_mock):
api_mock.update_share.return_value = SHARE
runner = CliRunner()
runner.invoke(
delta_sharing_cli.update_share_model_cli,
args=[
'--share', SHARE_NAME,
'--model', 'update.model',
'--comment', 'update.comment'
])
expected_data = {
'updates': [
{
'action': 'UPDATE',
'data_object': {
'data_object_type': 'MODEL',
'name': 'update.model',
'comment': 'update.comment'
}
}
]
}
api_mock.update_share.assert_called_once_with(SHARE_NAME, expected_data)
echo_mock.assert_called_once_with(mc_pretty_format(SHARE))


@provide_conf
def test_remove_share_model_cli_by_model(api_mock, echo_mock):
api_mock.update_share.return_value = SHARE
runner = CliRunner()
runner.invoke(
delta_sharing_cli.remove_share_model_cli,
args=[
'--share', SHARE_NAME,
'--model', 'remove.model',
])
expected_data = {
'updates': [
{
'action': 'REMOVE',
'data_object': {
'data_object_type': 'MODEL',
'name': 'remove.model',
}
}
]
}
api_mock.update_share.assert_called_once_with(SHARE_NAME, expected_data)
echo_mock.assert_called_once_with(mc_pretty_format(SHARE))


@provide_conf
def test_update_share_cli_with_json(api_mock, echo_mock):
api_mock.update_share.return_value = SHARE
Expand Down
Loading