From 45f03142f2c3bf155ee50eb366069b816622b38a Mon Sep 17 00:00:00 2001 From: Hippolyte HENRY Date: Wed, 4 Mar 2020 15:51:49 +0100 Subject: [PATCH] support DD_API_KEY environment variable in dogwrap --- datadog/dogshell/wrap.py | 4 +++- tests/unit/dogwrap/test_dogwrap.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/datadog/dogshell/wrap.py b/datadog/dogshell/wrap.py index 4f9f8c51e..022b33317 100644 --- a/datadog/dogshell/wrap.py +++ b/datadog/dogshell/wrap.py @@ -22,6 +22,8 @@ ''' # stdlib from __future__ import print_function + +import os from copy import copy import optparse import subprocess @@ -260,7 +262,7 @@ def parse_options(raw_args=None): parser.add_option('-n', '--name', action='store', type='string', help="the name of the event \ as it should appear on your Datadog stream") parser.add_option('-k', '--api_key', action='store', type='string', - help="your DataDog API Key") + help="your DataDog API Key", default=os.environ.get("DD_API_KEY")) parser.add_option('-s', '--site', action='store', type='choice', default='datadoghq.com', choices=['datadoghq.com', 'us', 'datadoghq.eu', 'eu'], help="The site \ to send data, US (datadoghq.com) or EU (datadoghq.eu), default: US") parser.add_option('-m', '--submit_mode', action='store', type='choice', diff --git a/tests/unit/dogwrap/test_dogwrap.py b/tests/unit/dogwrap/test_dogwrap.py index f89cccf48..e4cd2a3c9 100644 --- a/tests/unit/dogwrap/test_dogwrap.py +++ b/tests/unit/dogwrap/test_dogwrap.py @@ -94,6 +94,10 @@ def test_parse_options(self): with self.assertRaises(SystemExit): parse_options(['--proc_poll_interval', 'invalid']) + with mock.patch.dict(os.environ, values={"DD_API_KEY": "the_key"}, clear=True): + options, _ = parse_options([]) + self.assertEqual(options.api_key, "the_key") + def test_poll_proc(self): mock_proc = mock.Mock() mock_proc.poll.side_effect = [None, 0]