Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2020, 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
12 ANSIBLE_METADATA = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18
19 DOCUMENTATION = """
20 ---
21 module: purefa_pod_replica
22 short_description: Manage ActiveDR pod replica links between Pure Storage FlashArrays
23 version_added: '1.0.0'
24 description:
25 - This module manages ActiveDR pod replica links between Pure Storage FlashArrays.
26 author: Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 name:
29 description:
30 - ActiveDR source pod name
31 required: true
32 type: str
33 state:
34 description:
35 - Creates or modifies a pod replica link
36 required: false
37 default: present
38 type: str
39 choices: [ "present", "absent" ]
40 target_array:
41 description:
42 - Remote array name to create replica on.
43 required: false
44 type: str
45 target_pod:
46 description:
47 - Name of target pod
48 - Must not be the same as the local pod.
49 type: str
50 required: false
51 pause:
52 description:
53 - Pause/unpause a pod replica link
54 required: false
55 type: bool
56 context:
57 description:
58 - Name of fleet member on which to perform the operation.
59 - This requires the array receiving the request is a member of a fleet
60 and the context name to be a member of the same fleet.
61 type: str
62 default: ""
63 version_added: '1.33.0'
64 extends_documentation_fragment:
65 - purestorage.flasharray.purestorage.fa
66 """
67
68 EXAMPLES = """
69 - name: Create new pod replica link from foo to bar on arrayB
70 purestorage.flasharray.purefa_pod_replica:
71 name: foo
72 target_array: arrayB
73 target_pod: bar
74 state: present
75 fa_url: 10.10.10.2
76 api_token: e31060a7-21fc-e277-6240-25983c6c4592
77
78 - name: Pause an pod replica link
79 purestorage.flasharray.purefa_pod_replica:
80 name: foo
81 pause: true
82 fa_url: 10.10.10.2
83 api_token: e31060a7-21fc-e277-6240-25983c6c4592
84
85 - name: Delete pod replica link
86 purestorage.flasharray.purefa_pod_replica:
87 name: foo
88 state: absent
89 fa_url: 10.10.10.2
90 api_token: e31060a7-21fc-e277-6240-25983c6c4592
91 """
92
93 RETURN = """
94 """
95
96 HAS_PURESTORAGE = True
97 try:
98 from pypureclient.flasharray import PodReplicaLinkPatch
99 except ImportError:
100 HAS_PURESTORAGE = False
101
102 from ansible.module_utils.basic import AnsibleModule
103 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
104 get_array,
105 purefa_argument_spec,
106 )
107 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
108 LooseVersion,
109 )
110
111 CONTEXT_VERSION = "2.38"
112
113
114 def get_local_pod(module, array):
115 """Return Pod or None"""
116 api_version = array.get_rest_version()
117 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
118 res = array.get_pods(
119 names=[module.params["name"]], context_names=[module.params["context"]]
120 )
121 else:
122 res = array.get_pods(names=[module.params["name"]])
123 if res.status_code != 200:
124 return None
125 return list(res.items)[0]
126
127
128 def get_local_rl(module, array):
129 """Return Pod Replica Link or None"""
130 api_version = array.get_rest_version()
131 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
132 res = array.get_pod_replica_links(
133 local_pod_names=[module.params["name"]],
134 context_names=[module.params["context"]],
135 total_item_count=True,
136 )
137 else:
138 res = array.get_pod_replica_links(
139 local_pod_names=[module.params["name"]], total_item_count=True
140 )
141 if res.total_item_count == 0:
142 return None
143 return list(res.items)[0]
144
145
146 def update_rl(module, array, local_rl):
147 """Create Pod Replica Link"""
148 api_version = array.get_rest_version()
149 changed = False
150 if module.params["pause"] is not None:
151 if local_rl.status != "paused" and module.params["pause"]:
152 changed = True
153 if not module.check_mode:
154 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
155 res = array.patch_pod_replica_links(
156 local_pod_names=module.params["name"],
157 context_names=[module.params["context"]],
158 remote_pod_names=local_rl["remote_pod"]["name"],
159 pod_replica_link=PodReplicaLinkPatch(paused=True),
160 )
161 else:
162 res = array.patch_pod_replica_links(
163 local_pod_names=module.params["name"],
164 remote_pod_names=local_rl["remote_pod"]["name"],
165 pod_replica_link=PodReplicaLinkPatch(paused=True),
166 )
167 if res.status_code != 200:
168 module.fail_json(
169 msg="Failed to pause replica link {0}. Error: {1}".format(
170 module.params["name"], res.errors[0].message
171 )
172 )
173 elif local_rl.status == "paused" and not module.params["pause"]:
174 changed = True
175 if not module.check_mode:
176 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
177 res = array.patch_pod_replica_links(
178 local_pod_names=module.params["name"],
179 context_names=[module.params["context"]],
180 remote_pod_names=local_rl["remote_pod"]["name"],
181 pod_replica_link=PodReplicaLinkPatch(paused=False),
182 )
183 else:
184 res = array.patch_pod_replica_links(
185 local_pod_names=module.params["name"],
186 remote_pod_names=local_rl["remote_pod"]["name"],
187 pod_replica_link=PodReplicaLinkPatch(paused=False),
188 )
189 if res.status_code != 200:
190 module.fail_json(
191 msg="Failed to resume replica link {0}. Error: {1}".format(
192 module.params["name"], res.errors[0].message
193 )
194 )
195 module.exit_json(changed=changed)
196
197
198 def create_rl(module, array):
199 """Create Pod Replica Link"""
200 api_version = array.get_rest_version()
201 changed = True
202 if not module.params["target_pod"]:
203 module.fail_json(msg="target_pod required to create a new replica link.")
204 if not module.params["target_array"]:
205 module.fail_json(msg="target_array required to create a new replica link.")
206 if array.get_array_connections(total_item_count=True).total_item_count == 0:
207 module.fail_json(msg="No connected arrays.")
208 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
209 res = array.get_array_connections(
210 names=[module.params["target_array"]],
211 context_names=[module.params["context"]],
212 )
213 else:
214 res = array.get_array_connections(names=[module.params["target_array"]])
215 if res.status_code != 200:
216 module.fail_json(
217 msg="Target array {0} is not connected to the source array.".format(
218 module.params["target_array"]
219 )
220 )
221 connection = list(res.items)[0]
222 if connection.status in [
223 "connected",
224 "connecting",
225 "partially_connected",
226 ]:
227 if not module.check_mode:
228 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
229 res = array.post_pod_replica_links(
230 context_names=[module.params["context"]],
231 local_pod_names=[module.params["name"]],
232 remote_names=[module.params["target_array"]],
233 remote_pod_names=[module.params["target_pod"]],
234 )
235 else:
236 res = array.post_pod_replica_links(
237 local_pod_names=[module.params["name"]],
238 remote_names=[module.params["target_array"]],
239 remote_pod_names=[module.params["target_pod"]],
240 )
241 if res.status_code != 200:
242 module.fail_json(
243 msg="Failed to create replica link {0} to target array {1}. Error: {2}".format(
244 module.params["name"],
245 module.params["target_array"],
246 res.errors[0].message,
247 )
248 )
249 else:
250 module.fail_json(
251 msg="Failed to create replica link for pod {0}. Bad status: {1}".format(
252 module.params["name"], connection.status
253 )
254 )
255 module.exit_json(changed=changed)
256
257
258 def delete_rl(module, array, local_rl):
259 """Delete Pod Replica Link"""
260 api_version = array.get_rest_version()
261 changed = True
262 if not module.check_mode:
263 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
264 res = array.delete_pod_replica_links(
265 local_pod_names=[module.params["name"]],
266 remote_pod_names=[local_rl["remote_pod"]["name"]],
267 context_names=[module.params["context"]],
268 )
269 else:
270 res = array.delete_pod_replica_links(
271 local_pod_names=[module.params["name"]],
272 remote_pod_names=[local_rl["remote_pod"]["name"]],
273 )
274 if res.status_code != 200:
275 module.fail_json(
276 msg="Failed to delete replica link for pod {0}. Error: {1}".format(
277 module.params["name"], res.errors[0].message
278 )
279 )
280 module.exit_json(changed=changed)
281
282
283 def main():
284 argument_spec = purefa_argument_spec()
285 argument_spec.update(
286 dict(
287 name=dict(type="str", required=True),
288 target_pod=dict(type="str"),
289 target_array=dict(type="str"),
290 pause=dict(type="bool"),
291 state=dict(default="present", choices=["present", "absent"]),
292 context=dict(type="str", default=""),
293 )
294 )
295
296 module = AnsibleModule(argument_spec, supports_check_mode=True)
297
298 state = module.params["state"]
299 array = get_array(module)
300
301 local_pod = get_local_pod(module, array)
302
303 if not local_pod:
304 module.fail_json(
305 msg="Selected local pod {0} does not exist.".format(module.params["name"])
306 )
307
308 if local_pod.array_count > 1:
309 module.fail_json(
310 msg="Local Pod {0} is already stretched to a remote array.".format(
311 module.params["name"]
312 )
313 )
314
315 local_replica_link = get_local_rl(module, array)
316 if local_replica_link:
317 if local_replica_link.status == "unhealthy":
318 module.fail_json(msg="Replca Link unhealthy - please check remote array")
319 if state == "present" and not local_replica_link:
320 create_rl(module, array)
321 elif state == "present" and local_replica_link:
322 update_rl(module, array, local_replica_link)
323 elif state == "absent" and local_replica_link:
324 delete_rl(module, array, local_replica_link)
325
326 module.exit_json(changed=False)
327
328
329 if __name__ == "__main__":
330 main()