|
| 1 | +#!/usr/bin/python |
| 2 | +# coding: utf-8 -*- |
| 3 | + |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | + |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | + |
| 11 | +ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} |
| 12 | + |
| 13 | + |
| 14 | +DOCUMENTATION = ''' |
| 15 | +--- |
| 16 | +module: role_user_assignment |
| 17 | +author: "Tom Page (@Tompage1994)" |
| 18 | +short_description: Gives a user permission to a resource or an organization. |
| 19 | +description: |
| 20 | + - Use this endpoint to give a user permission to a resource or an organization. |
| 21 | + - After creation, the assignment cannot be edited, but can be deleted to remove those permissions. |
| 22 | +options: |
| 23 | + role_definition: |
| 24 | + description: |
| 25 | + - The name of the role definition to assign to the user. |
| 26 | + required: True |
| 27 | + type: str |
| 28 | + object_id: |
| 29 | + description: |
| 30 | + - Primary key of the object this assignment applies to. |
| 31 | + required: False |
| 32 | + type: int |
| 33 | + user: |
| 34 | + description: |
| 35 | + - The name of the user to assign to the object. |
| 36 | + required: False |
| 37 | + type: str |
| 38 | + object_ansible_id: |
| 39 | + description: |
| 40 | + - Resource id of the object this role applies to. Alternative to the object_id field. |
| 41 | + required: False |
| 42 | + type: str |
| 43 | + user_ansible_id: |
| 44 | + description: |
| 45 | + - Resource id of the user who will receive permissions from this assignment. Alternative to user field. |
| 46 | + required: False |
| 47 | + type: str |
| 48 | + state: |
| 49 | + description: |
| 50 | + - Desired state of the resource. |
| 51 | + choices: ["present", "absent", "exists"] |
| 52 | + default: "present" |
| 53 | + type: str |
| 54 | +extends_documentation_fragment: |
| 55 | +- ansible.platform.auth |
| 56 | +''' |
| 57 | + |
| 58 | + |
| 59 | +EXAMPLES = ''' |
| 60 | +- name: Give Administrators organization admin role for org 1 |
| 61 | + ansible.platform.role_user_assignment: |
| 62 | + role_definition: Organization Admin |
| 63 | + object_id: 1 |
| 64 | + user: Administrators |
| 65 | + state: present |
| 66 | +... |
| 67 | +''' |
| 68 | + |
| 69 | +from ansible.module_utils.basic import AnsibleModule |
| 70 | + |
| 71 | +from ..module_utils.arguments import AUTH_ARGSPEC |
| 72 | +from ..module_utils.client import Client |
| 73 | +from ..module_utils.common import lookup_resource_id |
| 74 | +from ..module_utils.controller import Controller |
| 75 | +from ..module_utils.errors import EDAError |
| 76 | + |
| 77 | +def main(): |
| 78 | + # Any additional arguments that are not fields of the item can be added here |
| 79 | + argument_spec = dict( |
| 80 | + user=dict(required=False, type='str'), |
| 81 | + object_id=dict(required=False, type='int'), |
| 82 | + role_definition=dict(required=True, type='str'), |
| 83 | + object_ansible_id=dict(required=False, type='str'), |
| 84 | + user_ansible_id=dict(required=False, type='str'), |
| 85 | + state=dict(default='present', choices=['present', 'absent', 'exists']), |
| 86 | + ) |
| 87 | + |
| 88 | + argument_spec.update(AUTH_ARGSPEC) |
| 89 | + |
| 90 | + module = AnsibleModule( |
| 91 | + argument_spec=argument_spec, |
| 92 | + supports_check_mode=True, |
| 93 | + mutually_exclusive=[ |
| 94 | + ('user', 'user_ansible_id'), |
| 95 | + ('object_id', 'object_ansible_id'), |
| 96 | + ], |
| 97 | + required_one_of=[ |
| 98 | + ('user', 'user_ansible_id'), |
| 99 | + ('object_id', 'object_ansible_id'), |
| 100 | + ] |
| 101 | + ) |
| 102 | + |
| 103 | + client = Client( |
| 104 | + host=module.params.get("controller_host"), |
| 105 | + username=module.params.get("controller_username"), |
| 106 | + password=module.params.get("controller_password"), |
| 107 | + timeout=module.params.get("request_timeout"), |
| 108 | + validate_certs=module.params.get("validate_certs"), |
| 109 | + ) |
| 110 | + |
| 111 | + user_param = module.params.get('user') |
| 112 | + object_id = module.params.get('object_id') |
| 113 | + role_definition_str = module.params.get('role_definition') |
| 114 | + object_ansible_id = module.params.get('object_ansible_id') |
| 115 | + user_ansible_id = module.params.get('user_ansible_id') |
| 116 | + state = module.params.get('state') |
| 117 | + |
| 118 | + controller = Controller(client, module) |
| 119 | + |
| 120 | + role_definition = controller.get_exactly_one('role_definitions', name=role_definition_str) |
| 121 | + user = controller.get_exactly_one('users', name=user_param) |
| 122 | + |
| 123 | + new_item = { |
| 124 | + 'role_definition': role_definition['id'] |
| 125 | + } |
| 126 | + |
| 127 | + if object_id is not None: |
| 128 | + new_item['object_id'] = object_id |
| 129 | + if user is not None: |
| 130 | + new_item['user'] = user['id'] if user else None |
| 131 | + if object_ansible_id is not None: |
| 132 | + new_item['object_ansible_id'] = object_ansible_id |
| 133 | + if user_ansible_id is not None: |
| 134 | + new_item['user_ansible_id'] = user_ansible_id |
| 135 | + |
| 136 | + try: |
| 137 | + assignment = controller.get_one_or_many( |
| 138 | + 'role_user_assignments', |
| 139 | + **{'data': new_item} |
| 140 | + ) |
| 141 | + assignment = assignment[0] if len(assignment) == 1 else None |
| 142 | + except EDAError as eda_err: |
| 143 | + module.fail_json(msg=str(eda_err)) |
| 144 | + |
| 145 | + if state == 'absent': |
| 146 | + try: |
| 147 | + ret = controller.delete_if_needed( |
| 148 | + assignment, |
| 149 | + endpoint='role_user_assignments' |
| 150 | + ) |
| 151 | + except EDAError as eda_err: |
| 152 | + module.fail_json(msg=str(eda_err)) |
| 153 | + |
| 154 | + elif state == 'present' and assignment is None: |
| 155 | + try: |
| 156 | + ret = controller.create_if_needed( |
| 157 | + new_item=new_item, |
| 158 | + endpoint='role_user_assignments', |
| 159 | + item_type='role_user_assignment', |
| 160 | + ) |
| 161 | + except EDAError as eda_err: |
| 162 | + module.fail_json(msg=str(eda_err)) |
| 163 | + |
| 164 | + else: |
| 165 | + ret = {'changed': False} |
| 166 | + |
| 167 | + module.exit_json(**ret) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + main() |
0 commit comments