Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bucket versioning support #340

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Bucket(_MetadataMixin):
'acl': 'get_acl',
'defaultObjectAcl': 'get_default_object_acl',
'lifecycle': 'get_lifecycle',
'versioning': 'get_versioning',
}
"""Mapping of field name -> accessor for fields w/ custom accessors."""

Expand Down Expand Up @@ -471,6 +472,36 @@ def update_lifecycle(self, rules):
"""
self.patch_metadata({'lifecycle': {'rule': rules}})

def get_versioning(self):
"""Is versioning enabled for this bucket?

See: https://cloud.google.com/storage/docs/object-versioning for
details.

:rtype: boolean
:returns: True if enabled, else False.
"""
if not self.has_metadata(field='versioning'):

This comment was marked as spam.

This comment was marked as spam.

self.reload_metadata()
versioning = self.metadata.get('versioning', {})
return versioning.get('enabled', False)

def enable_versioning(self):
"""Enable versioning for this bucket.

See: https://cloud.google.com/storage/docs/object-versioning for
details.
"""
self.patch_metadata({'versioning': {'enabled': True}})

def disable_versioning(self):
"""Disable versioning for this bucket.

See: https://cloud.google.com/storage/docs/object-versioning for
details.
"""
self.patch_metadata({'versioning': {'enabled': False}})


class BucketIterator(Iterator):
"""An iterator listing all buckets.
Expand Down
71 changes: 71 additions & 0 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,23 @@ def test_get_metadata_lifecycle_w_default(self):
kw = connection._requested
self.assertEqual(len(kw), 0)

def test_get_metadata_versioning_no_default(self):
NAME = 'name'
connection = _Connection()
bucket = self._makeOne(connection, NAME)
self.assertRaises(KeyError, bucket.get_metadata, 'versioning')
kw = connection._requested
self.assertEqual(len(kw), 0)

def test_get_metadata_versioning_w_default(self):
NAME = 'name'
connection = _Connection()
bucket = self._makeOne(connection, NAME)
default = object()
self.assertRaises(KeyError, bucket.get_metadata, 'versioning', default)
kw = connection._requested
self.assertEqual(len(kw), 0)

def test_get_metadata_miss(self):
NAME = 'name'
before = {'bar': 'Bar'}
Expand Down Expand Up @@ -781,6 +798,60 @@ def test_update_lifecycle(self):
self.assertEqual(entries[0]['action']['type'], 'Delete')
self.assertEqual(entries[0]['condition']['age'], 42)

def test_get_versioning_eager(self):
NAME = 'name'
before = {'bar': 'Bar', 'versioning': {'enabled': True}}
connection = _Connection()
bucket = self._makeOne(connection, NAME, before)
self.assertEqual(bucket.get_versioning(), True)
kw = connection._requested
self.assertEqual(len(kw), 0)

def test_get_versioning_lazy(self):
NAME = 'name'
before = {'bar': 'Bar'}
after = {'bar': 'Bar', 'versioning': {'enabled': True}}
connection = _Connection(after)
bucket = self._makeOne(connection, NAME, before)
self.assertEqual(bucket.get_versioning(), True)
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'GET')
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})

def test_enable_versioning(self):
NAME = 'name'
before = {'versioning': {'enabled': False}}
after = {'versioning': {'enabled': True}}
connection = _Connection(after)
bucket = self._makeOne(connection, NAME, before)
self.assertFalse(bucket.get_versioning())
bucket.enable_versioning()
self.assertTrue(bucket.get_versioning())
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
self.assertEqual(kw[0]['data'], {'versioning': {'enabled': True}})
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})

def test_disable_versioning(self):
NAME = 'name'
before = {'versioning': {'enabled': True}}
after = {'versioning': {'enabled': False}}
connection = _Connection(after)
bucket = self._makeOne(connection, NAME, before)
self.assertTrue(bucket.get_versioning())
bucket.disable_versioning()
self.assertFalse(bucket.get_versioning())
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
self.assertEqual(kw[0]['data'], {'versioning': {'enabled': False}})
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})


class TestBucketIterator(unittest2.TestCase):

Expand Down