Skip to content

Commit

Permalink
Rename IAMConfiguration properties to indicate scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Jan 23, 2019
1 parent 924d112 commit c84f099
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
39 changes: 23 additions & 16 deletions storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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", {})
Expand Down
6 changes: 3 additions & 3 deletions storage/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -1536,15 +1536,15 @@ 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
with self.assertRaises(exceptions.BadRequest):
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
Expand Down
40 changes: 21 additions & 19 deletions storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit c84f099

Please sign in to comment.