|
12 | 12 | import traceback |
13 | 13 |
|
14 | 14 | from ansible.module_utils._text import to_text, to_native |
15 | | -from ansible.module_utils.basic import missing_required_lib |
| 15 | +from ansible.module_utils.basic import missing_required_lib, env_fallback |
16 | 16 |
|
17 | 17 | CS_IMP_ERR = None |
18 | 18 | try: |
19 | | - from cs import CloudStack, CloudStackException, read_config |
| 19 | + from cs import CloudStack, CloudStackException |
20 | 20 | HAS_LIB_CS = True |
21 | 21 | except ImportError: |
22 | 22 | CS_IMP_ERR = traceback.format_exc() |
|
29 | 29 |
|
30 | 30 | def cs_argument_spec(): |
31 | 31 | return dict( |
32 | | - api_key=dict(default=os.environ.get('CLOUDSTACK_KEY')), |
33 | | - api_secret=dict(default=os.environ.get('CLOUDSTACK_SECRET'), no_log=True), |
34 | | - api_url=dict(default=os.environ.get('CLOUDSTACK_ENDPOINT')), |
35 | | - api_http_method=dict(choices=['get', 'post'], default=os.environ.get('CLOUDSTACK_METHOD')), |
36 | | - api_timeout=dict(type='int', default=os.environ.get('CLOUDSTACK_TIMEOUT')), |
37 | | - api_region=dict(default=os.environ.get('CLOUDSTACK_REGION') or 'cloudstack'), |
38 | | - api_verify_ssl_cert=dict(default=os.environ.get('CLOUDSTACK_VERIFY')), |
| 32 | + api_key=dict(type='str', fallback=(env_fallback, ['CLOUDSTACK_KEY']), required=True), |
| 33 | + api_secret=dict(type='str', fallback=(env_fallback, ['CLOUDSTACK_SECRET']), required=True, no_log=True), |
| 34 | + api_url=dict(type='str', fallback=(env_fallback, ['CLOUDSTACK_ENDPOINT']), required=True), |
| 35 | + api_http_method=dict(type='str', fallback=(env_fallback, ['CLOUDSTACK_METHOD']), choices=['get', 'post'], default='get'), |
| 36 | + api_timeout=dict(type='int', fallback=(env_fallback, ['CLOUDSTACK_TIMEOUT']), default=10), |
| 37 | + api_verify_ssl_cert=dict(type='str', fallback=(env_fallback, ['CLOUDSTACK_VERIFY'])), |
39 | 38 | ) |
40 | 39 |
|
41 | 40 |
|
42 | 41 | def cs_required_together(): |
43 | | - return [['api_key', 'api_secret']] |
| 42 | + return [] |
44 | 43 |
|
45 | 44 |
|
46 | 45 | class AnsibleCloudStack: |
@@ -114,30 +113,21 @@ def cs(self): |
114 | 113 | return self._cs |
115 | 114 |
|
116 | 115 | def get_api_config(self): |
117 | | - api_region = self.module.params.get('api_region') or os.environ.get('CLOUDSTACK_REGION') |
118 | | - try: |
119 | | - config = read_config(api_region) |
120 | | - except KeyError: |
121 | | - config = {} |
122 | | - |
123 | 116 | api_config = { |
124 | | - 'endpoint': self.module.params.get('api_url') or config.get('endpoint'), |
125 | | - 'key': self.module.params.get('api_key') or config.get('key'), |
126 | | - 'secret': self.module.params.get('api_secret') or config.get('secret'), |
127 | | - 'timeout': self.module.params.get('api_timeout') or config.get('timeout') or 10, |
128 | | - 'method': self.module.params.get('api_http_method') or config.get('method') or 'get', |
129 | | - 'verify': self.module.params.get('api_verify_ssl_cert') or config.get('verify'), |
| 117 | + 'endpoint': self.module.params.get('api_url'), |
| 118 | + 'key': self.module.params.get('api_key'), |
| 119 | + 'secret': self.module.params.get('api_secret'), |
| 120 | + 'timeout': self.module.params.get('api_timeout'), |
| 121 | + 'method': self.module.params.get('api_http_method'), |
| 122 | + 'verify': self.module.params.get('api_verify_ssl_cert'), |
130 | 123 | } |
131 | 124 | self.result.update({ |
132 | | - 'api_region': api_region, |
133 | 125 | 'api_url': api_config['endpoint'], |
134 | 126 | 'api_key': api_config['key'], |
135 | 127 | 'api_timeout': int(api_config['timeout']), |
136 | 128 | 'api_http_method': api_config['method'], |
137 | 129 | 'api_verify_ssl_cert': api_config['verify'], |
138 | 130 | }) |
139 | | - if not all([api_config['endpoint'], api_config['key'], api_config['secret']]): |
140 | | - self.fail_json(msg="Missing api credentials: can not authenticate") |
141 | 131 | return api_config |
142 | 132 |
|
143 | 133 | def fail_json(self, **kwargs): |
|
0 commit comments