Skip to content

Commit 80129f1

Browse files
committed
Add 'Bucket.lock_retention_policy' API wrapper. (#448)
Toward #445.
1 parent 6a1866e commit 80129f1

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

storage/google/cloud/storage/bucket.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,3 +1513,37 @@ def generate_upload_policy(
15131513
}
15141514

15151515
return fields
1516+
1517+
def lock_retention_policy(self, client=None):
1518+
"""Lock the bucket's retention policy.
1519+
1520+
:raises ValueError:
1521+
if the bucket has no metageneration (i.e., new or never reloaded);
1522+
if the bucket has no retention policy assigned;
1523+
if the bucket's retention policy is already locked.
1524+
"""
1525+
if 'metageneration' not in self._properties:
1526+
raise ValueError(
1527+
"Bucket has no retention policy assigned: try 'reload'?")
1528+
1529+
policy = self._properties.get('retentionPolicy')
1530+
1531+
if policy is None:
1532+
raise ValueError(
1533+
"Bucket has no retention policy assigned: try 'reload'?")
1534+
1535+
if policy.get('isLocked'):
1536+
raise ValueError("Bucket's retention policy is already locked.")
1537+
1538+
client = self._require_client(client)
1539+
1540+
query_params = {'metageneration': self.metageneration}
1541+
1542+
if self.user_project is not None:
1543+
query_params['userProject'] = self.user_project
1544+
1545+
path = '/b/{}/lockRetentionPolicy'.format(self.name)
1546+
api_response = client._connection.api_request(
1547+
method='POST', path=path, query_params=query_params,
1548+
_target_object=self)
1549+
self._set_properties(api_response)

storage/tests/unit/test_bucket.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,114 @@ def test_generate_upload_policy_bad_credentials(self):
20482048
with self.assertRaises(AttributeError):
20492049
bucket.generate_upload_policy([])
20502050

2051+
def test_lock_retention_policy_no_policy_set(self):
2052+
credentials = object()
2053+
connection = _Connection()
2054+
connection.credentials = credentials
2055+
client = _Client(connection)
2056+
name = 'name'
2057+
bucket = self._make_one(client=client, name=name)
2058+
bucket._properties['metageneration'] = 1234
2059+
2060+
with self.assertRaises(ValueError):
2061+
bucket.lock_retention_policy()
2062+
2063+
def test_lock_retention_policy_no_metageneration(self):
2064+
credentials = object()
2065+
connection = _Connection()
2066+
connection.credentials = credentials
2067+
client = _Client(connection)
2068+
name = 'name'
2069+
bucket = self._make_one(client=client, name=name)
2070+
bucket._properties['retentionPolicy'] = {
2071+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2072+
'retentionPeriod': 86400 * 100, # 100 days
2073+
}
2074+
2075+
with self.assertRaises(ValueError):
2076+
bucket.lock_retention_policy()
2077+
2078+
def test_lock_retention_policy_already_locked(self):
2079+
credentials = object()
2080+
connection = _Connection()
2081+
connection.credentials = credentials
2082+
client = _Client(connection)
2083+
name = 'name'
2084+
bucket = self._make_one(client=client, name=name)
2085+
bucket._properties['metageneration'] = 1234
2086+
bucket._properties['retentionPolicy'] = {
2087+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2088+
'isLocked': True,
2089+
'retentionPeriod': 86400 * 100, # 100 days
2090+
}
2091+
2092+
with self.assertRaises(ValueError):
2093+
bucket.lock_retention_policy()
2094+
2095+
def test_lock_retention_policy_ok(self):
2096+
name = 'name'
2097+
response = {
2098+
'name': name,
2099+
'metageneration': 1235,
2100+
'retentionPolicy': {
2101+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2102+
'isLocked': True,
2103+
'retentionPeriod': 86400 * 100, # 100 days
2104+
},
2105+
}
2106+
credentials = object()
2107+
connection = _Connection(response)
2108+
connection.credentials = credentials
2109+
client = _Client(connection)
2110+
bucket = self._make_one(client=client, name=name)
2111+
bucket._properties['metageneration'] = 1234
2112+
bucket._properties['retentionPolicy'] = {
2113+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2114+
'retentionPeriod': 86400 * 100, # 100 days
2115+
}
2116+
2117+
bucket.lock_retention_policy()
2118+
2119+
kw, = connection._requested
2120+
self.assertEqual(kw['method'], 'POST')
2121+
self.assertEqual(kw['path'], '/b/{}/lockRetentionPolicy'.format(name))
2122+
self.assertEqual(kw['query_params'], {'metageneration': 1234})
2123+
2124+
def test_lock_retention_policy_w_user_project(self):
2125+
name = 'name'
2126+
user_project = 'user-project-123'
2127+
response = {
2128+
'name': name,
2129+
'metageneration': 1235,
2130+
'retentionPolicy': {
2131+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2132+
'isLocked': True,
2133+
'retentionPeriod': 86400 * 100, # 100 days
2134+
},
2135+
}
2136+
credentials = object()
2137+
connection = _Connection(response)
2138+
connection.credentials = credentials
2139+
client = _Client(connection)
2140+
bucket = self._make_one(
2141+
client=client, name=name, user_project=user_project)
2142+
bucket._properties['metageneration'] = 1234
2143+
bucket._properties['retentionPolicy'] = {
2144+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2145+
'retentionPeriod': 86400 * 100, # 100 days
2146+
}
2147+
2148+
bucket.lock_retention_policy()
2149+
2150+
kw, = connection._requested
2151+
self.assertEqual(kw['method'], 'POST')
2152+
self.assertEqual(kw['path'], '/b/{}/lockRetentionPolicy'.format(name))
2153+
self.assertEqual(
2154+
kw['query_params'], {
2155+
'metageneration': 1234,
2156+
'userProject': user_project,
2157+
})
2158+
20512159

20522160
class _Connection(object):
20532161
_delete_bucket = False

0 commit comments

Comments
 (0)