Skip to content

Commit 1a5f382

Browse files
committed
update conformance tests and split default behavior of ACL
1 parent 02d720a commit 1a5f382

File tree

5 files changed

+245
-14
lines changed

5 files changed

+245
-14
lines changed

google/cloud/storage/acl.py

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,185 @@ def save_path(self):
752752
def user_project(self):
753753
"""Compute the user project charged for API requests for this ACL."""
754754
return self.blob.user_project
755+
756+
def save(
757+
self,
758+
acl=None,
759+
client=None,
760+
if_generation_match=None,
761+
if_generation_not_match=None,
762+
if_metageneration_match=None,
763+
if_metageneration_not_match=None,
764+
timeout=_DEFAULT_TIMEOUT,
765+
retry=DEFAULT_RETRY,
766+
):
767+
"""Save this ACL for the current object.
768+
769+
If :attr:`user_project` is set, bills the API request to that project.
770+
771+
:type acl: :class:`google.cloud.storage.acl.ACL`, or a compatible list.
772+
:param acl: The ACL object to save. If left blank, this will save
773+
current entries.
774+
775+
:type client: :class:`~google.cloud.storage.client.Client` or
776+
``NoneType``
777+
:param client: (Optional) The client to use. If not passed, falls back
778+
to the ``client`` stored on the ACL's parent.
779+
780+
:type if_generation_match: long
781+
:param if_generation_match:
782+
(Optional) See :ref:`using-if-generation-match`
783+
784+
:type if_generation_not_match: long
785+
:param if_generation_not_match:
786+
(Optional) See :ref:`using-if-generation-not-match`
787+
788+
:type if_metageneration_match: long
789+
:param if_metageneration_match:
790+
(Optional) See :ref:`using-if-metageneration-match`
791+
792+
:type if_metageneration_not_match: long
793+
:param if_metageneration_not_match:
794+
(Optional) See :ref:`using-if-metageneration-not-match`
795+
796+
:type timeout: float or tuple
797+
:param timeout:
798+
(Optional) The amount of time, in seconds, to wait
799+
for the server response. See: :ref:`configuring_timeouts`
800+
801+
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
802+
:param retry:
803+
(Optional) How to retry the RPC. See: :ref:`configuring_retries`
804+
"""
805+
super().save(
806+
acl=acl,
807+
client=client,
808+
if_generation_match=if_generation_match,
809+
if_generation_not_match=if_generation_not_match,
810+
if_metageneration_match=if_metageneration_match,
811+
if_metageneration_not_match=if_metageneration_not_match,
812+
timeout=timeout,
813+
retry=retry,
814+
)
815+
816+
def save_predefined(
817+
self,
818+
predefined,
819+
client=None,
820+
if_generation_match=None,
821+
if_generation_not_match=None,
822+
if_metageneration_match=None,
823+
if_metageneration_not_match=None,
824+
timeout=_DEFAULT_TIMEOUT,
825+
retry=DEFAULT_RETRY,
826+
):
827+
"""Save this ACL for the current object using a predefined ACL.
828+
829+
If :attr:`user_project` is set, bills the API request to that project.
830+
831+
:type predefined: str
832+
:param predefined: An identifier for a predefined ACL. Must be one
833+
of the keys in :attr:`PREDEFINED_JSON_ACLS`
834+
or :attr:`PREDEFINED_XML_ACLS` (which will be
835+
aliased to the corresponding JSON name).
836+
If passed, `acl` must be None.
837+
838+
:type client: :class:`~google.cloud.storage.client.Client` or
839+
``NoneType``
840+
:param client: (Optional) The client to use. If not passed, falls back
841+
to the ``client`` stored on the ACL's parent.
842+
843+
:type if_generation_match: long
844+
:param if_generation_match:
845+
(Optional) See :ref:`using-if-generation-match`
846+
847+
:type if_generation_not_match: long
848+
:param if_generation_not_match:
849+
(Optional) See :ref:`using-if-generation-not-match`
850+
851+
:type if_metageneration_match: long
852+
:param if_metageneration_match:
853+
(Optional) See :ref:`using-if-metageneration-match`
854+
855+
:type if_metageneration_not_match: long
856+
:param if_metageneration_not_match:
857+
(Optional) See :ref:`using-if-metageneration-not-match`
858+
859+
:type timeout: float or tuple
860+
:param timeout:
861+
(Optional) The amount of time, in seconds, to wait
862+
for the server response. See: :ref:`configuring_timeouts`
863+
864+
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
865+
:param retry:
866+
(Optional) How to retry the RPC. See: :ref:`configuring_retries`
867+
"""
868+
super().save_predefined(
869+
predefined=predefined,
870+
client=client,
871+
if_generation_match=if_generation_match,
872+
if_generation_not_match=if_generation_not_match,
873+
if_metageneration_match=if_metageneration_match,
874+
if_metageneration_not_match=if_metageneration_not_match,
875+
timeout=timeout,
876+
retry=retry,
877+
)
878+
879+
def clear(
880+
self,
881+
client=None,
882+
if_generation_match=None,
883+
if_generation_not_match=None,
884+
if_metageneration_match=None,
885+
if_metageneration_not_match=None,
886+
timeout=_DEFAULT_TIMEOUT,
887+
retry=DEFAULT_RETRY,
888+
):
889+
"""Remove all ACL entries.
890+
891+
If :attr:`user_project` is set, bills the API request to that project.
892+
893+
Note that this won't actually remove *ALL* the rules, but it
894+
will remove all the non-default rules. In short, you'll still
895+
have access to a bucket that you created even after you clear
896+
ACL rules with this method.
897+
898+
:type client: :class:`~google.cloud.storage.client.Client` or
899+
``NoneType``
900+
:param client: (Optional) The client to use. If not passed, falls back
901+
to the ``client`` stored on the ACL's parent.
902+
903+
:type if_generation_match: long
904+
:param if_generation_match:
905+
(Optional) See :ref:`using-if-generation-match`
906+
907+
:type if_generation_not_match: long
908+
:param if_generation_not_match:
909+
(Optional) See :ref:`using-if-generation-not-match`
910+
911+
:type if_metageneration_match: long
912+
:param if_metageneration_match:
913+
(Optional) See :ref:`using-if-metageneration-match`
914+
915+
:type if_metageneration_not_match: long
916+
:param if_metageneration_not_match:
917+
(Optional) See :ref:`using-if-metageneration-not-match`
918+
919+
:type timeout: float or tuple
920+
:param timeout:
921+
(Optional) The amount of time, in seconds, to wait
922+
for the server response. See: :ref:`configuring_timeouts`
923+
924+
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
925+
:param retry:
926+
(Optional) How to retry the RPC. See: :ref:`configuring_retries`
927+
"""
928+
super().clear(
929+
client=client,
930+
if_generation_match=if_generation_match,
931+
if_generation_not_match=if_generation_not_match,
932+
if_metageneration_match=if_metageneration_match,
933+
if_metageneration_not_match=if_metageneration_not_match,
934+
timeout=timeout,
935+
retry=retry,
936+
)

