Skip to content

Commit 39627c0

Browse files
committed
Merge pull request #700 from dhermes/add-back-storage-bucket-lookup
Adding back storage.lookup_bucket.
2 parents 19bdb13 + f50d036 commit 39627c0

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

gcloud/storage/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from gcloud.storage._implicit_environ import get_default_bucket
4545
from gcloud.storage._implicit_environ import get_default_connection
4646
from gcloud.storage._implicit_environ import get_default_project
47+
from gcloud.storage.api import lookup_bucket
4748
from gcloud.storage.blob import Blob
4849
from gcloud.storage.bucket import Bucket
4950
from gcloud.storage.connection import Connection

gcloud/storage/api.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Methods for interacting with Google Cloud Storage.
16+
17+
Allows interacting with Cloud Storage via user-friendly objects
18+
rather than via Connection.
19+
"""
20+
21+
from gcloud.exceptions import NotFound
22+
from gcloud.storage._implicit_environ import get_default_connection
23+
24+
25+
def lookup_bucket(bucket_name, connection=None):
26+
"""Get a bucket by name, returning None if not found.
27+
28+
You can use this if you would rather checking for a None value
29+
than catching an exception::
30+
31+
>>> from gcloud import storage
32+
>>> storage.set_defaults()
33+
>>> bucket = storage.lookup_bucket('doesnt-exist')
34+
>>> print bucket
35+
None
36+
>>> bucket = storage.lookup_bucket('my-bucket')
37+
>>> print bucket
38+
<Bucket: my-bucket>
39+
40+
:type bucket_name: string
41+
:param bucket_name: The name of the bucket to get.
42+
43+
:type connection: :class:`gcloud.storage.connection.Connection` or
44+
``NoneType``
45+
:param connection: Optional. The connection to use when sending requests.
46+
If not provided, falls back to default.
47+
48+
:rtype: :class:`gcloud.storage.bucket.Bucket`
49+
:returns: The bucket matching the name provided or None if not found.
50+
"""
51+
if connection is None:
52+
connection = get_default_connection()
53+
54+
try:
55+
return connection.get_bucket(bucket_name)
56+
except NotFound:
57+
return None

gcloud/storage/test_api.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest2
16+
17+
18+
class Test_lookup_bucket(unittest2.TestCase):
19+
20+
def _callFUT(self, bucket_name, connection=None):
21+
from gcloud.storage.api import lookup_bucket
22+
return lookup_bucket(bucket_name, connection=connection)
23+
24+
def test_lookup_bucket_miss(self):
25+
from gcloud.storage.connection import Connection
26+
PROJECT = 'project'
27+
NONESUCH = 'nonesuch'
28+
conn = Connection(PROJECT)
29+
URI = '/'.join([
30+
conn.API_BASE_URL,
31+
'storage',
32+
conn.API_VERSION,
33+
'b',
34+
'nonesuch?project=%s' % PROJECT,
35+
])
36+
http = conn._http = Http(
37+
{'status': '404', 'content-type': 'application/json'},
38+
'{}',
39+
)
40+
bucket = self._callFUT(NONESUCH, connection=conn)
41+
self.assertEqual(bucket, None)
42+
self.assertEqual(http._called_with['method'], 'GET')
43+
self.assertEqual(http._called_with['uri'], URI)
44+
45+
def _lookup_bucket_hit_helper(self, use_default=False):
46+
from gcloud.storage._testing import _monkey_defaults
47+
from gcloud.storage.bucket import Bucket
48+
from gcloud.storage.connection import Connection
49+
PROJECT = 'project'
50+
BLOB_NAME = 'blob-name'
51+
conn = Connection(PROJECT)
52+
URI = '/'.join([
53+
conn.API_BASE_URL,
54+
'storage',
55+
conn.API_VERSION,
56+
'b',
57+
'%s?project=%s' % (BLOB_NAME, PROJECT),
58+
])
59+
http = conn._http = Http(
60+
{'status': '200', 'content-type': 'application/json'},
61+
'{"name": "%s"}' % BLOB_NAME,
62+
)
63+
64+
if use_default:
65+
with _monkey_defaults(connection=conn):
66+
bucket = self._callFUT(BLOB_NAME)
67+
else:
68+
bucket = self._callFUT(BLOB_NAME, connection=conn)
69+
70+
self.assertTrue(isinstance(bucket, Bucket))
71+
self.assertTrue(bucket.connection is conn)
72+
self.assertEqual(bucket.name, BLOB_NAME)
73+
self.assertEqual(http._called_with['method'], 'GET')
74+
self.assertEqual(http._called_with['uri'], URI)
75+
76+
def test_lookup_bucket_hit(self):
77+
self._lookup_bucket_hit_helper(use_default=False)
78+
79+
def test_lookup_bucket_use_default(self):
80+
self._lookup_bucket_hit_helper(use_default=True)
81+
82+
83+
class Http(object):
84+
85+
_called_with = None
86+
87+
def __init__(self, headers, content):
88+
from httplib2 import Response
89+
self._response = Response(headers)
90+
self._content = content
91+
92+
def request(self, **kw):
93+
self._called_with = kw
94+
return self._response, self._content

0 commit comments

Comments
 (0)