Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,7 @@ def execute(self, context: Context) -> None:
gcp_conn_id=self.gcp_conn_id,
impersonation_chain=self.impersonation_chain,
)
try:
hook.delete_instance(project_id=self.project_id, instance_id=self.instance_id)
except google.api_core.exceptions.NotFound:
self.log.info(
"The instance '%s' does not exist in project '%s'. Consider it as deleted",
self.instance_id,
self.project_id,
)
except google.api_core.exceptions.GoogleAPICallError as e:
self.log.error("An error occurred. Exiting.")
raise e
hook.delete_instance(project_id=self.project_id, instance_id=self.instance_id)


class BigtableCreateTableOperator(GoogleCloudBaseOperator, BigtableValidationMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,66 +584,20 @@ def test_empty_attribute(self, mock_hook, missing_attribute, project_id, instanc
mock_hook.assert_not_called()

@mock.patch("airflow.providers.google.cloud.operators.bigtable.BigtableHook")
def test_deleting_instance_that_doesnt_exists(self, mock_hook):
op = BigtableDeleteInstanceOperator(
project_id=PROJECT_ID,
instance_id=INSTANCE_ID,
task_id="id",
gcp_conn_id=GCP_CONN_ID,
impersonation_chain=IMPERSONATION_CHAIN,
)
mock_hook.return_value.delete_instance.side_effect = mock.Mock(
side_effect=google.api_core.exceptions.NotFound("Instance not found.")
)
op.execute(None)
mock_hook.assert_called_once_with(
gcp_conn_id=GCP_CONN_ID,
impersonation_chain=IMPERSONATION_CHAIN,
)
mock_hook.return_value.delete_instance.assert_called_once_with(
project_id=PROJECT_ID, instance_id=INSTANCE_ID
)

@mock.patch("airflow.providers.google.cloud.operators.bigtable.BigtableHook")
def test_deleting_instance_that_doesnt_exists_empty_project_id(self, mock_hook):
op = BigtableDeleteInstanceOperator(
instance_id=INSTANCE_ID,
task_id="id",
gcp_conn_id=GCP_CONN_ID,
impersonation_chain=IMPERSONATION_CHAIN,
)
mock_hook.return_value.delete_instance.side_effect = mock.Mock(
side_effect=google.api_core.exceptions.NotFound("Instance not found.")
)
op.execute(None)
mock_hook.assert_called_once_with(
gcp_conn_id=GCP_CONN_ID,
impersonation_chain=IMPERSONATION_CHAIN,
)
mock_hook.return_value.delete_instance.assert_called_once_with(
project_id=None, instance_id=INSTANCE_ID
)
def test_delete_instance_when_instance_missing_does_not_fail(self, mock_hook):
# Simulate hook handling missing instance gracefully
mock_hook.return_value.delete_instance.return_value = None

@mock.patch("airflow.providers.google.cloud.operators.bigtable.BigtableHook")
def test_different_error_reraised(self, mock_hook):
op = BigtableDeleteInstanceOperator(
project_id=PROJECT_ID,
instance_id=INSTANCE_ID,
task_id="id",
gcp_conn_id=GCP_CONN_ID,
impersonation_chain=IMPERSONATION_CHAIN,
)
mock_hook.return_value.delete_instance.side_effect = mock.Mock(
side_effect=google.api_core.exceptions.GoogleAPICallError("error")
)

with pytest.raises(google.api_core.exceptions.GoogleAPICallError):
op.execute(None)
op.execute(None)

mock_hook.assert_called_once_with(
gcp_conn_id=GCP_CONN_ID,
impersonation_chain=IMPERSONATION_CHAIN,
)
mock_hook.return_value.delete_instance.assert_called_once_with(
project_id=PROJECT_ID, instance_id=INSTANCE_ID
)
Expand Down