diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 2dc28e0c6abd..db7cc3c9e331 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -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. diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index 72ba8e73d97e..7b27165ff8d0 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -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' @@ -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'