Skip to content

Commit 2f22ae1

Browse files
committed
Add 'Bucket.lock_retention_policy' API wrapper. (#448)
Toward #445.
1 parent 3c7054e commit 2f22ae1

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
@@ -1778,3 +1778,37 @@ def generate_upload_policy(
17781778
}
17791779

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

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

24122520
class _Connection(object):
24132521
_delete_bucket = False

0 commit comments

Comments
 (0)