cloudstack: cs_account: overhaul code style

This commit is contained in:
Rene Moser 2016-06-08 17:54:21 +02:00 committed by Matt Clay
parent 29f803b16a
commit f9be547a87

View file

@ -190,28 +190,26 @@ class AnsibleCloudStackAccount(AnsibleCloudStack):
'domain_admin': 2, 'domain_admin': 2,
} }
def get_account_type(self): def get_account_type(self):
account_type = self.module.params.get('account_type') account_type = self.module.params.get('account_type')
return self.account_types[account_type] return self.account_types[account_type]
def get_account(self): def get_account(self):
if not self.account: if not self.account:
args = {} args = {
args['listall'] = True 'listall': True,
args['domainid'] = self.get_domain('id') 'domainid': self.get_domain(key='id'),
}
accounts = self.cs.listAccounts(**args) accounts = self.cs.listAccounts(**args)
if accounts: if accounts:
account_name = self.module.params.get('name') account_name = self.module.params.get('name')
for a in accounts['account']: for a in accounts['account']:
if account_name in [ a['name'] ]: if account_name == a['name']:
self.account = a self.account = a
break break
return self.account return self.account
def enable_account(self): def enable_account(self):
account = self.get_account() account = self.get_account()
if not account: if not account:
@ -219,10 +217,11 @@ class AnsibleCloudStackAccount(AnsibleCloudStack):
if account['state'].lower() != 'enabled': if account['state'].lower() != 'enabled':
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['id'] = account['id'] 'id': account['id'],
args['account'] = self.module.params.get('name') 'account': self.module.params.get('name'),
args['domainid'] = self.get_domain('id') 'domainid': self.get_domain(key='id')
}
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.enableAccount(**args) res = self.cs.enableAccount(**args)
if 'errortext' in res: if 'errortext' in res:
@ -230,15 +229,12 @@ class AnsibleCloudStackAccount(AnsibleCloudStack):
account = res['account'] account = res['account']
return account return account
def lock_account(self): def lock_account(self):
return self.lock_or_disable_account(lock=True) return self.lock_or_disable_account(lock=True)
def disable_account(self): def disable_account(self):
return self.lock_or_disable_account() return self.lock_or_disable_account()
def lock_or_disable_account(self, lock=False): def lock_or_disable_account(self, lock=False):
account = self.get_account() account = self.get_account()
if not account: if not account:
@ -248,14 +244,15 @@ class AnsibleCloudStackAccount(AnsibleCloudStack):
if lock and account['state'].lower() == 'disabled': if lock and account['state'].lower() == 'disabled':
account = self.enable_account() account = self.enable_account()
if lock and account['state'].lower() != 'locked' \ if (lock and account['state'].lower() != 'locked' or
or not lock and account['state'].lower() != 'disabled': not lock and account['state'].lower() != 'disabled'):
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['id'] = account['id'] 'id': account['id'],
args['account'] = self.module.params.get('name') 'account': self.module.params.get('name'),
args['domainid'] = self.get_domain('id') 'domainid': self.get_domain(key='id'),
args['lock'] = lock 'lock': lock,
}
if not self.module.check_mode: if not self.module.check_mode:
account = self.cs.disableAccount(**args) account = self.cs.disableAccount(**args)
@ -267,39 +264,33 @@ class AnsibleCloudStackAccount(AnsibleCloudStack):
account = self._poll_job(account, 'account') account = self._poll_job(account, 'account')
return account return account
def present_account(self): def present_account(self):
missing_params = [] required_params = [
missing_params = []
for required_params in [
'email', 'email',
'username', 'username',
'password', 'password',
'first_name', 'first_name',
'last_name', 'last_name',
]: ]
if not self.module.params.get(required_params): self.module.fail_on_missing_params(required_params=required_params)
missing_params.append(required_params)
if missing_params:
self.module.fail_json(msg="missing required arguments: %s" % ','.join(missing_params))
account = self.get_account() account = self.get_account()
if not account: if not account:
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['account'] = self.module.params.get('name') 'account': self.module.params.get('name'),
args['domainid'] = self.get_domain('id') 'domainid': self.get_domain(key='id'),
args['accounttype'] = self.get_account_type() 'accounttype': self.get_account_type(),
args['networkdomain'] = self.module.params.get('network_domain') 'networkdomain': self.module.params.get('network_domain'),
args['username'] = self.module.params.get('username') 'username': self.module.params.get('username'),
args['password'] = self.module.params.get('password') 'password': self.module.params.get('password'),
args['firstname'] = self.module.params.get('first_name') 'firstname': self.module.params.get('first_name'),
args['lastname'] = self.module.params.get('last_name') 'lastname': self.module.params.get('last_name'),
args['email'] = self.module.params.get('email') 'email': self.module.params.get('email'),
args['timezone'] = self.module.params.get('timezone') 'timezone': self.module.params.get('timezone')
}
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.createAccount(**args) res = self.cs.createAccount(**args)
if 'errortext' in res: if 'errortext' in res:
@ -307,7 +298,6 @@ class AnsibleCloudStackAccount(AnsibleCloudStack):
account = res['account'] account = res['account']
return account return account
def absent_account(self): def absent_account(self):
account = self.get_account() account = self.get_account()
if account: if account:
@ -321,10 +311,9 @@ class AnsibleCloudStackAccount(AnsibleCloudStack):
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
res = self._poll_job(res, 'account') self._poll_job(res, 'account')
return account return account
def get_result(self, account): def get_result(self, account):
super(AnsibleCloudStackAccount, self).get_result(account) super(AnsibleCloudStackAccount, self).get_result(account)
if account: if account:
@ -350,7 +339,7 @@ def main():
username=dict(default=None), username=dict(default=None),
password=dict(default=None, no_log=True), password=dict(default=None, no_log=True),
timezone=dict(default=None), timezone=dict(default=None),
poll_async = dict(choices=BOOLEANS, default=True), poll_async=dict(type='bool', default=True),
)) ))
module = AnsibleModule( module = AnsibleModule(