Skip to content
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
38 changes: 24 additions & 14 deletions gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

from gcloud import credentials
from gcloud.storage import _implicit_environ
from gcloud.storage.bucket import Bucket
from gcloud.storage.connection import Connection


Expand All @@ -52,23 +53,30 @@
_PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'


def set_default_bucket_name(bucket_name=None):
"""Set default bucket name either explicitly or implicitly as fall-back.
def set_default_bucket(bucket=None):
"""Set default bucket either explicitly or implicitly as fall-back.

In implicit case, currently only supports enviroment variable but will
support App Engine, Compute Engine and other environments in the future.

In the implicit case, relies on an implicit connection in addition to the
implicit bucket name.

Local environment variable used is:
- GCLOUD_BUCKET_NAME

:type bucket_name: string
:param bucket_name: Optional. The bucket name to use as default.
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: Optional. The bucket to use as default.
"""
if bucket_name is None:
if bucket is None:
bucket_name = os.getenv(_BUCKET_ENV_VAR_NAME)
connection = _implicit_environ.CONNECTION

if bucket_name is not None:
_implicit_environ.BUCKET_NAME = bucket_name
if bucket_name is not None and connection is not None:
bucket = Bucket(connection=connection, name=bucket_name)

if bucket is not None:
_implicit_environ.BUCKET = bucket


def set_default_project(project=None):
Expand Down Expand Up @@ -96,7 +104,7 @@ def set_default_connection(project=None, connection=None):
:type project: string
:param project: Optional. The name of the project to connect to.

:type connection: :class:`gcloud.datastore.connection.Connection`
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: A connection provided to be the default.
"""
if project is None:
Expand All @@ -106,25 +114,27 @@ def set_default_connection(project=None, connection=None):
_implicit_environ.CONNECTION = connection


def set_defaults(bucket_name=None, project=None, connection=None):
def set_defaults(bucket=None, project=None, connection=None):
"""Set defaults either explicitly or implicitly as fall-back.

Uses the arguments to call the individual default methods.

:type bucket_name: string
:param bucket_name: Optional. The bucket name to use as default.
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: Optional. The bucket to use as default.

:type project: string
:param project: Optional. The name of the project to connect to.

:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: A connection provided to be the default.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: Optional. A connection provided to be the default.
"""
set_default_bucket_name(bucket_name=bucket_name)
# NOTE: `set_default_project` is called before `set_default_connection`
# since `set_default_connection` falls back to implicit project.
set_default_project(project=project)
set_default_connection(project=project, connection=connection)
# NOTE: `set_default_bucket` is called after `set_default_connection`
# since `set_default_bucket` falls back to implicit connection.
set_default_bucket(bucket=bucket)


def get_connection(project):
Expand Down
4 changes: 2 additions & 2 deletions gcloud/storage/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
PROJECT = None
"""Module global to allow persistent implied project from enviroment."""

BUCKET_NAME = None
"""Module global to allow persistent implied bucket name from enviroment."""
BUCKET = None
"""Module global to allow persistent implied bucket from enviroment."""

CONNECTION = None
"""Module global to allow persistent implied connection from enviroment."""
85 changes: 52 additions & 33 deletions gcloud/storage/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,68 +71,87 @@ def get_connection(*args, **kw):
self.assertEqual(connection._called_with, BUCKET)


class Test_set_default_bucket_name(unittest2.TestCase):
class Test_set_default_bucket(unittest2.TestCase):

def setUp(self):
from gcloud.storage import _implicit_environ
self._replaced_bucket_name = _implicit_environ.BUCKET_NAME
_implicit_environ.BUCKET_NAME = None
self._replaced_bucket = _implicit_environ.BUCKET
_implicit_environ.BUCKET = None

def tearDown(self):
from gcloud.storage import _implicit_environ
_implicit_environ.BUCKET_NAME = self._replaced_bucket_name
_implicit_environ.BUCKET = self._replaced_bucket

def _callFUT(self, bucket_name=None):
from gcloud.storage import set_default_bucket_name
return set_default_bucket_name(bucket_name=bucket_name)
def _callFUT(self, bucket=None):
from gcloud.storage import set_default_bucket
return set_default_bucket(bucket=bucket)

def _monkey(self, implicit_bucket_name):
def _monkey(self, implicit_bucket_name, connection=None):
from contextlib import nested
import os
from gcloud.storage import _BUCKET_ENV_VAR_NAME

from gcloud._testing import _Monkey
from gcloud.storage import _BUCKET_ENV_VAR_NAME
from gcloud.storage import _implicit_environ

environ = {_BUCKET_ENV_VAR_NAME: implicit_bucket_name}
return _Monkey(os, getenv=environ.get)
return nested(_Monkey(os, getenv=environ.get),
_Monkey(_implicit_environ, CONNECTION=connection))

def test_no_env_var_set(self):
from gcloud.storage import _implicit_environ
with self._monkey(None):
self._callFUT()
self.assertEqual(_implicit_environ.BUCKET_NAME, None)
self.assertEqual(_implicit_environ.BUCKET, None)

