From 17d69d3d3049ce8e0966afc8e9adf0e3cb5f2b81 Mon Sep 17 00:00:00 2001 From: Kirill Izotov Date: Wed, 25 May 2016 13:31:47 +0600 Subject: [PATCH] Load API key from config --- st2client/st2client/client.py | 7 ++++++- st2client/st2client/config_parser.py | 4 ++++ st2client/st2client/shell.py | 1 + st2client/tests/unit/test_config_parser.py | 3 ++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/st2client/st2client/client.py b/st2client/st2client/client.py index 56de648734..b7e4a569db 100644 --- a/st2client/st2client/client.py +++ b/st2client/st2client/client.py @@ -37,7 +37,7 @@ class Client(object): def __init__(self, base_url=None, auth_url=None, api_url=None, api_version=None, cacert=None, - debug=False, token=None): + debug=False, token=None, apikey=None): # Get CLI options. If not given, then try to get it from the environment. self.endpoints = dict() @@ -79,6 +79,11 @@ def __init__(self, base_url=None, auth_url=None, api_url=None, api_version=None, self.token = token + if apikey: + os.environ['ST2_API_KEY'] = apikey + + self.apikey = apikey + # Instantiate resource managers and assign appropriate API endpoint. self.managers = dict() self.managers['Token'] = ResourceManager( diff --git a/st2client/st2client/config_parser.py b/st2client/st2client/config_parser.py index 491899bc65..e8ae8b10f1 100644 --- a/st2client/st2client/config_parser.py +++ b/st2client/st2client/config_parser.py @@ -79,6 +79,10 @@ 'password': { 'type': 'string', 'default': None + }, + 'apikey': { + 'type': 'string', + 'default': None } }, 'api': { diff --git a/st2client/st2client/shell.py b/st2client/st2client/shell.py index 157ec470b0..b242b3376a 100755 --- a/st2client/st2client/shell.py +++ b/st2client/st2client/shell.py @@ -79,6 +79,7 @@ 'auth_url': ['auth', 'url'], 'api_url': ['api', 'url'], 'api_version': ['general', 'api_version'], + 'apikey': ['credentials', 'apikey'], 'cacert': ['general', 'cacert'], 'debug': ['cli', 'debug'] } diff --git a/st2client/tests/unit/test_config_parser.py b/st2client/tests/unit/test_config_parser.py index d5f1055fba..daa2b7c6e3 100644 --- a/st2client/tests/unit/test_config_parser.py +++ b/st2client/tests/unit/test_config_parser.py @@ -55,7 +55,8 @@ def test_parse(self): }, 'credentials': { 'username': 'test1', - 'password': 'test1' + 'password': 'test1', + 'apikey': None }, 'api': { 'url': 'http://127.0.0.1:9101/v1'