Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
tanganellilore committed Dec 2, 2024
1 parent 7614a86 commit d1b7c04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
8 changes: 4 additions & 4 deletions plugins/lookup/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ class LookupModule(LookupBase):
display = Display()

def handle_error(self, **kwargs):
raise AnsibleError(to_native(kwargs.get('msg')))
raise AnsibleError(to_native(kwargs.get("msg")))

def warn_callback(self, warning):
self.display.warning(warning)

def run(self, terms, variables=None, **kwargs):
if len(terms) != 1:
raise AnsibleError('You must pass exactly one endpoint to query')
raise AnsibleError("You must pass exactly one endpoint to query")

self.set_options(direct=kwargs)

module = AnsibleCloudStackAPI(argument_spec={}, direct_params=kwargs, error_callback=self.handle_error, warn_callback=self.warn_callback)

args = {}
if self.get_option('query_params'):
args.update(self.get_option('query_params', {}))
if self.get_option("query_params"):
args.update(self.get_option("query_params", {}))

res = module.query_api(terms[0], **args)

Expand Down
49 changes: 21 additions & 28 deletions plugins/module_utils/cloudstack_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CS_IMP_ERR = None
try:
from cs import CloudStack, CloudStackException

HAS_LIB_CS = True
except ImportError:
CS_IMP_ERR = traceback.format_exc()
Expand All @@ -33,24 +34,24 @@ class AnsibleCloudStackAPI(AnsibleModule):
error_callback = None
warn_callback = None
AUTH_ARGSPEC = dict(
api_key=os.getenv('CLOUDSTACK_KEY'),
api_secret=os.getenv('CLOUDSTACK_SECRET'),
api_url=os.getenv('CLOUDSTACK_ENDPOINT'),
api_http_method=os.getenv('CLOUDSTACK_METHOD', 'get'),
api_timeout=os.getenv('CLOUDSTACK_TIMEOUT', 10),
api_verify_ssl_cert=os.getenv('CLOUDSTACK_VERIFY'),
validate_certs=os.getenv('CLOUDSTACK_DANGEROUS_NO_TLS_VERIFY', True),
api_key=os.getenv("CLOUDSTACK_KEY"),
api_secret=os.getenv("CLOUDSTACK_SECRET"),
api_url=os.getenv("CLOUDSTACK_ENDPOINT"),
api_http_method=os.getenv("CLOUDSTACK_METHOD", "get"),
api_timeout=os.getenv("CLOUDSTACK_TIMEOUT", 10),
api_verify_ssl_cert=os.getenv("CLOUDSTACK_VERIFY"),
validate_certs=os.getenv("CLOUDSTACK_DANGEROUS_NO_TLS_VERIFY", True),
)

def __init__(self, argument_spec=None, direct_params=None, error_callback=None, warn_callback=None, **kwargs):

if not HAS_LIB_CS:
self.fail_json(msg=missing_required_lib('cs'), exception=CS_IMP_ERR)
self.fail_json(msg=missing_required_lib("cs"), exception=CS_IMP_ERR)

full_argspec = {}
full_argspec.update(AnsibleCloudStackAPI.AUTH_ARGSPEC)
full_argspec.update(argument_spec)
kwargs['supports_check_mode'] = True
kwargs["supports_check_mode"] = True

self.error_callback = error_callback
self.warn_callback = warn_callback
Expand All @@ -68,7 +69,7 @@ def __init__(self, argument_spec=None, direct_params=None, error_callback=None,
super(AnsibleCloudStackAPI, self).__init__(argument_spec=full_argspec, **kwargs)

# Perform some basic validation
if not re.match('^https{0,1}://', self.api_url):
if not re.match("^https{0,1}://", self.api_url):
self.api_url = "https://{0}".format(self.api_url)

def fail_json(self, **kwargs):
Expand All @@ -89,35 +90,27 @@ def cs(self):

def get_api_config(self):
api_config = {
'endpoint': self.api_url,
'key': self.api_key,
'secret': self.api_secret,
'timeout': self.api_timeout,
'method': self.api_http_method,
'verify': self.api_verify_ssl_cert,
'dangerous_no_tls_verify': not self.validate_certs,
"endpoint": self.api_url,
"key": self.api_key,
"secret": self.api_secret,
"timeout": self.api_timeout,
"method": self.api_http_method,
"verify": self.api_verify_ssl_cert,
"dangerous_no_tls_verify": not self.validate_certs,
}

# self.result.update({
# 'api_url': api_config['endpoint'],
# 'api_key': api_config['key'],
# 'api_timeout': int(api_config['timeout']),
# 'api_http_method': api_config['method'],
# 'api_verify_ssl_cert': api_config['verify'],
# 'validate_certs': not api_config['dangerous_no_tls_verify'],
# })
return api_config

def query_api(self, command, **args):

try:
res = getattr(self.cs, command)(**args)

if 'errortext' in res:
self.fail_json(msg="Failed: '%s'" % res['errortext'])
if "errortext" in res:
self.fail_json(msg="Failed: '%s'" % res["errortext"])

except CloudStackException as e:
self.fail_json(msg='CloudStackException: %s' % to_native(e))
self.fail_json(msg="CloudStackException: %s" % to_native(e))

except Exception as e:
self.fail_json(msg=to_native(e))
Expand Down

0 comments on commit d1b7c04

Please sign in to comment.