Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Shchederkin committed Dec 16, 2024
1 parent 1e22396 commit 7de3333
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
10 changes: 8 additions & 2 deletions aws-lambda/src/databricks_cdk/resources/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
create_or_update_secret_scope,
delete_secret_scope,
)
from databricks_cdk.resources.service_principals.service_principal import (
ServicePrincipalProperties,
create_or_update_service_principal,
delete_service_principal,
)
from databricks_cdk.resources.sql_warehouses.sql_warehouses import (
SQLWarehouseProperties,
create_or_update_warehouse,
Expand Down Expand Up @@ -125,7 +130,6 @@
delete_storage_credential,
)
from databricks_cdk.resources.unity_catalog.volumes import VolumeProperties, create_or_update_volume, delete_volume
from databricks_cdk.resources.service_principals.service_principal import create_or_update_service_principal, delete_service_principal, ServicePrincipalProperties
from databricks_cdk.utils import CnfResponse

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -329,7 +333,9 @@ def delete_resource(event: DatabricksEvent) -> CnfResponse:
elif action == "volume":
return delete_volume(VolumeProperties(**event.ResourceProperties), event.PhysicalResourceId)
elif action == "service-principal":
return delete_service_principal(ServicePrincipalProperties(**event.ResourceProperties), event.PhysicalResourceId)
return delete_service_principal(
ServicePrincipalProperties(**event.ResourceProperties), event.PhysicalResourceId
)
else:
raise RuntimeError(f"Unknown action: {action}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ def get_service_principal(physical_resource_id: str, workspace_client: Workspace
service_principal = workspace_client.service_principals.get(id=physical_resource_id)
except NotFound:
raise ServicePrincipalNotFoundError(f"Service principal with id {physical_resource_id} not found")

return service_principal


def create_service_principal(service_principal: ServicePrincipal, workspace_client: WorkspaceClient) -> ServicePrincipalResponse:
def create_service_principal(
service_principal: ServicePrincipal, workspace_client: WorkspaceClient
) -> ServicePrincipalResponse:
"""Create service principal on databricks"""

created_service_principal = workspace_client.service_principals.create(
Expand All @@ -74,7 +76,9 @@ def create_service_principal(service_principal: ServicePrincipal, workspace_clie
if created_service_principal.id is None:
raise ServicePrincipalCreationError("Service principal creation failed, there was no id found")

return ServicePrincipalResponse(name=created_service_principal.display_name, physical_resource_id=created_service_principal.id)
return ServicePrincipalResponse(
name=created_service_principal.display_name, physical_resource_id=created_service_principal.id
)


def update_service_principal(
Expand Down
2 changes: 2 additions & 0 deletions aws-lambda/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from databricks.sdk import AccountClient, CredentialsAPI, ExperimentsAPI, ModelRegistryAPI, VolumesAPI, WorkspaceClient
from databricks.sdk.service.iam import ServicePrincipalsAPI


@pytest.fixture(scope="function", autouse=True)
Expand All @@ -24,6 +25,7 @@ def workspace_client():
workspace_client.model_registry = MagicMock(spec=ModelRegistryAPI)
workspace_client.experiments = MagicMock(spec=ExperimentsAPI)
workspace_client.volumes = MagicMock(spec=VolumesAPI)
workspace_client.service_principals = MagicMock(spec=ServicePrincipalsAPI)

return workspace_client

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from unittest.mock import patch

import pytest

from databricks.sdk.service.iam import ServicePrincipal, ComplexValue
from databricks.sdk.errors import NotFound
from databricks.sdk.service.iam import ComplexValue, ServicePrincipal

from databricks_cdk.resources.service_principals.service_principal import (
ServicePrincipalCreationError,
ServicePrincipalNotFoundError,
ServicePrincipalProperties,
ServicePrincipalResponse,
ServicePrincipalNotFoundError,
create_or_update_service_principal,
create_service_principal,
delete_service_principal,
update_service_principal,
get_service_principal,
update_service_principal,
)
from databricks_cdk.utils import CnfResponse

Expand All @@ -24,10 +24,10 @@
@patch("databricks_cdk.resources.service_principals.service_principal.get_service_principal")
def test_create_or_update_service_principal_create(
patched_get_service_principal,
patched_create_service_principal,
patched_update_service_principal,
patched_get_workspace_client,
workspace_client
patched_create_service_principal,
patched_update_service_principal,
patched_get_workspace_client,
workspace_client,
):
patched_get_workspace_client.return_value = workspace_client
mock_properties = ServicePrincipalProperties(
Expand All @@ -42,7 +42,7 @@ def test_create_or_update_service_principal_create(

patched_create_service_principal.assert_called_once_with(
ServicePrincipal(
active=True,
active=True,
display_name="mock_name",
id=None,
application_id=None,
Expand All @@ -51,8 +51,8 @@ def test_create_or_update_service_principal_create(
groups=None,
roles=None,
schemas=None,
),
workspace_client
),
workspace_client,
)
patched_get_service_principal.assert_not_called()
patched_update_service_principal.assert_not_called()
Expand All @@ -64,10 +64,10 @@ def test_create_or_update_service_principal_create(
@patch("databricks_cdk.resources.service_principals.service_principal.get_service_principal")
def test_create_or_update_service_principal_update(
patched_get_service_principal,
patched_create_service_principal,
patched_update_service_principal,
patched_get_workspace_client,
workspace_client
patched_create_service_principal,
patched_update_service_principal,
patched_get_workspace_client,
workspace_client,
):
patched_get_workspace_client.return_value = workspace_client
mock_physical_resource_id = "some_id"
Expand Down Expand Up @@ -101,8 +101,8 @@ def test_create_or_update_service_principal_update(
external_id=None,
groups=None,
schemas=None,
),
workspace_client
),
workspace_client,
)
patched_create_service_principal.assert_not_called()

Expand Down

0 comments on commit 7de3333

Please sign in to comment.