Skip to content

Commit

Permalink
Add 'Bucket.labels' property. (googleapis#3478)
Browse files Browse the repository at this point in the history
* Avoid UnicodeWarning reported by new py.test

* Add 'Bucket.labels' property.

See: https://cloud.google.com/storage/docs/json_api/v1/buckets#labels

Closes googleapis#3473.
  • Loading branch information
tseaver authored and landrito committed Aug 21, 2017
1 parent d79122f commit 1f56120
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
30 changes: 30 additions & 0 deletions storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,36 @@ def cors(self, entries):
"""
self._patch_property('cors', entries)

@property
def labels(self):
"""Retrieve or set CORS policies configured for this bucket.
See
https://cloud.google.com/storage/docs/json_api/v1/buckets#labels
:setter: Set labels for this bucket.
:getter: Gets the labels for this bucket.
:rtype: :class:`dict`
:returns: Name-value pairs (string->string) labelling the bucket.
"""
labels = self._properties.get('labels')
if labels is None:
return {}
return copy.deepcopy(labels)

@labels.setter
def labels(self, mapping):
"""Set CORS policies configured for this bucket.
See
https://cloud.google.com/storage/docs/json_api/v1/buckets#labels
:type mapping: :class:`dict`
:param mapping: Name-value pairs (string->string) labelling the bucket.
"""
self._patch_property('labels', copy.deepcopy(mapping))

@property
def etag(self):
"""Retrieve the ETag for the bucket.
Expand Down
4 changes: 3 additions & 1 deletion storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import unittest

import mock
import six
from six.moves import http_client


Expand Down Expand Up @@ -55,7 +56,8 @@ def test_ctor_with_encoded_unicode(self):
blob_name = b'wet \xe2\x9b\xb5'
blob = self._make_one(blob_name, bucket=None)
unicode_name = u'wet \N{sailboat}'
self.assertNotEqual(blob.name, blob_name)
self.assertNotIsInstance(blob.name, bytes)
self.assertIsInstance(blob.name, six.text_type)
self.assertEqual(blob.name, unicode_name)

def test_ctor_w_encryption_key(self):
Expand Down
24 changes: 24 additions & 0 deletions storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def test_create_w_extra_properties(self):
"condition": {"age": 365}
}]
LOCATION = 'eu'
LABELS = {'color': 'red', 'flavor': 'cherry'}
STORAGE_CLASS = 'NEARLINE'
DATA = {
'name': BUCKET_NAME,
Expand All @@ -175,6 +176,7 @@ def test_create_w_extra_properties(self):
'location': LOCATION,
'storageClass': STORAGE_CLASS,
'versioning': {'enabled': True},
'labels': LABELS,
}
connection = _Connection(DATA)
client = _Client(connection, project=PROJECT)
Expand All @@ -184,6 +186,7 @@ def test_create_w_extra_properties(self):
bucket.location = LOCATION
bucket.storage_class = STORAGE_CLASS
bucket.versioning_enabled = True
bucket.labels = LABELS
bucket.create()

kw, = connection._requested
Expand Down Expand Up @@ -663,6 +666,27 @@ def test_cors_setter(self):
self.assertEqual(bucket.cors, [CORS_ENTRY])
self.assertTrue('cors' in bucket._changes)

def test_labels_getter(self):
NAME = 'name'
LABELS = {'color': 'red', 'flavor': 'cherry'}
properties = {'labels': LABELS}
bucket = self._make_one(name=NAME, properties=properties)
labels = bucket.labels
self.assertEqual(labels, LABELS)
# Make sure it was a copy, not the same object.
self.assertIsNot(labels, LABELS)

def test_labels_setter(self):
NAME = 'name'
LABELS = {'color': 'red', 'flavor': 'cherry'}
bucket = self._make_one(name=NAME)

self.assertEqual(bucket.labels, {})
bucket.labels = LABELS
self.assertEqual(bucket.labels, LABELS)
self.assertIsNot(bucket._properties['labels'], LABELS)
self.assertIn('labels', bucket._changes)

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

0 comments on commit 1f56120

Please sign in to comment.