GeezerVison is showing github.com — fix: project/location parsing for nested resources (#1700) · googleapis/python-aiplatform@9e1d796
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/base.py +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1077,6 +1077,13 @@ def _list( | 1077 | 1077 | Returns: | 1078 | 1078 | List[VertexAiResourceNoun] - A list of SDK resource objects | 1079 | 1079 | """ | | 1080 | + if parent: | | 1081 | + parent_resources = utils.extract_project_and_location_from_parent(parent) | | 1082 | + if parent_resources: | | 1083 | + project, location = ( | | 1084 | + parent_resources["project"], | | 1085 | + parent_resources["location"], | | 1086 | + ) | 1080 | 1087 |
| 1081 | 1088 | resource = cls._empty_constructor( | 1082 | 1089 | project=project, location=location, credentials=credentials | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/utils/__init__.py +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -325,6 +325,34 @@ def extract_bucket_and_prefix_from_gcs_path(gcs_path: str) -> Tuple[str, Optiona | 325 | 325 | return (gcs_bucket, gcs_blob_prefix) | 326 | 326 |
| 327 | 327 |
| | 328 | +def extract_project_and_location_from_parent( | | 329 | + parent: str, | | 330 | +) -> Dict[str, str]: | | 331 | + """Given a complete parent resource name, return the project and location as a dict. | | 332 | +
| | 333 | + Example Usage: | | 334 | +
| | 335 | + parent_resources = extract_project_and_location_from_parent( | | 336 | + "projects/123/locations/us-central1/datasets/456" | | 337 | + ) | | 338 | +
| | 339 | + parent_resources["project"] = "123" | | 340 | + parent_resources["location"] = "us-central1" | | 341 | +
| | 342 | + Args: | | 343 | + parent (str): | | 344 | + Required. A complete parent resource name. | | 345 | +
| | 346 | + Returns: | | 347 | + Dict[str, str] | | 348 | + A project, location dict from provided parent resource name. | | 349 | + """ | | 350 | + parent_resources = re.match( | | 351 | + r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)(/|$)", parent | | 352 | + ) | | 353 | + return parent_resources.groupdict() if parent_resources else {} | | 354 | + | | 355 | + | 328 | 356 | class ClientWithOverride: | 329 | 357 | class WrappedClient: | 330 | 358 | """Wrapper class for client that creates client at API invocation | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_featurestores.py +35-2Lines changed: 35 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1023,7 +1023,23 @@ def test_list_entity_types(self, list_entity_types_mock): | 1023 | 1023 | aiplatform.init(project=_TEST_PROJECT) | 1024 | 1024 |
| 1025 | 1025 | my_featurestore = aiplatform.Featurestore( | 1026 | | - featurestore_name=_TEST_FEATURESTORE_ID | | 1026 | + featurestore_name=_TEST_FEATURESTORE_ID, | | 1027 | + ) | | 1028 | + my_entity_type_list = my_featurestore.list_entity_types() | | 1029 | + | | 1030 | + list_entity_types_mock.assert_called_once_with( | | 1031 | + request={"parent": _TEST_FEATURESTORE_NAME} | | 1032 | + ) | | 1033 | + assert len(my_entity_type_list) == len(_TEST_ENTITY_TYPE_LIST) | | 1034 | + for my_entity_type in my_entity_type_list: | | 1035 | + assert type(my_entity_type) == aiplatform.EntityType | | 1036 | + | | 1037 | + @pytest.mark.usefixtures("get_featurestore_mock") | | 1038 | + def test_list_entity_types_with_no_init(self, list_entity_types_mock): | | 1039 | + my_featurestore = aiplatform.Featurestore( | | 1040 | + featurestore_name=_TEST_FEATURESTORE_ID, | | 1041 | + project=_TEST_PROJECT, | | 1042 | + location=_TEST_LOCATION, | 1027 | 1043 | ) | 1028 | 1044 | my_entity_type_list = my_featurestore.list_entity_types() | 1029 | 1045 |
| @@ -1762,7 +1778,7 @@ def test_update_entity_type(self, update_entity_type_mock): | 1762 | 1778 | @pytest.mark.parametrize( | 1763 | 1779 | "featurestore_name", [_TEST_FEATURESTORE_NAME, _TEST_FEATURESTORE_ID] | 1764 | 1780 | ) | 1765 | | - def test_list_entity_types(self, featurestore_name, list_entity_types_mock): | | 1781 | + def test_list_entity_type(self, featurestore_name, list_entity_types_mock): | 1766 | 1782 | aiplatform.init(project=_TEST_PROJECT) | 1767 | 1783 |
| 1768 | 1784 | my_entity_type_list = aiplatform.EntityType.list( | @@ -1790,6 +1806,23 @@ def test_list_features(self, list_features_mock): | 1790 | 1806 | for my_feature in my_feature_list: | 1791 | 1807 | assert type(my_feature) == aiplatform.Feature | 1792 | 1808 |
| | 1809 | + @pytest.mark.usefixtures("get_entity_type_mock") | | 1810 | + def test_list_features_with_no_init(self, list_features_mock): | | 1811 | + my_entity_type = aiplatform.EntityType( | | 1812 | + entity_type_name=_TEST_ENTITY_TYPE_ID, | | 1813 | + featurestore_id=_TEST_FEATURESTORE_ID, | | 1814 | + project=_TEST_PROJECT, | | 1815 | + location=_TEST_LOCATION, | | 1816 | + ) | | 1817 | + my_feature_list = my_entity_type.list_features() | | 1818 | + | | 1819 | + list_features_mock.assert_called_once_with( | | 1820 | + request={"parent": _TEST_ENTITY_TYPE_NAME} | | 1821 | + ) | | 1822 | + assert len(my_feature_list) == len(_TEST_FEATURE_LIST) | | 1823 | + for my_feature in my_feature_list: | | 1824 | + assert type(my_feature) == aiplatform.Feature | | 1825 | + | 1793 | 1826 | @pytest.mark.parametrize("sync", [True, False]) | 1794 | 1827 | @pytest.mark.usefixtures("get_entity_type_mock", "get_feature_mock") | 1795 | 1828 | def test_delete_features(self, delete_feature_mock, sync): | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_utils.py +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -320,6 +320,30 @@ def test_extract_bucket_and_prefix_from_gcs_path(gcs_path: str, expected: tuple) | 320 | 320 | assert expected == utils.extract_bucket_and_prefix_from_gcs_path(gcs_path) | 321 | 321 |
| 322 | 322 |
| | 323 | +@pytest.mark.parametrize( | | 324 | + "parent, expected", | | 325 | + [ | | 326 | + ( | | 327 | + "projects/123/locations/us-central1/datasets/456", | | 328 | + {"project": "123", "location": "us-central1"}, | | 329 | + ), | | 330 | + ( | | 331 | + "projects/123/locations/us-central1/", | | 332 | + {"project": "123", "location": "us-central1"}, | | 333 | + ), | | 334 | + ( | | 335 | + "projects/123/locations/us-central1", | | 336 | + {"project": "123", "location": "us-central1"}, | | 337 | + ), | | 338 | + ("projects/123/locations/", {}), | | 339 | + ("projects/123", {}), | | 340 | + ], | | 341 | +) | | 342 | +def test_extract_project_and_location_from_parent(parent: str, expected: tuple): | | 343 | + # Given a parent resource name, ensure correct project and location are extracted | | 344 | + assert expected == utils.extract_project_and_location_from_parent(parent) | | 345 | + | | 346 | + | 323 | 347 | @pytest.mark.usefixtures("google_auth_mock") | 324 | 348 | def test_wrapped_client(): | 325 | 349 | test_client_info = gapic_v1.client_info.ClientInfo() | |
|