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:
parent
2a751e1753
commit
1d907de966
2 changed files with 98 additions and 110 deletions
|
@ -202,8 +202,12 @@ domain:
|
||||||
sample: ROOT
|
sample: ROOT
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# import cloudstack common
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.cloudstack import *
|
from ansible.module_utils.cloudstack import (
|
||||||
|
AnsibleCloudStack,
|
||||||
|
cs_argument_spec,
|
||||||
|
cs_required_together,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AnsibleCloudStackUser(AnsibleCloudStack):
|
class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
|
@ -211,32 +215,33 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
super(AnsibleCloudStackUser, self).__init__(module)
|
super(AnsibleCloudStackUser, self).__init__(module)
|
||||||
self.returns = {
|
self.returns = {
|
||||||
'username': 'username',
|
'username': 'username',
|
||||||
'firstname': 'first_name',
|
'firstname': 'first_name',
|
||||||
'lastname': 'last_name',
|
'lastname': 'last_name',
|
||||||
'email': 'email',
|
'email': 'email',
|
||||||
'secretkey': 'api_secret',
|
'secretkey': 'api_secret',
|
||||||
'apikey': 'api_key',
|
'apikey': 'api_key',
|
||||||
'timezone': 'timezone',
|
'timezone': 'timezone',
|
||||||
}
|
}
|
||||||
self.account_types = {
|
self.account_types = {
|
||||||
'user': 0,
|
'user': 0,
|
||||||
'root_admin': 1,
|
'root_admin': 1,
|
||||||
'domain_admin': 2,
|
'domain_admin': 2,
|
||||||
}
|
}
|
||||||
self.user = None
|
self.user = None
|
||||||
|
|
||||||
|
|
||||||
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_user(self):
|
def get_user(self):
|
||||||
if not self.user:
|
if not self.user:
|
||||||
args = {}
|
args = {
|
||||||
args['domainid'] = self.get_domain('id')
|
'domainid': self.get_domain('id'),
|
||||||
users = self.cs.listUsers(**args)
|
}
|
||||||
|
|
||||||
|
users = self.query_api('listUsers', **args)
|
||||||
|
|
||||||
if users:
|
if users:
|
||||||
user_name = self.module.params.get('username')
|
user_name = self.module.params.get('username')
|
||||||
for u in users['user']:
|
for u in users['user']:
|
||||||
|
@ -245,7 +250,6 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
break
|
break
|
||||||
return self.user
|
return self.user
|
||||||
|
|
||||||
|
|
||||||
def enable_user(self):
|
def enable_user(self):
|
||||||
user = self.get_user()
|
user = self.get_user()
|
||||||
if not user:
|
if not user:
|
||||||
|
@ -253,16 +257,14 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
|
|
||||||
if user['state'].lower() != 'enabled':
|
if user['state'].lower() != 'enabled':
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
args = {}
|
args = {
|
||||||
args['id'] = user['id']
|
'id': user['id'],
|
||||||
|
}
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.enableUser(**args)
|
res = self.query_api('enableUser', **args)
|
||||||
if 'errortext' in res:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
|
|
||||||
user = res['user']
|
user = res['user']
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
def lock_user(self):
|
def lock_user(self):
|
||||||
user = self.get_user()
|
user = self.get_user()
|
||||||
if not user:
|
if not user:
|
||||||
|
@ -274,17 +276,16 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
|
|
||||||
if user['state'].lower() != 'locked':
|
if user['state'].lower() != 'locked':
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
args = {}
|
|
||||||
args['id'] = user['id']
|
args = {
|
||||||
|
'id': user['id'],
|
||||||
|
}
|
||||||
|
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.lockUser(**args)
|
res = self.query_api('lockUser', **args)
|
||||||
|
|
||||||
if 'errortext' in res:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
|
|
||||||
|
|
||||||
user = res['user']
|
user = res['user']
|
||||||
return user
|
|
||||||
|
|
||||||
|
return user
|
||||||
|
|
||||||
def disable_user(self):
|
def disable_user(self):
|
||||||
user = self.get_user()
|
user = self.get_user()
|
||||||
|
@ -293,32 +294,26 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
|
|
||||||
if user['state'].lower() != 'disabled':
|
if user['state'].lower() != 'disabled':
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
args = {}
|
args = {
|
||||||
args['id'] = user['id']
|
'id': user['id'],
|
||||||
|
}
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
user = self.cs.disableUser(**args)
|
user = self.query_api('disableUser', **args)
|
||||||
if 'errortext' in user:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % user['errortext'])
|
|
||||||
|
|
||||||
poll_async = self.module.params.get('poll_async')
|
poll_async = self.module.params.get('poll_async')
|
||||||
if poll_async:
|
if poll_async:
|
||||||
user = self.poll_job(user, 'user')
|
user = self.poll_job(user, 'user')
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
def present_user(self):
|
def present_user(self):
|
||||||
missing_params = []
|
required_params = [
|
||||||
for required_params in [
|
|
||||||
'account',
|
'account',
|
||||||
'email',
|
'email',
|
||||||
'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))
|
|
||||||
|
|
||||||
user = self.get_user()
|
user = self.get_user()
|
||||||
if user:
|
if user:
|
||||||
|
@ -327,56 +322,58 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
user = self._create_user(user)
|
user = self._create_user(user)
|
||||||
return 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):
|
def _create_user(self, user):
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
|
|
||||||
args = {}
|
args = self._get_common_args()
|
||||||
args['account'] = self.get_account(key='name')
|
args.update({
|
||||||
args['domainid'] = self.get_domain('id')
|
'account': self.get_account(key='name'),
|
||||||
args['username'] = self.module.params.get('username')
|
'domainid': self.get_domain('id'),
|
||||||
args['password'] = self.module.params.get('password')
|
'username': self.module.params.get('username'),
|
||||||
args['firstname'] = self.module.params.get('first_name')
|
'password': self.module.params.get('password'),
|
||||||
args['lastname'] = self.module.params.get('last_name')
|
})
|
||||||
args['email'] = self.module.params.get('email')
|
|
||||||
args['timezone'] = self.module.params.get('timezone')
|
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.createUser(**args)
|
res = self.query_api('createUser', **args)
|
||||||
if 'errortext' in res:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
|
|
||||||
user = res['user']
|
user = res['user']
|
||||||
|
|
||||||
# register user api keys
|
# register user api keys
|
||||||
res = self.cs.registerUserKeys(id=user['id'])
|
res = self.query_api('registerUserKeys', id=user['id'])
|
||||||
if 'errortext' in res:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
|
|
||||||
user.update(res['userkeys'])
|
user.update(res['userkeys'])
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
def _update_user(self, user):
|
def _update_user(self, user):
|
||||||
args = {}
|
args = self._get_common_args()
|
||||||
args['id'] = user['id']
|
args.update({
|
||||||
args['firstname'] = self.module.params.get('first_name')
|
'id': user['id'],
|
||||||
args['lastname'] = self.module.params.get('last_name')
|
})
|
||||||
args['email'] = self.module.params.get('email')
|
|
||||||
args['timezone'] = self.module.params.get('timezone')
|
|
||||||
if self.has_changed(args, user):
|
if self.has_changed(args, user):
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
|
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.updateUser(**args)
|
res = self.query_api('updateUser', **args)
|
||||||
if 'errortext' in res:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
|
|
||||||
user = res['user']
|
user = res['user']
|
||||||
|
|
||||||
# register user api keys
|
# register user api keys
|
||||||
if 'apikey' not in user:
|
if 'apikey' not in user:
|
||||||
self.result['changed'] = True
|
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):
|
def absent_user(self):
|
||||||
user = self.get_user()
|
user = self.get_user()
|
||||||
|
@ -384,18 +381,15 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
|
|
||||||
if not self.module.check_mode:
|
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
|
return user
|
||||||
|
|
||||||
|
|
||||||
def get_result(self, user):
|
def get_result(self, user):
|
||||||
super(AnsibleCloudStackUser, self).get_result(user)
|
super(AnsibleCloudStackUser, self).get_result(user)
|
||||||
if user:
|
if user:
|
||||||
if 'accounttype' in user:
|
if 'accounttype' in user:
|
||||||
for key,value in self.account_types.items():
|
for key, value in self.account_types.items():
|
||||||
if value == user['accounttype']:
|
if value == user['accounttype']:
|
||||||
self.result['account_type'] = key
|
self.result['account_type'] = key
|
||||||
break
|
break
|
||||||
|
@ -405,16 +399,16 @@ class AnsibleCloudStackUser(AnsibleCloudStack):
|
||||||
def main():
|
def main():
|
||||||
argument_spec = cs_argument_spec()
|
argument_spec = cs_argument_spec()
|
||||||
argument_spec.update(dict(
|
argument_spec.update(dict(
|
||||||
username = dict(required=True),
|
username=dict(required=True),
|
||||||
account = dict(default=None),
|
account=dict(),
|
||||||
state = dict(choices=['present', 'absent', 'enabled', 'disabled', 'locked', 'unlocked'], default='present'),
|
state=dict(choices=['present', 'absent', 'enabled', 'disabled', 'locked', 'unlocked'], default='present'),
|
||||||
domain = dict(default='ROOT'),
|
domain=dict(default='ROOT'),
|
||||||
email = dict(default=None),
|
email=dict(),
|
||||||
first_name = dict(default=None),
|
first_name=dict(),
|
||||||
last_name = dict(default=None),
|
last_name=dict(),
|
||||||
password = dict(default=None, no_log=True),
|
password=dict(no_log=True),
|
||||||
timezone = dict(default=None),
|
timezone=dict(),
|
||||||
poll_async = dict(type='bool', default=True),
|
poll_async=dict(type='bool', default=True),
|
||||||
))
|
))
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
@ -423,34 +417,29 @@ def main():
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
acs_acc = AnsibleCloudStackUser(module)
|
||||||
acs_acc = AnsibleCloudStackUser(module)
|
|
||||||
|
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
|
|
||||||
if state in ['absent']:
|
if state in ['absent']:
|
||||||
user = acs_acc.absent_user()
|
user = acs_acc.absent_user()
|
||||||
|
|
||||||
elif state in ['enabled', 'unlocked']:
|
elif state in ['enabled', 'unlocked']:
|
||||||
user = acs_acc.enable_user()
|
user = acs_acc.enable_user()
|
||||||
|
|
||||||
elif state in ['disabled']:
|
elif state in ['disabled']:
|
||||||
user = acs_acc.disable_user()
|
user = acs_acc.disable_user()
|
||||||
|
|
||||||
elif state in ['locked']:
|
elif state in ['locked']:
|
||||||
user = acs_acc.lock_user()
|
user = acs_acc.lock_user()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
user = acs_acc.present_user()
|
user = acs_acc.present_user()
|
||||||
|
|
||||||
result = acs_acc.get_result(user)
|
result = acs_acc.get_result(user)
|
||||||
|
|
||||||
except CloudStackException as e:
|
|
||||||
module.fail_json(msg='CloudStackException: %s' % str(e))
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import *
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -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_securitygroup_rule.py
|
||||||
lib/ansible/modules/cloud/cloudstack/cs_snapshot_policy.py
|
lib/ansible/modules/cloud/cloudstack/cs_snapshot_policy.py
|
||||||
lib/ansible/modules/cloud/cloudstack/cs_template.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_vmsnapshot.py
|
||||||
lib/ansible/modules/cloud/cloudstack/cs_volume.py
|
lib/ansible/modules/cloud/cloudstack/cs_volume.py
|
||||||
lib/ansible/modules/cloud/docker/_docker.py
|
lib/ansible/modules/cloud/docker/_docker.py
|
||||||
|
|
Loading…
Reference in a new issue