Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9be17de

Browse files
committed
Renaming storage.get_all_buckets to storage.list_buckets.
Also adding support for optional query parameters of storage.buckets.list.
1 parent 41b5b72 commit 9be17de

File tree

8 files changed

+133
-31
lines changed

8 files changed

+133
-31
lines changed

docs/_components/storage-getting-started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ If you have a full bucket, you can delete it this way::
184184
Listing available buckets
185185
-------------------------
186186

187-
>>> for bucket in storage.get_all_buckets(connection):
187+
>>> for bucket in storage.list_buckets(connection):
188188
... print bucket.name
189189

190190
Managing access control

docs/_components/storage-quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Once you have the connection,
5656
you can create buckets and blobs::
5757

5858
>>> from gcloud import storage
59-
>>> storage.get_all_buckets(connection)
59+
>>> storage.list_buckets(connection)
6060
[<Bucket: ...>, ...]
6161
>>> bucket = storage.create_bucket('my-new-bucket', connection=connection)
6262
>>> print bucket

gcloud/storage/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
from gcloud.storage._implicit_environ import get_default_bucket
4949
from gcloud.storage._implicit_environ import get_default_connection
5050
from gcloud.storage.api import create_bucket
51-
from gcloud.storage.api import get_all_buckets
5251
from gcloud.storage.api import get_bucket
52+
from gcloud.storage.api import list_buckets
5353
from gcloud.storage.api import lookup_bucket
5454
from gcloud.storage.batch import Batch
5555
from gcloud.storage.blob import Blob

gcloud/storage/api.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,45 @@ def lookup_bucket(bucket_name, connection=None):
5959
return None
6060

6161