google/cloud/storage/blob.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
from google.cloud.storage.retry import DEFAULT_RETRY
7777
from google.cloud.storage.retry import DEFAULT_RETRY_IF_ETAG_IN_JSON
7878
from google.cloud.storage.retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
79-
from google.cloud.storage.retry import DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED
8079
from google.cloud.storage.fileio import BlobReader
8180
from google.cloud.storage.fileio import BlobWriter
8281

@@ -3439,7 +3438,7 @@ def make_public(
34393438
if_generation_not_match=None,
34403439
if_metageneration_match=None,
34413440
if_metageneration_not_match=None,
3442-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
3441+
retry=DEFAULT_RETRY,
34433442
):
34443443
"""Update blob's ACL, granting read access to anonymous users.
34453444
@@ -3493,7 +3492,7 @@ def make_private(
34933492
if_generation_not_match=None,
34943493
if_metageneration_match=None,
34953494
if_metageneration_not_match=None,
3496-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
3495+
retry=DEFAULT_RETRY,
34973496
):
34983497
"""Update blob's ACL, revoking read access for anonymous users.
34993498

tests/unit/test_acl.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,59 @@ def test_user_project(self):
10701070
blob.user_project = USER_PROJECT
10711071
self.assertEqual(acl.user_project, USER_PROJECT)
10721072

