Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/modules/netapp_e_storage_system.py

raw
1 #!/usr/bin/python
2
3 # (c) 2016, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9
10 ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['deprecated'],
12 'supported_by': 'community'}
13
14
15 DOCUMENTATION = '''
16 module: netapp_e_storage_system
17 version_added: "2.2.0"
18 short_description: NetApp E-Series Web Services Proxy manage storage arrays
19 description:
20 - Manage the arrays accessible via a NetApp Web Services Proxy for NetApp E-series storage arrays.
21 options:
22 api_username:
23 description:
24 - The username to authenticate with the SANtricity WebServices Proxy or embedded REST API.
25 type: str
26 required: true
27 api_password:
28 description:
29 - The password to authenticate with the SANtricity WebServices Proxy or embedded REST API.
30 type: str
31 required: true
32 api_url:
33 description:
34 - The url to the SANtricity WebServices Proxy or embedded REST API.
35 required: true
36 type: str
37 validate_certs:
38 description:
39 - Should https certificates be validated?
40 type: bool
41 default: 'yes'
42 ssid:
43 description:
44 - The ID of the array to manage. This value must be unique for each array.
45 type: str
46 required: true
47 state:
48 description:
49 - Whether the specified array should be configured on the Web Services Proxy or not.
50 required: true
51 type: str
52 choices: ['present', 'absent']
53 controller_addresses:
54 description:
55 - The list addresses for the out-of-band management adapter or the agent host. Mutually exclusive of array_wwn parameter.
56 type: list
57 elements: str
58 required: true
59 array_wwn:
60 description:
61 - The WWN of the array to manage. Only necessary if in-band managing multiple arrays on the same agent host. Mutually exclusive of
62 controller_addresses parameter.
63 type: str
64 array_password:
65 description:
66 - The management password of the array to manage, if set.
67 type: str
68 enable_trace:
69 description:
70 - Enable trace logging for SYMbol calls to the storage system.
71 type: bool
72 default: 'no'
73 meta_tags:
74 description:
75 - Optional meta tags to associate to this storage system
76 type: list
77 elements: str
78 array_status_timeout_sec:
79 description:
80 - Array status timeout measured in seconds
81 default: 60
82 type: int
83 author: Kevin Hulquest (@hulquest)
84 '''
85
86 EXAMPLES = '''
87 ---
88 - name: Presence of storage system
89 netapp_e_storage_system:
90 ssid: "{{ item.key }}"
91 state: present
92 api_url: "{{ netapp_api_url }}"
93 api_username: "{{ netapp_api_username }}"
94 api_password: "{{ netapp_api_password }}"
95 validate_certs: "{{ netapp_api_validate_certs }}"
96 controller_addresses:
97 - "{{ item.value.address1 }}"
98 - "{{ item.value.address2 }}"
99 with_dict: "{{ storage_systems }}"
100 when: check_storage_system
101 '''
102
103 RETURN = '''
104 msg:
105 description: State of request
106 type: str
107 returned: always
108 sample: 'Storage system removed.'
109 '''
110 import json
111 from datetime import datetime as dt, timedelta
112 from time import sleep
113
114 from ansible.module_utils.api import basic_auth_argument_spec
115 from ansible.module_utils.basic import AnsibleModule
116 from ansible.module_utils._text import to_native
117 from ansible.module_utils.urls import open_url
118 from ansible.module_utils.six.moves.urllib.error import HTTPError
119
120
121 def request(url, data=None, headers=None, method='GET', use_proxy=True,
122 force=False, last_mod_time=None, timeout=10, validate_certs=True,
123 url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False):
124 try:
125 r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy,
126 force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs,
127 url_username=url_username, url_password=url_password, http_agent=http_agent,
128 force_basic_auth=force_basic_auth)
129 except HTTPError as err:
130 r = err.fp
131
132 try:
133 raw_data = r.read()
134 if raw_data:
135 data = json.loads(raw_data)
136 else:
137 raw_data = None
138 except Exception:
139 if ignore_errors:
140 pass
141 else:
142 raise Exception(raw_data)
143
144 resp_code = r.getcode()
145
146 if resp_code >= 400 and not ignore_errors:
147 raise Exception(resp_code, data)
148 else:
149 return resp_code, data
150
151
152 def do_post(ssid, api_url, post_headers, api_usr, api_pwd, validate_certs, request_body, timeout):
153 (rc, resp) = request(api_url + "/storage-systems", data=request_body, headers=post_headers,
154 method='POST', url_username=api_usr, url_password=api_pwd,
155 validate_certs=validate_certs)
156 status = None
157 return_resp = resp
158 if 'status' in resp:
159 status = resp['status']
160
161 if rc == 201:
162 status = 'neverContacted'
163 fail_after_time = dt.utcnow() + timedelta(seconds=timeout)
164
165 while status == 'neverContacted':
166 if dt.utcnow() > fail_after_time:
167 raise Exception("web proxy timed out waiting for array status")
168
169 sleep(1)
170 (rc, system_resp) = request(api_url + "/storage-systems/%s" % ssid,
171 headers=dict(Accept="application/json"), url_username=api_usr,
172 url_password=api_pwd, validate_certs=validate_certs,
173 ignore_errors=True)
174 status = system_resp['status']
175 return_resp = system_resp
176
177 return status, return_resp
178
179
180 def main():
181 argument_spec = basic_auth_argument_spec()
182 argument_spec.update(dict(
183 api_username=dict(type='str', required=True),
184 api_password=dict(type='str', required=True, no_log=True),
185 api_url=dict(type='str', required=True),
186 state=dict(required=True, choices=['present', 'absent']),
187 ssid=dict(required=True, type='str'),
188 controller_addresses=dict(type='list', elements='str', required=True),
189 array_wwn=dict(required=False, type='str'),
190 array_password=dict(required=False, type='str', no_log=True),
191 array_status_timeout_sec=dict(default=60, type='int'),
192 enable_trace=dict(default=False, type='bool'),
193 meta_tags=dict(type='list', elements="str")
194 ))
195 module = AnsibleModule(
196 argument_spec=argument_spec,
197 supports_check_mode=True,
198 mutually_exclusive=[['controller_addresses', 'array_wwn']],
199 required_if=[('state', 'present', ['controller_addresses'])]
200 )
201
202 p = module.params
203
204 state = p['state']
205 ssid = p['ssid']
206 controller_addresses = p['controller_addresses']
207 array_wwn = p['array_wwn']
208 array_password = p['array_password']
209 array_status_timeout_sec = p['array_status_timeout_sec']
210 validate_certs = p['validate_certs']
211 meta_tags = p['meta_tags']
212 enable_trace = p['enable_trace']
213
214 api_usr = p['api_username']
215 api_pwd = p['api_password']
216 api_url = p['api_url']
217
218 changed = False
219 array_exists = False
220
221 try:
222 (rc, resp) = request(api_url + "/storage-systems/%s" % ssid, headers=dict(Accept="application/json"),
223 url_username=api_usr, url_password=api_pwd, validate_certs=validate_certs,
224 ignore_errors=True)
225 except Exception as err:
226 module.fail_json(msg="Error accessing storage-system with id [%s]. Error [%s]" % (ssid, to_native(err)))
227
228 array_exists = True
229 array_detail = resp
230
231 if rc == 200:
232 if state == 'absent':
233 changed = True
234 array_exists = False
235 elif state == 'present':
236 current_addresses = frozenset(i for i in (array_detail['ip1'], array_detail['ip2']) if i)
237 if set(controller_addresses) != current_addresses:
238 changed = True
239 if array_detail['wwn'] != array_wwn and array_wwn is not None:
240 module.fail_json(
241 msg='It seems you may have specified a bad WWN. The storage system ID you specified, %s, currently has the WWN of %s' %
242 (ssid, array_detail['wwn'])
243 )
244 elif rc == 404:
245 if state == 'present':
246 changed = True
247 array_exists = False
248 else:
249 changed = False
250 module.exit_json(changed=changed, msg="Storage system was not present.")
251
252 if changed and not module.check_mode:
253 if state == 'present':
254 if not array_exists:
255 # add the array
256 array_add_req = dict(
257 id=ssid,
258 controllerAddresses=controller_addresses,
259 metaTags=meta_tags,
260 enableTrace=enable_trace
261 )
262
263 if array_wwn:
264 array_add_req['wwn'] = array_wwn
265
266 if array_password:
267 array_add_req['password'] = array_password
268
269 post_headers = dict(Accept="application/json")
270 post_headers['Content-Type'] = 'application/json'
271 request_data = json.dumps(array_add_req)
272
273 try:
274 (rc, resp) = do_post(ssid, api_url, post_headers, api_usr, api_pwd, validate_certs, request_data,
275 array_status_timeout_sec)
276 except Exception as err:
277 module.fail_json(msg="Failed to add storage system. Id[%s]. Request body [%s]. Error[%s]." %
278 (ssid, request_data, to_native(err)))
279
280 else: # array exists, modify...
281 post_headers = dict(Accept="application/json")
282 post_headers['Content-Type'] = 'application/json'
283 post_body = dict(
284 controllerAddresses=controller_addresses,
285 removeAllTags=True,
286 enableTrace=enable_trace,
287 metaTags=meta_tags
288 )
289
290 try:
291 (rc, resp) = do_post(ssid, api_url, post_headers, api_usr, api_pwd, validate_certs, post_body,
292 array_status_timeout_sec)
293 except Exception as err:
294 module.fail_json(msg="Failed to update storage system. Id[%s]. Request body [%s]. Error[%s]." %
295 (ssid, post_body, to_native(err)))
296
297 elif state == 'absent':
298 # delete the array
299 try:
300 (rc, resp) = request(api_url + "/storage-systems/%s" % ssid, method='DELETE',
301 url_username=api_usr,
302 url_password=api_pwd, validate_certs=validate_certs)
303 except Exception as err:
304 module.fail_json(msg="Failed to remove storage array. Id[%s]. Error[%s]." % (ssid, to_native(err)))
305
306 if rc == 422:
307 module.exit_json(changed=changed, msg="Storage system was not presented.")
308 if rc == 204:
309 module.exit_json(changed=changed, msg="Storage system removed.")
310
311 module.exit_json(changed=changed, **resp)
312
313
314 if __name__ == '__main__':
315 main()