62-
def get_all_buckets(project=None, connection=None):
62+
def list_buckets(project=None, max_results=None, page_token=None, prefix=None,
63+
projection='noAcl', fields=None, connection=None):
6364
"""Get all buckets in the project.
6465
6566
This will not populate the list of blobs available in each
6667
bucket.
6768
6869
>>> from gcloud import storage
69-
>>> for bucket in storage.get_all_buckets():
70+
>>> for bucket in storage.list_buckets():
7071
>>> print bucket
7172
7273
This implements "storage.buckets.list".
7374
74-
:type project: string
75+
:type project: string or ``NoneType``
7576
:param project: Optional. The project to use when listing all buckets.
7677
If not provided, falls back to default.
7778
79+
:type max_results: integer or ``NoneType``
80+
:param max_results: Optional. Maximum number of buckets to return.
81+
82+
:type page_token: string or ``NoneType``
83+
:param page_token: Optional. Opaque marker for the next "page" of buckets.
84+
If not passed, will return the first page of buckets.
85+
86+
:type prefix: string or ``NoneType``
87+
:param prefix: Optional. Filter results to buckets whose names begin with
88+
this prefix.
89+
90+
:type projection: string or ``NoneType``
91+
:param projection: If used, must be 'full' or 'noAcl'. Defaults to
92+
'noAcl'. Specifies the set of properties to return.
93+
94+
:type fields: string or ``NoneType``
95+
:param fields: Selector specifying which fields to include in a
96+
partial response. Must be a list of fields. For example
97+
to get a partial response with just the next page token
98+
and the language of each bucket returned:
99+
'items/id,nextPageToken'
100+
78101
:type connection: :class:`gcloud.storage.connection.Connection` or
79102
``NoneType``
80103
:param connection: Optional. The connection to use when sending requests.
@@ -87,8 +110,25 @@ def get_all_buckets(project=None, connection=None):
87110
if project is None:
88111
project = get_default_project()
89112
extra_params = {'project': project}
90-
return iter(_BucketIterator(connection=connection,
91-
extra_params=extra_params))
113+
114+
if max_results is not None:
115+
extra_params['maxResults'] = max_results
116+
117+
if prefix is not None:
118+
extra_params['prefix'] = prefix
119+
120+
extra_params['projection'] = projection
121+
122+
if fields is not None:
123+
extra_params['fields'] = fields
124+
125+
result = _BucketIterator(connection=connection,
126+
extra_params=extra_params)
127+
# Page token must be handled specially since the base `Iterator`
128+
# class has it as a reserved property.
129+
if page_token is not None:
130+
result.next_page_token = page_token
131+
return iter(result)
92132

93133

94134
def get_bucket(bucket_name, connection=None):

gcloud/storage/demo/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
import os
1616
from gcloud import storage
1717

18-
__all__ = ['create_bucket', 'get_all_buckets', 'PROJECT_ID']
18+
__all__ = ['create_bucket', 'list_buckets', 'PROJECT_ID']
1919

2020
PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')
2121

2222

23-
def get_all_buckets(connection):
24-
return list(storage.get_all_buckets(PROJECT_ID, connection))
23+
def list_buckets(connection):
24+
return list(storage.list_buckets(project=PROJECT_ID,
25+
connection=connection))
2526

2627

2728
def create_bucket(bucket_name, connection):

gcloud/storage/demo/demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
connection = storage.get_connection()
2626

2727
# OK, now let's look at all of the buckets...
28-
print(demo.get_all_buckets(connection)) # This might take a second...
28+
print(list(demo.list_buckets(connection))) # This might take a second...
2929

3030
# Now let's create a new bucket...
3131
bucket_name = ("bucket-%s" % time.time()).replace(".", "") # Get rid of dots.
@@ -34,7 +34,7 @@
3434
print(bucket)
3535

3636
# Let's look at all of the buckets again...
37-
print(demo.get_all_buckets(connection))
37+
print(list(demo.list_buckets(connection)))
3838

3939
# How about we create a new blob inside this bucket.
4040
blob = storage.Blob("my-new-file.txt", bucket=bucket)

gcloud/storage/test_api.py

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,55 @@ def test_use_default(self):
7878
self._lookup_bucket_hit_helper(use_default=True)
7979

8080

81-
class Test_get_all_buckets(unittest2.TestCase):
81+
class Test_list_buckets(unittest2.TestCase):
8282

83-
def _callFUT(self, project=None, connection=None):
84-
from gcloud.storage.api import get_all_buckets
85-
return get_all_buckets(project=project, connection=connection)
83+
def _callFUT(self, *args, **kwargs):
84+
from gcloud.storage.api import list_buckets
85+
return list_buckets(*args, **kwargs)
8686

8787
def test_empty(self):
88+
from six.moves.urllib.parse import parse_qs
89+
from six.moves.urllib.parse import urlparse
8890
from gcloud.storage.connection import Connection
8991
PROJECT = 'project'
9092
conn = Connection()
91-
URI = '/'.join([
92-
conn.API_BASE_URL,
93-
'storage',
94-
conn.API_VERSION,
95-
'b?project=%s' % PROJECT,
96-
])
93+
EXPECTED_QUERY = {
94+
'project': [PROJECT],
95+
'projection': ['noAcl'],
96+
}
9797
http = conn._http = Http(
9898
{'status': '200', 'content-type': 'application/json'},
9999
b'{}',
100100
)
101-
buckets = list(self._callFUT(PROJECT, conn))
101+
buckets = list(self._callFUT(project=PROJECT, connection=conn))
102102
self.assertEqual(len(buckets), 0)
103103
self.assertEqual(http._called_with['method'], 'GET')
104-
self.assertEqual(http._called_with['uri'], URI)
104+
self.assertEqual(http._called_with['body'], None)
105+
106+
BASE_URI = '/'.join([
107+
conn.API_BASE_URL,
108+
'storage',
109+
conn.API_VERSION,
110+
'b',
111+
])
112+
URI = http._called_with['uri']
113+
self.assertTrue(URI.startswith(BASE_URI))
114+
uri_parts = urlparse(URI)
115+
self.assertEqual(parse_qs(uri_parts.query), EXPECTED_QUERY)
105116

106-
def _get_all_buckets_non_empty_helper(self, project, use_default=False):
117+
def _list_buckets_non_empty_helper(self, project, use_default=False):
118+
from six.moves.urllib.parse import urlencode
107119
from gcloud._testing import _monkey_defaults as _base_monkey_defaults
108120
from gcloud.storage._testing import _monkey_defaults
109121
from gcloud.storage.connection import Connection
110122
BUCKET_NAME = 'bucket-name'
111123
conn = Connection()
124+
query_params = urlencode({'project': project, 'projection': 'noAcl'})
112125
URI = '/'.join([
113126
conn.API_BASE_URL,
114127
'storage',
115128
conn.API_VERSION,
116-
'b?project=%s' % project,
129+
'b?%s' % (query_params,),
117130
])
118131
http = conn._http = Http(
119132
{'status': '200', 'content-type': 'application/json'},
@@ -126,18 +139,66 @@ def _get_all_buckets_non_empty_helper(self, project, use_default=False):
126139
with _monkey_defaults(connection=conn):
127140
buckets = list(self._callFUT())
128141
else:
129-
buckets = list(self._callFUT(project, conn))
142+
buckets = list(self._callFUT(project=project, connection=conn))
130143

131144
self.assertEqual(len(buckets), 1)
132145
self.assertEqual(buckets[0].name, BUCKET_NAME)
133146
self.assertEqual(http._called_with['method'], 'GET')
134147
self.assertEqual(http._called_with['uri'], URI)
135148

136149
def test_non_empty(self):
137-
self._get_all_buckets_non_empty_helper('PROJECT', use_default=False)
150+
self._list_buckets_non_empty_helper('PROJECT', use_default=False)
138151

139152
def test_non_use_default(self):
140-
self._get_all_buckets_non_empty_helper('PROJECT', use_default=True)
153+
self._list_buckets_non_empty_helper('PROJECT', use_default=True)
154+
155+
def test_all_arguments(self):
156+
from six.moves.urllib.parse import parse_qs
157+
from six.moves.urllib.parse import urlparse
158+
from gcloud.storage.connection import Connection
159+
PROJECT = 'foo-bar'
160+
MAX_RESULTS = 10
161+
PAGE_TOKEN = 'ABCD'
162+
PREFIX = 'subfolder'
163+
PROJECTION = 'full'
164+
FIELDS = 'items/id,nextPageToken'
165+
EXPECTED_QUERY = {
166+
'project': [PROJECT],
167+
'maxResults': [str(MAX_RESULTS)],
168+
'pageToken': [PAGE_TOKEN],
169+
'prefix': [PREFIX],
170+
'projection': [PROJECTION],
171+
'fields': [FIELDS],
172+
}
173+
CONNECTION = Connection()
174+
http = CONNECTION._http = Http(
175+
{'status': '200', 'content-type': 'application/json'},
176+
'{"items": []}',
177+
)
178+
iterator = self._callFUT(
179+
project=PROJECT,
180+
max_results=MAX_RESULTS,
181+
page_token=PAGE_TOKEN,
182+
prefix=PREFIX,
183+
projection=PROJECTION,
184+
fields=FIELDS,
185+
connection=CONNECTION,
186+
)
187+
buckets = list(iterator)
188+
self.assertEqual(buckets, [])
189+
self.assertEqual(http._called_with['method'], 'GET')
190+
self.assertEqual(http._called_with['body'], None)
191+
192+
BASE_URI = '/'.join([
193+
CONNECTION.API_BASE_URL,
194+
'storage',
195+
CONNECTION.API_VERSION,
196+
'b'
197+
])
198+
URI = http._called_with['uri']
199+
self.assertTrue(URI.startswith(BASE_URI))
200+
uri_parts = urlparse(URI)
201+
self.assertEqual(parse_qs(uri_parts.query), EXPECTED_QUERY)
141202

142203

143204
class Test_get_bucket(unittest2.TestCase):

regression/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_get_buckets(self):
7575
self.case_buckets_to_delete.append(bucket_name)
7676

7777
# Retrieve the buckets.
78-
all_buckets = storage.get_all_buckets()
78+
all_buckets = storage.list_buckets()
7979
created_buckets = [bucket for bucket in all_buckets
8080
if bucket.name in buckets_to_create]
8181
self.assertEqual(len(created_buckets), len(buckets_to_create))

0 commit comments

Comments
 (0)