def test_set_from_env_var(self):
from gcloud.storage import _implicit_environ
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
with self._monkey(IMPLICIT_BUCKET_NAME):
CONNECTION = object()
with self._monkey(IMPLICIT_BUCKET_NAME, connection=CONNECTION):
self._callFUT()
self.assertEqual(_implicit_environ.BUCKET_NAME, IMPLICIT_BUCKET_NAME)

self.assertEqual(_implicit_environ.BUCKET.name, IMPLICIT_BUCKET_NAME)
self.assertEqual(_implicit_environ.BUCKET.connection, CONNECTION)

def test_set_explicit_w_env_var_set(self):
from gcloud.storage import _implicit_environ
EXPLICIT_BUCKET_NAME = 'EXPLICIT'
EXPLICIT_BUCKET = object()
with self._monkey(None):
self._callFUT(EXPLICIT_BUCKET_NAME)
self.assertEqual(_implicit_environ.BUCKET_NAME, EXPLICIT_BUCKET_NAME)
self._callFUT(EXPLICIT_BUCKET)
self.assertEqual(_implicit_environ.BUCKET, EXPLICIT_BUCKET)

def test_set_explicit_no_env_var_set(self):
from gcloud.storage import _implicit_environ
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
EXPLICIT_BUCKET_NAME = 'EXPLICIT'
with self._monkey(IMPLICIT_BUCKET_NAME):
self._callFUT(EXPLICIT_BUCKET_NAME)
self.assertEqual(_implicit_environ.BUCKET_NAME, EXPLICIT_BUCKET_NAME)
CONNECTION = object()
EXPLICIT_BUCKET = object()
with self._monkey(IMPLICIT_BUCKET_NAME, connection=CONNECTION):
self._callFUT(EXPLICIT_BUCKET)
self.assertEqual(_implicit_environ.BUCKET, EXPLICIT_BUCKET)

def test_set_explicit_None_wo_env_var_set(self):
from gcloud.storage import _implicit_environ
with self._monkey(None):
CONNECTION = object()
with self._monkey(None, connection=CONNECTION):
self._callFUT(None)
self.assertEqual(_implicit_environ.BUCKET, None)

def test_set_explicit_None_wo_connection_set(self):
from gcloud.storage import _implicit_environ
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
with self._monkey(IMPLICIT_BUCKET_NAME, connection=None):
self._callFUT(None)
self.assertEqual(_implicit_environ.BUCKET_NAME, None)
self.assertEqual(_implicit_environ.BUCKET, None)

def test_set_explicit_None_w_env_var_set(self):
from gcloud.storage import _implicit_environ
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
with self._monkey(IMPLICIT_BUCKET_NAME):
CONNECTION = object()
with self._monkey(IMPLICIT_BUCKET_NAME, connection=CONNECTION):
self._callFUT(None)
self.assertEqual(_implicit_environ.BUCKET_NAME, IMPLICIT_BUCKET_NAME)
self.assertEqual(_implicit_environ.BUCKET.name, IMPLICIT_BUCKET_NAME)
self.assertEqual(_implicit_environ.BUCKET.connection, CONNECTION)


class Test_set_default_project(unittest2.TestCase):
Expand Down Expand Up @@ -299,23 +318,23 @@ def mock_get_connection(*args, **kwargs):

class Test_set_defaults(unittest2.TestCase):

def _callFUT(self, bucket_name=None, project=None, connection=None):
def _callFUT(self, bucket=None, project=None, connection=None):
from gcloud.storage import set_defaults
return set_defaults(bucket_name=bucket_name, project=project,
return set_defaults(bucket=bucket, project=project,
connection=connection)

def test_it(self):
from gcloud._testing import _Monkey
from gcloud import storage

BUCKET_NAME = object()
BUCKET = object()
PROJECT = object()
CONNECTION = object()

SET_BUCKET_NAME_CALLED = []
SET_BUCKET_CALLED = []

def call_set_bucket_name(bucket_name=None):
SET_BUCKET_NAME_CALLED.append(bucket_name)
def call_set_bucket(bucket=None):
SET_BUCKET_CALLED.append(bucket)

SET_PROJECT_CALLED = []

Expand All @@ -327,12 +346,12 @@ def call_set_project(project=None):
def call_set_connection(project=None, connection=None):
SET_CONNECTION_CALLED.append((project, connection))

with _Monkey(storage, set_default_bucket_name=call_set_bucket_name,
with _Monkey(storage, set_default_bucket=call_set_bucket,
set_default_connection=call_set_connection,
set_default_project=call_set_project):
self._callFUT(bucket_name=BUCKET_NAME, project=PROJECT,
self._callFUT(bucket=BUCKET, project=PROJECT,
connection=CONNECTION)

self.assertEqual(SET_BUCKET_NAME_CALLED, [BUCKET_NAME])
self.assertEqual(SET_PROJECT_CALLED, [PROJECT])
self.assertEqual(SET_CONNECTION_CALLED, [(PROJECT, CONNECTION)])
self.assertEqual(SET_BUCKET_CALLED, [BUCKET])