Skip to content

Commit

Permalink
Merge branch 'pr/15'
Browse files Browse the repository at this point in the history
Thanks @detiber!!
  • Loading branch information
bstinsonmhk committed Sep 26, 2017
2 parents b9b6ecb + 596f6ff commit 4ccacbf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
34 changes: 24 additions & 10 deletions cicoclient/ansible/cico.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# License for the specific language governing permissions and limitations
# under the License.
#

from ansible.module_utils.basic import AnsibleModule

DOCUMENTATION = '''
---
module: cico
Expand Down Expand Up @@ -40,7 +43,17 @@
flavor:
description:
- The flavor (size) of an altarch Node
choices: [tiny, small, medium, lram.tiny, lram.small, lram.medium, xram.tiny, xram.small, xram.medium, xram.large]
choices:
- tiny
- small
- medium
- lram.tiny
- lram.small
- lram.medium
- xram.tiny
- xram.small
- xram.medium
- xram.large
default: small
count:
Expand All @@ -62,7 +75,9 @@
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 +140,7 @@
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 @@ -136,7 +151,8 @@
def main():
argument_spec = dict(
action=dict(required=True, choices=['get', 'done', 'list']),
arch=dict(default='x86_64', choices=['i386', 'x86_64', 'aarch64', 'ppc64le']),
arch=dict(default='x86_64', choices=['i386', 'x86_64', 'aarch64',
'ppc64le']),
flavor=dict(default='small', choices=['tiny', 'small', 'medium',
'lram.tiny', 'lram.small',
'xram.tiny', 'xram.small',
Expand All @@ -146,7 +162,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 +181,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 +190,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 Expand Up @@ -210,7 +225,6 @@ def main():
except Exception as e:
module.fail_json(msg=e.message)

from ansible.module_utils.basic import * # noqa

if __name__ == '__main__':
main()
1 change: 0 additions & 1 deletion cicoclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#

import logging
import sys

from cliff.lister import Lister
from cicoclient.wrapper import CicoWrapper
Expand Down
9 changes: 5 additions & 4 deletions cicoclient/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#

import sys
import os

import cicoclient
from cliff.app import App
Expand Down Expand Up @@ -47,9 +46,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
Original file line number Diff line number Diff line change
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 4ccacbf

Please sign in to comment.