Debian Codegraph @

ansible_collections/community/general/plugins/modules/pritunl_org_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_org_info
10 author: Florian Dambrine (@Lowess)
11 version_added: 2.5.0
12 short_description: List Pritunl Organizations using the Pritunl API
13 description:
14 - A module to list Pritunl organizations 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 aliases:
23 - org
24 default: null
25 description:
26 - Name of the Pritunl organization to search for. If none provided, the module returns all Pritunl organizations.
27 """
28
29 EXAMPLES = r"""
30 - name: List all existing Pritunl organizations
31 community.general.pritunl_org_info:
32
33 - name: Search for an organization named MyOrg
34 community.general.pritunl_user_info:
35 organization: MyOrg
36 """
37
38 RETURN = r"""
39 organizations:
40 description: List of Pritunl organizations.
41 returned: success
42 type: list
43 elements: dict
44 sample:
45 [
46 {
47 "auth_api": false,
48 "name": "FooOrg",
49 "auth_token": null,
50 "user_count": 0,
51 "auth_secret": null,
52 "id": "csftwlu6uhralzi2dpmhekz3"
53 },
54 {
55 "auth_api": false,
56 "name": "MyOrg",
57 "auth_token": null,
58 "user_count": 3,
59 "auth_secret": null,
60 "id": "58070daee63f3b2e6e472c36"
61 },
62 {
63 "auth_api": false,
64 "name": "BarOrg",
65 "auth_token": null,
66 "user_count": 0,
67 "auth_secret": null,
68 "id": "v1sncsxxybnsylc8gpqg85pg"
69 }
70 ]
71 """
72
73 from ansible.module_utils.basic import AnsibleModule
74 from ansible.module_utils.common.dict_transformations import dict_merge
75
76 from ansible_collections.community.general.plugins.module_utils._pritunl_api import (
77 PritunlException,
78 get_pritunl_settings,
79 list_pritunl_organizations,
80 pritunl_argument_spec,
81 )
82
83
84 def get_pritunl_organizations(module):
85 org_name = module.params.get("organization")
86
87 organizations = []
88
89 organizations = list_pritunl_organizations(
90 **dict_merge(
91 get_pritunl_settings(module),
92 {"filters": {"name": org_name} if org_name else None},
93 )
94 )
95
96 if org_name and len(organizations) == 0:
97 # When an org_name is provided but no organization match return an error
98 module.fail_json(msg=f"Organization '{org_name}' does not exist")
99
100 result = {}
101 result["changed"] = False
102 result["organizations"] = organizations
103
104 module.exit_json(**result)
105
106
107 def main():
108 argument_spec = pritunl_argument_spec()
109
110 argument_spec.update(dict(organization=dict(type="str", aliases=["org"])))
111
112 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
113
114 try:
115 get_pritunl_organizations(module)
116 except PritunlException as e:
117 module.fail_json(msg=f"{e}")
118
119
120 if __name__ == "__main__":
121 main()