Debian Codegraph @

ansible_collections/community/general/plugins/modules/pritunl_user_info.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2021, Florian Dambrine <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: pritunl_user_info
10 author: "Florian Dambrine (@Lowess)"
11 version_added: 2.3.0
12 short_description: List Pritunl Users using the Pritunl API
13 description:
14 - A module to list Pritunl users using the Pritunl API.
15 extends_documentation_fragment:
16 - community.general._pritunl
17 - community.general._attributes
18 - community.general._attributes.info_module
19 options:
20 organization:
21 type: str
22 required: true
23 aliases:
24 - org
25 description:
26 - The name of the organization the user is part of.
27 user_name:
28 type: str
29 description:
30 - Name of the user to filter on Pritunl.
31 user_type:
32 type: str
33 default: client
34 choices:
35 - client
36 - server
37 description:
38 - Type of the user O(user_name).
39 """
40
41 EXAMPLES = r"""
42 - name: List all existing users part of the organization MyOrg
43 community.general.pritunl_user_info:
44 state: list
45 organization: MyOrg
46
47 - name: Search for the user named Florian part of the organization MyOrg
48 community.general.pritunl_user_info:
49 state: list
50 organization: MyOrg
51 user_name: Florian
52 """
53
54 RETURN = r"""
55 users:
56 description: List of Pritunl users.
57 returned: success
58 type: list
59 elements: dict
60 sample:
61 [
62 {
63 "audit": false,
64 "auth_type": "google",
65 "bypass_secondary": false,
66 "client_to_client": false,
67 "disabled": false,
68 "dns_mapping": null,
69 "dns_servers": null,
70 "dns_suffix": null,
71 "email": "[email protected]",
72 "gravatar": true,
73 "groups": [
74 "foo",
75 "bar"
76 ],
77 "id": "5d070dafe63q3b2e6s472c3b",
78 "name": "[email protected]",
79 "network_links": [],
80 "organization": "58070daee6sf342e6e4s2c36",
81 "organization_name": "Acme",
82 "otp_auth": true,
83 "otp_secret": "35H5EJA3XB2$4CWG",
84 "pin": false,
85 "port_forwarding": [],
86 "servers": []
87 }
88 ]
89 """
90
91 from ansible.module_utils.basic import AnsibleModule
92 from ansible.module_utils.common.dict_transformations import dict_merge
93
94 from ansible_collections.community.general.plugins.module_utils._pritunl_api import (
95 PritunlException,
96 get_pritunl_settings,
97 list_pritunl_organizations,
98 list_pritunl_users,
99 pritunl_argument_spec,
100 )
101
102
103 def get_pritunl_user(module):
104 user_name = module.params.get("user_name")
105 user_type = module.params.get("user_type")
106 org_name = module.params.get("organization")
107
108 org_obj_list = []
109
110 org_obj_list = list_pritunl_organizations(
111 **dict_merge(get_pritunl_settings(module), {"filters": {"name": org_name}})
112 )
113
114 if len(org_obj_list) == 0:
115 module.fail_json(msg=f"Can not list users from the organization '{org_name}' which does not exist")
116
117 org_id = org_obj_list[0]["id"]
118
119 users = list_pritunl_users(
120 **dict_merge(
121 get_pritunl_settings(module),
122 {
123 "organization_id": org_id,
124 "filters": ({"type": user_type} if user_name is None else {"name": user_name, "type": user_type}),
125 },
126 )
127 )
128
129 result = {}
130 result["changed"] = False
131 result["users"] = users
132
133 module.exit_json(**result)
134
135
136 def main():
137 argument_spec = pritunl_argument_spec()
138
139 argument_spec.update(
140 dict(
141 organization=dict(required=True, type="str", aliases=["org"]),
142 user_name=dict(type="str"),
143 user_type=dict(choices=["client", "server"], default="client"),
144 )
145 )
146
147 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
148
149 try:
150 get_pritunl_user(module)
151 except PritunlException as e:
152 module.fail_json(msg=f"{e}")
153
154
155 if __name__ == "__main__":
156 main()