Skip to content

Commit b721f42

Browse files
committed
1 parent 826ea69 commit b721f42

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

gcloud/storage/bucket.py

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

@@ -471,6 +472,30 @@ def update_lifecycle(self, rules):
471472
"""
472473
self.patch_metadata({'lifecycle': {'rule': rules}})
473474

475+
def get_location(self):
476+
"""Retrieve location configured for this bucket.
477+
478+
See: https://cloud.google.com/storage/docs/json_api/v1/buckets and
479+
https://cloud.google.com/storage/docs/concepts-techniques#specifyinglocations
480+
481+
:rtype: string
482+
:returns: The configured location.
483+
"""
484+
if not self.has_metadata('location'):
485+
self.reload_metadata()
486+
return self.metadata.get('location')
487+
488+
def set_location(self, location):
489+
"""Update location configured for this bucket.
490+
491+
See: https://cloud.google.com/storage/docs/json_api/v1/buckets and
492+
https://cloud.google.com/storage/docs/concepts-techniques#specifyinglocations
493+
494+
:type location: string
495+
:param location: The new configured location.
496+
"""
497+
self.patch_metadata({'location': location})
498+
474499

475500
class BucketIterator(Iterator):
476501
"""An iterator listing all buckets.

gcloud/storage/test_bucket.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,23 @@ def test_get_metadata_lifecycle_w_default(self):
510510
kw = connection._requested
511511
self.assertEqual(len(kw), 0)
512512

513+
def test_get_metadata_location_no_default(self):
514+
NAME = 'name'
515+
connection = _Connection()
516+
bucket = self._makeOne(connection, NAME)
517+
self.assertRaises(KeyError, bucket.get_metadata, 'location')
518+
kw = connection._requested
519+
self.assertEqual(len(kw), 0)
520+
521+
def test_get_metadata_location_w_default(self):
522+
NAME = 'name'
523+
connection = _Connection()
524+
bucket = self._makeOne(connection, NAME)
525+
default = object()
526+
self.assertRaises(KeyError, bucket.get_metadata, 'location', default)
527+
kw = connection._requested
528+
self.assertEqual(len(kw), 0)
529+
513530
def test_get_metadata_miss(self):
514531
NAME = 'name'
515532
before = {'bar': 'Bar'}
@@ -781,6 +798,38 @@ def test_update_lifecycle(self):
781798
self.assertEqual(entries[0]['action']['type'], 'Delete')
782799
self.assertEqual(entries[0]['condition']['age'], 42)
783800

801+
def test_get_location_eager(self):
802+
NAME = 'name'
803+
connection = _Connection()
804+
before = {'location': 'AS'}
805+
bucket = self._makeOne(connection, NAME, before)
806+
self.assertEqual(bucket.get_location(), 'AS')
807+
kw = connection._requested
808+
self.assertEqual(len(kw), 0)
809+
810+
def test_get_location_lazy(self):
811+
NAME = 'name'
812+
connection = _Connection({'location': 'AS'})
813+
bucket = self._makeOne(connection, NAME)
814+
self.assertEqual(bucket.get_location(), 'AS')
815+
kw = connection._requested
816+
self.assertEqual(len(kw), 1)
817+
self.assertEqual(kw[0]['method'], 'GET')
818+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
819+
820+
def test_update_location(self):
821+
NAME = 'name'
822+
connection = _Connection({'location': 'AS'})
823+
bucket = self._makeOne(connection, NAME)
824+
bucket.set_location('AS')
825+
self.assertEqual(bucket.get_location(), 'AS')
826+
kw = connection._requested
827+
self.assertEqual(len(kw), 1)
828+
self.assertEqual(kw[0]['method'], 'PATCH')
829+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
830+
self.assertEqual(kw[0]['data'], {'location': 'AS'})
831+
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
832+
784833

785834
class TestBucketIterator(unittest2.TestCase):
786835

0 commit comments

Comments
 (0)