Debian Codegraph @

ansible_collections/community/general/plugins/modules/pritunl_org.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
10 author: Florian Dambrine (@Lowess)
11 version_added: 2.5.0
12 short_description: Manages Pritunl Organizations using the Pritunl API
13 description:
14 - A module to manage Pritunl organizations using the Pritunl API.
15 extends_documentation_fragment:
16 - community.general._pritunl
17 - community.general._attributes
18 attributes:
19 check_mode:
20 support: none
21 diff_mode:
22 support: none
23 options:
24 name:
25 type: str
26 required: true
27 aliases:
28 - org
29 description:
30 - The name of the organization to manage in Pritunl.
31 force:
32 type: bool
33 default: false
34 description:
35 - If O(force) is V(true) and O(state) is V(absent), the module deletes the organization, no matter if it contains users
36 or not. By default O(force) is V(false), which causes the module to fail the deletion of the organization when it
37 contains users.
38 state:
39 type: str
40 default: 'present'
41 choices:
42 - present
43 - absent
44 description:
45 - If V(present), the module adds organization O(name) to Pritunl. If V(absent), attempt to delete the organization from
46 Pritunl (please read about O(force) usage).
47 """
48
49 EXAMPLES = r"""
50 - name: Ensure the organization named MyOrg exists
51 community.general.pritunl_org:
52 state: present
53 name: MyOrg
54
55 - name: Ensure the organization named MyOrg does not exist
56 community.general.pritunl_org:
57 state: absent
58 name: MyOrg
59 """
60
61 RETURN = r"""
62 response:
63 description: JSON representation of a Pritunl Organization.
64 returned: success
65 type: dict
66 sample:
67 {
68 "auth_api": false,
69 "name": "Foo",
70 "auth_token": null,
71 "user_count": 0,
72 "auth_secret": null,
73 "id": "csftwlu6uhralzi2dpmhekz3"
74 }
75 """
76
77
78 from ansible.module_utils.basic import AnsibleModule
79 from ansible.module_utils.common.dict_transformations import dict_merge
80
81 from ansible_collections.community.general.plugins.module_utils._pritunl_api import (
82 PritunlException,
83 delete_pritunl_organization,
84 get_pritunl_settings,
85 list_pritunl_organizations,
86 post_pritunl_organization,
87 pritunl_argument_spec,
88 )
89
90
91 def add_pritunl_organization(module):
92 result = {}
93
94 org_name = module.params.get("name")
95
96 org_obj_list = list_pritunl_organizations(
97 **dict_merge(
98 get_pritunl_settings(module),
99 {"filters": {"name": org_name}},
100 )
101 )
102
103 # If the organization already exists
104 if len(org_obj_list) > 0:
105 result["changed"] = False
106 result["response"] = org_obj_list[0]
107 else:
108 # Otherwise create it
109 response = post_pritunl_organization(
110 **dict_merge(
111 get_pritunl_settings(module),
112 {"organization_name": org_name},
113 )
114 )
115 result["changed"] = True
116 result["response"] = response
117
118 module.exit_json(**result)
119
120
121 def remove_pritunl_organization(module):
122 result = {}
123
124 org_name = module.params.get("name")
125 force = module.params.get("force")
126
127 org_obj_list = []
128
129 org_obj_list = list_pritunl_organizations(
130 **dict_merge(
131 get_pritunl_settings(module),
132 {
133 "filters": {"name": org_name},
134 },
135 )
136 )
137
138 # No organization found
139 if len(org_obj_list) == 0:
140 result["changed"] = False
141 result["response"] = {}
142
143 else:
144 # Otherwise attempt to delete it
145 org = org_obj_list[0]
146
147 # Only accept deletion under specific conditions
148 if force or org["user_count"] == 0:
149 response = delete_pritunl_organization(
150 **dict_merge(
151 get_pritunl_settings(module),
152 {"organization_id": org["id"]},
153 )
154 )
155 result["changed"] = True
156 result["response"] = response
157 else:
158 module.fail_json(
159 msg=(
160 f"Can not remove organization '{org_name}' with {org['user_count']} attached users. "
161 "Either set 'force' option to true or remove active users "
162 "from the organization"
163 )
164 )
165
166 module.exit_json(**result)
167
168
169 def main():
170 argument_spec = pritunl_argument_spec()
171
172 argument_spec.update(
173 dict(
174 name=dict(required=True, type="str", aliases=["org"]),
175 force=dict(type="bool", default=False),
176 state=dict(choices=["present", "absent"], default="present"),
177 )
178 )
179
180 module = AnsibleModule(argument_spec=argument_spec)
181
182 state = module.params.get("state")
183
184 try:
185 if state == "present":
186 add_pritunl_organization(module)
187 elif state == "absent":
188 remove_pritunl_organization(module)
189 except PritunlException as e:
190 module.fail_json(msg=f"{e}")
191
192
193 if __name__ == "__main__":
194 main()