diff --git a/airflow/providers/google/cloud/hooks/cloud_build.py b/airflow/providers/google/cloud/hooks/cloud_build.py index 80aec8c0d4478..28da2b3acc719 100644 --- a/airflow/providers/google/cloud/hooks/cloud_build.py +++ b/airflow/providers/google/cloud/hooks/cloud_build.py @@ -189,52 +189,6 @@ def create_build_without_waiting_for_result( return operation, id_ - @GoogleBaseHook.fallback_to_default_project_id - def create_build( - self, - build: dict | Build, - project_id: str = PROVIDE_PROJECT_ID, - wait: bool = True, - retry: Retry | _MethodDefault = DEFAULT, - timeout: float | None = None, - metadata: Sequence[tuple[str, str]] = (), - ) -> Build: - """ - Starts a build with the specified configuration. - - :param build: The build resource to create. If a dict is provided, it must be of the same form - as the protobuf message `google.cloud.devtools.cloudbuild_v1.types.Build` - :param project_id: Optional, Google Cloud Project project_id where the function belongs. - If set to None or missing, the default project_id from the GCP connection is used. - :param wait: Optional, wait for operation to finish. - :param retry: Optional, a retry object used to retry requests. If `None` is specified, requests - will not be retried. - :param timeout: Optional, the amount of time, in seconds, to wait for the request to complete. - Note that if `retry` is specified, the timeout applies to each individual attempt. - :param metadata: Optional, additional metadata that is provided to the method. - - """ - client = self.get_conn() - - self.log.info("Start creating build...") - - operation = client.create_build( - request={"project_id": project_id, "build": build}, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - 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) - - self.wait_for_operation(operation, timeout) - - return self.get_build(id_=id_, project_id=project_id) - @GoogleBaseHook.fallback_to_default_project_id def create_build_trigger( self, diff --git a/tests/providers/google/cloud/hooks/test_cloud_build.py b/tests/providers/google/cloud/hooks/test_cloud_build.py index 01ea91d1bf7fb..c854a1416e4fc 100644 --- a/tests/providers/google/cloud/hooks/test_cloud_build.py +++ b/tests/providers/google/cloud/hooks/test_cloud_build.py @@ -90,44 +90,22 @@ def test_cancel_build(self, get_conn): @mock.patch( "airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook._get_build_id_from_operation" ) - @mock.patch("airflow.providers.google.cloud.hooks.cloud_build.TIME_TO_SLEEP_IN_SECONDS") @mock.patch("airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook.get_conn") - def test_create_build_with_wait(self, get_conn, wait_time, mock_get_id_from_operation): + 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 - wait_time.return_value = 0 - - 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=() - ) - - get_conn.return_value.create_build.return_value.result.assert_called_once_with() - - get_conn.return_value.get_build.assert_called_once_with( - request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=() + self.hook.create_build_without_waiting_for_result( + build=BUILD, project_id=PROJECT_ID, location=LOCATION ) - @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_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) - mock_operation = get_conn.return_value.create_build mock_operation.assert_called_once_with( - request={"project_id": PROJECT_ID, "build": BUILD}, retry=DEFAULT, timeout=None, metadata=() - ) - - get_conn.return_value.get_build.assert_called_once_with( - request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=() + 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()) @@ -220,7 +198,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=() @@ -274,7 +252,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=()