From 0661cd342aadd29fc4b3845a29c7a92fbbdb73ae Mon Sep 17 00:00:00 2001 From: sina chavoshi Date: Tue, 23 Aug 2022 22:44:05 +0000 Subject: [PATCH 1/3] feat: Support ResourceName with Version. --- google/cloud/aiplatform/metadata/resource.py | 2 +- google/cloud/aiplatform/metadata/schema/utils.py | 4 ++-- tests/unit/aiplatform/test_metadata_schema.py | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/google/cloud/aiplatform/metadata/resource.py b/google/cloud/aiplatform/metadata/resource.py index 00133ba789..84fefc2131 100644 --- a/google/cloud/aiplatform/metadata/resource.py +++ b/google/cloud/aiplatform/metadata/resource.py @@ -539,7 +539,7 @@ def _extract_metadata_store_id(resource_name, resource_noun) -> str: pattern = re.compile( r"^projects\/(?P[\w-]+)\/locations\/(?P[\w-]+)\/metadataStores\/(?P[\w-]+)\/" + resource_noun - + r"\/(?P[\w-]+)$" + + r"\/(?P[\w-]+)(?P@[\w-]+)?$" ) match = pattern.match(resource_name) if not match: diff --git a/google/cloud/aiplatform/metadata/schema/utils.py b/google/cloud/aiplatform/metadata/schema/utils.py index c742824b76..50d05595f8 100644 --- a/google/cloud/aiplatform/metadata/schema/utils.py +++ b/google/cloud/aiplatform/metadata/schema/utils.py @@ -159,11 +159,11 @@ def create_uri_from_resource_name(resource_name: str) -> bool: """ # TODO: support nested resource names such as models/123/evaluations/456 match_results = re.match( - r"^projects\/[A-Za-z0-9-]*\/locations\/([A-Za-z0-9-]*)(\/metadataStores\/[A-Za-z0-9-]*)?(\/[A-Za-z0-9-]*\/[A-Za-z0-9-]*)+$", + r"^projects\/(?P[\w-]+)\/locations\/(?P[\w-]+)(\/metadataStores\/(?P[\w-]+))?\/[\w-]+\/(?P[\w-]+)(?P@[\w-]+)?$", resource_name, ) if not match_results: raise ValueError(f"Invalid resource_name format for {resource_name}.") - location = match_results.group(1) + location = match_results["location"] return f"https://{location}-aiplatform.googleapis.com/v1/{resource_name}" diff --git a/tests/unit/aiplatform/test_metadata_schema.py b/tests/unit/aiplatform/test_metadata_schema.py index 65de1304ea..b1094111a9 100644 --- a/tests/unit/aiplatform/test_metadata_schema.py +++ b/tests/unit/aiplatform/test_metadata_schema.py @@ -649,6 +649,7 @@ def test_vertex_model_constructor_parameters_are_set_correctly(self): description=_TEST_DESCRIPTION, metadata={}, ) + assert ( artifact.uri == "https://us-central1-aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/models/model-id" From fbfab0f4fbe02ab967443e70a5cfd1a5461802b8 Mon Sep 17 00:00:00 2001 From: sina chavoshi Date: Tue, 23 Aug 2022 22:47:30 +0000 Subject: [PATCH 2/3] remove extra line --- tests/unit/aiplatform/test_metadata_schema.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/aiplatform/test_metadata_schema.py b/tests/unit/aiplatform/test_metadata_schema.py index b1094111a9..65de1304ea 100644 --- a/tests/unit/aiplatform/test_metadata_schema.py +++ b/tests/unit/aiplatform/test_metadata_schema.py @@ -649,7 +649,6 @@ def test_vertex_model_constructor_parameters_are_set_correctly(self): description=_TEST_DESCRIPTION, metadata={}, ) - assert ( artifact.uri == "https://us-central1-aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/models/model-id" From 84c7d584d220987e76e87e966316852e9a01cef8 Mon Sep 17 00:00:00 2001 From: sina chavoshi Date: Wed, 24 Aug 2022 21:36:41 +0000 Subject: [PATCH 3/3] add unit tests for resouce_name version support --- .../cloud/aiplatform/metadata/schema/utils.py | 7 +++++-- .../aiplatform/test_metadata_resources.py | 20 +++++++++++++++++++ tests/unit/aiplatform/test_metadata_schema.py | 20 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/google/cloud/aiplatform/metadata/schema/utils.py b/google/cloud/aiplatform/metadata/schema/utils.py index 50d05595f8..1b4a5e4f6c 100644 --- a/google/cloud/aiplatform/metadata/schema/utils.py +++ b/google/cloud/aiplatform/metadata/schema/utils.py @@ -143,12 +143,15 @@ def to_dict(self): return results -def create_uri_from_resource_name(resource_name: str) -> bool: +def create_uri_from_resource_name(resource_name: str) -> str: """Construct the service URI for a given resource_name. Args: resource_name (str): - The name of the Vertex resource, in a form of + The name of the Vertex resource, in one of the forms: projects/{project}/locations/{location}/{resource_type}/{resource_id} + projects/{project}/locations/{location}/{resource_type}/{resource_id}@{version} + projects/{project}/locations/{location}/metadataStores/{store_id}/{resource_type}/{resource_id} + projects/{project}/locations/{location}/metadataStores/{store_id}/{resource_type}/{resource_id}@{version} Returns: The resource URI in the form of: https://{service-endpoint}/v1/{resource_name}, diff --git a/tests/unit/aiplatform/test_metadata_resources.py b/tests/unit/aiplatform/test_metadata_resources.py index 247657e08f..733eb1dd3f 100644 --- a/tests/unit/aiplatform/test_metadata_resources.py +++ b/tests/unit/aiplatform/test_metadata_resources.py @@ -892,6 +892,26 @@ def test_init_artifact_with_id(self, get_artifact_mock): name=_TEST_ARTIFACT_NAME, retry=base._DEFAULT_RETRY ) + def test_extract_metadata_store_id_with_valid_resource_name(self): + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) + assert "store_id" == artifact.Artifact._extract_metadata_store_id( + resource_name="projects/project/locations/location/metadataStores/store_id/resource_type/resource_id", + resource_noun="resource_type", + ) + assert "store_id" == artifact.Artifact._extract_metadata_store_id( + resource_name="projects/project/locations/location/metadataStores/store_id/resource_type/resource_id@version", + resource_noun="resource_type", + ) + + def test_extract_metadata_store_id_with_invalid_resource_name(self): + invalid_resouce_name = ( + "projects/project/locations/location/resource_type/resource_id/" + ) + with pytest.raises(ValueError): + artifact.Artifact._extract_metadata_store_id( + resource_name=invalid_resouce_name, resource_noun="resource_type" + ) + def test_get_or_create_artifact( self, get_artifact_for_get_or_create_mock, create_artifact_mock ): diff --git a/tests/unit/aiplatform/test_metadata_schema.py b/tests/unit/aiplatform/test_metadata_schema.py index 65de1304ea..0003838968 100644 --- a/tests/unit/aiplatform/test_metadata_schema.py +++ b/tests/unit/aiplatform/test_metadata_schema.py @@ -1093,6 +1093,26 @@ def test_predict_schemata_to_dict_method_returns_correct_schema(self): assert json.dumps(predict_schema_ta.to_dict()) == json.dumps(expected_results) + def test_create_uri_from_resource_name_for_valid_resouce_names(self): + valid_resouce_names = [ + "projects/project/locations/location/resource_type/resource_id", + "projects/project/locations/location/resource_type/resource_id@version", + "projects/project/locations/location/metadataStores/store_id/resource_type/resource_id", + "projects/project/locations/location/metadataStores/store_id/resource_type/resource_id@version", + ] + for resouce_name in valid_resouce_names: + uri = utils.create_uri_from_resource_name(resource_name=resouce_name) + assert ( + uri == f"https://location-aiplatform.googleapis.com/v1/{resouce_name}" + ) + + def test_create_uri_from_resource_name_for_invalid_resouce_names(self): + invalid_resouce_name = ( + "projects/project/locations/location/resource_type/resource_id/" + ) + with pytest.raises(ValueError): + utils.create_uri_from_resource_name(resource_name=invalid_resouce_name) + def test_container_spec_to_dict_method_returns_correct_schema(self): container_spec = utils.ContainerSpec( image_uri="gcr.io/some_container_image_uri",