diff --git a/google/cloud/aiplatform/jobs.py b/google/cloud/aiplatform/jobs.py index 2ce9d8bbb9..be928a1c1b 100644 --- a/google/cloud/aiplatform/jobs.py +++ b/google/cloud/aiplatform/jobs.py @@ -850,6 +850,39 @@ def __init__( self._logged_web_access_uris = set() + @classmethod + def _empty_constructor( + cls, + project: Optional[str] = None, + location: Optional[str] = None, + credentials: Optional[auth_credentials.Credentials] = None, + resource_name: Optional[str] = None, + ) -> "_RunnableJob": + """Initializes with all attributes set to None. + + The attributes should be populated after a future is complete. This allows + scheduling of additional API calls before the resource is created. + + Args: + project (str): Optional. Project of the resource noun. + location (str): Optional. The location of the resource noun. + credentials(google.auth.credentials.Credentials): + Optional. custom credentials to use when accessing interacting with + resource noun. + resource_name(str): Optional. A fully-qualified resource name or ID. + Returns: + An instance of this class with attributes set to None. + """ + self = super()._empty_constructor( + project=project, + location=location, + credentials=credentials, + resource_name=resource_name, + ) + + self._logged_web_access_uris = set() + return self + @property def web_access_uris(self) -> Dict[str, Union[str, Dict[str, str]]]: """Fetch the runnable job again and return the latest web access uris. diff --git a/tests/unit/aiplatform/test_custom_job.py b/tests/unit/aiplatform/test_custom_job.py index f865385e7d..4f202d5e1c 100644 --- a/tests/unit/aiplatform/test_custom_job.py +++ b/tests/unit/aiplatform/test_custom_job.py @@ -547,6 +547,13 @@ def test_get_web_access_uris(self, get_custom_job_mock_with_enable_web_access): assert job.web_access_uris == _TEST_WEB_ACCESS_URIS break + def test_log_access_web_uris_after_get( + self, get_custom_job_mock_with_enable_web_access + ): + job = aiplatform.CustomJob.get(_TEST_CUSTOM_JOB_NAME) + job._block_until_complete() + assert job._logged_web_access_uris == set(_TEST_WEB_ACCESS_URIS.values()) + def test_get_web_access_uris_job_succeeded( self, get_custom_job_mock_with_enable_web_access_succeeded ): diff --git a/tests/unit/aiplatform/test_hyperparameter_tuning_job.py b/tests/unit/aiplatform/test_hyperparameter_tuning_job.py index c14309ace6..0ec9022df2 100644 --- a/tests/unit/aiplatform/test_hyperparameter_tuning_job.py +++ b/tests/unit/aiplatform/test_hyperparameter_tuning_job.py @@ -732,3 +732,15 @@ def test_create_hyperparameter_tuning_job_with_enable_web_access( assert job.trials == [] caplog.clear() + + def test_log_enable_web_access_after_get_hyperparameter_tuning_job( + self, get_hyperparameter_tuning_job_mock_with_enable_web_access, + ): + + hp_job = aiplatform.HyperparameterTuningJob.get( + _TEST_HYPERPARAMETERTUNING_JOB_NAME + ) + hp_job._block_until_complete() + assert hp_job._logged_web_access_uris == set( + test_custom_job._TEST_WEB_ACCESS_URIS.values() + )