Skip to content

Commit

Permalink
Apply formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
shunping committed Jun 8, 2024
1 parent 9bc43f3 commit 9788a48
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
46 changes: 24 additions & 22 deletions sdks/python/apache_beam/options/pipeline_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,29 +918,31 @@ def _create_default_gcs_bucket(self):
# The function returns true only if the policy is enabled.
# If the policy is disabled or there is any exception
def _warn_if_soft_delete_policy_enabled(self, arg_name):
gcs_path = getattr(self, arg_name, None)
gcs_path = getattr(self, arg_name, None)
try:
from google.api_core.exceptions import GoogleAPICallError
from apache_beam.io.gcp import gcsio
try:
from google.api_core.exceptions import GoogleAPICallError
from apache_beam.io.gcp import gcsio
try:
bucket_name, _ = gcsio.parse_gcs_path(gcs_path)
bucket = gcsio.GcsIO().get_bucket(bucket_name)
if (bucket.soft_delete_policy is not None and
bucket.soft_delete_policy.retention_duration_seconds > 0):
_LOGGER.warning(
"Bucket %s specified in %s has soft-delete policy enabled."
" To avoid being billed for unnecessary storage costs, turn"
" off the soft delete feature on buckets that your Dataflow"
" jobs use for temporary and staging storage. For more"
" information, see"
" https://cloud.google.com/storage/docs/use-soft-delete#remove-soft-delete-policy."
% (bucket_name, arg_name))
return True
except GoogleAPICallError:
_LOGGER.warning('Unable to check soft delete policy in the bucket of %s.' % gcs_path)
except ImportError:
_LOGGER.warning('Missing dependencies to check soft delete policy.')
return False
bucket_name, _ = gcsio.parse_gcs_path(gcs_path)
bucket = gcsio.GcsIO().get_bucket(bucket_name)
if (bucket.soft_delete_policy is not None and
bucket.soft_delete_policy.retention_duration_seconds > 0):
_LOGGER.warning(
"Bucket %s specified in %s has soft-delete policy enabled."
" To avoid being billed for unnecessary storage costs, turn"
" off the soft delete feature on buckets that your Dataflow"
" jobs use for temporary and staging storage. For more"
" information, see"
" https://cloud.google.com/storage/docs/use-soft-delete#remove-soft-delete-policy."
% (bucket_name, arg_name))
return True
except GoogleAPICallError:
_LOGGER.warning(
'Unable to check soft delete policy in the bucket of %s.' %
gcs_path)
except ImportError:
_LOGGER.warning('Missing dependencies to check soft delete policy.')
return False

# If either temp or staging location has an issue, we use the valid one for
# both locations. If both are bad we return an error.
Expand Down
16 changes: 8 additions & 8 deletions sdks/python/apache_beam/options/pipeline_options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,21 +793,21 @@ def test_validation_bad_stg_bad_temp_no_default(self):
def test_validation_temp_with_soft_delete(self):
from google.cloud.storage import Bucket
bucket = Bucket(None)
bucket.soft_delete_policy.retention_duration_seconds = 0
with mock.patch("apache_beam.io.gcp.gcsio.GcsIO.get_bucket") as mock_get_bucket:
options = MockGoogleCloudOptionsWithBucket([
'--project=myproject',
'--temp_location=gs://beam/tmp'
])
with mock.patch(
"apache_beam.io.gcp.gcsio.GcsIO.get_bucket") as mock_get_bucket:
options = MockGoogleCloudOptionsWithBucket(
['--project=myproject', '--temp_location=gs://beam/tmp'])
mock_get_bucket.return_value = bucket

# soft delete policy enabled
bucket.soft_delete_policy.retention_duration_seconds = 1024
self.assertTrue(options._warn_if_soft_delete_policy_enabled("temp_location"))
self.assertTrue(
options._warn_if_soft_delete_policy_enabled("temp_location"))

# soft delete policy disabled
bucket.soft_delete_policy.retention_duration_seconds = 0
self.assertFalse(options._warn_if_soft_delete_policy_enabled("temp_location"))
self.assertFalse(
options._warn_if_soft_delete_policy_enabled("temp_location"))


if __name__ == '__main__':
Expand Down

0 comments on commit 9788a48

Please sign in to comment.