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

Allow disconnecting service-connector from stack component #1864

Merged
merged 6 commits into from
Oct 30, 2023
Merged
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
58 changes: 58 additions & 0 deletions src/zenml/cli/stack_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,55 @@ def connect_stack_component_command(
return connect_stack_component_command


def generate_stack_component_disconnect_command(
component_type: StackComponentType,
) -> Callable[[str], None]:
"""Generates a `disconnect` command for the specific stack component type.

Args:
component_type: Type of the component to generate the command for.

Returns:
A function that can be used as a `click` command.
"""
display_name = _component_display_name(component_type)

@click.argument(
"name_id_or_prefix",
type=str,
required=True,
)
def disconnect_stack_component_command(name_id_or_prefix: str) -> None:
"""Disconnect a stack component from a service connector.

Args:
name_id_or_prefix: The name of the stack component to disconnect.
"""
if component_type == StackComponentType.SECRETS_MANAGER:
warn_deprecated_secrets_manager()

client = Client()

with console.status(
f"Disconnecting service-connector from {display_name} '{name_id_or_prefix}'...\n"
):
try:
updated_component = client.update_stack_component(
name_id_or_prefix=name_id_or_prefix,
component_type=component_type,
disconnect=True,
)
except (KeyError, IllegalOperationError) as err:
cli_utils.error(str(err))

cli_utils.declare(
f"Successfully disconnected the service-connector from {display_name} `{name_id_or_prefix}`."
)
print_model_url(get_component_url(updated_component))

return disconnect_stack_component_command


def register_single_stack_component_cli_commands(
component_type: StackComponentType, parent_group: click.Group
) -> None:
Expand Down Expand Up @@ -1879,6 +1928,15 @@ def command_group() -> None:
help=f"Connect {singular_display_name} to a service connector.",
)(connect_command)

# zenml stack-component connect
disconnect_command = generate_stack_component_disconnect_command(
component_type
)
command_group.command(
"disconnect",
help=f"Disconnect {singular_display_name} from a service connector.",
)(disconnect_command)

# zenml stack-component explain
explain_command = generate_stack_component_explain_command(component_type)
command_group.command(
Expand Down
19 changes: 17 additions & 2 deletions src/zenml/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,7 @@ def update_stack_component(
configuration: Optional[Dict[str, Any]] = None,
labels: Optional[Dict[str, Any]] = None,
is_shared: Optional[bool] = None,
disconnect: Optional[bool] = None,
connector_id: Optional[UUID] = None,
connector_resource_id: Optional[str] = None,
) -> "ComponentResponseModel":
Expand All @@ -2206,6 +2207,8 @@ def update_stack_component(
configuration: The new configuration of the stack component.
labels: The new labels of the stack component.
is_shared: The new shared status of the stack component.
disconnect: Whether to disconnect the stack component from its
service connector.
connector_id: The new connector id of the stack component.
connector_resource_id: The new connector resource id of the
stack component.
Expand Down Expand Up @@ -2291,10 +2294,22 @@ def update_stack_component(
}
update_model.labels = existing_labels

if connector_id is not None:
if disconnect:
update_model.connector = None
update_model.connector_resource_id = None
else:
existing_component = self.get_stack_component(
name_id_or_prefix=name_id_or_prefix,
component_type=component_type,
allow_name_prefix_match=False,
)
update_model.connector = connector_id
if connector_resource_id is not None:
update_model.connector_resource_id = connector_resource_id
if connector_id is None and existing_component.connector:
update_model.connector = existing_component.connector.id
update_model.connector_resource_id = (
existing_component.connector_resource_id
)

# Send the updated component to the ZenStore
return self.zen_store.update_stack_component(
Expand Down
9 changes: 6 additions & 3 deletions src/zenml/zen_stores/sql_zen_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,6 @@ def update_stack_component(

existing_component.update(component_update=component_update)

service_connector: Optional[ServiceConnectorSchema] = None
if component_update.connector:
service_connector = session.exec(
select(ServiceConnectorSchema).where(
Expand All @@ -1551,9 +1550,13 @@ def update_stack_component(
"Service connector with ID "
f"{component_update.connector} not found."
)

if service_connector:
existing_component.connector = service_connector
existing_component.connector_resource_id = (
component_update.connector_resource_id
)
else:
existing_component.connector = None
existing_component.connector_resource_id = None
Comment on lines +1557 to +1559
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, if any value in an update model is None, it means that this value was not provided and should not be updated. So I think the current design is breaking this convention since connector=None now disconnects the existing connector and thereby leads to updates.

Instead, I would suggest to add disconnect as a field of StackComponentUpdateModel explicitly and then use this here instead:

Suggested change
else:
existing_component.connector = None
existing_component.connector_resource_id = None
elif component_update.disconnect:
existing_component.connector = None
existing_component.connector_resource_id = None

Copy link
Contributor

@stefannica stefannica Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as it is, because this immediately fixes the UI as well. The alternative also requires dashboard changes. If it helps, this is the same approach used for service connector updates: some update fields are "special", in the sense that if set to None or omitted from the update request, they are interpreted as a value removal rather than a value omission.


session.add(existing_component)
session.commit()
Expand Down
Loading