From 2532d506b44fc1ef0fa0a996822d29e7459c465a Mon Sep 17 00:00:00 2001 From: cojenco <59401799+cojenco@users.noreply.github.com> Date: Sat, 8 May 2021 00:58:43 -0700 Subject: [PATCH] fix: replace python lifecycle action parsing ValueError with warning (#437) * fix: replace python lifecycle action parsing ValueError with warning * fix lint * add client upgrade suggestion to unknown OLM rule warning * update warning message --- google/cloud/storage/bucket.py | 8 +++++++- tests/unit/test_bucket.py | 13 ++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/google/cloud/storage/bucket.py b/google/cloud/storage/bucket.py index 889a65888..ac38208a3 100644 --- a/google/cloud/storage/bucket.py +++ b/google/cloud/storage/bucket.py @@ -2361,7 +2361,13 @@ def lifecycle_rules(self): elif action_type == "SetStorageClass": yield LifecycleRuleSetStorageClass.from_api_repr(rule) else: - raise ValueError("Unknown lifecycle rule: {}".format(rule)) + warnings.warn( + "Unknown lifecycle rule type received: {}. Please upgrade to the latest version of google-cloud-storage.".format( + rule + ), + UserWarning, + stacklevel=1, + ) @lifecycle_rules.setter def lifecycle_rules(self, rules): diff --git a/tests/unit/test_bucket.py b/tests/unit/test_bucket.py index 4d776c365..22984a343 100644 --- a/tests/unit/test_bucket.py +++ b/tests/unit/test_bucket.py @@ -1783,15 +1783,22 @@ def test_iam_configuration_policy_w_entry(self): self.assertTrue(config.uniform_bucket_level_access_enabled) self.assertEqual(config.uniform_bucket_level_access_locked_time, now) - def test_lifecycle_rules_getter_unknown_action_type(self): + @mock.patch("warnings.warn") + def test_lifecycle_rules_getter_unknown_action_type(self, mock_warn): NAME = "name" BOGUS_RULE = {"action": {"type": "Bogus"}, "condition": {"age": 42}} rules = [BOGUS_RULE] properties = {"lifecycle": {"rule": rules}} bucket = self._make_one(name=NAME, properties=properties) - with self.assertRaises(ValueError): - list(bucket.lifecycle_rules) + list(bucket.lifecycle_rules) + mock_warn.assert_called_with( + "Unknown lifecycle rule type received: {}. Please upgrade to the latest version of google-cloud-storage.".format( + BOGUS_RULE + ), + UserWarning, + stacklevel=1, + ) def test_lifecycle_rules_getter(self): from google.cloud.storage.bucket import (