Skip to content

Commit 871f26e

Browse files
committed
Merge pull request #1010 from dhermes/single-env-var-file
Bringing all environment variables into a single place.
2 parents 00c82b6 + 43df1d4 commit 871f26e

16 files changed

+101
-37
lines changed

gcloud/_helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Local(object):
3131
except ImportError:
3232
app_identity = None
3333

34+
from gcloud.environment_vars import PROJECT
35+
36+
3437
_RFC3339_MICROS = '%Y-%m-%dT%H:%M:%S.%fZ'
3538

3639

@@ -141,12 +144,9 @@ def _compute_engine_id():
141144
connection.close()
142145

143146

144-
_PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'
145-
146-
147147
def _get_production_project():
148148
"""Gets the production project if it can be inferred."""
149-
return os.getenv(_PROJECT_ENV_VAR_NAME)
149+
return os.getenv(PROJECT)
150150

151151

152152
def _determine_default_project(project=None):

gcloud/datastore/client.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,22 @@
2626
from gcloud.datastore.key import Key
2727
from gcloud.datastore.query import Query
2828
from gcloud.datastore.transaction import Transaction
29+
from gcloud.environment_vars import DATASET
30+
from gcloud.environment_vars import GCD_DATASET
2931

3032

3133
_MAX_LOOPS = 128
3234
"""Maximum number of iterations to wait for deferred keys."""
3335

34-
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
35-
"""Environment variable defining default dataset ID."""
36-
37-
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
38-
"""Environment variable defining default dataset ID under GCD."""
39-
4036

4137
def _get_production_dataset_id():
4238
"""Gets the production application ID if it can be inferred."""
43-
return os.getenv(_DATASET_ENV_VAR_NAME)
39+
return os.getenv(DATASET)
4440

4541

4642
def _get_gcd_dataset_id():
4743
"""Gets the GCD application ID if it can be inferred."""
48-
return os.getenv(_GCD_DATASET_ENV_VAR_NAME)
44+
return os.getenv(GCD_DATASET)
4945

5046

5147
def _determine_default_dataset_id(dataset_id=None):

gcloud/datastore/connection.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818

1919
from gcloud import connection
20+
from gcloud.environment_vars import GCD_HOST
2021
from gcloud.exceptions import make_exception
2122
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb
2223

@@ -25,8 +26,6 @@
2526
'https://www.googleapis.com/auth/userinfo.email')
2627
"""The scopes required for authenticating as a Cloud Datastore consumer."""
2728

28-
_GCD_HOST_ENV_VAR_NAME = 'DATASTORE_HOST'
29-
3029

