|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright: (c) 2023, XLAB Steampunk <[email protected]> |
| 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 | +from ..module_utils.utils import PayloadMapper |
| 11 | +from ..module_utils.role import Role |
| 12 | +from ..module_utils.rest_client import RestClient |
| 13 | + |
| 14 | + |
| 15 | +class User(PayloadMapper): |
| 16 | + def __init__(self, uuid, username, full_name, role_uuids, session_limit): |
| 17 | + self.uuid = uuid |
| 18 | + self.username = username |
| 19 | + self.full_name = full_name |
| 20 | + self.role_uuids = role_uuids |
| 21 | + self.session_limit = session_limit |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def from_ansible(cls): |
| 25 | + pass |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def from_hypercore(cls, user_dict): |
| 29 | + if not user_dict: # In case for get_record, return None if no result is found |
| 30 | + return None |
| 31 | + return cls( |
| 32 | + uuid=user_dict["uuid"], |
| 33 | + username=user_dict["username"], |
| 34 | + full_name=user_dict["fullName"], |
| 35 | + role_uuids=user_dict["roleUUIDs"], |
| 36 | + session_limit=user_dict["sessionLimit"], |
| 37 | + ) |
| 38 | + |
| 39 | + def to_hypercore(self): |
| 40 | + pass |
| 41 | + |
| 42 | + def to_ansible(self, rest_client: RestClient): |
| 43 | + return dict( |
| 44 | + uuid=self.uuid, |
| 45 | + username=self.username, |
| 46 | + full_name=self.full_name, |
| 47 | + roles=[ |
| 48 | + Role.get_role_from_uuid( |
| 49 | + role_uuid, rest_client, must_exist=False |
| 50 | + ).to_ansible() |
| 51 | + for role_uuid in self.role_uuids |
| 52 | + ], |
| 53 | + session_limit=self.session_limit, |
| 54 | + ) |
| 55 | + |
| 56 | + def __eq__(self, other): |
| 57 | + """ |
| 58 | + One User is equal to another if it has ALL attributes exactly the same. |
| 59 | + This method is used only in tests. |
| 60 | + """ |
| 61 | + return all( |
| 62 | + ( |
| 63 | + self.uuid == other.uuid, |
| 64 | + self.username == other.username, |
| 65 | + self.full_name == other.full_name, |
| 66 | + self.role_uuids == other.role_uuids, |
| 67 | + self.session_limit == other.session_limit, |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def get_user(cls, query, rest_client: RestClient, must_exist=False): |
| 73 | + hypercore_dict = rest_client.get_record( |
| 74 | + "/rest/v1/User", query, must_exist=must_exist |
| 75 | + ) |
| 76 | + user = cls.from_hypercore(hypercore_dict) |
| 77 | + return user |
0 commit comments