Skip to content

Commit dd99e2c

Browse files
committed
Move dataset ID / connection discovery from '_implicit_environ' to 'client'.
1 parent 94c9c11 commit dd99e2c

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

gcloud/datastore/client.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,80 @@
1313
# limitations under the License.
1414
"""Convenience wrapper for invoking APIs/factories w/ a dataset ID."""
1515

16+
import os
17+
1618
from gcloud._helpers import _LocalStack
19+
from gcloud._helpers import _app_engine_id
20+
from gcloud._helpers import _compute_engine_id
1721
from gcloud.datastore import helpers
22+
from gcloud.datastore.connection import Connection
1823
from gcloud.datastore.batch import Batch
1924
from gcloud.datastore.entity import Entity
2025
from gcloud.datastore.key import Key
2126
from gcloud.datastore.query import Query
2227
from gcloud.datastore.transaction import Transaction
23-
from gcloud.datastore._implicit_environ import _determine_default_dataset_id
24-
from gcloud.datastore._implicit_environ import get_connection
2528

2629

2730
_MAX_LOOPS = 128
2831
"""Maximum number of iterations to wait for deferred keys."""
2932

33+
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
34+
"""Environment variable defining default dataset ID."""
35+
36+
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
37+
"""Environment variable defining default dataset ID under GCD."""
38+
39+
40+
def _get_production_dataset_id():
41+
"""Gets the production application ID if it can be inferred."""
42+
return os.getenv(_DATASET_ENV_VAR_NAME)
43+
44+
45+
def _get_gcd_dataset_id():
46+
"""Gets the GCD application ID if it can be inferred."""
47+
return os.getenv(_GCD_DATASET_ENV_VAR_NAME)
48+
49+
50+
def _determine_default_dataset_id(dataset_id=None):
51+
"""Determine default dataset ID explicitly or implicitly as fall-back.
52+
53+
In implicit case, supports four environments. In order of precedence, the
54+
implicit environments are:
55+
56+
* GCLOUD_DATASET_ID environment variable
57+
* DATASTORE_DATASET environment variable (for ``gcd`` testing)
58+
* Google App Engine application ID
59+
* Google Compute Engine project ID (from metadata server)
60+
61+
:type dataset_id: string
62+
:param dataset_id: Optional. The dataset ID to use as default.
63+
64+
:rtype: string or ``NoneType``
65+
:returns: Default dataset ID if it can be determined.
66+
"""
67+
if dataset_id is None:
68+
dataset_id = _get_production_dataset_id()
69+
70+
if dataset_id is None:
71+
dataset_id = _get_gcd_dataset_id()
72+
73+
if dataset_id is None:
74+
dataset_id = _app_engine_id()
75+
76+
if dataset_id is None:
77+
dataset_id = _compute_engine_id()
78+
79+
return dataset_id
80+
81+
82+
def _get_connection():
83+
"""Shortcut method to establish a connection to the Cloud Datastore.
84+
85+
:rtype: :class:`gcloud.datastore.connection.Connection`
86+
:returns: A connection defined with the proper credentials.
87+
"""
88+
return Connection.from_environment()
89+
3090

3191
def _extended_lookup(connection, dataset_id, key_pbs,
3292
missing=None, deferred=None,
@@ -126,7 +186,7 @@ def __init__(self, dataset_id=None, namespace=None, connection=None):
126186
raise EnvironmentError('Dataset ID could not be inferred.')
127187
self.dataset_id = dataset_id
128188
if connection is None:
129-
connection = get_connection()
189+
connection = _get_connection()
130190
self.connection = connection
131191
self._batch_stack = _LocalStack()
132192
self.namespace = namespace

gcloud/datastore/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_ctor_w_implicit_inputs(self):
5858
klass = self._getTargetClass()
5959
with _Monkey(_MUT,
6060
_determine_default_dataset_id=lambda x: x or OTHER,
61-
get_connection=lambda: conn):
61+
_get_connection=lambda: conn):
6262
client = klass()
6363
self.assertEqual(client.dataset_id, OTHER)
6464
self.assertEqual(client.namespace, None)

0 commit comments

Comments
 (0)