Skip to content

Commit 3e61294

Browse files
author
Remi Hakim
committed
Merge pull request #30 from DataDog/doug/api_datadog_host
Sniff DATADOG_HOST environment variable to determine which API host to use
2 parents 52e999e + 2df4f97 commit 3e61294

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

datadog/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* datadog.dogshell: a command-line tool, wrapping datadog.api, to interact with Datadog REST API.
99
"""
1010
from pkg_resources import get_distribution, DistributionNotFound
11+
import os
1112
import os.path
1213

1314
from datadog import api
@@ -30,7 +31,7 @@
3031
__version__ = _dist.version
3132

3233

33-
def initialize(api_key=None, app_key=None, host_name=None, api_host="https://app.datadoghq.com",
34+
def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
3435
proxies=None, statsd_host=None, statsd_port=None):
3536
"""
3637
Initialize and configure Datadog.api and Datadog.statsd modules
@@ -57,7 +58,8 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host="https://app
5758
api._api_key = api_key
5859
api._application_key = app_key
5960
api._host_name = host_name if host_name is not None else get_hostname()
60-
api._api_host = api_host
61+
api._api_host = api_host if api_host is not None else \
62+
os.environ.get('DATADOG_HOST', 'https://app.datadoghq.com')
6163
api._proxies = proxies
6264

6365
# Given statsd_host and statsd_port, overrides statsd instance

tests/unit/api/test_api.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# stdlib
2+
from functools import wraps
23
import os
34
import tempfile
45
import time
56

7+
68
# 3p
79
import mock
810
from nose.tools import assert_raises, assert_true, assert_false
@@ -26,6 +28,23 @@
2628
HOST_NAME,
2729
FAKE_PROXY)
2830

31+
def preserve_environ_datadog_host(func):
32+
"""
33+
Decorator to preserve the original environment value of DATADOG_HOST.
34+
"""
35+
@wraps(func)
36+
def wrapper(*args, **kwds):
37+
environ_api_host = os.environ.get('DATADOG_HOST')
38+
try:
39+
return func(*args, **kwds)
40+
finally:
41+
# restore the original environ value
42+
if environ_api_host:
43+
os.environ['DATADOG_HOST'] = environ_api_host
44+
elif os.environ.get('DATADOG_HOST'):
45+
del os.environ['DATADOG_HOST']
46+
47+
return wrapper
2948

3049
class TestInitialization(DatadogAPINoInitialization):
3150

@@ -90,6 +109,25 @@ def test_request_parameters(self):
90109
assert 'headers' in options
91110
assert options['headers'] == {'Content-Type': 'application/json'}
92111

112+
@preserve_environ_datadog_host
113+
def test_api_host_from_env(self):
114+
os.environ['DATADOG_HOST'] = 'http://localhost'
115+
initialize()
116+
self.assertEquals(api._api_host, 'http://localhost')
117+
118+
@preserve_environ_datadog_host
119+
def test_api_host_default(self):
120+
if os.environ.get('DATADOG_HOST'):
121+
del os.environ['DATADOG_HOST']
122+
initialize()
123+
self.assertEquals(api._api_host, 'https://app.datadoghq.com')
124+
125+
@preserve_environ_datadog_host
126+
def test_api_host_param(self):
127+
if os.environ.get('DATADOG_HOST'):
128+
del os.environ['DATADOG_HOST']
129+
initialize(api_host='http://localhost')
130+
self.assertEquals(api._api_host, 'http://localhost')
93131

94132
class TestResources(DatadogAPIWithInitialization):
95133

0 commit comments

Comments
 (0)