3130
class Connection(connection.Connection):
3231
"""A connection to the Google Cloud Datastore via the Protobuf API.
@@ -56,7 +55,7 @@ def __init__(self, credentials=None, http=None, api_base_url=None):
5655
credentials = self._create_scoped_credentials(credentials, SCOPE)
5756
super(Connection, self).__init__(credentials=credentials, http=http)
5857
if api_base_url is None:
59-
api_base_url = os.getenv(_GCD_HOST_ENV_VAR_NAME,
58+
api_base_url = os.getenv(GCD_HOST,
6059
connection.API_BASE_URL)
6160
self.api_base_url = api_base_url
6261

gcloud/datastore/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ def test_no_value(self):
4949
def test_value_set(self):
5050
import os
5151
from gcloud._testing import _Monkey
52-
from gcloud.datastore.client import _DATASET_ENV_VAR_NAME
52+
from gcloud.datastore.client import DATASET
5353

5454
MOCK_DATASET_ID = object()
55-
environ = {_DATASET_ENV_VAR_NAME: MOCK_DATASET_ID}
55+
environ = {DATASET: MOCK_DATASET_ID}
5656
with _Monkey(os, getenv=environ.get):
5757
dataset_id = self._callFUT()
5858
self.assertEqual(dataset_id, MOCK_DATASET_ID)
@@ -76,10 +76,10 @@ def test_no_value(self):
7676
def test_value_set(self):
7777
import os
7878
from gcloud._testing import _Monkey
79-
from gcloud.datastore.client import _GCD_DATASET_ENV_VAR_NAME
79+
from gcloud.datastore.client import GCD_DATASET
8080

8181
MOCK_DATASET_ID = object()
82-
environ = {_GCD_DATASET_ENV_VAR_NAME: MOCK_DATASET_ID}
82+
environ = {GCD_DATASET: MOCK_DATASET_ID}
8383
with _Monkey(os, getenv=environ.get):
8484
dataset_id = self._callFUT()
8585
self.assertEqual(dataset_id, MOCK_DATASET_ID)

gcloud/datastore/test_connection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def test_custom_url_from_env(self):
5656
import os
5757
from gcloud._testing import _Monkey
5858
from gcloud.connection import API_BASE_URL
59-
from gcloud.datastore.connection import _GCD_HOST_ENV_VAR_NAME
59+
from gcloud.datastore.connection import GCD_HOST
6060

6161
HOST = object()
62-
fake_environ = {_GCD_HOST_ENV_VAR_NAME: HOST}
62+
fake_environ = {GCD_HOST: HOST}
6363

6464
with _Monkey(os, getenv=fake_environ.get):
6565
conn = self._makeOne()
@@ -79,11 +79,11 @@ def test_custom_url_constructor_and_env(self):
7979
import os
8080
from gcloud._testing import _Monkey
8181
from gcloud.connection import API_BASE_URL
82-
from gcloud.datastore.connection import _GCD_HOST_ENV_VAR_NAME
82+
from gcloud.datastore.connection import GCD_HOST
8383

8484
HOST1 = object()
8585
HOST2 = object()
86-
fake_environ = {_GCD_HOST_ENV_VAR_NAME: HOST1}
86+
fake_environ = {GCD_HOST: HOST1}
8787

8888
with _Monkey(os, getenv=fake_environ.get):
8989
conn = self._makeOne(api_base_url=HOST2)

gcloud/environment_vars.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
"""Comprehensive list of environment variables used in gcloud.
16+
17+
These enable many types of implicit behavior in both production
18+
and tests.
19+
"""
20+
21+
PROJECT = 'GCLOUD_PROJECT'
22+
"""Environment variable defining default project."""
23+
24+
TESTS_PROJECT = 'GCLOUD_TESTS_PROJECT_ID'
25+
"""Environment variable defining project for tests."""
26+
27+
DATASET = 'GCLOUD_DATASET_ID'
28+
"""Environment variable defining default dataset ID."""
29+
30+
GCD_DATASET = 'DATASTORE_DATASET'
31+
"""Environment variable defining default dataset ID under GCD."""
32+
33+
GCD_HOST = 'DATASTORE_HOST'
34+
"""Environment variable defining host for GCD dataset server."""
35+
36+
TESTS_DATASET = 'GCLOUD_TESTS_DATASET_ID'
37+
"""Environment variable defining dataset ID for tests."""
38+
39+
CREDENTIALS = 'GOOGLE_APPLICATION_CREDENTIALS'
40+
"""Environment variable defining location of Google credentials."""

gcloud/test__helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ def test_no_value(self):
150150
def test_value_set(self):
151151
import os
152152
from gcloud._testing import _Monkey
153-
from gcloud._helpers import _PROJECT_ENV_VAR_NAME
153+
from gcloud._helpers import PROJECT
154154

155155
MOCK_PROJECT = object()
156-
environ = {_PROJECT_ENV_VAR_NAME: MOCK_PROJECT}
156+
environ = {PROJECT: MOCK_PROJECT}
157157
with _Monkey(os, getenv=environ.get):
158158
project = self._callFUT()
159159
self.assertEqual(project, MOCK_PROJECT)

system_tests/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.

system_tests/clear_datastore.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
from gcloud import datastore
2020
from gcloud.datastore import client
21+
from gcloud.environment_vars import TESTS_DATASET
2122

2223

23-
client._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID'
24+
client.DATASET = TESTS_DATASET
2425
CLIENT = datastore.Client()
2526

2627

system_tests/datastore.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818

1919
from gcloud import datastore
2020
from gcloud.datastore import client
21+
from gcloud.environment_vars import TESTS_DATASET
2122
# This assumes the command is being run via tox hence the
2223
# repository root is the current directory.
2324
from system_tests import populate_datastore
2425

2526

26-
client._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID'
27+
client.DATASET = TESTS_DATASET
2728
CLIENT = datastore.Client()
2829

2930

0 commit comments

Comments
 (0)