From 8477525d2b1005d1932e6309640079a6bcaf3fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Sun, 16 Jul 2017 12:02:28 +0200 Subject: [PATCH] cloudstack: cs_account: remove CloudStackException dependency (#26870) --- .../modules/cloud/cloudstack/cs_account.py | 51 +++++++------------ 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_account.py b/lib/ansible/modules/cloud/cloudstack/cs_account.py index a35089d54ff..a79f4e4cb91 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_account.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_account.py @@ -181,7 +181,6 @@ domain: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -211,7 +210,7 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): 'listall': True, 'domainid': self.get_domain(key='id'), } - accounts = self.cs.listAccounts(**args) + accounts = self.query_api('listAccounts', **args) if accounts: account_name = self.module.params.get('name') for a in accounts['account']: @@ -234,9 +233,7 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): 'domainid': self.get_domain(key='id') } if not self.module.check_mode: - res = self.cs.enableAccount(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('enableAccount', **args) account = res['account'] return account @@ -265,10 +262,7 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): 'lock': lock, } if not self.module.check_mode: - account = self.cs.disableAccount(**args) - - if 'errortext' in account: - self.module.fail_json(msg="Failed: '%s'" % account['errortext']) + account = self.query_api('disableAccount', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -303,9 +297,7 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): 'timezone': self.module.params.get('timezone') } if not self.module.check_mode: - res = self.cs.createAccount(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createAccount', **args) account = res['account'] return account @@ -315,10 +307,7 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.deleteAccount(id=account['id']) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteAccount', id=account['id']) poll_async = self.module.params.get('poll_async') if poll_async: @@ -359,30 +348,26 @@ def main(): supports_check_mode=True ) - try: - acs_acc = AnsibleCloudStackAccount(module) - - state = module.params.get('state') + acs_acc = AnsibleCloudStackAccount(module) - if state in ['absent']: - account = acs_acc.absent_account() + state = module.params.get('state') - elif state in ['enabled', 'unlocked']: - account = acs_acc.enable_account() + if state in ['absent']: + account = acs_acc.absent_account() - elif state in ['disabled']: - account = acs_acc.disable_account() + elif state in ['enabled', 'unlocked']: + account = acs_acc.enable_account() - elif state in ['locked']: - account = acs_acc.lock_account() + elif state in ['disabled']: + account = acs_acc.disable_account() - else: - account = acs_acc.present_account() + elif state in ['locked']: + account = acs_acc.lock_account() - result = acs_acc.get_result(account) + else: + account = acs_acc.present_account() - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_acc.get_result(account) module.exit_json(**result)