Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Bringing all environment variables into a single place. #1010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2015
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
8 changes: 4 additions & 4 deletions 8 gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Local(object):
except ImportError:
app_identity = None

from gcloud.environment_vars import PROJECT


_RFC3339_MICROS = '%Y-%m-%dT%H:%M:%S.%fZ'


Expand Down Expand Up @@ -141,12 +144,9 @@ def _compute_engine_id():
connection.close()


_PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'


def _get_production_project():
"""Gets the production project if it can be inferred."""
return os.getenv(_PROJECT_ENV_VAR_NAME)
return os.getenv(PROJECT)


def _determine_default_project(project=None):
Expand Down
12 changes: 4 additions & 8 deletions 12 gcloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,22 @@
from gcloud.datastore.key import Key
from gcloud.datastore.query import Query
from gcloud.datastore.transaction import Transaction
from gcloud.environment_vars import DATASET
from gcloud.environment_vars import GCD_DATASET


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

_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
"""Environment variable defining default dataset ID."""

_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
"""Environment variable defining default dataset ID under GCD."""


def _get_production_dataset_id():
"""Gets the production application ID if it can be inferred."""
return os.getenv(_DATASET_ENV_VAR_NAME)
return os.getenv(DATASET)


def _get_gcd_dataset_id():
"""Gets the GCD application ID if it can be inferred."""
return os.getenv(_GCD_DATASET_ENV_VAR_NAME)
return os.getenv(GCD_DATASET)


def _determine_default_dataset_id(dataset_id=None):
Expand Down
5 changes: 2 additions & 3 deletions 5 gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os

from gcloud import connection
from gcloud.environment_vars import GCD_HOST
from gcloud.exceptions import make_exception
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb

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

_GCD_HOST_ENV_VAR_NAME = 'DATASTORE_HOST'


class Connection(connection.Connection):
"""A connection to the Google Cloud Datastore via the Protobuf API.
Expand Down Expand Up @@ -56,7 +55,7 @@ def __init__(self, credentials=None, http=None, api_base_url=None):
credentials = self._create_scoped_credentials(credentials, SCOPE)
super(Connection, self).__init__(credentials=credentials, http=http)
if api_base_url is None:
api_base_url = os.getenv(_GCD_HOST_ENV_VAR_NAME,
api_base_url = os.getenv(GCD_HOST,
connection.API_BASE_URL)
self.api_base_url = api_base_url

Expand Down
8 changes: 4 additions & 4 deletions 8 gcloud/datastore/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def test_no_value(self):
def test_value_set(self):
import os
from gcloud._testing import _Monkey
from gcloud.datastore.client import _DATASET_ENV_VAR_NAME
from gcloud.datastore.client import DATASET

MOCK_DATASET_ID = object()
environ = {_DATASET_ENV_VAR_NAME: MOCK_DATASET_ID}
environ = {DATASET: MOCK_DATASET_ID}
with _Monkey(os, getenv=environ.get):
dataset_id = self._callFUT()
self.assertEqual(dataset_id, MOCK_DATASET_ID)
Expand All @@ -76,10 +76,10 @@ def test_no_value(self):
def test_value_set(self):
import os
from gcloud._testing import _Monkey
from gcloud.datastore.client import _GCD_DATASET_ENV_VAR_NAME
from gcloud.datastore.client import GCD_DATASET

MOCK_DATASET_ID = object()
environ = {_GCD_DATASET_ENV_VAR_NAME: MOCK_DATASET_ID}
environ = {GCD_DATASET: MOCK_DATASET_ID}
with _Monkey(os, getenv=environ.get):
dataset_id = self._callFUT()
self.assertEqual(dataset_id, MOCK_DATASET_ID)
Expand Down
8 changes: 4 additions & 4 deletions 8 gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def test_custom_url_from_env(self):
import os
from gcloud._testing import _Monkey
from gcloud.connection import API_BASE_URL
from gcloud.datastore.connection import _GCD_HOST_ENV_VAR_NAME
from gcloud.datastore.connection import GCD_HOST

HOST = object()
fake_environ = {_GCD_HOST_ENV_VAR_NAME: HOST}
fake_environ = {GCD_HOST: HOST}

with _Monkey(os, getenv=fake_environ.get):
conn = self._makeOne()
Expand All @@ -79,11 +79,11 @@ def test_custom_url_constructor_and_env(self):
import os
from gcloud._testing import _Monkey
from gcloud.connection import API_BASE_URL
from gcloud.datastore.connection import _GCD_HOST_ENV_VAR_NAME
from gcloud.datastore.connection import GCD_HOST

HOST1 = object()
HOST2 = object()
fake_environ = {_GCD_HOST_ENV_VAR_NAME: HOST1}
fake_environ = {GCD_HOST: HOST1}

with _Monkey(os, getenv=fake_environ.get):
conn = self._makeOne(api_base_url=HOST2)
Expand Down
40 changes: 40 additions & 0 deletions 40 gcloud/environment_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Comprehensive list of environment variables used in gcloud.

These enable many types of implicit behavior in both production
and tests.
"""

PROJECT = 'GCLOUD_PROJECT'
"""Environment variable defining default project."""

TESTS_PROJECT = 'GCLOUD_TESTS_PROJECT_ID'
"""Environment variable defining project for tests."""

DATASET = 'GCLOUD_DATASET_ID'
"""Environment variable defining default dataset ID."""

