Skip to content

Commit 80e54e4

Browse files
authored
Add user_info module (#74)
1 parent 72f4931 commit 80e54e4

File tree

8 files changed

+708
-0
lines changed

8 files changed

+708
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
major_changes:
3+
- Added user_info module. (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/pull/74)

plugins/module_utils/role.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright: (c) 2022, 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+
7+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
from ..module_utils.utils import PayloadMapper
12+
from ..module_utils.rest_client import RestClient
13+
14+
15+
class Role(PayloadMapper):
16+
def __init__(self, uuid, name):
17+
self.uuid = uuid
18+
self.name = name
19+
20+
@classmethod
21+
def from_ansible(cls):
22+
pass
23+
24+
@classmethod
25+
def from_hypercore(cls, role_dict):
26+
if not role_dict: # In case for get_record, return None if no result is found
27+
return None
28+
return cls(
29+
uuid=role_dict["uuid"],
30+
name=role_dict["name"],
31+
)
32+
33+
def to_hypercore(self):
34+
pass
35+
36+
def to_ansible(self):
37+
return dict(
38+
uuid=self.uuid,
39+
name=self.name,
40+
)
41+
42+
def __eq__(self, other):
43+
"""
44+
One User is equal to another if it has ALL attributes exactly the same.
45+
This method is used only in tests.
46+
"""
47+
return all(
48+
(
49+
self.uuid == other.uuid,
50+
self.name == other.name,
51+
)
52+
)
53+
54+
@classmethod
55+
def get_role_from_uuid(cls, role_uuid, rest_client: RestClient, must_exist=False):
56+
hypercore_dict = rest_client.get_record(
57+
"/rest/v1/Role/{0}".format(role_uuid), must_exist=must_exist
58+
)
59+
role = cls.from_hypercore(hypercore_dict)
60+
return role

plugins/module_utils/user.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

plugins/modules/user_info.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2023, XLAB Steampunk <[email protected]>
4+
#
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
7+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
DOCUMENTATION = r"""
12+
module: user_info
13+
14+
author:
15+
- Polona Mihalič (@PolonaM)
16+
short_description: Returns information about the users.
17+
description:
18+
- Returns information about the users.
19+
version_added: 1.2.0
20+
extends_documentation_fragment:
21+
- scale_computing.hypercore.cluster_instance
22+
seealso:
23+
- module: scale_computing.hypercore.user
24+
options:
25+
username:
26+
description:
27+
- The user name.
28+
- Serves as unique identifier.
29+
type: str
30+
"""
31+
32+
EXAMPLES = r"""
33+
- name: List all users
34+
scale_computing.hypercore.user_info:
35+
register: users
36+
37+
- name: List selected user
38+
scale_computing.hypercore.user_info:
39+
username: my_username
40+
register: user
41+
"""
42+
43+
RETURN = r"""
44+
records:
45+
description:
46+
- A list of user records.
47+
returned: success
48+
type: list
49+
sample:
50+
- fullname: xlab
51+
roles:
52+
- uuid: 38b346c6-a626-444b-b6ab-92ecd671afc0
53+
name: Admin
54+
- uuid: 7224a2bd-5a08-4b99-a0de-9977089c66a4
55+
name: Backup
56+
session_limit: 0
57+
username: xlab
58+
uuid: 51e6d073-7566-4273-9196-58720117bd7f
59+
"""
60+
61+
62+
from ansible.module_utils.basic import AnsibleModule
63+
64+
from ..module_utils import arguments, errors
65+
from ..module_utils.rest_client import RestClient
66+
from ..module_utils.client import Client
67+
from ..module_utils.user import User
68+
from ..module_utils.utils import get_query
69+
70+
71+
def run(module, rest_client):
72+
query = get_query(
73+
module.params, "username", ansible_hypercore_map=dict(username="username")
74+
)
75+
return [
76+
User.from_hypercore(user_dict=hypercore_dict).to_ansible(rest_client)
77+
for hypercore_dict in rest_client.list_records("/rest/v1/User", query)
78+
]
79+
80+
81+
def main():
82+
module = AnsibleModule(
83+
supports_check_mode=True,
84+
argument_spec=dict(
85+
arguments.get_spec("cluster_instance"), username=dict(type="str")
86+
),
87+
)
88+
89+
try:
90+
client = Client.get_client(module.params["cluster_instance"])
91+
rest_client = RestClient(client)
92+
records = run(module, rest_client)
93+
module.exit_json(changed=False, records=records)
94+
95+
except errors.ScaleComputingError as e:
96+
module.fail_json(msg=str(e))
97+
98+
99+
if __name__ == "__main__":
100+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
- environment:
3+
SC_HOST: "{{ sc_host }}"
4+
SC_USERNAME: "{{ sc_username }}"
5+
SC_PASSWORD: "{{ sc_password }}"
6+
SC_TIMEOUT: "{{ sc_timeout }}"
7+
8+
block:
9+
- name: Retrieve info about the users
10+
scale_computing.hypercore.user_info:
11+
register: users
12+
- ansible.builtin.assert:
13+
that:
14+
- users.records != []
15+
- users.records[0].keys() | sort == ['full_name', 'roles', 'session_limit', 'username', 'uuid']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from __future__ import absolute_import, division, print_function
2+
3+
__metaclass__ = type
4+
5+
import sys
6+
7+
import pytest
8+
9+
from ansible_collections.scale_computing.hypercore.plugins.module_utils.role import Role
10+
11+
pytestmark = pytest.mark.skipif(
12+
sys.version_info < (2, 7), reason="requires python2.7 or higher"
13+
)
14+
15+
16+
class TestRole:
17+
def test_role_from_hypercore_dict_not_empty(self):
18+
role = Role(
19+
name="admin",
20+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
21+
)
22+
23+
hypercore_dict = dict(
24+
name="admin",
25+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
26+
)
27+
28+
role_from_hypercore = Role.from_hypercore(hypercore_dict)
29+
assert role == role_from_hypercore
30+
31+
def test_role_from_hypercore_dict_empty(self):
32+
assert Role.from_hypercore([]) is None
33+
34+
def test_role_to_ansible(self):
35+
role = Role(
36+
name="admin",
37+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
38+
)
39+
40+
ansible_dict = dict(
41+
name="admin",
42+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
43+
)
44+
45+
assert role.to_ansible() == ansible_dict
46+
47+
def test_role_equal_true(self):
48+
role1 = Role(
49+
name="admin",
50+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
51+
)
52+
role2 = Role(
53+
name="admin",
54+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
55+
)
56+
57+
assert role1 == role2
58+
59+
def test_role_equal_false(self):
60+
role1 = Role(
61+
name="admin",
62+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
63+
)
64+
role2 = Role(
65+
name="name",
66+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
67+
)
68+
69+
assert role1 != role2
70+
71+
def test_get_role_from_uuid(self, rest_client):
72+
role_uuid = "51e6d073-7566-4273-9196-58720117bd7f"
73+
rest_client.get_record.return_value = dict(
74+
name="admin",
75+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
76+
)
77+
78+
role_from_hypercore = Role.get_role_from_uuid(role_uuid, rest_client)
79+
80+
assert role_from_hypercore == Role(
81+
name="admin",
82+
uuid="f01249a6-2369-4471-bbc2-b4997067b6a6",
83+
)

0 commit comments

Comments
 (0)