Skip to content
Merged
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
16 changes: 9 additions & 7 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,28 +457,30 @@ def upload_file_object(self, file_obj, blob_name=None):
blob.upload_from_file(file_obj)
return blob

def get_cors(self):
@property
def cors(self):
"""Retrieve CORS policies configured for this bucket.

See: http://www.w3.org/TR/cors/ and
https://cloud.google.com/storage/docs/json_api/v1/buckets

:rtype: list(dict)
:rtype: list of dictionaries
:returns: A sequence of mappings describing each CORS policy.
"""
return [policy.copy() for policy in self.properties.get('cors', ())]
return [copy.deepcopy(policy)
for policy in self.properties.get('cors', ())]

def update_cors(self, entries):
"""Update CORS policies configured for this bucket.
@cors.setter
def cors(self, entries):
"""Set CORS policies configured for this bucket.

See: http://www.w3.org/TR/cors/ and
https://cloud.google.com/storage/docs/json_api/v1/buckets

:type entries: list(dict)
:type entries: list of dictionaries
:param entries: A sequence of mappings describing each CORS policy.
"""
self._patch_properties({'cors': entries})
self.patch()

def get_default_object_acl(self):
"""Get the current Default Object ACL rules.
Expand Down
115 changes: 41 additions & 74 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,80 +564,6 @@ def upload_from_file(self, fh):
self.assertEqual(found._name, BLOB_NAME)
self.assertTrue(found._bucket is bucket)

def test_get_cors_eager(self):
NAME = 'name'
CORS_ENTRY = {
'maxAgeSeconds': 1234,
'method': ['OPTIONS', 'GET'],
'origin': ['127.0.0.1'],
'responseHeader': ['Content-Type'],
}
before = {'cors': [CORS_ENTRY, {}]}
connection = _Connection()
bucket = self._makeOne(NAME, connection, properties=before)
entries = bucket.get_cors()
self.assertEqual(len(entries), 2)
self.assertEqual(entries[0]['maxAgeSeconds'],
CORS_ENTRY['maxAgeSeconds'])
self.assertEqual(entries[0]['method'],
CORS_ENTRY['method'])
self.assertEqual(entries[0]['origin'],
CORS_ENTRY['origin'])
self.assertEqual(entries[0]['responseHeader'],
CORS_ENTRY['responseHeader'])
self.assertEqual(entries[1], {})
kw = connection._requested
self.assertEqual(len(kw), 0)

def test_get_cors_lazy(self):
NAME = 'name'
CORS_ENTRY = {
'maxAgeSeconds': 1234,
'method': ['OPTIONS', 'GET'],
'origin': ['127.0.0.1'],
'responseHeader': ['Content-Type'],
}
after = {'cors': [CORS_ENTRY]}
connection = _Connection(after)
bucket = self._makeOne(NAME, connection)
bucket._reload_properties()
entries = bucket.get_cors()
self.assertEqual(len(entries), 1)
self.assertEqual(entries[0]['maxAgeSeconds'],
CORS_ENTRY['maxAgeSeconds'])
self.assertEqual(entries[0]['method'],
CORS_ENTRY['method'])
self.assertEqual(entries[0]['origin'],
CORS_ENTRY['origin'])
self.assertEqual(entries[0]['responseHeader'],
CORS_ENTRY['responseHeader'])
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_update_cors(self):
NAME = 'name'
CORS_ENTRY = {
'maxAgeSeconds': 1234,
'method': ['OPTIONS', 'GET'],
'origin': ['127.0.0.1'],
'responseHeader': ['Content-Type'],
}
after = {'cors': [CORS_ENTRY, {}]}
connection = _Connection(after)
bucket = self._makeOne(NAME, connection)
bucket.update_cors([CORS_ENTRY, {}])
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'], after)
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
entries = bucket.get_cors()
self.assertEqual(entries, [CORS_ENTRY, {}])

def test_get_default_object_acl_lazy(self):
from gcloud.storage.acl import BucketACL
NAME = 'name'
Expand Down Expand Up @@ -727,6 +653,47 @@ def test_lifecycle_rules_setter(self):
self.assertEqual(kw[0]['data'], after)
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})

def test_cors_getter(self):
NAME = 'name'
CORS_ENTRY = {
'maxAgeSeconds': 1234,
'method': ['OPTIONS', 'GET'],
'origin': ['127.0.0.1'],
'responseHeader': ['Content-Type'],
}
properties = {'cors': [CORS_ENTRY, {}]}
bucket = self._makeOne(NAME, properties=properties)
entries = bucket.cors
self.assertEqual(len(entries), 2)
self.assertEqual(entries[0], CORS_ENTRY)
self.assertEqual(entries[1], {})
# Make sure it was a copy, not the same object.
self.assertFalse(entries[0] is CORS_ENTRY)

def test_cors_setter(self):
NAME = 'name'
CORS_ENTRY = {
'maxAgeSeconds': 1234,
'method': ['OPTIONS', 'GET'],
'origin': ['127.0.0.1'],
'responseHeader': ['Content-Type'],
}
DATA = {'cors': [CORS_ENTRY]}
connection = _Connection(DATA)
bucket = self._makeOne(NAME, connection)

self.assertEqual(bucket.cors, [])

bucket.cors = [CORS_ENTRY]
bucket.patch()
self.assertEqual(bucket.cors, [CORS_ENTRY])
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'], DATA)
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})

def test_get_logging_w_prefix(self):
NAME = 'name'
LOG_BUCKET = 'logs'
Expand Down