Skip to content

Commit 16d7da0

Browse files
committed
Add 'Bucket.lock_retention_policy' API wrapper. (#448)
Toward #445.
1 parent b03b6fe commit 16d7da0

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
@@ -1781,3 +1781,37 @@ def generate_upload_policy(
17811781
}
17821782

17831783
return fields
1784+
1785+
def lock_retention_policy(self, client=None):
1786+
"""Lock the bucket's retention policy.
1787+
1788+
:raises ValueError:
1789+
if the bucket has no metageneration (i.e., new or never reloaded);
1790+
if the bucket has no retention policy assigned;
1791+
if the bucket's retention policy is already locked.
1792+
"""
1793+
if 'metageneration' not in self._properties:
1794+
raise ValueError(
1795+
"Bucket has no retention policy assigned: try 'reload'?")
1796+
1797+
policy = self._properties.get('retentionPolicy')
1798+
1799+
if policy is None:
1800+
raise ValueError(
1801+
"Bucket has no retention policy assigned: try 'reload'?")
1802+
1803+
if policy.get('isLocked'):
1804+
raise ValueError("Bucket's retention policy is already locked.")
1805+
1806+
client = self._require_client(client)
1807+
1808+
query_params = {'metageneration': self.metageneration}
1809+
1810+
if self.user_project is not None:
1811+
query_params['userProject'] = self.user_project
1812+
1813+
path = '/b/{}/lockRetentionPolicy'.format(self.name)
1814+
api_response = client._connection.api_request(
1815+
method='POST', path=path, query_params=query_params,
1816+
_target_object=self)
1817+
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
@@ -2414,6 +2414,114 @@ def test_generate_upload_policy_bad_credentials(self):
24142414
with self.assertRaises(AttributeError):
24152415
bucket.generate_upload_policy([])
24162416

2417+
def test_lock_retention_policy_no_policy_set(self):
2418+
credentials = object()
2419+
connection = _Connection()
2420+
connection.credentials = credentials
2421+
client = _Client(connection)
2422+
name = 'name'
2423+
bucket = self._make_one(client=client, name=name)
2424+
bucket._properties['metageneration'] = 1234
2425+
2426+
with self.assertRaises(ValueError):
2427+
bucket.lock_retention_policy()
2428+
2429+
def test_lock_retention_policy_no_metageneration(self):
2430+
credentials = object()
2431+
connection = _Connection()
2432+
connection.credentials = credentials
2433+
client = _Client(connection)
2434+
name = 'name'
2435+
bucket = self._make_one(client=client, name=name)
2436+
bucket._properties['retentionPolicy'] = {
2437+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2438+
'retentionPeriod': 86400 * 100, # 100 days
2439+
}
2440+
2441+
with self.assertRaises(ValueError):
2442+
bucket.lock_retention_policy()
2443+
2444+
def test_lock_retention_policy_already_locked(self):
2445+
credentials = object()
2446+
connection = _Connection()
2447+
connection.credentials = credentials
2448+
client = _Client(connection)
2449+
name = 'name'
2450+
bucket = self._make_one(client=client, name=name)
2451+
bucket._properties['metageneration'] = 1234
2452+
bucket._properties['retentionPolicy'] = {
2453+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2454+
'isLocked': True,
2455+
'retentionPeriod': 86400 * 100, # 100 days
2456+
}
2457+
2458+
with self.assertRaises(ValueError):
2459+
bucket.lock_retention_policy()
2460+
2461+
def test_lock_retention_policy_ok(self):
2462+
name = 'name'
2463+
response = {
2464+
'name': name,
2465+
'metageneration': 1235,
2466+
'retentionPolicy': {
2467+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2468+
'isLocked': True,
2469+
'retentionPeriod': 86400 * 100, # 100 days
2470+
},
2471+
}
2472+
credentials = object()
2473+
connection = _Connection(response)
2474+
connection.credentials = credentials
2475+
client = _Client(connection)
2476+
bucket = self._make_one(client=client, name=name)
2477+
bucket._properties['metageneration'] = 1234
2478+
bucket._properties['retentionPolicy'] = {
2479+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2480+
'retentionPeriod': 86400 * 100, # 100 days
2481+
}
2482+
2483+
bucket.lock_retention_policy()
2484+
2485+
kw, = connection._requested
2486+
self.assertEqual(kw['method'], 'POST')
2487+
self.assertEqual(kw['path'], '/b/{}/lockRetentionPolicy'.format(name))
2488+
self.assertEqual(kw['query_params'], {'metageneration': 1234})
2489+
2490+
def test_lock_retention_policy_w_user_project(self):
2491+
name = 'name'
2492+
user_project = 'user-project-123'
2493+
response = {
2494+
'name': name,
2495+
'metageneration': 1235,
2496+
'retentionPolicy': {
2497+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2498+
'isLocked': True,
2499+
'retentionPeriod': 86400 * 100, # 100 days
2500+
},
2501+
}
2502+
credentials = object()
2503+
connection = _Connection(response)
2504+
connection.credentials = credentials
2505+
client = _Client(connection)
2506+
bucket = self._make_one(
2507+
client=client, name=name, user_project=user_project)
2508+
bucket._properties['metageneration'] = 1234
2509+
bucket._properties['retentionPolicy'] = {
2510+
'effectiveTime': '2018-03-01T16:46:27.123456Z',
2511+
'retentionPeriod': 86400 * 100, # 100 days
2512+
}
2513+
2514+
bucket.lock_retention_policy()
2515+
2516+
kw, = connection._requested
2517+
self.assertEqual(kw['method'], 'POST')
2518+
self.assertEqual(kw['path'], '/b/{}/lockRetentionPolicy'.format(name))
2519+
self.assertEqual(
2520+
kw['query_params'], {
2521+
'metageneration': 1234,
2522+
'userProject': user_project,
2523+
})
2524+
24172525

24182526
class _Connection(object):
24192527
_delete_bucket = False

0 commit comments

Comments
 (0)