From c139c1ca47a1ba29c00c9fb09b0f8bd45d17676c Mon Sep 17 00:00:00 2001 From: Morgan Du Date: Thu, 15 Jul 2021 17:39:26 -0700 Subject: [PATCH] addressing feedbacks --- google/cloud/aiplatform/__init__.py | 6 +- .../cloud/aiplatform/tensorboard/__init__.py | 6 +- .../aiplatform/tensorboard/tensorboard.py | 356 ++++++------------ tests/unit/aiplatform/test_tensorboard.py | 18 +- 4 files changed, 130 insertions(+), 256 deletions(-) diff --git a/google/cloud/aiplatform/__init__.py b/google/cloud/aiplatform/__init__.py index 1a273827fc..17c6952f35 100644 --- a/google/cloud/aiplatform/__init__.py +++ b/google/cloud/aiplatform/__init__.py @@ -36,10 +36,7 @@ CustomJob, HyperparameterTuningJob, ) -from google.cloud.aiplatform.tensorboard import ( - Tensorboard, - TensorBoardUploader, -) +from google.cloud.aiplatform.tensorboard import Tensorboard from google.cloud.aiplatform.training_jobs import ( CustomTrainingJob, CustomContainerTrainingJob, @@ -96,5 +93,4 @@ "TimeSeriesDataset", "VideoDataset", "Tensorboard", - "TensorBoardUploader", ) diff --git a/google/cloud/aiplatform/tensorboard/__init__.py b/google/cloud/aiplatform/tensorboard/__init__.py index d171256dd6..93c48cd46c 100644 --- a/google/cloud/aiplatform/tensorboard/__init__.py +++ b/google/cloud/aiplatform/tensorboard/__init__.py @@ -16,10 +16,6 @@ # from google.cloud.aiplatform.tensorboard.tensorboard import Tensorboard -from google.cloud.aiplatform.tensorboard.uploader import TensorBoardUploader -__all__ = ( - "Tensorboard", - "TensorBoardUploader", -) +__all__ = ("Tensorboard",) diff --git a/google/cloud/aiplatform/tensorboard/tensorboard.py b/google/cloud/aiplatform/tensorboard/tensorboard.py index 368b6d59db..b65636c849 100644 --- a/google/cloud/aiplatform/tensorboard/tensorboard.py +++ b/google/cloud/aiplatform/tensorboard/tensorboard.py @@ -17,7 +17,6 @@ from typing import Optional, Sequence, Dict, Tuple -from google.api_core import operation from google.auth import credentials as auth_credentials from google.cloud.aiplatform import base @@ -25,12 +24,8 @@ from google.cloud.aiplatform import initializer from google.cloud.aiplatform import utils -from google.cloud.aiplatform.compat.services import tensorboard_service_client_v1beta1 -from google.cloud.aiplatform.compat.types import ( - tensorboard_v1beta1 as gca_tensorboard, - encryption_spec_v1beta1 as gca_encryption_spec, -) +from google.cloud.aiplatform.compat.types import tensorboard_v1beta1 as gca_tensorboard from google.protobuf import field_mask_pb2 @@ -47,8 +42,6 @@ class Tensorboard(base.VertexAiResourceNounWithFutureManager): _list_method = "list_tensorboards" _delete_method = "delete_tensorboard" - _supported_metadata_schema_uris: Tuple[str] = () - def __init__( self, tensorboard_name: str, @@ -58,21 +51,21 @@ def __init__( ): """Retrieves an existing managed tensorboard given a tensorboard name or ID. - Args: - tensorboard_name (str): - Required. A fully-qualified tensorboard resource name or tensorboard ID. - Example: "projects/123/locations/us-central1/tensorboards/456" or - "456" when project and location are initialized or passed. - project (str): - Optional project to retrieve tensorboard from. If not set, project - set in aiplatform.init will be used. - location (str): - Optional location to retrieve tensorboard from. If not set, location - set in aiplatform.init will be used. - credentials (auth_credentials.Credentials): - Custom credentials to use to retreive this Tensorboard. Overrides - credentials set in aiplatform.init. - """ + Args: + tensorboard_name (str): + Required. A fully-qualified tensorboard resource name or tensorboard ID. + Example: "projects/123/locations/us-central1/tensorboards/456" or + "456" when project and location are initialized or passed. + project (str): + Optional project to retrieve tensorboard from. If not set, project + set in aiplatform.init will be used. + location (str): + Optional location to retrieve tensorboard from. If not set, location + set in aiplatform.init will be used. + credentials (auth_credentials.Credentials): + Optional custom credentials to use to retreive this Tensorboard. Overrides + credentials set in aiplatform.init. + """ super().__init__( project=project, @@ -87,7 +80,7 @@ def create( cls, display_name: str, description: Optional[str] = None, - labels: Optional[Dict] = None, + labels: Optional[Dict[str, str]] = None, project: Optional[str] = None, location: Optional[str] = None, credentials: Optional[auth_credentials.Credentials] = None, @@ -96,134 +89,74 @@ def create( ) -> "Tensorboard": """Creates a new tensorboard. - Args: - display_name (str): - Required. The user-defined name of the Tensorboard. - The name can be up to 128 characters long and can be consist - of any UTF-8 characters. - description (str): - Description of this Tensorboard. - labels (str): - The labels with user-defined metadata to organize your Tensorboards. - Label keys and values can be no longer than 64 characters - (Unicode codepoints), can only contain lowercase letters, numeric - characters, underscores and dashes. International characters are allowed. - No more than 64 user labels can be associated with one Tensorboard - (System labels are excluded). - See https://goo.gl/xmQnxf for more information and examples of labels. - System reserved label keys are prefixed with "aiplatform.googleapis.com/" - and are immutable. - project (str): - Project to upload this model to. Overrides project set in - aiplatform.init. - location (str): - Location to upload this model to. Overrides location set in - aiplatform.init. - credentials (auth_credentials.Credentials): - Custom credentials to use to upload this model. Overrides - credentials set in aiplatform.init. - request_metadata (Sequence[Tuple[str, str]]): - Strings which should be sent along with the request as metadata. - encryption_spec_key_name (str): - The Cloud KMS resource identifier of the customer - managed encryption key used to protect the tensorboard. Has the - form: - ``projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key``. - The key needs to be in the same region as where the compute - resource is created. - - If set, this Tensorboard and all sub-resources of this Tensorboard will be secured by this key. - - Overrides encryption_spec_key_name set in aiplatform.init. - - Returns: - tensorboard (Tensorboard): - Instantiated representation of the managed tensorboard resource. - """ + Example Usage: + + tb = aiplatform.Tensorboard.create( + display_name='my display name', + description='my description', + labels={ + 'key1': 'value1', + 'key2': 'value2' + } + ) + + Args: + display_name (str): + Required. The user-defined name of the Tensorboard. + The name can be up to 128 characters long and can be consist + of any UTF-8 characters. + description (str): + Optional description of this Tensorboard. + labels (Dict[str, str]): + Optional labels with user-defined metadata to organize your Tensorboards. + Label keys and values can be no longer than 64 characters + (Unicode codepoints), can only contain lowercase letters, numeric + characters, underscores and dashes. International characters are allowed. + No more than 64 user labels can be associated with one Tensorboard + (System labels are excluded). + See https://goo.gl/xmQnxf for more information and examples of labels. + System reserved label keys are prefixed with "aiplatform.googleapis.com/" + and are immutable. + project (str): + Optional project to upload this model to. Overrides project set in + aiplatform.init. + location (str): + Optional location to upload this model to. Overrides location set in + aiplatform.init. + credentials (auth_credentials.Credentials): + Optional custom credentials to use to upload this model. Overrides + credentials set in aiplatform.init. + request_metadata (Sequence[Tuple[str, str]]): + Optional strings which should be sent along with the request as metadata. + encryption_spec_key_name (str): + Optional Cloud KMS resource identifier of the customer + managed encryption key used to protect the tensorboard. Has the + form: + ``projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key``. + The key needs to be in the same region as where the compute + resource is created. + + If set, this Tensorboard and all sub-resources of this Tensorboard will be secured by this key. + + Overrides encryption_spec_key_name set in aiplatform.init. + + Returns: + tensorboard (Tensorboard): + Instantiated representation of the managed tensorboard resource. + """ utils.validate_display_name(display_name) api_client = cls._instantiate_client(location=location, credentials=credentials) - return cls._create( - api_client=api_client, - parent=initializer.global_config.common_location_path( - project=project, location=location - ), - project=project or initializer.global_config.project, - location=location or initializer.global_config.location, - display_name=display_name, - description=description, - labels=labels, - request_metadata=request_metadata, - encryption_spec=initializer.global_config.get_encryption_spec( - encryption_spec_key_name=encryption_spec_key_name, - select_version=compat.V1BETA1, - ), + parent = initializer.global_config.common_location_path( + project=project, location=location ) - @classmethod - def _create( - cls, - api_client: tensorboard_service_client_v1beta1.TensorboardServiceClient, - parent: str, - project: str, - location: str, - display_name: str, - description: Optional[str] = None, - labels: Optional[Dict] = None, - request_metadata: Optional[Sequence[Tuple[str, str]]] = (), - credentials: Optional[auth_credentials.Credentials] = None, - encryption_spec: Optional[gca_encryption_spec.EncryptionSpec] = None, - ) -> operation.Operation: - """Creates a new managed tensorboard by directly calling API client. - - Args: - api_client (tensorboard_service_client.TensorboardServiceClient): - Required. An instance of TensorboardServiceClient with the correct api_endpoint - already set based on user's preferences. - parent (str): - Required. Also known as common location path, that usually contains the - project and location that the user provided to the upstream method. - Example: "projects/my-prj/locations/us-central1" - project (str): - Required. Project to retrieve endpoint from. If not set, project - set in aiplatform.init will be used. - location (str): - Required. Location to retrieve endpoint from. If not set, location - set in aiplatform.init will be used. - display_name (str): - Required. The user-defined name of the Tensorboard. - The name can be up to 128 characters long and can be consist - of any UTF-8 characters. - description (str): - Description of this Tensorboard. - labels (str): - The labels with user-defined metadata to organize your Tensorboards. - Label keys and values can be no longer than 64 characters - (Unicode codepoints), can only contain lowercase letters, numeric - characters, underscores and dashes. International characters are allowed. - No more than 64 user labels can be associated with one Tensorboard - (System labels are excluded). - See https://goo.gl/xmQnxf for more information and examples of labels. - System reserved label keys are prefixed with "aiplatform.googleapis.com/" - and are immutable. - request_metadata (Sequence[Tuple[str, str]]): - Strings which should be sent along with the create_tensorboard - request as metadata. Usually to specify special tensorboard config. - credentials (auth_credentials.Credentials): - Custom credentials to use to upload this model. Overrides - credentials set in aiplatform.init. - encryption_spec (gca_encryption_spec.EncryptionSpec): - The Cloud KMS customer managed encryption key used to protect the tensorboard. - The key needs to be in the same region as where the compute - resource is created. - - If set, this Tensorboard and all sub-resources of this Tensorboard will be secured by this key. - Returns: - operation (Operation): - An object representing a long-running operation. - """ + encryption_spec = initializer.global_config.get_encryption_spec( + encryption_spec_key_name=encryption_spec_key_name, + select_version=compat.V1BETA1, + ) gapic_tensorboard = gca_tensorboard.Tensorboard( display_name=display_name, @@ -244,8 +177,8 @@ def _create( return cls( tensorboard_name=created_tensorboard.name, - project=project, - location=location, + project=project or initializer.global_config.project, + location=location or initializer.global_config.location, credentials=credentials, ) @@ -253,47 +186,55 @@ def update( self, display_name: Optional[str] = None, description: Optional[str] = None, - labels: Optional[Dict] = None, + labels: Optional[Dict[str, str]] = None, request_metadata: Optional[Sequence[Tuple[str, str]]] = (), encryption_spec_key_name: Optional[str] = None, ) -> "Tensorboard": """Updates an existing tensorboard. - Args: - display_name (str): - The user-defined name of the Tensorboard. - The name can be up to 128 characters long and can be consist - of any UTF-8 characters. - description (str): - Description of this Tensorboard. - labels (Dict): - The labels with user-defined metadata to organize your Tensorboards. - Label keys and values can be no longer than 64 characters - (Unicode codepoints), can only contain lowercase letters, numeric - characters, underscores and dashes. International characters are allowed. - No more than 64 user labels can be associated with one Tensorboard - (System labels are excluded). - See https://goo.gl/xmQnxf for more information and examples of labels. - System reserved label keys are prefixed with "aiplatform.googleapis.com/" - and are immutable. - request_metadata (Sequence[Tuple[str, str]]): - Strings which should be sent along with the request as metadata. - encryption_spec_key_name (str): - The Cloud KMS resource identifier of the customer - managed encryption key used to protect the tensorboard. Has the - form: - ``projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key``. - The key needs to be in the same region as where the compute - resource is created. - - If set, this Tensorboard and all sub-resources of this Tensorboard will be secured by this key. - - Overrides encryption_spec_key_name set in aiplatform.init. - - Returns: - tensorboard (Tensorboard): - The managed tensorboard resource. - """ + Example Usage: + + tb = aiplatform.Tensorboard(tensorboard_name='123456') + tb.update( + display_name='update my display name', + description='update my description', + ) + + Args: + display_name (str): + Optional user-defined name of the Tensorboard. + The name can be up to 128 characters long and can be consist + of any UTF-8 characters. + description (str): + Optional description of this Tensorboard. + labels (Dict[str, str]): + Optional labels with user-defined metadata to organize your Tensorboards. + Label keys and values can be no longer than 64 characters + (Unicode codepoints), can only contain lowercase letters, numeric + characters, underscores and dashes. International characters are allowed. + No more than 64 user labels can be associated with one Tensorboard + (System labels are excluded). + See https://goo.gl/xmQnxf for more information and examples of labels. + System reserved label keys are prefixed with "aiplatform.googleapis.com/" + and are immutable. + request_metadata (Sequence[Tuple[str, str]]): + Optional strings which should be sent along with the request as metadata. + encryption_spec_key_name (str): + Optional Cloud KMS resource identifier of the customer + managed encryption key used to protect the tensorboard. Has the + form: + ``projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key``. + The key needs to be in the same region as where the compute + resource is created. + + If set, this Tensorboard and all sub-resources of this Tensorboard will be secured by this key. + + Overrides encryption_spec_key_name set in aiplatform.init. + + Returns: + tensorboard (Tensorboard): + The managed tensorboard resource. + """ update_mask = list() if display_name: @@ -314,57 +255,10 @@ def update( ) update_mask.append("encryption_spec") - return self._update( - display_name=display_name, - description=description, - labels=labels, - encryption_spec=encryption_spec, - update_mask=update_mask, - request_metadata=request_metadata, - ) - - def _update( - self, - update_mask: Sequence[str] = (), - request_metadata: Sequence[Tuple[str, str]] = (), - display_name: Optional[str] = None, - description: Optional[str] = None, - labels: Optional[Dict] = None, - encryption_spec: Optional[gca_encryption_spec.EncryptionSpec] = None, - ) -> operation.Operation: - """Updates a managed tensorboard by directly calling API client. - - Args: - update_mask (Sequence[str]): - Field mask is used to specify the fields to be overwritten in the - Tensorboard resource by the update. - The fields specified in the update_mask are relative to the resource, not - the full request. A field will be overwritten if it is in the mask. If the - user does not provide a mask then all fields will be overwritten if new - values are specified. - request_metadata (Sequence[Tuple[str, str]]): - Strings which should be sent along with the create_tensorboard - request as metadata. Usually to specify special tensorboard config. - display_name (str): - The user-defined name of the Tensorboard. - The name can be up to 128 characters long and can be consist - of any UTF-8 characters. - description (str): - Description of this Tensorboard. - encryption_spec (gca_encryption_spec.EncryptionSpec): - The Cloud KMS customer managed encryption key used to protect the tensorboard. - The key needs to be in the same region as where the compute - resource is created. - - If set, this Tensorboard and all sub-resources of this Tensorboard will be secured by this key. - Returns: - operation (Operation): - An object representing a long-running operation. - """ - update_mask = field_mask_pb2.FieldMask(paths=update_mask) gapic_tensorboard = gca_tensorboard.Tensorboard( + name=self.resource_name, display_name=display_name, description=description, labels=labels, diff --git a/tests/unit/aiplatform/test_tensorboard.py b/tests/unit/aiplatform/test_tensorboard.py index a08df3b382..60f85683df 100644 --- a/tests/unit/aiplatform/test_tensorboard.py +++ b/tests/unit/aiplatform/test_tensorboard.py @@ -20,8 +20,8 @@ import pytest from unittest import mock -from importlib import reload from unittest.mock import patch +from importlib import reload from google.api_core import operation from google.auth.exceptions import GoogleAuthError @@ -89,17 +89,6 @@ def get_tensorboard_mock(): yield get_tensorboard_mock -@pytest.fixture -def get_tensorboard_without_name_mock(): - with patch.object( - tensorboard_service_client.TensorboardServiceClient, "get_tensorboard" - ) as get_tensorboard_mock: - get_tensorboard_mock.return_value = gca_tensorboard.Tensorboard( - display_name=_TEST_DISPLAY_NAME, encryption_spec=_TEST_ENCRYPTION_SPEC, - ) - yield get_tensorboard_mock - - @pytest.fixture def create_tensorboard_mock(): with patch.object( @@ -195,7 +184,6 @@ def test_init_tensorboard_with_project_and_alt_location(self): location=_TEST_ALT_LOCATION, ) - @pytest.mark.usefixtures("get_tensorboard_without_name_mock") @patch.dict( os.environ, {"GOOGLE_CLOUD_PROJECT": "", "GOOGLE_APPLICATION_CREDENTIALS": ""} ) @@ -277,7 +265,7 @@ def test_update_tensorboard_display_name(self, update_tensorboard_mock): my_tensorboard.update(display_name=_TEST_DISPLAY_NAME_UPDATE) expected_tensorboard = gca_tensorboard.Tensorboard( - display_name=_TEST_DISPLAY_NAME_UPDATE, + name=_TEST_NAME, display_name=_TEST_DISPLAY_NAME_UPDATE, ) update_tensorboard_mock.assert_called_once_with( update_mask=field_mask_pb2.FieldMask(paths=["display_name"]), @@ -293,7 +281,7 @@ def test_update_tensorboard_encryption_spec(self, update_tensorboard_mock): my_tensorboard.update(encryption_spec_key_name=_TEST_ENCRYPTION_KEY_NAME) expected_tensorboard = gca_tensorboard.Tensorboard( - encryption_spec=_TEST_ENCRYPTION_SPEC, + name=_TEST_NAME, encryption_spec=_TEST_ENCRYPTION_SPEC, ) update_tensorboard_mock.assert_called_once_with( update_mask=field_mask_pb2.FieldMask(paths=["encryption_spec"]),