Skip to content

Commit

Permalink
Merge pull request #656 from dhermes/fix-650
Browse files Browse the repository at this point in the history
Adding support for GCD dev server in datastore package.
  • Loading branch information
dhermes committed Feb 18, 2015
2 parents 018b467 + b9aaf10 commit f56abdf
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 39 deletions.
7 changes: 4 additions & 3 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import httplib2


API_BASE_URL = 'https://www.googleapis.com'
"""The base of the API call URL."""


class Connection(object):
"""A generic connection to Google Cloud Platform.
Expand Down Expand Up @@ -54,9 +58,6 @@ class Connection(object):
:param http: An optional HTTP object to make requests.
"""

API_BASE_URL = 'https://www.googleapis.com'
"""The base of the API call URL."""

USER_AGENT = "gcloud-python/{0}".format(get_distribution('gcloud').version)
"""The user agent for gcloud-python requests."""

Expand Down
4 changes: 4 additions & 0 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"""The scopes required for authenticating as a Cloud Datastore consumer."""

_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'


def set_default_dataset_id(dataset_id=None):
Expand All @@ -86,6 +87,9 @@ def set_default_dataset_id(dataset_id=None):
if dataset_id is None:
dataset_id = os.getenv(_DATASET_ENV_VAR_NAME)

if dataset_id is None:
dataset_id = os.getenv(_GCD_DATASET_ENV_VAR_NAME)

if dataset_id is None:
dataset_id = _implicit_environ.app_engine_id()

Expand Down
25 changes: 20 additions & 5 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

"""Connections to gcloud datastore API servers."""

import os

from gcloud import connection
from gcloud.exceptions import make_exception
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb
from gcloud.datastore import helpers


_GCD_HOST_ENV_VAR_NAME = 'DATASTORE_HOST'


class Connection(connection.Connection):
"""A connection to the Google Cloud Datastore via the Protobuf API.
Expand All @@ -28,6 +33,10 @@ class Connection(connection.Connection):
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for this connection.
:type api_base_url: string
:param api_base_url: The base of the API call URL. Defaults to the value
from :mod:`gcloud.connection`.
"""

API_VERSION = 'v1beta2'
Expand All @@ -37,6 +46,13 @@ class Connection(connection.Connection):
'/datasets/{dataset_id}/{method}')
"""A template for the URL of a particular API call."""

def __init__(self, credentials=None, http=None, api_base_url=None):
super(Connection, self).__init__(credentials=credentials, http=http)
if api_base_url is None:
api_base_url = os.getenv(_GCD_HOST_ENV_VAR_NAME,
connection.API_BASE_URL)
self.api_base_url = api_base_url

def _request(self, dataset_id, method, data):
"""Make a request over the Http transport to the Cloud Datastore API.
Expand Down Expand Up @@ -93,8 +109,7 @@ def _rpc(self, dataset_id, method, request_pb, response_pb_cls):
data=request_pb.SerializeToString())
return response_pb_cls.FromString(response)

@classmethod
def build_api_url(cls, dataset_id, method, base_url=None,
def build_api_url(self, dataset_id, method, base_url=None,
api_version=None):
"""Construct the URL for a particular API call.
Expand All @@ -116,9 +131,9 @@ def build_api_url(cls, dataset_id, method, base_url=None,
:param api_version: The version of the API to connect to.
You shouldn't have to provide this.
"""
return cls.API_URL_TEMPLATE.format(
api_base=(base_url or cls.API_BASE_URL),
api_version=(api_version or cls.API_VERSION),
return self.API_URL_TEMPLATE.format(
api_base=(base_url or self.api_base_url),
api_version=(api_version or self.API_VERSION),
dataset_id=dataset_id, method=method)

def lookup(self, dataset_id, key_pbs,
Expand Down
53 changes: 51 additions & 2 deletions gcloud/datastore/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def _callFUT(self, dataset_id=None):
from gcloud.datastore import set_default_dataset_id
return set_default_dataset_id(dataset_id=dataset_id)

def _monkeyEnviron(self, implicit_dataset_id):
def _monkeyEnviron(self, implicit_dataset_id, environ=None):
import os
from gcloud._testing import _Monkey
from gcloud.datastore import _DATASET_ENV_VAR_NAME
environ = {_DATASET_ENV_VAR_NAME: implicit_dataset_id}
environ = environ or {_DATASET_ENV_VAR_NAME: implicit_dataset_id}
return _Monkey(os, getenv=environ.get)

def _monkeyImplicit(self, connection=None, app_identity=None):
Expand Down Expand Up @@ -112,6 +112,55 @@ def test_set_explicit_None_w_env_var_set(self):

self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)

def test_set_from_gcd_env_var(self):
from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME
from gcloud.datastore import _implicit_environ

GCD_DATASET_ID = 'GCD-IMPLICIT'
ENVIRON = {_GCD_DATASET_ENV_VAR_NAME: GCD_DATASET_ID}

with self._monkeyEnviron(None, environ=ENVIRON):
with self._monkeyImplicit():
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, GCD_DATASET_ID)

def test_set_gcd_and_production_env_vars(self):
from gcloud.datastore import _DATASET_ENV_VAR_NAME
from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME
from gcloud.datastore import _implicit_environ

IMPLICIT_DATASET_ID = 'IMPLICIT'
GCD_DATASET_ID = 'GCD-IMPLICIT'
ENVIRON = {
_DATASET_ENV_VAR_NAME: IMPLICIT_DATASET_ID,
_GCD_DATASET_ENV_VAR_NAME: GCD_DATASET_ID,
}

with self._monkeyEnviron(None, environ=ENVIRON):
with self._monkeyImplicit():
self._callFUT()

self.assertNotEqual(_implicit_environ.DATASET_ID, GCD_DATASET_ID)
self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)

def test_set_gcd_env_vars_and_appengine(self):
from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME
from gcloud.datastore import _implicit_environ

GCD_DATASET_ID = 'GCD-IMPLICIT'
ENVIRON = {_GCD_DATASET_ENV_VAR_NAME: GCD_DATASET_ID}

APP_ENGINE_ID = 'GAE'
APP_IDENTITY = _AppIdentity(APP_ENGINE_ID)

with self._monkeyEnviron(None, environ=ENVIRON):
with self._monkeyImplicit(app_identity=APP_IDENTITY):
self._callFUT()

self.assertNotEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID)
self.assertEqual(_implicit_environ.DATASET_ID, GCD_DATASET_ID)

def test_set_implicit_from_appengine(self):
from gcloud.datastore import _implicit_environ

Expand Down
2 changes: 1 addition & 1 deletion gcloud/datastore/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def test_w_deferred_from_backend_but_not_passed(self):

# Make URI to check for requests.
URI = '/'.join([
conn.API_BASE_URL,
conn.api_base_url,
'datastore',
conn.API_VERSION,
'datasets',
Expand Down
Loading

0 comments on commit f56abdf

Please sign in to comment.