Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting 'bucket.storage_class'. #1213

Merged
merged 1 commit into from
Nov 10, 2015
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
18 changes: 18 additions & 0 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class Bucket(_PropertyMixin):
This is used in Bucket.delete() and Bucket.make_public().
"""

_STORAGE_CLASSES = ('STANDARD', 'NEARLINE', 'DURABLE_REDUCED_AVAILABILITY')

def __init__(self, client, name=None):
super(Bucket, self).__init__(name=name)
self._client = client
Expand Down Expand Up @@ -675,6 +677,22 @@ def storage_class(self):
"""
return self._properties.get('storageClass')

@storage_class.setter
def storage_class(self, value):
"""Set the storage class for the bucket.

See: https://cloud.google.com/storage/docs/storage-classes
https://cloud.google.com/storage/docs/nearline-storage
https://cloud.google.com/storage/docs/durable-reduced-availability

:type value: string
:param value: one of "STANDARD", "NEARLINE", or
"DURABLE_REDUCED_AVAILABILITY"
"""
if value not in self._STORAGE_CLASSES:
raise ValueError('Invalid storage class: %s' % (value,))
self._patch_property('storageClass', value)

@property
def time_created(self):
"""Retrieve the timestamp at which the bucket was created.
Expand Down
33 changes: 32 additions & 1 deletion gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ def test_location_setter(self):
self.assertEqual(bucket.location, None)
bucket.location = 'AS'
self.assertEqual(bucket.location, 'AS')
self.assertTrue('location' in bucket._changes)

This comment was marked as spam.


def test_lifecycle_rules_getter(self):
NAME = 'name'
Expand All @@ -588,6 +589,7 @@ def test_lifecycle_rules_setter(self):
self.assertEqual(bucket.lifecycle_rules, [])
bucket.lifecycle_rules = rules
self.assertEqual(bucket.lifecycle_rules, rules)
self.assertTrue('lifecycle' in bucket._changes)

def test_cors_getter(self):
NAME = 'name'
Expand Down Expand Up @@ -619,6 +621,7 @@ def test_cors_setter(self):
self.assertEqual(bucket.cors, [])
bucket.cors = [CORS_ENTRY]
self.assertEqual(bucket.cors, [CORS_ENTRY])
self.assertTrue('cors' in bucket._changes)

def test_get_logging_w_prefix(self):
NAME = 'name'
Expand Down Expand Up @@ -712,12 +715,40 @@ def test_self_link(self):
bucket = self._makeOne(properties=properties)
self.assertEqual(bucket.self_link, SELF_LINK)

def test_storage_class(self):
def test_storage_class_getter(self):
STORAGE_CLASS = 'http://example.com/self/'
properties = {'storageClass': STORAGE_CLASS}
bucket = self._makeOne(properties=properties)
self.assertEqual(bucket.storage_class, STORAGE_CLASS)

def test_storage_class_setter_invalid(self):
NAME = 'name'
bucket = self._makeOne(name=NAME)
with self.assertRaises(ValueError):
bucket.storage_class = 'BOGUS'
self.assertFalse('storageClass' in bucket._changes)

def test_storage_class_setter_STANDARD(self):
NAME = 'name'
bucket = self._makeOne(name=NAME)
bucket.storage_class = 'STANDARD'
self.assertEqual(bucket.storage_class, 'STANDARD')
self.assertTrue('storageClass' in bucket._changes)

def test_storage_class_setter_NEARLINE(self):
NAME = 'name'
bucket = self._makeOne(name=NAME)
bucket.storage_class = 'NEARLINE'
self.assertEqual(bucket.storage_class, 'NEARLINE')
self.assertTrue('storageClass' in bucket._changes)

def test_storage_class_setter_DURABLE_REDUCED_AVAILABILITY(self):
NAME = 'name'
bucket = self._makeOne(name=NAME)
bucket.storage_class = 'DURABLE_REDUCED_AVAILABILITY'
self.assertEqual(bucket.storage_class, 'DURABLE_REDUCED_AVAILABILITY')
self.assertTrue('storageClass' in bucket._changes)

def test_time_created(self):
import datetime
from gcloud._helpers import _RFC3339_MICROS
Expand Down