Skip to content

Commit

Permalink
refactor: move all validate and get resource ids for featurestore, en…
Browse files Browse the repository at this point in the history
…tity_type and feature into featurestore_utils

add: tests for validate and get resource ids
  • Loading branch information
morgandu committed Nov 19, 2021
1 parent c046f84 commit 6aece79
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 99 deletions.
62 changes: 21 additions & 41 deletions google/cloud/aiplatform/_featurestores/entity_type.py
Expand Up @@ -83,27 +83,14 @@ def __init__(
credentials (auth_credentials.Credentials):
Optional. Custom credentials to use to retrieve this EntityType. Overrides
credentials set in aiplatform.init.
Raises:
ValueError if the provided entity_type_name is not in form of a fully-qualified
entityType resource name nor an entity_type ID with featurestore_id passed.
"""

match = featurestore_utils.validate_entity_type_name(entity_type_name)

if match:
self._featurestore_id = match["featurestore_id"]
elif (
featurestore_utils.validate_id(entity_type_name)
and featurestore_id
and featurestore_utils.validate_id(featurestore_id)
):
self._featurestore_id = featurestore_id
else:
raise ValueError(
f"{entity_type_name} is not in form of a fully-qualified entityType resource name "
f"nor an entity_type ID with featurestore_id passed."
)
(
self._featurestore_id,
_,
) = featurestore_utils.validate_and_get_entity_type_resource_ids(
entity_type_name=entity_type_name, featurestore_id=featurestore_id
)

self._resource_noun = featurestore_utils.get_entity_type_resource_noun(
featurestore_id=self._featurestore_id
Expand Down Expand Up @@ -286,22 +273,11 @@ def list(
Returns:
List[EntityTypes] - A list of managed entityType resource objects
Raises:
ValueError if the provided featurestore_name is not in form of a fully-qualified
featurestore resource name nor an featurestore ID.
"""

match = featurestore_utils.validate_featurestore_name(featurestore_name)

if match:
cls._featurestore_id = match["featurestore_id"]
elif featurestore_utils.validate_id(featurestore_name):
cls._featurestore_id = featurestore_name
else:
raise ValueError(
f"{featurestore_name} is not in form of a fully-qualified featurestore resource name nor an featurestore ID."
)
cls._featurestore_id = featurestore_utils.validate_and_get_featurestore_resource_id(
featurestore_name=featurestore_name
)

cls._resource_noun = featurestore_utils.get_entity_type_resource_noun(
featurestore_id=cls._featurestore_id,
Expand Down Expand Up @@ -352,13 +328,17 @@ def list_features(
)

@base.optional_sync()
def delete_features(self, feature_ids: List[str], sync: bool = True,) -> None:
def delete_features(
self, feature_ids: List[str], sync: Optional[bool] = True,
) -> None:
"""Deletes feature resources in this EntityType given their feature IDs.
WARNING: This deletion is permanent.
Args:
feature_ids (List[str]):
Required. The list of feature IDs to be deleted.
sync (bool):
Whether to execute this deletion synchronously. If False, this method
Optional. Whether to execute this deletion synchronously. If False, this method
will be executed in concurrent Future and any downstream object will
be immediately returned and synced when the Future has completed.
"""
Expand All @@ -378,7 +358,7 @@ def create(
labels: Optional[Dict[str, str]] = None,
snapshot_analysis_disabled: bool = True,
monitoring_interval_days: Optional[int] = None,
sync: bool = True,
sync: Optional[bool] = True,
) -> "EntityType":
""""""
raise NotImplementedError
Expand All @@ -391,15 +371,15 @@ def create_feature(
labels: Optional[Dict[str, str]] = None,
snapshot_analysis_disabled: Optional[bool] = True,
monitoring_interval_days: Optional[int] = None,
sync: bool = True,
sync: Optional[bool] = True,
) -> "_featurestores.Feature":
""""""
raise NotImplementedError

def batch_create_features(
self,
feature_configs: List[Dict[str, Union[bool, int, Dict[str, str], str]]],
sync: bool = True,
sync: Optional[bool] = True,
) -> "EntityType":
""""""
raise NotImplementedError
Expand All @@ -420,7 +400,7 @@ def ingest_from_bq(
feature_time: Optional[datetime.datetime] = None,
disable_online_serving: Optional[bool] = False,
worker_count: Optional[int] = 1,
sync: bool = True,
sync: Optional[bool] = True,
) -> "EntityType":
""""""
raise NotImplementedError
Expand All @@ -435,7 +415,7 @@ def ingest_from_gcs(
feature_time: Optional[datetime.datetime] = None,
disable_online_serving: Optional[bool] = False,
worker_count: Optional[int] = 1,
sync: bool = True,
sync: Optional[bool] = True,
) -> "EntityType":
""""""
raise NotImplementedError
Expand All @@ -450,7 +430,7 @@ def ingest_from_df(
feature_time: Optional[datetime.datetime] = None,
disable_online_serving: Optional[bool] = False,
worker_count: Optional[int] = 1,
sync: bool = True,
sync: Optional[bool] = True,
) -> "EntityType":
""""""
raise NotImplementedError
72 changes: 23 additions & 49 deletions google/cloud/aiplatform/_featurestores/feature.py
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
#

from typing import Dict, List, Optional, Sequence, Tuple, Union
from typing import Dict, List, Optional, Sequence, Tuple

from google.auth import credentials as auth_credentials
from google.protobuf import field_mask_pb2
Expand Down Expand Up @@ -83,31 +83,16 @@ def __init__(
credentials (auth_credentials.Credentials):
Optional. Custom credentials to use to retrieve this Feature. Overrides
credentials set in aiplatform.init.
Raises:
ValueError if the provided feature_name is not in form of a fully-qualified
feature resource name nor a feature ID with featurestore_id and entity_type_id passed.
"""

match = featurestore_utils.validate_feature_name(feature_name)

if match:
self._featurestore_id = match["featurestore_id"]
self._entity_type_id = match["entity_type_id"]
elif (
featurestore_utils.validate_id(feature_name)
and featurestore_id
and entity_type_id
and featurestore_utils.validate_id(featurestore_id)
and featurestore_utils.validate_id(entity_type_id)
):
self._featurestore_id = featurestore_id
self._entity_type_id = entity_type_id
else:
raise ValueError(
f"{feature_name} is not in form of a fully-qualified feature resource name "
f"nor a feature ID with featurestore_id and entity_type_id passed."
)
(
self._featurestore_id,
self._entity_type_id,
_,
) = featurestore_utils.validate_and_get_feature_resource_ids(
feature_name=feature_name,
entity_type_id=entity_type_id,
featurestore_id=featurestore_id,
)

self._resource_noun = featurestore_utils.get_feature_resource_noun(
featurestore_id=self._featurestore_id, entity_type_id=self._entity_type_id
Expand Down Expand Up @@ -289,29 +274,13 @@ def list(
Returns:
List[Features] - A list of managed feature resource objects
Raises:
ValueError if the provided entity_type_name is not in form of a fully-qualified
entityType resource name nor an entity_type ID with featurestore_id passed.
"""

match = featurestore_utils.validate_entity_type_name(entity_type_name)

if match:
cls._featurestore_id = match["featurestore_id"]
cls._entity_type_id = match["entity_type_id"]
elif (
featurestore_utils.validate_id(entity_type_name)
and featurestore_id
and featurestore_utils.validate_id(featurestore_id)
):
cls._featurestore_id = featurestore_id
cls._entity_type_id = entity_type_name
else:
raise ValueError(
f"{entity_type_name} is not in form of a fully-qualified entityType resource name "
f"nor an entity_type ID with featurestore_id passed."
)
(
cls._featurestore_id,
cls._entity_type_id,
) = featurestore_utils.validate_and_get_entity_type_resource_ids(
entity_type_name=entity_type_name, featurestore_id=featurestore_id,
)

cls._resource_noun = featurestore_utils.get_feature_resource_noun(
featurestore_id=cls._featurestore_id, entity_type_id=cls._entity_type_id
Expand All @@ -337,12 +306,17 @@ def list(
@classmethod
def create(
cls,
entity_type: Union[_featurestores.EntityType, str],
feature_id: str,
value_type: str,
entity_type_name: str,
featurestore_id: Optional[str] = None,
description: Optional[str] = None,
labels: Optional[Dict[str, str]] = None,
sync: bool = True,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
request_metadata: Optional[Sequence[Tuple[str, str]]] = (),
sync: Optional[bool] = True,
) -> "Feature":
""""""
raise NotImplementedError
18 changes: 10 additions & 8 deletions google/cloud/aiplatform/_featurestores/featurestore.py
Expand Up @@ -109,10 +109,10 @@ def get_entity_type(self, entity_type_id: str) -> "_featurestores.EntityType":
featurestores.EntityType - The managed entityType resource object.
Raises:
ValueError if the provided entity_type_id is not in form of a entity_type ID.
ValueError if the provided entity_type_id is not in form of an entity_type ID.
"""
if not featurestore_utils.validate_id(entity_type_id):
raise ValueError(f"{entity_type_id} is not in form of a entity_type ID.")
raise ValueError(f"{entity_type_id} is not in form of an entity_type ID.")
entity_type_name = self._get_entity_type_name(entity_type_id)
return _featurestores.EntityType(entity_type_name=entity_type_name)

Expand Down Expand Up @@ -304,14 +304,16 @@ def list_entity_types(

@base.optional_sync()
def delete_entity_types(
self, entity_type_ids: List[str], sync: bool = True,
self, entity_type_ids: List[str], sync: Optional[bool] = True,
) -> None:
"""Deletes entity_type resources in this Featurestre given their entity_type IDs.
WARNING: This deletion is permanent.
Args:
entity_type_ids (List[str]):
Required. The list of entity_type IDs to be deleted.
sync (bool):
Whether to execute this deletion synchronously. If False, this method
Optional. Whether to execute this deletion synchronously. If False, this method
will be executed in concurrent Future and any downstream object will
be immediately returned and synced when the Future has completed.
"""
Expand Down Expand Up @@ -472,7 +474,7 @@ def create(
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
encryption_spec_key_name: Optional[str] = None,
sync: bool = True,
sync: Optional[bool] = True,
) -> "Featurestore":
""""""
raise NotImplementedError
Expand All @@ -484,7 +486,7 @@ def create_entity_type(
labels: Optional[Dict[str, str]] = None,
snapshot_analysis_disabled: Optional[bool] = True,
monitoring_interval_days: Optional[int] = None,
sync: bool = True,
sync: Optional[bool] = True,
) -> "_featurestores.EntityType":
""""""
raise NotImplementedError
Expand All @@ -496,7 +498,7 @@ def batch_serve_to_bq(
read_instance: Optional[Union[str, List[str], pd.DataFrame]] = None,
pass_through_field: Optional[List[str]] = None,
bq_destination_output_uri: Optional[str] = None,
sync: bool = True,
sync: Optional[bool] = True,
) -> None:
""""""
raise NotImplementedError
Expand All @@ -509,7 +511,7 @@ def batch_serve_to_gcs(
pass_through_field: Optional[List[str]] = None,
gcs_destination_output_uri_prefix: Optional[str] = None,
gcs_destination_type: Optional[str] = None,
sync: bool = True,
sync: Optional[bool] = True,
) -> None:
""""""
raise NotImplementedError
Expand Down

0 comments on commit 6aece79

Please sign in to comment.