diff --git a/airflow/providers/google/cloud/hooks/cloud_build.py b/airflow/providers/google/cloud/hooks/cloud_build.py index a88935810b324..0ac929a4ad2fb 100644 --- a/airflow/providers/google/cloud/hooks/cloud_build.py +++ b/airflow/providers/google/cloud/hooks/cloud_build.py @@ -18,6 +18,7 @@ """Hook for Google Cloud Build service.""" from __future__ import annotations +import warnings from typing import Sequence from google.api_core.client_options import ClientOptions @@ -28,7 +29,7 @@ from google.cloud.devtools.cloudbuild_v1 import CloudBuildAsyncClient, CloudBuildClient, GetBuildRequest from google.cloud.devtools.cloudbuild_v1.types import Build, BuildTrigger, RepoSource -from airflow.exceptions import AirflowException +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning from airflow.providers.google.common.consts import CLIENT_INFO from airflow.providers.google.common.hooks.base_google import PROVIDE_PROJECT_ID, GoogleBaseHook @@ -180,6 +181,8 @@ def create_build_without_waiting_for_result( metadata=metadata, ) id_ = self._get_build_id_from_operation(operation) + self.log.info("Build has been created: %s.", id_) + return operation, id_ @GoogleBaseHook.fallback_to_default_project_id @@ -207,6 +210,11 @@ def create_build( :param metadata: Optional, additional metadata that is provided to the method. """ + warnings.warn( + "This method is deprecated. Please use `create_build_without_waiting_for_result`.", + AirflowProviderDeprecationWarning, + stacklevel=2, + ) client = self.get_conn() self.log.info("Start creating build...") @@ -520,13 +528,12 @@ def retry_build( ) id_ = self._get_build_id_from_operation(operation) + self.log.info("Build has been retried: %s.", id_) if not wait: return self.get_build(id_=id_, project_id=project_id, location=location) - operation.result() - - self.log.info("Build has been retried: %s.", id_) + self.wait_for_operation(operation, timeout) return self.get_build(id_=id_, project_id=project_id, location=location) @@ -567,14 +574,15 @@ def run_build_trigger( timeout=timeout, metadata=metadata, ) + self.log.info("Build trigger has been run: %s.", trigger_id) id_ = self._get_build_id_from_operation(operation) + self.log.info("Build has been created: %s.", id_) if not wait: return self.get_build(id_=id_, project_id=project_id, location=location) - operation.result() - self.log.info("Build trigger has been run: %s.", trigger_id) + self.wait_for_operation(operation, timeout) return self.get_build(id_=id_, project_id=project_id, location=location) diff --git a/tests/providers/google/cloud/hooks/test_cloud_build.py b/tests/providers/google/cloud/hooks/test_cloud_build.py index 2e21bf5ae9a86..070356ba68189 100644 --- a/tests/providers/google/cloud/hooks/test_cloud_build.py +++ b/tests/providers/google/cloud/hooks/test_cloud_build.py @@ -28,7 +28,7 @@ from google.api_core.gapic_v1.method import DEFAULT from google.cloud.devtools.cloudbuild_v1 import CloudBuildAsyncClient, GetBuildRequest -from airflow import AirflowException +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning from airflow.providers.google.cloud.hooks.cloud_build import CloudBuildAsyncHook, CloudBuildHook from airflow.providers.google.common.consts import CLIENT_INFO from tests.providers.google.cloud.utils.base_gcp_mock import mock_base_gcp_hook_no_default_project_id @@ -102,7 +102,8 @@ def test_create_build_with_wait(self, get_conn, wait_time, mock_get_id_from_oper wait_time.return_value = 0 - self.hook.create_build(build=BUILD, project_id=PROJECT_ID) + with pytest.warns(AirflowProviderDeprecationWarning, match="This method is deprecated"): + self.hook.create_build(build=BUILD, project_id=PROJECT_ID) get_conn.return_value.create_build.assert_called_once_with( request={"project_id": PROJECT_ID, "build": BUILD}, retry=DEFAULT, timeout=None, metadata=() @@ -122,7 +123,8 @@ def test_create_build_without_wait(self, get_conn, mock_get_id_from_operation): get_conn.return_value.run_build_trigger.return_value = mock.MagicMock() mock_get_id_from_operation.return_value = BUILD_ID - self.hook.create_build(build=BUILD, project_id=PROJECT_ID, wait=False) + with pytest.warns(AirflowProviderDeprecationWarning, match="This method is deprecated"): + self.hook.create_build(build=BUILD, project_id=PROJECT_ID, wait=False) mock_operation = get_conn.return_value.create_build @@ -136,6 +138,29 @@ def test_create_build_without_wait(self, get_conn, mock_get_id_from_operation): mock_get_id_from_operation.assert_called_once_with(mock_operation()) + @mock.patch( + "airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook._get_build_id_from_operation" + ) + @mock.patch("airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook.get_conn") + def test_create_build_without_waiting_for_result(self, get_conn, mock_get_id_from_operation): + get_conn.return_value.run_build_trigger.return_value = mock.MagicMock() + mock_get_id_from_operation.return_value = BUILD_ID + + self.hook.create_build_without_waiting_for_result( + build=BUILD, project_id=PROJECT_ID, location=LOCATION + ) + + mock_operation = get_conn.return_value.create_build + + mock_operation.assert_called_once_with( + request={"parent": PARENT, "project_id": PROJECT_ID, "build": BUILD}, + retry=DEFAULT, + timeout=None, + metadata=(), + ) + + mock_get_id_from_operation.assert_called_once_with(mock_operation()) + @mock.patch("airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook.get_conn") def test_create_build_trigger(self, get_conn): self.hook.create_build_trigger(trigger=BUILD_TRIGGER, project_id=PROJECT_ID) @@ -224,7 +249,7 @@ def test_retry_build_with_wait(self, get_conn, wait_time, mock_get_id_from_opera request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=() ) - get_conn.return_value.retry_build.return_value.result.assert_called_once_with() + get_conn.return_value.retry_build.return_value.result.assert_called_once_with(timeout=None) get_conn.return_value.get_build.assert_called_once_with( request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=() @@ -278,7 +303,7 @@ def test_run_build_trigger_with_wait(self, get_conn, wait_time, mock_get_id_from metadata=(), ) - get_conn.return_value.run_build_trigger.return_value.result.assert_called_once_with() + get_conn.return_value.run_build_trigger.return_value.result.assert_called_once_with(timeout=None) get_conn.return_value.get_build.assert_called_once_with( request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()