Skip to content

Commit 737dc2b

Browse files
authored
feat: Support ResourceName with Version. (#1609)
* feat: Support ResourceName with Version. * remove extra line * add unit tests for resouce_name version support
1 parent 9ca5cbf commit 737dc2b

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

google/cloud/aiplatform/metadata/resource.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def _extract_metadata_store_id(resource_name, resource_noun) -> str:
539539
pattern = re.compile(
540540
r"^projects\/(?P<project>[\w-]+)\/locations\/(?P<location>[\w-]+)\/metadataStores\/(?P<store>[\w-]+)\/"
541541
+ resource_noun
542-
+ r"\/(?P<id>[\w-]+)$"
542+
+ r"\/(?P<id>[\w-]+)(?P<version>@[\w-]+)?$"
543543
)
544544
match = pattern.match(resource_name)
545545
if not match:

google/cloud/aiplatform/metadata/schema/utils.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,15 @@ def to_dict(self):
143143
return results
144144

145145

146-
def create_uri_from_resource_name(resource_name: str) -> bool:
146+
def create_uri_from_resource_name(resource_name: str) -> str:
147147
"""Construct the service URI for a given resource_name.
148148
Args:
149149
resource_name (str):
150-
The name of the Vertex resource, in a form of
150+
The name of the Vertex resource, in one of the forms:
151151
projects/{project}/locations/{location}/{resource_type}/{resource_id}
152+
projects/{project}/locations/{location}/{resource_type}/{resource_id}@{version}
153+
projects/{project}/locations/{location}/metadataStores/{store_id}/{resource_type}/{resource_id}
154+
projects/{project}/locations/{location}/metadataStores/{store_id}/{resource_type}/{resource_id}@{version}
152155
Returns:
153156
The resource URI in the form of:
154157
https://{service-endpoint}/v1/{resource_name},
@@ -159,11 +162,11 @@ def create_uri_from_resource_name(resource_name: str) -> bool:
159162
"""
160163
# TODO: support nested resource names such as models/123/evaluations/456
161164
match_results = re.match(
162-
r"^projects\/[A-Za-z0-9-]*\/locations\/([A-Za-z0-9-]*)(\/metadataStores\/[A-Za-z0-9-]*)?(\/[A-Za-z0-9-]*\/[A-Za-z0-9-]*)+$",
165+
r"^projects\/(?P<project>[\w-]+)\/locations\/(?P<location>[\w-]+)(\/metadataStores\/(?P<store>[\w-]+))?\/[\w-]+\/(?P<id>[\w-]+)(?P<version>@[\w-]+)?$",
163166
resource_name,
164167
)
165168
if not match_results:
166169
raise ValueError(f"Invalid resource_name format for {resource_name}.")
167170

168-
location = match_results.group(1)
171+
location = match_results["location"]
169172
return f"https://{location}-aiplatform.googleapis.com/v1/{resource_name}"

tests/unit/aiplatform/test_metadata_resources.py

+20
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,26 @@ def test_init_artifact_with_id(self, get_artifact_mock):
892892
name=_TEST_ARTIFACT_NAME, retry=base._DEFAULT_RETRY
893893
)
894894

895+
def test_extract_metadata_store_id_with_valid_resource_name(self):
896+
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
897+
assert "store_id" == artifact.Artifact._extract_metadata_store_id(
898+
resource_name="projects/project/locations/location/metadataStores/store_id/resource_type/resource_id",
899+
resource_noun="resource_type",
900+
)
901+
assert "store_id" == artifact.Artifact._extract_metadata_store_id(
902+
resource_name="projects/project/locations/location/metadataStores/store_id/resource_type/resource_id@version",
903+
resource_noun="resource_type",
904+
)
905+
906+
def test_extract_metadata_store_id_with_invalid_resource_name(self):
907+
invalid_resouce_name = (
908+
"projects/project/locations/location/resource_type/resource_id/"
909+
)
910+
with pytest.raises(ValueError):
911+
artifact.Artifact._extract_metadata_store_id(
912+
resource_name=invalid_resouce_name, resource_noun="resource_type"
913+
)
914+
895915
def test_get_or_create_artifact(
896916
self, get_artifact_for_get_or_create_mock, create_artifact_mock
897917
):

tests/unit/aiplatform/test_metadata_schema.py

+20
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,26 @@ def test_predict_schemata_to_dict_method_returns_correct_schema(self):
10931093

10941094
assert json.dumps(predict_schema_ta.to_dict()) == json.dumps(expected_results)
10951095

1096+
def test_create_uri_from_resource_name_for_valid_resouce_names(self):
1097+
valid_resouce_names = [
1098+
"projects/project/locations/location/resource_type/resource_id",
1099+
"projects/project/locations/location/resource_type/resource_id@version",
1100+
"projects/project/locations/location/metadataStores/store_id/resource_type/resource_id",
1101+
"projects/project/locations/location/metadataStores/store_id/resource_type/resource_id@version",
1102+
]
1103+
for resouce_name in valid_resouce_names:
1104+
uri = utils.create_uri_from_resource_name(resource_name=resouce_name)
1105+
assert (
1106+
uri == f"https://location-aiplatform.googleapis.com/v1/{resouce_name}"
1107+
)
1108+
1109+
def test_create_uri_from_resource_name_for_invalid_resouce_names(self):
1110+
invalid_resouce_name = (
1111+
"projects/project/locations/location/resource_type/resource_id/"
1112+
)
1113+
with pytest.raises(ValueError):
1114+
utils.create_uri_from_resource_name(resource_name=invalid_resouce_name)
1115+
10961116
def test_container_spec_to_dict_method_returns_correct_schema(self):
10971117
container_spec = utils.ContainerSpec(
10981118
image_uri="gcr.io/some_container_image_uri",

0 commit comments

Comments
 (0)