From a20476e701929176f9ec5e73a2315f29949cb98f Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Fri, 7 May 2021 12:57:37 -0700 Subject: [PATCH 1/4] fix: replace python lifecycle action parsing ValueError with warning --- google/cloud/storage/bucket.py | 6 +++++- tests/unit/test_bucket.py | 11 ++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/google/cloud/storage/bucket.py b/google/cloud/storage/bucket.py index 889a65888..e391486ef 100644 --- a/google/cloud/storage/bucket.py +++ b/google/cloud/storage/bucket.py @@ -2361,7 +2361,11 @@ 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: {}".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..94392c540 100644 --- a/tests/unit/test_bucket.py +++ b/tests/unit/test_bucket.py @@ -1783,15 +1783,20 @@ 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: {}".format(BOGUS_RULE), + UserWarning, + stacklevel=1, + ) def test_lifecycle_rules_getter(self): from google.cloud.storage.bucket import ( From 013c9c6bfcd6fd875ade6263f8cb0ae3581e2767 Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Fri, 7 May 2021 15:51:27 -0700 Subject: [PATCH 2/4] fix lint --- google/cloud/storage/bucket.py | 4 +--- tests/unit/test_bucket.py | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/google/cloud/storage/bucket.py b/google/cloud/storage/bucket.py index e391486ef..c028aba42 100644 --- a/google/cloud/storage/bucket.py +++ b/google/cloud/storage/bucket.py @@ -2362,9 +2362,7 @@ def lifecycle_rules(self): yield LifecycleRuleSetStorageClass.from_api_repr(rule) else: warnings.warn( - "Unknown lifecycle rule: {}".format(rule), - UserWarning, - stacklevel=1 + "Unknown lifecycle rule: {}".format(rule), UserWarning, stacklevel=1 ) @lifecycle_rules.setter diff --git a/tests/unit/test_bucket.py b/tests/unit/test_bucket.py index 94392c540..2aaffd02d 100644 --- a/tests/unit/test_bucket.py +++ b/tests/unit/test_bucket.py @@ -1793,9 +1793,7 @@ def test_lifecycle_rules_getter_unknown_action_type(self, mock_warn): list(bucket.lifecycle_rules) mock_warn.assert_called_with( - "Unknown lifecycle rule: {}".format(BOGUS_RULE), - UserWarning, - stacklevel=1, + "Unknown lifecycle rule: {}".format(BOGUS_RULE), UserWarning, stacklevel=1, ) def test_lifecycle_rules_getter(self): From 6ef73dae5679ffc666257df1aed240716a508d24 Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Fri, 7 May 2021 16:39:25 -0700 Subject: [PATCH 3/4] add client upgrade suggestion to unknown OLM rule warning --- google/cloud/storage/bucket.py | 6 +++++- tests/unit/test_bucket.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/google/cloud/storage/bucket.py b/google/cloud/storage/bucket.py index c028aba42..da8129e6b 100644 --- a/google/cloud/storage/bucket.py +++ b/google/cloud/storage/bucket.py @@ -2362,7 +2362,11 @@ def lifecycle_rules(self): yield LifecycleRuleSetStorageClass.from_api_repr(rule) else: warnings.warn( - "Unknown lifecycle rule: {}".format(rule), UserWarning, stacklevel=1 + "Unknown lifecycle rule by the client: {}. Please upgrade your client.".format( + rule + ), + UserWarning, + stacklevel=1, ) @lifecycle_rules.setter diff --git a/tests/unit/test_bucket.py b/tests/unit/test_bucket.py index 2aaffd02d..4eb9b1774 100644 --- a/tests/unit/test_bucket.py +++ b/tests/unit/test_bucket.py @@ -1793,7 +1793,11 @@ def test_lifecycle_rules_getter_unknown_action_type(self, mock_warn): list(bucket.lifecycle_rules) mock_warn.assert_called_with( - "Unknown lifecycle rule: {}".format(BOGUS_RULE), UserWarning, stacklevel=1, + "Unknown lifecycle rule by the client: {}. Please upgrade your client.".format( + BOGUS_RULE + ), + UserWarning, + stacklevel=1, ) def test_lifecycle_rules_getter(self): From a66e45df815c3f67199783aefc3833a263d75fba Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Sat, 8 May 2021 00:39:29 -0700 Subject: [PATCH 4/4] update warning message --- google/cloud/storage/bucket.py | 2 +- tests/unit/test_bucket.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/google/cloud/storage/bucket.py b/google/cloud/storage/bucket.py index da8129e6b..ac38208a3 100644 --- a/google/cloud/storage/bucket.py +++ b/google/cloud/storage/bucket.py @@ -2362,7 +2362,7 @@ def lifecycle_rules(self): yield LifecycleRuleSetStorageClass.from_api_repr(rule) else: warnings.warn( - "Unknown lifecycle rule by the client: {}. Please upgrade your client.".format( + "Unknown lifecycle rule type received: {}. Please upgrade to the latest version of google-cloud-storage.".format( rule ), UserWarning, diff --git a/tests/unit/test_bucket.py b/tests/unit/test_bucket.py index 4eb9b1774..22984a343 100644 --- a/tests/unit/test_bucket.py +++ b/tests/unit/test_bucket.py @@ -1793,7 +1793,7 @@ def test_lifecycle_rules_getter_unknown_action_type(self, mock_warn): list(bucket.lifecycle_rules) mock_warn.assert_called_with( - "Unknown lifecycle rule by the client: {}. Please upgrade your client.".format( + "Unknown lifecycle rule type received: {}. Please upgrade to the latest version of google-cloud-storage.".format( BOGUS_RULE ), UserWarning,