Skip to content

Commit 44685a0

Browse files
committed
1 parent aa22386 commit 44685a0

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

gcloud/storage/bucket.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Bucket(_MetadataMixin):
2424
CUSTOM_METADATA_FIELDS = {
2525
'acl': 'get_acl',
2626
'defaultObjectAcl': 'get_default_object_acl',
27+
'location': 'get_location',
2728
}
2829
"""Mapping of field name -> accessor for fields w/ custom accessors."""
2930

@@ -441,6 +442,30 @@ def make_public(self, recursive=False, future=False):
441442
key.get_acl().all().grant_read()
442443
key.save_acl()
443444

445+
def get_location(self):
446+
"""Retrieve location configured for this bucket.
447+
448+
See: https://cloud.google.com/storage/docs/json_api/v1/buckets and
449+
https://cloud.google.com/storage/docs/concepts-techniques#specifyinglocations
450+
451+
:rtype: string
452+
:returns: The configured location.
453+
"""
454+
if not self.has_metadata('location'):
455+
self.reload_metadata()
456+
return self.metadata.get('location')
457+
458+
def set_location(self, location):
459+
"""Update location configured for this bucket.
460+
461+
See: https://cloud.google.com/storage/docs/json_api/v1/buckets and
462+
https://cloud.google.com/storage/docs/concepts-techniques#specifyinglocations
463+
464+
:type location: string
465+
:param location: The new configured location.
466+
"""
467+
self.patch_metadata({'location': location})
468+
444469

445470
class BucketIterator(Iterator):
446471
"""An iterator listing all buckets.

gcloud/storage/test_bucket.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,23 @@ def test_get_metadata_none_set_defaultObjectAcl_miss_clear_default(self):
489489
kw = connection._requested
490490
self.assertEqual(len(kw), 0)
491491

492+
def test_get_metadata_location_no_default(self):
493+
NAME = 'name'
494+
connection = _Connection()
495+
bucket = self._makeOne(connection, NAME)
496+
self.assertRaises(KeyError, bucket.get_metadata, 'location')
497+
kw = connection._requested
498+
self.assertEqual(len(kw), 0)
499+
500+
def test_get_metadata_location_w_default(self):
501+
NAME = 'name'
502+
connection = _Connection()
503+
bucket = self._makeOne(connection, NAME)
504+
default = object()
505+
self.assertRaises(KeyError, bucket.get_metadata, 'location', default)
506+
kw = connection._requested
507+
self.assertEqual(len(kw), 0)
508+
492509
def test_get_metadata_miss(self):
493510
NAME = 'name'
494511
before = {'bar': 'Bar'}
@@ -713,6 +730,38 @@ def get_items_from_response(self, response):
713730
self.assertEqual(kw[1]['path'], '/b/%s/o' % NAME)
714731
self.assertEqual(kw[1]['query_params'], None)
715732

733+
def test_get_location_eager(self):
734+
NAME = 'name'
735+
connection = _Connection()
736+
before = {'location': 'AS'}
737+
bucket = self._makeOne(connection, NAME, before)
738+
self.assertEqual(bucket.get_location(), 'AS')
739+
kw = connection._requested
740+
self.assertEqual(len(kw), 0)
741+
742+
def test_get_location_lazy(self):
743+
NAME = 'name'
744+
connection = _Connection({'location': 'AS'})
745+
bucket = self._makeOne(connection, NAME)
746+
self.assertEqual(bucket.get_location(), 'AS')
747+
kw = connection._requested
748+
self.assertEqual(len(kw), 1)
749+
self.assertEqual(kw[0]['method'], 'GET')
750+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
751+
752+
def test_update_location(self):
753+
NAME = 'name'
754+
connection = _Connection({'location': 'AS'})
755+
bucket = self._makeOne(connection, NAME)
756+
bucket.set_location('AS')
757+
self.assertEqual(bucket.get_location(), 'AS')
758+
kw = connection._requested
759+
self.assertEqual(len(kw), 1)
760+
self.assertEqual(kw[0]['method'], 'PATCH')
761+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
762+
self.assertEqual(kw[0]['data'], {'location': 'AS'})
763+
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
764+
716765

717766
class TestBucketIterator(unittest2.TestCase):
718767

0 commit comments

Comments
 (0)