Skip to content

Commit 6814a4f

Browse files
committed
Express BPO as scalar properties, rather than a helper object.
1 parent d7cd74a commit 6814a4f

3 files changed

Lines changed: 142 additions & 229 deletions

File tree

storage/google/cloud/storage/bucket.py

Lines changed: 42 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -272,82 +272,6 @@ def from_api_repr(cls, resource):
272272
return instance
273273

274274

275-
class IAMConfiguration(dict):
276-
"""Map a bucket's IAM configuration.
277-
278-
:type bucket: :class:`Bucket`
279-
:params bucket: Bucket for which this instance is the policy.
280-
281-
:type enabled: bool
282-
:params enabled: (optional) whether the IAM-only policy is enabled for the bucket.
283-
284-
:type locked_time: :class:`datetime.datetime`
285-
: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.
286-
"""
287-
288-
def __init__(self, bucket, enabled=False, locked_time=None):
289-
data = {"bucketPolicyOnly": {"enabled": enabled}}
290-
if locked_time is not None:
291-
data["bucketPolicyOnly"]["lockedTime"] = _datetime_to_rfc3339(locked_time)
292-
super(IAMConfiguration, self).__init__(data)
293-
self._bucket = bucket
294-
295-
@classmethod
296-
def from_api_repr(cls, resource, bucket):
297-
"""Factory: construct instance from resource.
298-
299-
:type bucket: :class:`Bucket`
300-
:params bucket: Bucket for which this instance is the policy.
301-
302-
:type resource: dict
303-
:param resource: mapping as returned from API call.
304-
305-
:rtype: :class:`IAMConfiguration`
306-
:returns: Instance created from resource.
307-
"""
308-
instance = cls(bucket)
309-
instance.update(resource)
310-
return instance
311-
312-
@property
313-
def bucket(self):
314-
"""Bucket for which this instance is the policy.
315-
316-
:rtype: :class:`Bucket`
317-
:returns: the instance's bucket.
318-
"""
319-
return self._bucket
320-
321-
@property
322-
def bucket_policy_only(self):
323-
"""Is the bucket configured to allow only IAM policy?
324-
325-
:rtype: bool
326-
:returns: whether the bucket is configured to allow only IAM.
327-
"""
328-
bpo = self.get("bucketPolicyOnly", {})
329-
return bpo.get("enabled", False)
330-
331-
@bucket_policy_only.setter
332-
def bucket_policy_only(self, value):
333-
bpo = self.setdefault("bucketPolicyOnly", {})
334-
bpo["enabled"] = bool(value)
335-
self.bucket._patch_property("iamConfiguration", self)
336-
337-
@property
338-
def locked_time(self):
339-
"""When was the bucket configured to allow only IAM policy?
340-
341-
:rtype: Union[:class:`datetime.datetime`, None]
342-
:returns: (readonly) the time the bucket's IAM-only policy was set.
343-
"""
344-
bpo = self.get("bucketPolicyOnly", {})
345-
stamp = bpo.get("lockedTime")
346-
if stamp is not None:
347-
stamp = _rfc3339_to_datetime(stamp)
348-
return stamp
349-
350-
351275
class Bucket(_PropertyMixin):
352276
"""A class representing a Bucket on Cloud Storage.
353277
@@ -1210,16 +1134,6 @@ def id(self):
12101134
"""
12111135
return self._properties.get("id")
12121136

1213-
@property
1214-
def iam_configuration(self):
1215-
"""Retrieve IAM configuration for this bucket.
1216-
1217-
:rtype: :class:`IAMConfiguration`
1218-
:returns: an instance for managing the bucket's IAM configuration.
1219-
"""
1220-
info = self._properties.get("iamConfiguration", {})
1221-
return IAMConfiguration.from_api_repr(info, self)
1222-
12231137
@property
12241138
def lifecycle_rules(self):
12251139
"""Retrieve or set lifecycle rules configured for this bucket.
@@ -1482,6 +1396,48 @@ def retention_period(self, value):
14821396
policy = None
14831397
self._patch_property("retentionPolicy", policy)
14841398

1399+
@property
1400+
def iam_configuration_bucket_policy_only(self):
1401+
"""Get/set whether the bucket is configured to allow only bucket IAM.
1402+
1403+
If set, then ACLs are no longer in effect for the bucket or its
1404+
blobs.
1405+
1406+
:rtype: bool
1407+
:returns: True if the bucket allows only IAM, else false.
1408+
"""
1409+
iam_config = self._properties.get("iamConfiguration", {})
1410+
bpo = iam_config.get("bucketPolicyOnly", {})
1411+
return bpo.get("enabled")
1412+
1413+
@iam_configuration_bucket_policy_only.setter
1414+
def iam_configuration_bucket_policy_only(self, value):
1415+
"""Set whether the bucket is configured to allow only bucket IAM.
1416+
1417+
:type value: bool
1418+
:param value:
1419+
If true, the bucket will allow only IAM; if false, then the
1420+
ACLs for the bucket and its blobs will be effective.
1421+
"""
1422+
iam_config = self._properties.setdefault("iamConfiguration", {})
1423+
iam_config["bucketPolicyOnly"] = {"enabled": bool(value)}
1424+
self._patch_property("iamConfiguration", iam_config)
1425+
1426+
@property
1427+
def iam_configuration_locked_time(self):
1428+
"""Time when the bucket was configured to allow only bucket IAM.
1429+
1430+
:rtype: datetime.datetime or None
1431+
:returns:
1432+
point in time when the bucket was configured to allow only IAM.
1433+
"""
1434+
iam_config = self._properties.get("iamConfiguration", {})
1435+
bpo = iam_config.get("bucketPolicyOnly", {})
1436+
stamp = bpo.get('lockedTime')
1437+
if stamp is not None:
1438+
stamp = _rfc3339_to_datetime(stamp)
1439+
return stamp
1440+
14851441
@property
14861442
def self_link(self):
14871443
"""Retrieve the URI for the bucket.

storage/tests/system.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ def test_new_bucket_w_bpo(self):
14871487
exceptions.NotFound, Config.CLIENT.get_bucket, new_bucket_name
14881488
)
14891489
bucket = Config.CLIENT.bucket(new_bucket_name)
1490-
bucket.iam_configuration.bucket_policy_only = True
1490+
bucket.iam_configuration_bucket_policy_only = True
14911491
retry_429(bucket.create)()
14921492
self.case_buckets_to_delete.append(new_bucket_name)
14931493

@@ -1536,7 +1536,7 @@ def test_bpo_set_unset_preserves_acls(self):
15361536
blob_acl_before = list(bucket.acl)
15371537

15381538
# Set BPO
1539-
bucket.iam_configuration.bucket_policy_only = True
1539+
bucket.iam_configuration_bucket_policy_only = True
15401540
bucket.patch()
15411541

15421542
# While BPO is set, cannot get / set ACLs
@@ -1557,7 +1557,7 @@ def test_bpo_set_unset_preserves_acls(self):
15571557
blob.acl.clear()
15581558

15591559
# Clear BPO
1560-
bucket.iam_configuration.bucket_policy_only = False
1560+
bucket.iam_configuration_bucket_policy_only = False
15611561
bucket.patch()
15621562

15631563
# Query ACLs after clearing BPO

0 commit comments

Comments
 (0)