From 382641ff24f7dce8a3158d66dbd50cb04f5666df Mon Sep 17 00:00:00 2001 From: David Passante Date: Thu, 27 Sep 2018 07:37:56 +0200 Subject: [PATCH] cs_account: Implement role parameter support (#46166) --- .../modules/cloud/cloudstack/cs_account.py | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_account.py b/lib/ansible/modules/cloud/cloudstack/cs_account.py index 0b40c9674c0..1709b311407 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_account.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_account.py @@ -71,6 +71,10 @@ options: description: - Domain the account is related to. default: 'ROOT' + role: + description: + - Creates the account under the specified role name or id. + version_added: 2.8 state: description: - State of the account. @@ -96,6 +100,7 @@ EXAMPLES = ''' first_name: John email: john.doe@example.com domain: CUSTOMERS + role: Domain Admin # Lock an existing account in domain 'CUSTOMERS' - local_action: @@ -158,6 +163,11 @@ domain: returned: success type: string sample: ROOT +role: + description: The role name of the account + returned: success + type: string + sample: Domain Admin ''' # import cloudstack common @@ -175,6 +185,7 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): super(AnsibleCloudStackAccount, self).__init__(module) self.returns = { 'networkdomain': 'network_domain', + 'rolename': 'role', } self.account = None self.account_types = { @@ -183,6 +194,21 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): 'domain_admin': 2, } + def get_role_id(self): + role_param = self.module.params.get('role') + role_id = None + + if role_param: + role_list = self.query_api('listRoles') + for role in role_list['role']: + if role_param in [role['name'], role['id']]: + role_id = role['id'] + + if not role_id: + self.module.fail_json(msg="Role not found: %s" % role_param) + + return role_id + def get_account_type(self): account_type = self.module.params.get('account_type') return self.account_types[account_type] @@ -278,7 +304,8 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): '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') + 'timezone': self.module.params.get('timezone'), + 'roleid': self.get_role_id() } if not self.module.check_mode: res = self.query_api('createAccount', **args) @@ -323,6 +350,7 @@ def main(): username=dict(), password=dict(no_log=True), timezone=dict(), + role=dict(), poll_async=dict(type='bool', default=True), ))