Skip to content

Commit 928ebbc

Browse files
authored
fix: remove client side validations (#868)
* fix: remove client side validation hmac/notifications * remove client validation on storage class values * revive tests for allowing unspecified states
1 parent 0efa464 commit 928ebbc

File tree

7 files changed

+47
-34
lines changed

7 files changed

+47
-34
lines changed

google/cloud/storage/blob.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,9 +3719,6 @@ def update_storage_class(
37193719
:param retry:
37203720
(Optional) How to retry the RPC. See: :ref:`configuring_retries`
37213721
"""
3722-
if new_class not in self.STORAGE_CLASSES:
3723-
raise ValueError(f"Invalid storage class: {new_class}")
3724-
37253722
# Update current blob's storage class prior to rewrite
37263723
self._patch_property("storageClass", new_class)
37273724

google/cloud/storage/bucket.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,8 +2654,6 @@ def storage_class(self, value):
26542654
or
26552655
:attr:`~google.cloud.storage.constants.DURABLE_REDUCED_AVAILABILITY_LEGACY_STORAGE_CLASS`,
26562656
"""
2657-
if value not in self.STORAGE_CLASSES:
2658-
raise ValueError(f"Invalid storage class: {value}")
26592657
self._patch_property("storageClass", value)
26602658

26612659
@property

google/cloud/storage/hmac_key.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ def state(self):
131131

132132
@state.setter
133133
def state(self, value):
134-
if value not in self._SETTABLE_STATES:
135-
raise ValueError(
136-
f"State may only be set to one of: {', '.join(self._SETTABLE_STATES)}"
137-
)
138-
139134
self._properties["state"] = value
140135

141136
@property
@@ -289,9 +284,6 @@ def delete(self, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
289284
:raises :class:`~google.api_core.exceptions.NotFound`:
290285
if the key does not exist on the back-end.
291286
"""
292-
if self.state != self.INACTIVE_STATE:
293-
raise ValueError("Cannot delete key if not in 'INACTIVE' state.")
294-
295287
qs_params = {}
296288
if self.user_project is not None:
297289
qs_params["userProject"] = self.user_project

google/cloud/storage/notification.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def exists(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
306306
:raises ValueError: if the notification has no ID.
307307
"""
308308
if self.notification_id is None:
309-
raise ValueError("Notification not intialized by server")
309+
raise ValueError("Notification ID not set: set an explicit notification_id")
310310

311311
client = self._require_client(client)
312312

@@ -352,7 +352,7 @@ def reload(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
352352
:raises ValueError: if the notification has no ID.
353353
"""
354354
if self.notification_id is None:
355-
raise ValueError("Notification not intialized by server")
355+
raise ValueError("Notification ID not set: set an explicit notification_id")
356356

357357
client = self._require_client(client)
358358

@@ -395,7 +395,7 @@ def delete(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
395395
:raises ValueError: if the notification has no ID.
396396
"""
397397
if self.notification_id is None:
398-
raise ValueError("Notification not intialized by server")
398+
raise ValueError("Notification ID not set: set an explicit notification_id")
399399

400400
client = self._require_client(client)
401401

tests/unit/test_blob.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5002,17 +5002,6 @@ def test_rewrite_same_name_w_kms_key_w_version(self):
50025002
_target_object=dest,
50035003
)
50045004

5005-
def test_update_storage_class_invalid(self):
5006-
blob_name = "blob-name"
5007-
bucket = _Bucket()
5008-
blob = self._make_one(blob_name, bucket=bucket)
5009-
blob.rewrite = mock.Mock(spec=[])
5010-
5011-
with self.assertRaises(ValueError):
5012-
blob.update_storage_class("BOGUS")
5013-
5014-
blob.rewrite.assert_not_called()
5015-
50165005
def _update_storage_class_multi_pass_helper(self, **kw):
50175006
blob_name = "blob-name"
50185007
storage_class = "NEARLINE"
@@ -5223,6 +5212,38 @@ def test_update_storage_class_single_pass_w_retry(self):
52235212
retry = mock.Mock(spec=[])
52245213
self._update_storage_class_single_pass_helper(retry=retry)
52255214

5215+
def test_update_storage_class_invalid(self):
5216+
from google.cloud.exceptions import BadRequest
5217+
5218+
storage_class = "BOGUS"
5219+
blob_name = "blob-name"
5220+
client = mock.Mock(spec=[])
5221+
bucket = _Bucket(client=client)
5222+
blob = self._make_one(blob_name, bucket=bucket)
5223+
blob.rewrite = mock.Mock(spec=[])
5224+
blob.rewrite.side_effect = BadRequest("Invalid storage class")
5225+
5226+
with self.assertRaises(BadRequest):
5227+
blob.update_storage_class(storage_class)
5228+
5229+
# Test that invalid classes are allowed without client side validation.
5230+
# Fall back to server side validation and errors.
5231+
self.assertEqual(blob.storage_class, storage_class)
5232+
5233+
blob.rewrite.assert_called_once_with(
5234+
blob,
5235+
if_generation_match=None,
5236+
if_generation_not_match=None,
5237+
if_metageneration_match=None,
5238+
if_metageneration_not_match=None,
5239+
if_source_generation_match=None,
5240+
if_source_generation_not_match=None,
5241+
if_source_metageneration_match=None,
5242+
if_source_metageneration_not_match=None,
5243+
timeout=self._get_default_timeout(),
5244+
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
5245+
)
5246+
52265247
def test_cache_control_getter(self):
52275248
BLOB_NAME = "blob-name"
52285249
bucket = _Bucket()

tests/unit/test_bucket.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,11 +2813,15 @@ def test_storage_class_getter(self):
28132813
self.assertEqual(bucket.storage_class, NEARLINE_STORAGE_CLASS)
28142814

28152815
def test_storage_class_setter_invalid(self):
2816+
invalid_class = "BOGUS"
28162817
NAME = "name"
28172818
bucket = self._make_one(name=NAME)
2818-
with self.assertRaises(ValueError):
2819-
bucket.storage_class = "BOGUS"
2820-
self.assertFalse("storageClass" in bucket._changes)
2819+
bucket.storage_class = invalid_class
2820+
2821+
# Test that invalid classes are allowed without client side validation.
2822+
# Fall back to server side validation and errors.
2823+
self.assertEqual(bucket.storage_class, invalid_class)
2824+
self.assertTrue("storageClass" in bucket._changes)
28212825

28222826
def test_storage_class_setter_STANDARD(self):
28232827
from google.cloud.storage.constants import STANDARD_STORAGE_CLASS

tests/unit/test_hmac_key.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ def test_state_getter(self):
149149
def test_state_setter_invalid_state(self):
150150
metadata = self._make_one()
151151
expected = "INVALID"
152+
metadata.state = expected
152153

153-
with self.assertRaises(ValueError):
154-
metadata.state = expected
155-
156-
self.assertIsNone(metadata.state)
154+
# Test that invalid states are allowed without client side validation.
155+
# Fall back to server side validation and errors.
156+
self.assertEqual(metadata.state, expected)
157+
self.assertEqual(metadata._properties["state"], expected)
157158

158159
def test_state_setter_inactive(self):
159160
metadata = self._make_one()

0 commit comments

Comments
 (0)