|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# copyright (c) 2025, Matthias Dellweg |
| 4 | +# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | + |
| 7 | +DOCUMENTATION = r""" |
| 8 | +--- |
| 9 | +module: user |
| 10 | +short_description: Manage users of a pulp api server instance |
| 11 | +description: |
| 12 | + - "This performs CRUD operations on users in a pulp api server instance." |
| 13 | +options: |
| 14 | + user: |
| 15 | + description: |
| 16 | + - User object to manipulate |
| 17 | + type: dict |
| 18 | + suboptions: |
| 19 | + username: |
| 20 | + description: |
| 21 | + - Username of the user to query or manipulate |
| 22 | + type: str |
| 23 | + required: true |
| 24 | + password: |
| 25 | + description: |
| 26 | + - Users password |
| 27 | + type: str |
| 28 | + first_name: |
| 29 | + description: |
| 30 | + - Users first name |
| 31 | + type: str |
| 32 | + last_name: |
| 33 | + description: |
| 34 | + - Users last name |
| 35 | + type: str |
| 36 | + email: |
| 37 | + description: |
| 38 | + - Users e-mail |
| 39 | + type: str |
| 40 | + is_active: |
| 41 | + description: |
| 42 | + - Whether the user can log in |
| 43 | + type: bool |
| 44 | + is_staff: |
| 45 | + description: |
| 46 | + - Whether the user belongs to the staff |
| 47 | + - This django attribute has no effect on pulp operations |
| 48 | + type: bool |
| 49 | + groups: |
| 50 | + description: |
| 51 | + - List of groups the user should be in |
| 52 | + type: list |
| 53 | + elements: str |
| 54 | +extends_documentation_fragment: |
| 55 | + - pulp.squeezer.pulp.entity_state |
| 56 | + - pulp.squeezer.pulp |
| 57 | +author: |
| 58 | + - Matthias Dellweg (@mdellweg) |
| 59 | +""" |
| 60 | + |
| 61 | +EXAMPLES = r""" |
| 62 | +- name: Read list of users from pulp api server |
| 63 | + pulp.squeezer.user: |
| 64 | + pulp_url: https://pulp.example.org |
| 65 | + username: admin |
| 66 | + password: password |
| 67 | + register: users |
| 68 | +- name: Report users |
| 69 | + debug: |
| 70 | + var: users |
| 71 | +""" |
| 72 | + |
| 73 | +RETURN = r""" |
| 74 | + users: |
| 75 | + description: List of users |
| 76 | + type: list |
| 77 | + returned: when no user is given |
| 78 | + user: |
| 79 | + description: user details |
| 80 | + type: dict |
| 81 | + returned: when user.username is given |
| 82 | +""" |
| 83 | + |
| 84 | + |
| 85 | +import traceback |
| 86 | + |
| 87 | +from ansible_collections.pulp.squeezer.plugins.module_utils.pulp_glue import PulpEntityAnsibleModule |
| 88 | + |
| 89 | +try: |
| 90 | + from pulp_glue.core.context import PulpGroupContext, PulpGroupUserContext, PulpUserContext |
| 91 | + |
| 92 | + PULP_GLUE_IMPORT_ERR = None |
| 93 | +except ImportError: |
| 94 | + PULP_GLUE_IMPORT_ERR = traceback.format_exc() |
| 95 | + PulpUserContext = None |
| 96 | + PulpGroupContext = None |
| 97 | + PulpGroupUserContext = None |
| 98 | + |
| 99 | + |
| 100 | +class PulpUserAnsibleModule(PulpEntityAnsibleModule): |
| 101 | + def process_converge(self, desired_entity, defaults=None): |
| 102 | + # Ideally glue would do this for us... |
| 103 | + groups = desired_entity and desired_entity.pop("groups", None) |
| 104 | + changed, before, after = super().process_converge(desired_entity, defaults=defaults) |
| 105 | + if self.check_mode and after is not None: |
| 106 | + # Fake the groups. |
| 107 | + after.setdefault("groups", []) |
| 108 | + if groups is not None: |
| 109 | + desired_groups = set(groups) |
| 110 | + actual_groups = {g["name"] for g in after["groups"]} |
| 111 | + missing_groups = desired_groups - actual_groups |
| 112 | + superfluous_groups = actual_groups - desired_groups |
| 113 | + for group in missing_groups: |
| 114 | + group_ctx = PulpGroupContext(self.pulp_ctx, entity={"name": group}) |
| 115 | + # Apparently pulp_glue did never implement this. |
| 116 | + # group_ctx.add_user(after["username"]) |
| 117 | + # ---8<--- Workaround ----8<---- |
| 118 | + group_user_ctx = PulpGroupUserContext(self.pulp_ctx, group_ctx) |
| 119 | + group_user_ctx.create(body={"username": self.context.entity["username"]}) |
| 120 | + # ---8<-------8<---- |
| 121 | + after["groups"].append(group_ctx.entity) |
| 122 | + changed = True |
| 123 | + for group in superfluous_groups: |
| 124 | + group_ctx = PulpGroupContext(self.pulp_ctx, entity={"name": group}) |
| 125 | + group_ctx.remove_user(self.context) |
| 126 | + after["groups"] = [g for g in after["groups"] if g["name"] != group] |
| 127 | + changed = True |
| 128 | + |
| 129 | + return changed, before, after |
| 130 | + |
| 131 | + |
| 132 | +def main(): |
| 133 | + with PulpUserAnsibleModule( |
| 134 | + context_class=PulpUserContext, |
| 135 | + entity_singular="user", |
| 136 | + entity_plural="users", |
| 137 | + import_errors=[("pulp-glue", PULP_GLUE_IMPORT_ERR)], |
| 138 | + argument_spec={ |
| 139 | + "user": { |
| 140 | + "type": "dict", |
| 141 | + "options": { |
| 142 | + "username": {"required": True}, |
| 143 | + "password": {"no_log": True}, |
| 144 | + "first_name": {}, |
| 145 | + "last_name": {}, |
| 146 | + "email": {}, |
| 147 | + "is_active": {"type": "bool"}, |
| 148 | + "is_staff": {"type": "bool"}, |
| 149 | + "groups": {"type": "list", "elements": "str"}, |
| 150 | + }, |
| 151 | + } |
| 152 | + }, |
| 153 | + required_if=[("state", "present", ["user"]), ("state", "absent", ["user"])], |
| 154 | + ) as module: |
| 155 | + user = module.params["user"] |
| 156 | + natural_key = {"username": user and user["username"]} |
| 157 | + desired_attributes = {} |
| 158 | + if user is not None: |
| 159 | + for key, value in user.items(): |
| 160 | + if key not in ["username"] and value is not None: |
| 161 | + desired_attributes[key] = value |
| 162 | + |
| 163 | + module.process(natural_key, desired_attributes) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + main() |
0 commit comments