Skip to content

Commit

Permalink
Add api_key lookup
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
detiber committed Sep 20, 2017
1 parent b9b6ecb commit 90d4101
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
12 changes: 5 additions & 7 deletions cicoclient/ansible/cico.py
Expand Up @@ -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'.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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.')

Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion cicoclient/cli.py
Expand Up @@ -14,7 +14,6 @@
#

import logging
import sys

from cliff.lister import Lister
from cicoclient.wrapper import CicoWrapper
Expand Down
8 changes: 5 additions & 3 deletions cicoclient/shell.py
Expand Up @@ -47,9 +47,11 @@ def build_option_parser(self, description, version):
parser.add_argument(
'--api-key',
metavar='<api-key>',
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
Expand Down
22 changes: 19 additions & 3 deletions cicoclient/wrapper.py
Expand Up @@ -13,6 +13,7 @@
# under the License.
#

import os
import time

import cicoclient.client as client
Expand All @@ -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):
Expand Down

0 comments on commit 90d4101

Please sign in to comment.