GCD_DATASET = 'DATASTORE_DATASET'
"""Environment variable defining default dataset ID under GCD."""

GCD_HOST = 'DATASTORE_HOST'
"""Environment variable defining host for GCD dataset server."""

TESTS_DATASET = 'GCLOUD_TESTS_DATASET_ID'
"""Environment variable defining dataset ID for tests."""

CREDENTIALS = 'GOOGLE_APPLICATION_CREDENTIALS'
"""Environment variable defining location of Google credentials."""
4 changes: 2 additions & 2 deletions 4 gcloud/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def test_no_value(self):
def test_value_set(self):
import os
from gcloud._testing import _Monkey
from gcloud._helpers import _PROJECT_ENV_VAR_NAME
from gcloud._helpers import PROJECT

MOCK_PROJECT = object()
environ = {_PROJECT_ENV_VAR_NAME: MOCK_PROJECT}
environ = {PROJECT: MOCK_PROJECT}
with _Monkey(os, getenv=environ.get):
project = self._callFUT()
self.assertEqual(project, MOCK_PROJECT)
Expand Down
13 changes: 13 additions & 0 deletions 13 system_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
3 changes: 2 additions & 1 deletion 3 system_tests/clear_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

from gcloud import datastore
from gcloud.datastore import client
from gcloud.environment_vars import TESTS_DATASET


client._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID'
client.DATASET = TESTS_DATASET
CLIENT = datastore.Client()


Expand Down
3 changes: 2 additions & 1 deletion 3 system_tests/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

from gcloud import datastore
from gcloud.datastore import client
from gcloud.environment_vars import TESTS_DATASET
# This assumes the command is being run via tox hence the
# repository root is the current directory.
from system_tests import populate_datastore


client._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID'
client.DATASET = TESTS_DATASET
CLIENT = datastore.Client()


Expand Down
2 changes: 2 additions & 0 deletions 2 system_tests/local_test_setup.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export GOOGLE_APPLICATION_CREDENTIALS="app_credentials.json.sample"
export GCLOUD_TESTS_PROJECT_ID="my-project"
export GCLOUD_TESTS_DATASET_ID=${GCLOUD_TESTS_PROJECT_ID}
export GCLOUD_REMOTE_FOR_LINT="upstream"
export GCLOUD_BRANCH_FOR_LINT="master"
3 changes: 2 additions & 1 deletion 3 system_tests/populate_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

from gcloud import datastore
from gcloud.datastore import client
from gcloud.environment_vars import TESTS_DATASET


client._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID'
client.DATASET = TESTS_DATASET
CLIENT = datastore.Client()


Expand Down
3 changes: 2 additions & 1 deletion 3 system_tests/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
import unittest2

from gcloud import _helpers
from gcloud.environment_vars import TESTS_PROJECT
from gcloud import pubsub


_helpers._PROJECT_ENV_VAR_NAME = 'GCLOUD_TESTS_PROJECT_ID'
_helpers.PROJECT = TESTS_PROJECT
CLIENT = pubsub.Client()


Expand Down
4 changes: 4 additions & 0 deletions 4 system_tests/run_system_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def main():
system_test_utils.check_environ(require_datastore=True)
elif args.package == 'storage':
system_test_utils.check_environ(require_storage=True)
elif args.package == 'pubsub':
system_test_utils.check_environ(require_pubsub=True)
else:
raise ValueError('Unexpected package name.')
test_result = run_module_tests(args.package)
if not test_result.wasSuccessful():
sys.exit(1)
Expand Down
5 changes: 3 additions & 2 deletions 5 system_tests/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
import time
import unittest2

from gcloud import _helpers
from gcloud.environment_vars import TESTS_PROJECT
from gcloud import exceptions
from gcloud import storage
from gcloud import _helpers
from gcloud.storage._helpers import _base64_md5hash


HTTP = httplib2.Http()
SHARED_BUCKETS = {}

_helpers._PROJECT_ENV_VAR_NAME = 'GCLOUD_TESTS_PROJECT_ID'
_helpers.PROJECT = TESTS_PROJECT
CLIENT = storage.Client()


Expand Down
17 changes: 11 additions & 6 deletions 17 system_tests/system_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@
import os
import sys

from gcloud.environment_vars import CREDENTIALS
from gcloud.environment_vars import TESTS_DATASET
from gcloud.environment_vars import TESTS_PROJECT


# From shell environ. May be None.
PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')
DATASET_ID = os.getenv('GCLOUD_TESTS_DATASET_ID')
CREDENTIALS = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
PROJECT_ID = os.getenv(TESTS_PROJECT)
DATASET_ID = os.getenv(TESTS_DATASET)
CREDENTIALS = os.getenv(CREDENTIALS)

ENVIRON_ERROR_MSG = """\
To run the system tests, you need to set some environment variables.
Please check the Contributing guide for instructions.
Please check the CONTRIBUTING guide for instructions.
"""


def check_environ(require_datastore=False, require_storage=False):
def check_environ(require_datastore=False, require_storage=False,
require_pubsub=False):
if require_datastore:
if DATASET_ID is None or not os.path.isfile(CREDENTIALS):
print(ENVIRON_ERROR_MSG, file=sys.stderr)
sys.exit(1)

if require_storage:
if require_storage or require_pubsub:
if PROJECT_ID is None or not os.path.isfile(CREDENTIALS):
print(ENVIRON_ERROR_MSG, file=sys.stderr)
sys.exit(1)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.