Fix #3747 - FQDN model in ometa api (#3748)

Fix #3747 - FQDN model in ometa api (#3748)
This commit is contained in:
Pere Miquel Brull 2022-03-30 08:54:47 +02:00 committed by GitHub
parent b3087d08b9
commit c22381fdc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 23 deletions

View File

@ -13,7 +13,7 @@ from requests.models import Response
from metadata.generated.schema.type import basic
from metadata.generated.schema.type.entityHistory import EntityVersionHistory
from metadata.ingestion.ometa.client import REST
from metadata.ingestion.ometa.utils import uuid_to_str
from metadata.ingestion.ometa.utils import model_str
T = TypeVar("T", bound=BaseModel) # pylint: disable=invalid-name
logger = logging.getLogger(__name__)
@ -68,7 +68,7 @@ class OMetaVersionMixin(Generic[T]):
fields: List
List of fields to return
"""
entity_id = uuid_to_str(entity_id)
entity_id = model_str(entity_id)
version = self.version_to_str(version)
path = f"{entity_id}/versions/{version}"
@ -95,7 +95,7 @@ class OMetaVersionMixin(Generic[T]):
List
lists of available versions for a specific entity
"""
path = f"{uuid_to_str(entity_id)}/versions"
path = f"{model_str(entity_id)}/versions"
resp = self.client.get(f"{self.get_suffix(entity)}/{path}")

View File

@ -45,6 +45,7 @@ from metadata.generated.schema.entity.teams.role import Role
from metadata.generated.schema.entity.teams.team import Team
from metadata.generated.schema.entity.teams.user import User
from metadata.generated.schema.type import basic
from metadata.generated.schema.type.basic import FullyQualifiedEntityName
from metadata.generated.schema.type.entityHistory import EntityVersionHistory
from metadata.generated.schema.type.entityReference import EntityReference
from metadata.ingestion.ometa.auth_provider import AuthenticationProvider
@ -63,7 +64,7 @@ from metadata.ingestion.ometa.openmetadata_rest import (
NoOpAuthenticationProvider,
OktaAuthenticationProvider,
)
from metadata.ingestion.ometa.utils import get_entity_type, uuid_to_str
from metadata.ingestion.ometa.utils import get_entity_type, model_str
logger = logging.getLogger(__name__)
@ -386,13 +387,16 @@ class OpenMetadata(
return entity_class(**resp)
def get_by_name(
self, entity: Type[T], fqdn: str, fields: Optional[List[str]] = None
self,
entity: Type[T],
fqdn: Union[str, FullyQualifiedEntityName],
fields: Optional[List[str]] = None,
) -> Optional[T]:
"""
Return entity by name or None
"""
return self._get(entity=entity, path=f"name/{fqdn}", fields=fields)
return self._get(entity=entity, path=f"name/{model_str(fqdn)}", fields=fields)
def get_by_id(
self,
@ -404,7 +408,7 @@ class OpenMetadata(
Return entity by ID or None
"""
return self._get(entity=entity, path=uuid_to_str(entity_id), fields=fields)
return self._get(entity=entity, path=model_str(entity_id), fields=fields)
def _get(
self, entity: Type[T], path: str, fields: Optional[List[str]] = None
@ -459,7 +463,7 @@ class OpenMetadata(
return EntityReference(
id=instance.id,
type=get_entity_type(entity),
name=instance.fullyQualifiedName,
name=model_str(instance.fullyQualifiedName),
description=instance.description,
href=instance.href,
)
@ -505,7 +509,7 @@ class OpenMetadata(
"""
suffix = self.get_suffix(entity)
path = f"/{uuid_to_str(entity_id)}/versions"
path = f"/{model_str(entity_id)}/versions"
resp = self.client.get(f"{suffix}{path}")
if self._use_raw_data:
@ -538,7 +542,7 @@ class OpenMetadata(
Returns
None
"""
url = f"{self.get_suffix(entity)}/{uuid_to_str(entity_id)}"
url = f"{self.get_suffix(entity)}/{model_str(entity_id)}"
url += f"?recursive=true" if recursive else ""
self.client.delete(url)

View File

@ -14,6 +14,7 @@ Helper functions to handle OpenMetadata Entities' properties
import re
import string
from functools import singledispatch
from typing import Type, TypeVar, Union
from pydantic import BaseModel
@ -54,15 +55,19 @@ def get_entity_type(
return class_name
def uuid_to_str(entity_id: Union[str, basic.Uuid]) -> str:
@singledispatch
def model_str(arg) -> str:
"""
Given an entity_id, that can be a str or our pydantic
definition of Uuid, return a proper str to build
the endpoint path
:param entity_id: Entity ID to onvert to string
:return: str for the ID
Default model stringifying method
"""
if isinstance(entity_id, basic.Uuid):
return str(entity_id.__root__)
return str(arg)
return entity_id
@model_str.register(basic.Uuid)
@model_str.register(basic.FullyQualifiedEntityName)
@model_str.register(basic.EntityName)
def _(arg) -> str:
"""
Models with __root__
"""
return str(arg.__root__)

View File

@ -16,7 +16,7 @@ from unittest import TestCase
from metadata.generated.schema.entity.data.mlmodel import MlModel
from metadata.generated.schema.type import basic
from metadata.ingestion.ometa.utils import format_name, get_entity_type, uuid_to_str
from metadata.ingestion.ometa.utils import format_name, get_entity_type, model_str
class OMetaUtilsTest(TestCase):
@ -37,13 +37,20 @@ class OMetaUtilsTest(TestCase):
self.assertEqual(get_entity_type("hello"), "hello")
self.assertEqual(get_entity_type(MlModel), "mlmodel")
def test_uuid_to_str(self):
def test_model_str(self):
"""
Return Uuid as str
"""
self.assertEqual(uuid_to_str("random"), "random")
self.assertEqual(model_str("random"), "random")
self.assertEqual(
uuid_to_str(basic.Uuid(__root__="9fc58e81-7412-4023-a298-59f2494aab9d")),
model_str(basic.Uuid(__root__="9fc58e81-7412-4023-a298-59f2494aab9d")),
"9fc58e81-7412-4023-a298-59f2494aab9d",
)
self.assertEqual(
model_str(basic.EntityName(__root__="EntityName")), "EntityName"
)
self.assertEqual(
model_str(basic.FullyQualifiedEntityName(__root__="FQDN")), "FQDN"
)