Skip to content

Commit 48677ee

Browse files
committed
Merge branch '314-bucket_location_support' into 314-omnibus
2 parents 1fdacb4 + b721f42 commit 48677ee

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

gcloud/storage/bucket.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Bucket(_PropertyMixin):
2828
'etag': 'etag',
2929
'id': 'id',
3030
'lifecycle': 'get_lifecycle()',
31+
'location': 'get_location()',
3132
'logging': 'get_logging()',
3233
'metageneration': 'metageneration',
3334
'name': 'name',
@@ -688,6 +689,30 @@ def update_cors(self, entries):
688689
entry['responseHeader'] = entry.pop('headers')
689690
self.patch_metadata({'cors': to_patch})
690691

692+
def get_location(self):
693+
"""Retrieve location configured for this bucket.
694+
695+
See: https://cloud.google.com/storage/docs/json_api/v1/buckets and
696+
https://cloud.google.com/storage/docs/concepts-techniques#specifyinglocations
697+
698+
:rtype: string
699+
:returns: The configured location.
700+
"""
701+
if not self.has_metadata('location'):
702+
self.reload_metadata()
703+
return self.metadata.get('location')
704+
705+
def set_location(self, location):
706+
"""Update location configured for this bucket.
707+
708+
See: https://cloud.google.com/storage/docs/json_api/v1/buckets and
709+
https://cloud.google.com/storage/docs/concepts-techniques#specifyinglocations
710+
711+
:type location: string
712+
:param location: The new configured location.
713+
"""
714+
self.patch_metadata({'location': location})
715+
691716

692717
class BucketIterator(Iterator):
693718
"""An iterator listing all buckets.

gcloud/storage/test_bucket.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ def test_get_logging_lazy_wo_prefix(self):
745745
self.assertEqual(info['object_prefix'], '')
746746
kw = connection._requested
747747
self.assertEqual(len(kw), 1)
748+
self.assertEqual(kw[0]['method'], 'GET')
748749
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
749750
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
750751

@@ -866,13 +867,45 @@ def test_update_cors(self):
866867
connection = _Connection(after)
867868
bucket = self._makeOne(connection, NAME)
868869
bucket.update_cors([MAPPED, {}])
870+
kw = connection._requested
871+
self.assertEqual(len(kw), 1)
872+
self.assertEqual(kw[0]['method'], 'PATCH')
873+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
874+
self.assertEqual(kw[0]['data'], after)
875+
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
869876
entries = bucket.get_cors()
870877
self.assertEqual(entries, [MAPPED, {}])
878+
879+
def test_get_location_eager(self):
880+
NAME = 'name'
881+
connection = _Connection()
882+
before = {'location': 'AS'}
883+
bucket = self._makeOne(connection, NAME, before)
884+
self.assertEqual(bucket.get_location(), 'AS')
885+
kw = connection._requested
886+
self.assertEqual(len(kw), 0)
887+
888+
def test_get_location_lazy(self):
889+
NAME = 'name'
890+
connection = _Connection({'location': 'AS'})
891+
bucket = self._makeOne(connection, NAME)
892+
self.assertEqual(bucket.get_location(), 'AS')
893+
kw = connection._requested
894+
self.assertEqual(len(kw), 1)
895+
self.assertEqual(kw[0]['method'], 'GET')
896+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
897+
898+
def test_update_location(self):
899+
NAME = 'name'
900+
connection = _Connection({'location': 'AS'})
901+
bucket = self._makeOne(connection, NAME)
902+
bucket.set_location('AS')
903+
self.assertEqual(bucket.get_location(), 'AS')
871904
kw = connection._requested
872905
self.assertEqual(len(kw), 1)
873906
self.assertEqual(kw[0]['method'], 'PATCH')
874907
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
875-
self.assertEqual(kw[0]['data'], after)
908+
self.assertEqual(kw[0]['data'], {'location': 'AS'})
876909
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
877910

878911

0 commit comments

Comments
 (0)