diff --git a/storage/google/cloud/storage/bucket.py b/storage/google/cloud/storage/bucket.py index 4806260528c4..5603bde2d5f3 100644 --- a/storage/google/cloud/storage/bucket.py +++ b/storage/google/cloud/storage/bucket.py @@ -278,17 +278,24 @@ class IAMConfiguration(dict): :type bucket: :class:`Bucket` :params bucket: Bucket for which this instance is the policy. - :type enabled: bool - :params enabled: (optional) whether the IAM-only policy is enabled for the bucket. + :type bucket_policy_only_enabled: bool + :params bucket_policy_only_enabled: (optional) whether the IAM-only policy is enabled for the bucket. - :type locked_time: :class:`datetime.datetime` - :params locked_time: (optional) When the bucket's IAM-only policy was ehabled. This value should normally only be set by the back-end API. + :type bucket_policy_only_locked_time: :class:`datetime.datetime` + :params bucket_policy_only_locked_time: (optional) When the bucket's IAM-only policy was ehabled. This value should normally only be set by the back-end API. """ - def __init__(self, bucket, enabled=False, locked_time=None): - data = {"bucketPolicyOnly": {"enabled": enabled}} - if locked_time is not None: - data["bucketPolicyOnly"]["lockedTime"] = _datetime_to_rfc3339(locked_time) + def __init__( + self, + bucket, + bucket_policy_only_enabled=False, + bucket_policy_only_locked_time=None, + ): + data = {"bucketPolicyOnly": {"enabled": bucket_policy_only_enabled}} + if bucket_policy_only_locked_time is not None: + data["bucketPolicyOnly"]["lockedTime"] = _datetime_to_rfc3339( + bucket_policy_only_locked_time + ) super(IAMConfiguration, self).__init__(data) self._bucket = bucket @@ -319,7 +326,7 @@ def bucket(self): return self._bucket @property - def bucket_policy_only(self): + def bucket_policy_only_enabled(self): """If set, access checks only use bucket-level IAM policies or above. :rtype: bool @@ -328,24 +335,24 @@ def bucket_policy_only(self): bpo = self.get("bucketPolicyOnly", {}) return bpo.get("enabled", False) - @bucket_policy_only.setter - def bucket_policy_only(self, value): + @bucket_policy_only_enabled.setter + def bucket_policy_only_enabled(self, value): bpo = self.setdefault("bucketPolicyOnly", {}) bpo["enabled"] = bool(value) self.bucket._patch_property("iamConfiguration", self) @property - def locked_time(self): - """Deadline for changing :attr:`bucket_policy_only` from true to false. + def bucket_policy_only_locked_time(self): + """Deadline for changing :attr:`bucket_policy_only_enabled` from true to false. - If the bucket's :attr:`bucket_policy_only` is true, this property + If the bucket's :attr:`bucket_policy_only_enabled` is true, this property is time time after which that setting becomes immutable. - If the bucket's :attr:`bucket_policy_only` is false, this property + If the bucket's :attr:`bucket_policy_only_enabled` is false, this property is ``None``. :rtype: Union[:class:`datetime.datetime`, None] - :returns: (readonly) Time after which :attr:`bucket_policy_only` will + :returns: (readonly) Time after which :attr:`bucket_policy_only_enabled` will be frozen as true. """ bpo = self.get("bucketPolicyOnly", {}) diff --git a/storage/tests/system.py b/storage/tests/system.py index 41d1fc905d07..32609369e272 100644 --- a/storage/tests/system.py +++ b/storage/tests/system.py @@ -1487,7 +1487,7 @@ def test_new_bucket_w_bpo(self): exceptions.NotFound, Config.CLIENT.get_bucket, new_bucket_name ) bucket = Config.CLIENT.bucket(new_bucket_name) - bucket.iam_configuration.bucket_policy_only = True + bucket.iam_configuration.bucket_policy_only_enabled = True retry_429(bucket.create)() self.case_buckets_to_delete.append(new_bucket_name) @@ -1536,7 +1536,7 @@ def test_bpo_set_unset_preserves_acls(self): blob_acl_before = list(bucket.acl) # Set BPO - bucket.iam_configuration.bucket_policy_only = True + bucket.iam_configuration.bucket_policy_only_enabled = True bucket.patch() # While BPO is set, cannot get / set ACLs @@ -1544,7 +1544,7 @@ def test_bpo_set_unset_preserves_acls(self): bucket.acl.reload() # Clear BPO - bucket.iam_configuration.bucket_policy_only = False + bucket.iam_configuration.bucket_policy_only_enabled = False bucket.patch() # Query ACLs after clearing BPO diff --git a/storage/tests/unit/test_bucket.py b/storage/tests/unit/test_bucket.py index 1b4c437b5235..5ed9bdc723c9 100644 --- a/storage/tests/unit/test_bucket.py +++ b/storage/tests/unit/test_bucket.py @@ -197,8 +197,8 @@ def test_ctor_defaults(self): config = self._make_one(bucket) self.assertIs(config.bucket, bucket) - self.assertFalse(config.bucket_policy_only) - self.assertIsNone(config.locked_time) + self.assertFalse(config.bucket_policy_only_enabled) + self.assertIsNone(config.bucket_policy_only_locked_time) def test_ctor_explicit(self): import datetime @@ -207,11 +207,13 @@ def test_ctor_explicit(self): bucket = self._make_bucket() now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) - config = self._make_one(bucket, enabled=True, locked_time=now) + config = self._make_one( + bucket, bucket_policy_only_enabled=True, bucket_policy_only_locked_time=now + ) self.assertIs(config.bucket, bucket) - self.assertTrue(config.bucket_policy_only) - self.assertEqual(config.locked_time, now) + self.assertTrue(config.bucket_policy_only_enabled) + self.assertEqual(config.bucket_policy_only_locked_time, now) def test_from_api_repr_w_empty_resource(self): klass = self._get_target_class() @@ -221,8 +223,8 @@ def test_from_api_repr_w_empty_resource(self): config = klass.from_api_repr(resource, bucket) self.assertIs(config.bucket, bucket) - self.assertFalse(config.bucket_policy_only) - self.assertIsNone(config.locked_time) + self.assertFalse(config.bucket_policy_only_enabled) + self.assertIsNone(config.bucket_policy_only_locked_time) def test_from_api_repr_w_empty_bpo(self): klass = self._get_target_class() @@ -232,8 +234,8 @@ def test_from_api_repr_w_empty_bpo(self): config = klass.from_api_repr(resource, bucket) self.assertIs(config.bucket, bucket) - self.assertFalse(config.bucket_policy_only) - self.assertIsNone(config.locked_time) + self.assertFalse(config.bucket_policy_only_enabled) + self.assertIsNone(config.bucket_policy_only_locked_time) def test_from_api_repr_w_disabled(self): klass = self._get_target_class() @@ -243,8 +245,8 @@ def test_from_api_repr_w_disabled(self): config = klass.from_api_repr(resource, bucket) self.assertIs(config.bucket, bucket) - self.assertFalse(config.bucket_policy_only) - self.assertIsNone(config.locked_time) + self.assertFalse(config.bucket_policy_only_enabled) + self.assertIsNone(config.bucket_policy_only_locked_time) def test_from_api_repr_w_enabled(self): import datetime @@ -264,14 +266,14 @@ def test_from_api_repr_w_enabled(self): config = klass.from_api_repr(resource, bucket) self.assertIs(config.bucket, bucket) - self.assertTrue(config.bucket_policy_only) - self.assertEqual(config.locked_time, now) + self.assertTrue(config.bucket_policy_only_enabled) + self.assertEqual(config.bucket_policy_only_locked_time, now) - def test_bucket_policy_only_setter(self): + def test_bucket_policy_only_enabled_setter(self): bucket = self._make_bucket() config = self._make_one(bucket) - config.bucket_policy_only = True + config.bucket_policy_only_enabled = True self.assertTrue(config["bucketPolicyOnly"]["enabled"]) bucket._patch_property.assert_called_once_with("iamConfiguration", config) @@ -1204,8 +1206,8 @@ def test_iam_configuration_policy_missing(self): self.assertIsInstance(config, IAMConfiguration) self.assertIs(config.bucket, bucket) - self.assertFalse(config.bucket_policy_only) - self.assertIsNone(config.locked_time) + self.assertFalse(config.bucket_policy_only_enabled) + self.assertIsNone(config.bucket_policy_only_locked_time) def test_iam_configuration_policy_w_entry(self): import datetime @@ -1229,8 +1231,8 @@ def test_iam_configuration_policy_w_entry(self): self.assertIsInstance(config, IAMConfiguration) self.assertIs(config.bucket, bucket) - self.assertTrue(config.bucket_policy_only) - self.assertEqual(config.locked_time, now) + self.assertTrue(config.bucket_policy_only_enabled) + self.assertEqual(config.bucket_policy_only_locked_time, now) def test_lifecycle_rules_getter_unknown_action_type(self): NAME = "name"