13
13
# limitations under the License.
14
14
"""Convenience wrapper for invoking APIs/factories w/ a dataset ID."""
15
15
16
+ import os
17
+
16
18
from gcloud ._helpers import _LocalStack
19
+ from gcloud ._helpers import _app_engine_id
20
+ from gcloud ._helpers import _compute_engine_id
17
21
from gcloud .datastore import helpers
22
+ from gcloud .datastore .connection import Connection
18
23
from gcloud .datastore .batch import Batch
19
24
from gcloud .datastore .entity import Entity
20
25
from gcloud .datastore .key import Key
21
26
from gcloud .datastore .query import Query
22
27
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
25
28
26
29
27
30
_MAX_LOOPS = 128
28
31
"""Maximum number of iterations to wait for deferred keys."""
29
32
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
+
30
90
31
91
def _extended_lookup (connection , dataset_id , key_pbs ,
32
92
missing = None , deferred = None ,
@@ -126,7 +186,7 @@ def __init__(self, dataset_id=None, namespace=None, connection=None):
126
186
raise EnvironmentError ('Dataset ID could not be inferred.' )
127
187
self .dataset_id = dataset_id
128
188
if connection is None :
129
- connection = get_connection ()
189
+ connection = _get_connection ()
130
190
self .connection = connection
131
191
self ._batch_stack = _LocalStack ()
132
192
self .namespace = namespace
0 commit comments