1073+
def test_passthrough_methods(self):
1074+
NAME = "name"
1075+
BLOB_NAME = "blob-name"
1076+
bucket = _Bucket(NAME)
1077+
blob = _Blob(bucket, BLOB_NAME)
1078+
acl = self._make_one(blob)
1079+
1080+
client = mock.Mock()
1081+
1082+
with mock.patch("google.cloud.storage.acl.ACL.clear") as m:
1083+
kwargs = {
1084+
"client": client,
1085+
"if_generation_match": 1,
1086+
"if_generation_not_match": 2,
1087+
"if_metageneration_match": 3,
1088+
"if_metageneration_not_match": 4,
1089+
"timeout": 60,
1090+
"retry": None,
1091+
}
1092+
1093+
acl.clear(**kwargs)
1094+
m.assert_called_once_with(**kwargs)
1095+
1096+
with mock.patch("google.cloud.storage.acl.ACL.save") as m:
1097+
kwargs = {
1098+
"acl": [],
1099+
"client": client,
1100+
"if_generation_match": 1,
1101+
"if_generation_not_match": 2,
1102+
"if_metageneration_match": 3,
1103+
"if_metageneration_not_match": 4,
1104+
"timeout": 60,
1105+
"retry": None,
1106+
}
1107+
1108+
acl.save(**kwargs)
1109+
m.assert_called_once_with(**kwargs)
1110+
1111+
with mock.patch("google.cloud.storage.acl.ACL.save_predefined") as m:
1112+
kwargs = {
1113+
"predefined": "predef",
1114+
"client": client,
1115+
"if_generation_match": 1,
1116+
"if_generation_not_match": 2,
1117+
"if_metageneration_match": 3,
1118+
"if_metageneration_not_match": 4,
1119+
"timeout": 60,
1120+
"retry": None,
1121+
}
1122+
1123+
acl.save_predefined(**kwargs)
1124+
m.assert_called_once_with(**kwargs)
1125+
10731126

10741127
class _Blob(object):
10751128
user_project = None

tests/unit/test_blob.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
from google.cloud.storage._helpers import _UTC
3737
from google.cloud.storage.exceptions import DataCorruption
3838
from google.cloud.storage.exceptions import InvalidResponse
39-
from google.cloud.storage.retry import (
40-
DEFAULT_RETRY,
41-
DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
42-
)
39+
from google.cloud.storage.retry import DEFAULT_RETRY
4340
from google.cloud.storage.retry import DEFAULT_RETRY_IF_ETAG_IN_JSON
4441
from google.cloud.storage.retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
4542
from tests.unit.test__helpers import GCCL_INVOCATION_TEST_CONST
@@ -4146,7 +4143,7 @@ def test_make_public_w_defaults(self):
41464143
expected_patch_data,
41474144
query_params=expected_query_params,
41484145
timeout=self._get_default_timeout(),
4149-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
4146+
retry=DEFAULT_RETRY,
41504147
)
41514148

41524149
def test_make_public_w_timeout(self):
@@ -4173,7 +4170,7 @@ def test_make_public_w_timeout(self):
41734170
expected_patch_data,
41744171
query_params=expected_query_params,
41754172
timeout=timeout,
4176-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
4173+
retry=DEFAULT_RETRY,
41774174
)
41784175

41794176
def test_make_public_w_preconditions(self):
@@ -4203,7 +4200,7 @@ def test_make_public_w_preconditions(self):
42034200
expected_patch_data,
42044201
query_params=expected_query_params,
42054202
timeout=self._get_default_timeout(),
4206-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
4203+
retry=DEFAULT_RETRY,
42074204
)
42084205

42094206
def test_make_private_w_defaults(self):
@@ -4227,7 +4224,7 @@ def test_make_private_w_defaults(self):
42274224
expected_patch_data,
42284225
query_params=expected_query_params,
42294226
timeout=self._get_default_timeout(),
4230-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
4227+
retry=DEFAULT_RETRY,
42314228
)
42324229

42334230
def test_make_private_w_timeout(self):
@@ -4252,7 +4249,7 @@ def test_make_private_w_timeout(self):
42524249
expected_patch_data,
42534250
query_params=expected_query_params,
42544251
timeout=timeout,
4255-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
4252+
retry=DEFAULT_RETRY,
42564253
)
42574254

42584255
def test_make_private_w_preconditions(self):
@@ -4280,7 +4277,7 @@ def test_make_private_w_preconditions(self):
42804277
expected_patch_data,
42814278
query_params=expected_query_params,
42824279
timeout=self._get_default_timeout(),
4283-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
4280+
retry=DEFAULT_RETRY,
42844281
)
42854282

42864283
def test_compose_wo_content_type_set(self):

tests/unit/test_bucket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2252,7 +2252,7 @@ def test_copy_blob_w_preserve_acl_false_w_explicit_client(self):
22522252
expected_patch_data,
22532253
query_params=expected_patch_query_params,
22542254
timeout=self._get_default_timeout(),
2255-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
2255+
retry=DEFAULT_RETRY,
22562256
)
22572257

22582258
def test_copy_blob_w_name_and_user_project(self):

0 commit comments

Comments
 (0)