GeezerVison is showing github.com — feat: parse project location when passed full resource name to get ap… · googleapis/python-aiplatform@674227d
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 +59-3Lines changed: 59 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import functools | 21 | 21 | import inspect | 22 | 22 | import threading | 23 | | -from typing import Any, Callable, Dict, Optional, Sequence, Type, Union | | 23 | +from typing import Any, Callable, Dict, Optional, Sequence, Tuple, Type, Union | 24 | 24 |
| 25 | 25 | import proto | 26 | 26 |
| @@ -266,6 +266,7 @@ def __init__( | 266 | 266 | project: Optional[str] = None, | 267 | 267 | location: Optional[str] = None, | 268 | 268 | credentials: Optional[auth_credentials.Credentials] = None, | | 269 | + resource_name: Optional[str] = None, | 269 | 270 | ): | 270 | 271 | """Initializes class with project, location, and api_client. | 271 | 272 |
| @@ -274,8 +275,14 @@ def __init__( | 274 | 275 | location(str): The location of the resource noun. | 275 | 276 | credentials(google.auth.crendentials.Crendentials): Optional custom | 276 | 277 | credentials to use when accessing interacting with resource noun. | | 278 | + resource_name(str): A fully-qualified resource name or ID. | 277 | 279 | """ | 278 | 280 |
| | 281 | + if resource_name: | | 282 | + project, location = self._get_and_validate_project_location( | | 283 | + resource_name=resource_name, project=project, location=location | | 284 | + ) | | 285 | + | 279 | 286 | self.project = project or initializer.global_config.project | 280 | 287 | self.location = location or initializer.global_config.location | 281 | 288 | self.credentials = credentials or initializer.global_config.credentials | @@ -306,6 +313,41 @@ def _instantiate_client( | 306 | 313 | prediction_client=cls._is_client_prediction_client, | 307 | 314 | ) | 308 | 315 |
| | 316 | + def _get_and_validate_project_location( | | 317 | + self, | | 318 | + resource_name: str, | | 319 | + project: Optional[str] = None, | | 320 | + location: Optional[str] = None, | | 321 | + ) -> Tuple: | | 322 | + | | 323 | + """Validate the project and location for the resource. | | 324 | +
| | 325 | + Args: | | 326 | + resource_name(str): Required. A fully-qualified resource name or ID. | | 327 | + project(str): Project of the resource noun. | | 328 | + location(str): The location of the resource noun. | | 329 | +
| | 330 | + Raises: | | 331 | + RuntimeError if location is different from resource location | | 332 | + """ | | 333 | + | | 334 | + if not project and not location: | | 335 | + return project, location | | 336 | + | | 337 | + fields = utils.extract_fields_from_resource_name( | | 338 | + resource_name, self._resource_noun | | 339 | + ) | | 340 | + if not fields: | | 341 | + return project, location | | 342 | + | | 343 | + if location and fields.location != location: | | 344 | + raise RuntimeError( | | 345 | + f"location {location} is provided, but different from " | | 346 | + f"the resource location {fields.location}" | | 347 | + ) | | 348 | + | | 349 | + return fields.project, fields.location | | 350 | + | 309 | 351 | def _get_gca_resource(self, resource_name: str) -> proto.Message: | 310 | 352 | """Returns GAPIC service representation of client class resource.""" | 311 | 353 | """ | @@ -493,6 +535,7 @@ def __init__( | 493 | 535 | project: Optional[str] = None, | 494 | 536 | location: Optional[str] = None, | 495 | 537 | credentials: Optional[auth_credentials.Credentials] = None, | | 538 | + resource_name: Optional[str] = None, | 496 | 539 | ): | 497 | 540 | """Initializes class with project, location, and api_client. | 498 | 541 |
| @@ -502,9 +545,14 @@ def __init__( | 502 | 545 | credentials(google.auth.crendentials.Crendentials): | 503 | 546 | Optional. custom credentials to use when accessing interacting with | 504 | 547 | resource noun. | | 548 | + resource_name(str): A fully-qualified resource name or ID. | 505 | 549 | """ | 506 | 550 | AiPlatformResourceNoun.__init__( | 507 | | - self, project=project, location=location, credentials=credentials | | 551 | + self, | | 552 | + project=project, | | 553 | + location=location, | | 554 | + credentials=credentials, | | 555 | + resource_name=resource_name, | 508 | 556 | ) | 509 | 557 | FutureManager.__init__(self) | 510 | 558 |
| @@ -514,6 +562,7 @@ def _empty_constructor( | 514 | 562 | project: Optional[str] = None, | 515 | 563 | location: Optional[str] = None, | 516 | 564 | credentials: Optional[auth_credentials.Credentials] = None, | | 565 | + resource_name: Optional[str] = None, | 517 | 566 | ) -> "AiPlatformResourceNounWithFutureManager": | 518 | 567 | """Initializes with all attributes set to None. | 519 | 568 |
| @@ -526,11 +575,18 @@ def _empty_constructor( | 526 | 575 | credentials(google.auth.crendentials.Crendentials): | 527 | 576 | Optional. custom credentials to use when accessing interacting with | 528 | 577 | resource noun. | | 578 | + resource_name(str): A fully-qualified resource name or ID. | 529 | 579 | Returns: | 530 | 580 | An instance of this class with attributes set to None. | 531 | 581 | """ | 532 | 582 | self = cls.__new__(cls) | 533 | | - AiPlatformResourceNoun.__init__(self, project, location, credentials) | | 583 | + AiPlatformResourceNoun.__init__( | | 584 | + self, | | 585 | + project=project, | | 586 | + location=location, | | 587 | + credentials=credentials, | | 588 | + resource_name=resource_name, | | 589 | + ) | 534 | 590 | FutureManager.__init__(self) | 535 | 591 | self._gca_resource = None | 536 | 592 | return self | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/datasets/dataset.py +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,7 +71,10 @@ def __init__( | 71 | 71 | """ | 72 | 72 |
| 73 | 73 | super().__init__( | 74 | | - project=project, location=location, credentials=credentials, | | 74 | + project=project, | | 75 | + location=location, | | 76 | + credentials=credentials, | | 77 | + resource_name=dataset_name, | 75 | 78 | ) | 76 | 79 | self._gca_resource = self._get_gca_resource(resource_name=dataset_name) | 77 | 80 | self._validate_metadata_schema_uri() | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/jobs.py +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -107,7 +107,12 @@ def __init__( | 107 | 107 | Custom credentials to use. If not set, credentials set in | 108 | 108 | aiplatform.init will be used. | 109 | 109 | """ | 110 | | - super().__init__(project=project, location=location, credentials=credentials) | | 110 | + super().__init__( | | 111 | + project=project, | | 112 | + location=location, | | 113 | + credentials=credentials, | | 114 | + resource_name=job_name, | | 115 | + ) | 111 | 116 | self._gca_resource = self._get_gca_resource(resource_name=job_name) | 112 | 117 |
| 113 | 118 | @property | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/models.py +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -99,7 +99,12 @@ def __init__( | 99 | 99 | credentials set in aiplatform.init. | 100 | 100 | """ | 101 | 101 |
| 102 | | - super().__init__(project=project, location=location, credentials=credentials) | | 102 | + super().__init__( | | 103 | + project=project, | | 104 | + location=location, | | 105 | + credentials=credentials, | | 106 | + resource_name=endpoint_name, | | 107 | + ) | 103 | 108 | self._gca_resource = self._get_gca_resource(resource_name=endpoint_name) | 104 | 109 | self._prediction_client = self._instantiate_prediction_client( | 105 | 110 | location=location or initializer.global_config.location, | @@ -1144,7 +1149,12 @@ def __init__( | 1144 | 1149 | credentials set in aiplatform.init will be used. | 1145 | 1150 | """ | 1146 | 1151 |
| 1147 | | - super().__init__(project=project, location=location, credentials=credentials) | | 1152 | + super().__init__( | | 1153 | + project=project, | | 1154 | + location=location, | | 1155 | + credentials=credentials, | | 1156 | + resource_name=model_name, | | 1157 | + ) | 1148 | 1158 | self._gca_resource = self._get_gca_resource(resource_name=model_name) | 1149 | 1159 |
| 1150 | 1160 | # TODO(b/170979552) Add support for predict schemata | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/training_jobs.py +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -180,7 +180,10 @@ def get( | 180 | 180 | # These parameters won't be used as user can not run the job again. | 181 | 181 | # If they try, an exception will be raised. | 182 | 182 | self = cls._empty_constructor( | 183 | | - project=project, location=location, credentials=credentials | | 183 | + project=project, | | 184 | + location=location, | | 185 | + credentials=credentials, | | 186 | + resource_name=resource_name, | 184 | 187 | ) | 185 | 188 |
| 186 | 189 | self._gca_resource = self._get_gca_resource(resource_name=resource_name) | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_datasets.py +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 48 | 48 | _TEST_PROJECT = "test-project" | 49 | 49 | _TEST_LOCATION = "us-central1" | 50 | 50 | _TEST_PARENT = f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}" | | 51 | +_TEST_ALT_PROJECT = "test-project_alt" | 51 | 52 |
| 52 | 53 | _TEST_ALT_LOCATION = "europe-west4" | 53 | 54 | _TEST_INVALID_LOCATION = "us-central2" | @@ -259,6 +260,38 @@ def test_init_dataset(self, get_dataset_mock): | 259 | 260 | datasets.Dataset(dataset_name=_TEST_NAME) | 260 | 261 | get_dataset_mock.assert_called_once_with(name=_TEST_NAME) | 261 | 262 |
| | 263 | + def test_init_dataset_with_id_only_with_project_and_location( | | 264 | + self, get_dataset_mock | | 265 | + ): | | 266 | + aiplatform.init(project=_TEST_PROJECT) | | 267 | + datasets.Dataset( | | 268 | + dataset_name=_TEST_ID, project=_TEST_PROJECT, location=_TEST_LOCATION | | 269 | + ) | | 270 | + get_dataset_mock.assert_called_once_with(name=_TEST_NAME) | | 271 | + | | 272 | + def test_init_dataset_with_project_and_location(self, get_dataset_mock): | | 273 | + aiplatform.init(project=_TEST_PROJECT) | | 274 | + datasets.Dataset( | | 275 | + dataset_name=_TEST_NAME, project=_TEST_PROJECT, location=_TEST_LOCATION | | 276 | + ) | | 277 | + get_dataset_mock.assert_called_once_with(name=_TEST_NAME) | | 278 | + | | 279 | + def test_init_dataset_with_alt_project_and_location(self, get_dataset_mock): | | 280 | + aiplatform.init(project=_TEST_PROJECT) | | 281 | + datasets.Dataset( | | 282 | + dataset_name=_TEST_NAME, project=_TEST_ALT_PROJECT, location=_TEST_LOCATION | | 283 | + ) | | 284 | + get_dataset_mock.assert_called_once_with(name=_TEST_NAME) | | 285 | + | | 286 | + def test_init_dataset_with_project_and_alt_location(self): | | 287 | + aiplatform.init(project=_TEST_PROJECT) | | 288 | + with pytest.raises(RuntimeError): | | 289 | + datasets.Dataset( | | 290 | + dataset_name=_TEST_NAME, | | 291 | + project=_TEST_PROJECT, | | 292 | + location=_TEST_ALT_LOCATION, | | 293 | + ) | | 294 | + | 262 | 295 | def test_init_dataset_with_id_only(self, get_dataset_mock): | 263 | 296 | aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) | 264 | 297 | datasets.Dataset(dataset_name=_TEST_ID) | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_training_jobs.py +38-1Lines changed: 38 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 65 | 65 | _TEST_GCS_PATH_WITH_TRAILING_SLASH = f"{_TEST_GCS_PATH}/" | 66 | 66 | _TEST_LOCAL_SCRIPT_FILE_NAME = "____test____script.py" | 67 | 67 | _TEST_LOCAL_SCRIPT_FILE_PATH = f"path/to/{_TEST_LOCAL_SCRIPT_FILE_NAME}" | 68 | | -_TEST_PROJECT = "test-project" | 69 | 68 | _TEST_PYTHON_SOURCE = """ | 70 | 69 | print('hello world') | 71 | 70 | """ | | 107 | 106 | _TEST_NAME = ( | 108 | 107 | f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/trainingPipelines/{_TEST_ID}" | 109 | 108 | ) | | 109 | +_TEST_ALT_PROJECT = "test-project-alt" | | 110 | +_TEST_ALT_LOCATION = "europe-west4" | 110 | 111 |
| 111 | 112 | _TEST_MODEL_INSTANCE_SCHEMA_URI = "instance_schema_uri.yaml" | 112 | 113 | _TEST_MODEL_PARAMETERS_SCHEMA_URI = "parameters_schema_uri.yaml" | @@ -1381,6 +1382,42 @@ def test_get_training_job_with_id_only(self, get_training_job_custom_mock): | 1381 | 1382 | training_jobs.CustomTrainingJob.get(resource_name=_TEST_ID) | 1382 | 1383 | get_training_job_custom_mock.assert_called_once_with(name=_TEST_NAME) | 1383 | 1384 |
| | 1385 | + def test_get_training_job_with_id_only_with_project_and_location( | | 1386 | + self, get_training_job_custom_mock | | 1387 | + ): | | 1388 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) | | 1389 | + training_jobs.CustomTrainingJob.get( | | 1390 | + resource_name=_TEST_ID, project=_TEST_PROJECT, location=_TEST_LOCATION | | 1391 | + ) | | 1392 | + get_training_job_custom_mock.assert_called_once_with(name=_TEST_NAME) | | 1393 | + | | 1394 | + def test_get_training_job_with_project_and_location( | | 1395 | + self, get_training_job_custom_mock | | 1396 | + ): | | 1397 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) | | 1398 | + training_jobs.CustomTrainingJob.get( | | 1399 | + resource_name=_TEST_NAME, project=_TEST_PROJECT, location=_TEST_LOCATION | | 1400 | + ) | | 1401 | + get_training_job_custom_mock.assert_called_once_with(name=_TEST_NAME) | | 1402 | + | | 1403 | + def test_get_training_job_with_alt_project_and_location( | | 1404 | + self, get_training_job_custom_mock | | 1405 | + ): | | 1406 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) | | 1407 | + training_jobs.CustomTrainingJob.get( | | 1408 | + resource_name=_TEST_NAME, project=_TEST_ALT_PROJECT, location=_TEST_LOCATION | | 1409 | + ) | | 1410 | + get_training_job_custom_mock.assert_called_once_with(name=_TEST_NAME) | | 1411 | + | | 1412 | + def test_get_training_job_with_project_and_alt_location(self): | | 1413 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) | | 1414 | + with pytest.raises(RuntimeError): | | 1415 | + training_jobs.CustomTrainingJob.get( | | 1416 | + resource_name=_TEST_NAME, | | 1417 | + project=_TEST_PROJECT, | | 1418 | + location=_TEST_ALT_LOCATION, | | 1419 | + ) | | 1420 | + | 1384 | 1421 | @pytest.mark.parametrize("sync", [True, False]) | 1385 | 1422 | def test_run_call_pipeline_service_create_with_nontabular_dataset( | 1386 | 1423 | self, | |
|