Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_dsrole_old.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2024, Simon Dodsley ([email protected])
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 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_dsrole_old
20 version_added: '1.31.0'
21 short_description: Configure FlashArray Directory Service Roles (pre-6.6.3)
22 description:
23 - Set or erase directory services role configurations.
24 - Only available for FlashArray running Purity//FA 6.6.2 or lower
25 - Please use ``purefa_dsrole`` for FlashArrays running Purity//FA 6.6.3 or higher
26 author:
27 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
28 options:
29 state:
30 description:
31 - Create or delete directory service role
32 type: str
33 default: present
34 choices: [ absent, present ]
35 role:
36 description:
37 - The directory service role to work on
38 type: str
39 required: true
40 choices: [ array_admin, ops_admin, readonly, storage_admin ]
41 group_base:
42 type: str
43 description:
44 - Specifies where the configured group is located in the directory
45 tree. This field consists of Organizational Units (OUs) that combine
46 with the base DN attribute and the configured group CNs to complete
47 the full Distinguished Name of the groups. The group base should
48 specify OU= for each OU and multiple OUs should be separated by commas.
49 The order of OUs is important and should get larger in scope from left
50 to right.
51 - Each OU should not exceed 64 characters in length.
52 group:
53 type: str
54 description:
55 - Sets the common Name (CN) of the configured directory service group
56 containing users for the FlashBlade. This name should be just the
57 Common Name of the group without the CN= specifier.
58 - Common Names should not exceed 64 characters in length.
59 extends_documentation_fragment:
60 - purestorage.flasharray.purestorage.fa
61 """
62
63 EXAMPLES = r"""
64 - name: Delete existing array_admin directory service role
65 purestorage.flasharray.purefa_dsrole_old:
66 role: array_admin
67 state: absent
68 fa_url: 10.10.10.2
69 api_token: e31060a7-21fc-e277-6240-25983c6c4592
70
71 - name: Create array_admin directory service role
72 purestorage.flasharray.purefa_dsrole_old:
73 role: array_admin
74 group_base: "OU=PureGroups,OU=SANManagers"
75 group: pureadmins
76 fa_url: 10.10.10.2
77 api_token: e31060a7-21fc-e277-6240-25983c6c4592
78
79 - name: Update ops_admin directory service role
80 purestorage.flasharray.purefa_dsrole_old:
81 role: ops_admin
82 group_base: "OU=PureGroups"
83 group: opsgroup
84 fa_url: 10.10.10.2
85 api_token: e31060a7-21fc-e277-6240-25983c6c4592
86 """
87
88 RETURN = r"""
89 """
90
91
92 HAS_PURESTORAGE = True
93 try:
94 from pypureclient.flasharray import DirectoryServiceRole
95 except ImportError:
96 HAS_PURESTORAGE = False
97
98 from ansible.module_utils.basic import AnsibleModule
99 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
100 get_array,
101 purefa_argument_spec,
102 )
103 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
104 LooseVersion,
105 )
106
107 MAX_API_VERSION = "2.30"
108
109
110 def update_role(module, array):
111 """Update Directory Service Role"""
112 changed = False
113 role = list(
114 array.get_directory_services_roles(role_names=[module.params["role"]]).items
115 )[0]
116 if (
117 role.group_base != module.params["group_base"]
118 or role.group != module.params["group"]
119 ):
120 changed = True
121 if not module.check_mode:
122 res = array.patch_directory_services_roles(
123 role_names=[module.params["role"]],
124 directory_services_roles=DirectoryServiceRole(
125 group_base=module.params["group_base"],
126 group=module.params["group"],
127 ),
128 )
129 if res.status_code != 200:
130 module.fail_json(
131 msg="Update Directory Service Role {0} failed. Error: {1}".format(
132 module.params["role"], res.errors[0].message
133 )
134 )
135 module.exit_json(changed=changed)
136
137
138 def delete_role(module, array):
139 """Delete Directory Service Role"""
140 changed = True
141 if not module.check_mode:
142 res = array.patch_directory_services_roles(
143 role_names=[module.params["role"]],
144 directory_service_roles=DirectoryServiceRole(group_base="", group=""),
145 )
146 if res.status_code != 200:
147 module.fail_json(
148 msg="Delete Directory Service Role {0} failed. Error: {1}".format(
149 module.params["role"], res.errors[0].message
150 )
151 )
152 module.exit_json(changed=changed)
153
154
155 def create_role(module, array):
156 """Create Directory Service Role"""
157 changed = False
158 if not module.params["group"] == "" or not module.params["group_base"] == "":
159 changed = True
160 if not module.check_mode:
161 res = array.patch_directory_services_roles(
162 role_names=[module.params["role"]],
163 directory_service_roles=DirectoryServiceRole(
164 group_base=module.params["group_base"],
165 group=module.params["group"],
166 ),
167 )
168 if res.status_code != 200:
169 module.fail_json(
170 msg="Create Directory Service Role {0} failed. Error: {1}".format(
171 module.params["role"], res.errors[0].message
172 )
173 )
174 module.exit_json(changed=changed)
175
176
177 def main():
178 argument_spec = purefa_argument_spec()
179 argument_spec.update(
180 dict(
181 role=dict(
182 required=True,
183 type="str",
184 choices=["array_admin", "ops_admin", "readonly", "storage_admin"],
185 ),
186 state=dict(type="str", default="present", choices=["absent", "present"]),
187 group_base=dict(type="str"),
188 group=dict(type="str"),
189 )
190 )
191
192 required_together = [["group", "group_base"]]
193
194 module = AnsibleModule(
195 argument_spec, required_together=required_together, supports_check_mode=True
196 )
197
198 state = module.params["state"]
199 array = get_array(module)
200 api_version = array.get_rest_version()
201 if LooseVersion(api_version) > LooseVersion(MAX_API_VERSION):
202 module.fail_json(
203 msg="This module is deprecated for your version of Purity//FA. "
204 "Please use module ''purefa_dsrole`` instead."
205 )
206 role_configured = False
207 role = array.get_directory_services_roles(role_names=[module.params["role"]])
208 if hasattr(list(role.items)[0], "group"):
209 role_configured = True
210
211 if state == "absent" and role_configured:
212 delete_role(module, array)
213 elif role_configured and state == "present":
214 update_role(module, array)
215 elif not role_configured and state == "present":
216 create_role(module, array)
217 else:
218 module.exit_json(changed=False)
219
220
221 if __name__ == "__main__":
222 main()