From 90d4101c34deb590aaabf0d6bdec0f76e404614b Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Wed, 20 Sep 2017 11:48:23 -0400 Subject: [PATCH] Add api_key lookup - Add api_key lookup to wrapper.py - Use api_key if provided - Use value of CICO_API_KEY - Use contents of ~/.duffy.key - Use contents of ~/duffy.key - Update ansible module to simply default to None and use lookup from wrapper - Update shell.py to default to None and use lookup from wrapper. --- cicoclient/ansible/cico.py | 12 +++++------- cicoclient/cli.py | 1 - cicoclient/shell.py | 8 +++++--- cicoclient/wrapper.py | 22 +++++++++++++++++++--- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cicoclient/ansible/cico.py b/cicoclient/ansible/cico.py index 7fd2c3b..3492770 100644 --- a/cicoclient/ansible/cico.py +++ b/cicoclient/ansible/cico.py @@ -62,7 +62,7 @@ api_key: description: - API key - default: CICO_API_KEY environment variable or None + default: CICO_API_KEY environment variable, the contents of ~/.duffy.key, the contents of ~/duffy.key, or None ssid: description: - SessionID, required with action 'done', optional with 'list'. @@ -125,7 +125,6 @@ api_key: 723ef3ce-4ea4-4e8d-9c8a-20a8249b2955 ssid: 3e03553f-ae28-4a68-b879-f0fdbf949d5d ''' -import os try: from cicoclient.wrapper import CicoWrapper HAS_CICO = True @@ -146,7 +145,7 @@ def main(): retry_count=dict(default=1, type='int'), retry_interval=dict(default=10, type='int'), endpoint=dict(default='http://admin.ci.centos.org:8080/'), - api_key=dict(default=os.getenv('CICO_API_KEY', None), no_log=True), + api_key=dict(default=None, no_log=True), ssid=dict(default=None), ) module = AnsibleModule(argument_spec) @@ -165,10 +164,6 @@ def main(): ssid = module.params['ssid'] flavor = module.params['flavor'] - # Pre-flight validation - if api_key is None: - module.fail_json(msg='An API key is required for this module.') - if action == 'done' and ssid is None: module.fail_json(msg='A SSID is required when releasing nodes.') @@ -178,6 +173,9 @@ def main(): api_key=api_key ) + if api.api_key is None: + module.fail_json(msg='An API key is required for this module.') + if action == 'get': hosts, new_ssid = api.node_get(arch=arch, ver=release, count=count, retry_count=retry_count, diff --git a/cicoclient/cli.py b/cicoclient/cli.py index 8c59d9d..9c947a4 100644 --- a/cicoclient/cli.py +++ b/cicoclient/cli.py @@ -14,7 +14,6 @@ # import logging -import sys from cliff.lister import Lister from cicoclient.wrapper import CicoWrapper diff --git a/cicoclient/shell.py b/cicoclient/shell.py index 011a749..3e294f8 100644 --- a/cicoclient/shell.py +++ b/cicoclient/shell.py @@ -47,9 +47,11 @@ def build_option_parser(self, description, version): parser.add_argument( '--api-key', metavar='', - help='API key to admin.ci.centos.org service. Defaults to' - ' environment variable for CICO_API_KEY.', - default=os.getenv('CICO_API_KEY', None) + help='API key to admin.ci.centos.org service. If not provided the' + ' value of the CICO_API_KEY environment variable will be used' + ' if defined, followed by the contents of ~/.duffy.key if' + ' present, finally the contents of ~/duffy.key if present.', + default=None ) return parser diff --git a/cicoclient/wrapper.py b/cicoclient/wrapper.py index f2016dc..e0aece0 100644 --- a/cicoclient/wrapper.py +++ b/cicoclient/wrapper.py @@ -13,6 +13,7 @@ # under the License. # +import os import time import cicoclient.client as client @@ -27,12 +28,27 @@ class CicoWrapper(client.CicoClient): def __init__(self, **params): super(CicoWrapper, self).__init__(**params) self.user_agent = 'python-cicoclient-wrapper' + self.api_key = self._lookup_api_key(params.get('api_key')) + + self._full_inventory = self._self_inventory = None + + @staticmethod + def _lookup_api_key(api_key): + if api_key is not None: + return api_key + try: - self.api_key = params['api_key'] + return os.environ['CICO_API_KEY'] except KeyError: - self.api_key = None + pass - self._full_inventory = self._self_inventory = None + for key_file in ('~/.duffy.key', '~/duffy.key'): + try: + return open(os.path.expanduser(key_file)).read().strip() + except IOError: + pass + + return None @property def full_inventory(self):