1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

cloudstack: cs_user: fix pep8 and minor restyling (#26849)

This commit is contained in:
René Moser 2017-07-15 19:16:53 +02:00 committed by GitHub
parent 2a751e1753
commit 1d907de966
2 changed files with 98 additions and 110 deletions

View file

@ -202,8 +202,12 @@ domain:
sample: ROOT
'''
# import cloudstack common
from ansible.module_utils.cloudstack import *
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import (
AnsibleCloudStack,
cs_argument_spec,
cs_required_together,
)
class AnsibleCloudStackUser(AnsibleCloudStack):
@ -226,17 +230,18 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
}
self.user = None
def get_account_type(self):
account_type = self.module.params.get('account_type')
return self.account_types[account_type]
def get_user(self):
if not self.user:
args = {}
args['domainid'] = self.get_domain('id')
users = self.cs.listUsers(**args)
args = {
'domainid': self.get_domain('id'),
}
users = self.query_api('listUsers', **args)
if users:
user_name = self.module.params.get('username')
for u in users['user']:
@ -245,7 +250,6 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
break
return self.user
def enable_user(self):
user = self.get_user()
if not user:
@ -253,16 +257,14 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
if user['state'].lower() != 'enabled':
self.result['changed'] = True
args = {}
args['id'] = user['id']
args = {
'id': user['id'],
}
if not self.module.check_mode:
res = self.cs.enableUser(**args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
res = self.query_api('enableUser', **args)
user = res['user']
return user
def lock_user(self):
user = self.get_user()
if not user:
@ -274,17 +276,16 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
if user['state'].lower() != 'locked':
self.result['changed'] = True
args = {}
args['id'] = user['id']
args = {
'id': user['id'],
}
if not self.module.check_mode:
res = self.cs.lockUser(**args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
res = self.query_api('lockUser', **args)
user = res['user']
return user
return user
def disable_user(self):
user = self.get_user()
@ -293,32 +294,26 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
if user['state'].lower() != 'disabled':
self.result['changed'] = True
args = {}
args['id'] = user['id']
args = {
'id': user['id'],
}
if not self.module.check_mode:
user = self.cs.disableUser(**args)
if 'errortext' in user:
self.module.fail_json(msg="Failed: '%s'" % user['errortext'])
user = self.query_api('disableUser', **args)
poll_async = self.module.params.get('poll_async')
if poll_async:
user = self.poll_job(user, 'user')
return user
def present_user(self):
missing_params = []
for required_params in [
required_params = [
'account',
'email',
'password',
'first_name',
'last_name',
]:
if not self.module.params.get(required_params):
missing_params.append(required_params)
if missing_params:
self.module.fail_json(msg="missing required arguments: %s" % ','.join(missing_params))
]
self.module.fail_on_missing_params(required_params=required_params)
user = self.get_user()
if user:
@ -327,56 +322,58 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
user = self._create_user(user)
return user
def _get_common_args(self):
return {
'firstname': self.module.params.get('first_name'),
'lastname': self.module.params.get('last_name'),
'email': self.module.params.get('email'),
'timezone': self.module.params.get('timezone'),
}
def _create_user(self, user):
self.result['changed'] = True
args = {}
args['account'] = self.get_account(key='name')
args['domainid'] = self.get_domain('id')
args['username'] = self.module.params.get('username')
args['password'] = self.module.params.get('password')
args['firstname'] = self.module.params.get('first_name')
args['lastname'] = self.module.params.get('last_name')
args['email'] = self.module.params.get('email')
args['timezone'] = self.module.params.get('timezone')
args = self._get_common_args()
args.update({
'account': self.get_account(key='name'),
'domainid': self.get_domain('id'),
'username': self.module.params.get('username'),
'password': self.module.params.get('password'),
})
if not self.module.check_mode:
res = self.cs.createUser(**args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
res = self.query_api('createUser', **args)
user = res['user']
# register user api keys
res = self.cs.registerUserKeys(id=user['id'])
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
res = self.query_api('registerUserKeys', id=user['id'])
user.update(res['userkeys'])
return user
def _update_user(self, user):
args = {}
args['id'] = user['id']
args['firstname'] = self.module.params.get('first_name')
args['lastname'] = self.module.params.get('last_name')
args['email'] = self.module.params.get('email')
args['timezone'] = self.module.params.get('timezone')
args = self._get_common_args()
args.update({
'id': user['id'],
})
if self.has_changed(args, user):
self.result['changed'] = True
if not self.module.check_mode:
res = self.cs.updateUser(**args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
res = self.query_api('updateUser', **args)
user = res['user']
# register user api keys
if 'apikey' not in user:
self.result['changed'] = True
if not self.module.check_mode:
res = self.cs.registerUserKeys(id=user['id'])
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
user.update(res['userkeys'])
return user
if not self.module.check_mode:
res = self.query_api('registerUserKeys', id=user['id'])
user.update(res['userkeys'])
return user
def absent_user(self):
user = self.get_user()
@ -384,13 +381,10 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
self.result['changed'] = True
if not self.module.check_mode:
res = self.cs.deleteUser(id=user['id'])
self.query_api('deleteUser', id=user['id'])
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
return user
def get_result(self, user):
super(AnsibleCloudStackUser, self).get_result(user)
if user:
@ -406,14 +400,14 @@ def main():
argument_spec = cs_argument_spec()
argument_spec.update(dict(
username=dict(required=True),
account = dict(default=None),
account=dict(),
state=dict(choices=['present', 'absent', 'enabled', 'disabled', 'locked', 'unlocked'], default='present'),
domain=dict(default='ROOT'),
email = dict(default=None),
first_name = dict(default=None),
last_name = dict(default=None),
password = dict(default=None, no_log=True),
timezone = dict(default=None),
email=dict(),
first_name=dict(),
last_name=dict(),
password=dict(no_log=True),
timezone=dict(),
poll_async=dict(type='bool', default=True),
))
@ -423,7 +417,6 @@ def main():
supports_check_mode=True
)
try:
acs_acc = AnsibleCloudStackUser(module)
state = module.params.get('state')
@ -445,12 +438,8 @@ def main():
result = acs_acc.get_result(user)
except CloudStackException as e:
module.fail_json(msg='CloudStackException: %s' % str(e))
module.exit_json(**result)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()

View file

@ -93,7 +93,6 @@ lib/ansible/modules/cloud/cloudstack/cs_securitygroup.py
lib/ansible/modules/cloud/cloudstack/cs_securitygroup_rule.py
lib/ansible/modules/cloud/cloudstack/cs_snapshot_policy.py
lib/ansible/modules/cloud/cloudstack/cs_template.py
lib/ansible/modules/cloud/cloudstack/cs_user.py
lib/ansible/modules/cloud/cloudstack/cs_vmsnapshot.py
lib/ansible/modules/cloud/cloudstack/cs_volume.py
lib/ansible/modules/cloud/docker/_docker.py