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

Solving issue with worksheets for catalog Dbs #1079

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ShareItemActions,
ShareItemHealthStatus,
)
from dataall.modules.dataset_sharing.aws.glue_client import GlueClient
from dataall.modules.dataset_sharing.db.share_object_models import ShareObjectItem
from dataall.modules.dataset_sharing.db.share_object_repositories import (
ShareObjectRepository,
Expand Down Expand Up @@ -136,7 +137,7 @@ def add_shared_item(uri: str, data: dict = None):
item_uri = data.get("itemUri")
share = ShareObjectRepository.get_share_by_uri(session, uri)
dataset: Dataset = DatasetRepository.get_dataset_by_uri(session, share.datasetUri)
target_environment = EnvironmentService.get_environment_by_uri(session, dataset.environmentUri)
target_environment = EnvironmentService.get_environment_by_uri(session, share.environmentUri)

share_sm = ShareObjectSM(share.status)
new_share_state = share_sm.run_transition(ShareItemActions.AddItem.value)
Expand Down Expand Up @@ -173,7 +174,7 @@ def add_shared_item(uri: str, data: dict = None):
itemName=item.name,
status=ShareItemStatus.PendingApproval.value,
owner=context.username,
GlueDatabaseName=dataset.GlueDatabaseName if item_type == ShareableType.Table.value else "",
GlueDatabaseName=ShareItemService._get_glue_database_for_share(dataset.GlueDatabaseName, dataset.AwsAccountId, dataset.region) if item_type == ShareableType.Table.value else "",
GlueTableName=item.GlueTableName if item_type == ShareableType.Table.value else "",
S3AccessPointName=s3_access_point_name if item_type == ShareableType.StorageLocation.value else "",
)
Expand Down Expand Up @@ -225,3 +226,20 @@ def list_shareable_objects(share, filter, is_revokable=False):
@has_resource_permission(LIST_ENVIRONMENT_SHARED_WITH_OBJECTS)
def paginated_shared_with_environment_datasets(session, uri, data) -> dict:
return ShareObjectRepository.paginate_shared_datasets(session, uri, data)

@staticmethod
def _get_glue_database_for_share(glueDatabase, account_id, region):
# Check if a catalog account exists and return database accordingly
try:
catalog_dict = GlueClient(
account_id=account_id,
region=region,
database=glueDatabase,
).get_source_catalog()

if catalog_dict is not None:
return catalog_dict.get('database_name')
else:
return glueDatabase
except Exception as e:
raise e
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ShareItemSM,
)
from dataall.modules.dataset_sharing.services.share_exceptions import ShareItemsFound
from dataall.modules.dataset_sharing.services.share_item_service import ShareItemService
from dataall.modules.dataset_sharing.services.share_notification_service import ShareNotificationService
from dataall.modules.dataset_sharing.services.managed_share_policy_service import SharePolicyService
from dataall.modules.dataset_sharing.services.share_permissions import (
Expand Down Expand Up @@ -167,7 +168,7 @@ def create_share_object(
itemName=item.name,
status=ShareItemStatus.PendingApproval.value,
owner=context.username,
GlueDatabaseName=dataset.GlueDatabaseName if item_type == ShareableType.Table.value else "",
GlueDatabaseName=ShareItemService._get_glue_database_for_share(dataset.GlueDatabaseName, dataset.AwsAccountId, dataset.region) if item_type == ShareableType.Table.value else "",
GlueTableName=item.GlueTableName if item_type == ShareableType.Table.value else "",
S3AccessPointName=s3_access_point_name
if item_type == ShareableType.StorageLocation.value
Expand Down Expand Up @@ -411,14 +412,18 @@ def resolve_share_object_consumption_data(uri, datasetUri, principalId, environm
datasetUri + '-' + principalId,
max_length=50, lowercase=True, regex_pattern='[^a-zA-Z0-9-]', separator='-'
)
old_shared_db_name = f"{dataset.GlueDatabaseName}_shared_{uri}"[:254]
# Check if the share was made with a Glue Database
datasetGlueDatabase = ShareItemService._get_glue_database_for_share(dataset.GlueDatabaseName,
dataset.AwsAccountId,
dataset.region)
old_shared_db_name = f"{datasetGlueDatabase}_shared_{uri}"[:254]
database = GlueClient(
account_id=environment.AwsAccountId,
region=environment.region,
database=old_shared_db_name
).get_glue_database()
warn('old_shared_db_name will be deprecated in v2.6.0', DeprecationWarning, stacklevel=2)
sharedGlueDatabase = old_shared_db_name if database else f"{dataset.GlueDatabaseName}_shared"
sharedGlueDatabase = old_shared_db_name if database else f"{datasetGlueDatabase}_shared"
return {
's3AccessPointName': S3AccessPointName,
'sharedGlueDatabase': sharedGlueDatabase,
Expand Down
11 changes: 8 additions & 3 deletions tests/modules/datasets/test_share.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import typing
from unittest.mock import MagicMock

import pytest

Expand All @@ -12,6 +13,11 @@
from dataall.modules.dataset_sharing.db.share_object_repositories import ShareObjectRepository, ShareItemSM, ShareObjectSM
from dataall.modules.datasets_base.db.dataset_models import DatasetTable, Dataset

@pytest.fixture(scope='function')
def mock_glue_client(mocker):
glue_client = MagicMock()
mocker.patch('dataall.modules.dataset_sharing.services.share_item_service.GlueClient', return_value=glue_client)
glue_client.get_source_catalog.return_value = None

def random_table_name():
def cpltz(l):
Expand Down Expand Up @@ -1063,7 +1069,7 @@ def test_create_share_object_as_approver_and_requester(mocker, client, user, gro
assert create_share_object_response.data.createShareObject.userRoleForShareObject == 'ApproversAndRequesters'
assert create_share_object_response.data.createShareObject.requestPurpose == 'testShare'

def test_create_share_object_with_item_authorized(mocker, client, user2, group2, env2group, env2, dataset1, table1):
def test_create_share_object_with_item_authorized(mocker, client, user2, group2, env2group, env2, dataset1, table1, mock_glue_client):
# Given
# Existing dataset, table, target environment and group
# SharePolicy exists and is attached
Expand Down Expand Up @@ -1368,8 +1374,7 @@ def test_list_shares_from_me_requester(


def test_add_share_item(
client, user2, group2, share1_draft,

client, user2, group2, share1_draft, mock_glue_client
):
# Given
# Existing share object in status Draft (-> fixture share1_draft)
Expand Down
Loading