Debian Codegraph @

ansible_collections/ibm/storage_virtualize/plugins/modules/ibm_svc_manage_sra.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (C) 2021 IBM CORPORATION
5 # Author(s): Sanjaikumaar M <[email protected]>
6 #
7 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12 DOCUMENTATION = '''
13 ---
14 module: ibm_svc_manage_sra
15 short_description: This module manages remote support assistance configuration on IBM Storage Virtualize family systems
16 version_added: "1.7.0"
17 description:
18 - Ansible interface to manage 'chsra' support remote assistance command.
19 options:
20 clustername:
21 description:
22 - The hostname or management IP of the Storage Virtualize system.
23 required: true
24 type: str
25 domain:
26 description:
27 - Domain for the Storage Virtualize system.
28 - Valid when hostname is used for the parameter I(clustername).
29 type: str
30 username:
31 description:
32 - REST API username for the Storage Virtualize system.
33 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
34 type: str
35 password:
36 description:
37 - REST API password for the Storage Virtualize system.
38 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
39 type: str
40 token:
41 description:
42 - The authentication token to verify a user on the Storage Virtualize system.
43 - To generate a token, use the M(ibm.storage_virtualize.ibm_svc_auth) module.
44 type: str
45 log_path:
46 description:
47 - Path of debug log file.
48 type: str
49 state:
50 description:
51 - Enables (C(enabled)) or disables (C(disabled)) the remote support assistance.
52 choices: [ enabled, disabled ]
53 required: true
54 type: str
55 support:
56 description:
57 - Specifies the support assistance through C(remote) or C(onsite).
58 choices: [ remote, onsite ]
59 type: str
60 required: true
61 name:
62 description:
63 - Specifies the list of unique names for the support center or proxy to be defined.
64 - Required when I(support=remote), to enable remote support assistance.
65 type: list
66 elements: str
67 sra_ip:
68 description:
69 - Specifies the list of IP addresses or fully qualified domain names for the new support center or proxy server.
70 - Required when I(support=remote) and I(state=enabled), to enable support remote assistannce.
71 type: list
72 elements: str
73 sra_port:
74 description:
75 - Specifies the list of port numbers for the new support center or proxy server.
76 - Required when I(support=remote) and I(state=enabled), to enable support remote assistannce.
77 type: list
78 elements: str
79 validate_certs:
80 description:
81 - Validates certification.
82 default: false
83 type: bool
84 author:
85 - Sanjaikumaar M (@sanjaikumaar)
86 notes:
87 - This module supports C(check_mode).
88 '''
89
90 EXAMPLES = '''
91 - name: Enable support remote assistance
92 ibm.storage_virtualize.ibm_svc_manage_sra:
93 clustername: "{{ clustername }}"
94 domain: "{{ domain }}"
95 username: "{{ username }}"
96 password: "{{ password }}"
97 log_path: "{{ log_path }}"
98 support: remote
99 state: enabled
100 name:
101 - proxy_1
102 - proxy_2
103 - proxy_3
104 sra_ip:
105 - '0.0.0.0'
106 - '1.1.1.1'
107 - '2.1.2.2'
108 sra_port:
109 - 8888
110 - 9999
111 - 8800
112 - name: Disable support remote assistance
113 ibm.storage_virtualize.ibm_svc_manage_sra:
114 clustername: "{{ clustername }}"
115 domain: "{{ domain }}"
116 username: "{{ username }}"
117 password: "{{ password }}"
118 log_path: "{{ log_path }}"
119 support: remote
120 state: disabled
121 name:
122 - proxy_1
123 - proxy_2
124 - proxy_3
125 '''
126
127 RETURN = '''#'''
128
129 from traceback import format_exc
130 from ansible.module_utils.basic import AnsibleModule
131 from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import (
132 IBMSVCRestApi, svc_argument_spec,
133 get_logger
134 )
135 from ansible.module_utils._text import to_native
136
137
138 class IBMSVCSupportRemoteAssistance:
139
140 def __init__(self):
141 argument_spec = svc_argument_spec()
142 argument_spec.update(
143 dict(
144 state=dict(
145 type='str',
146 required=True,
147 choices=['enabled', 'disabled']
148 ),
149 support=dict(
150 type='str',
151 required=True,
152 choices=['remote', 'onsite']
153 ),
154 name=dict(type='list', elements='str'),
155 sra_ip=dict(type='list', elements='str'),
156 sra_port=dict(type='list', elements='str')
157 )
158 )
159
160 self.module = AnsibleModule(argument_spec=argument_spec,
161 supports_check_mode=True)
162
163 # Required parameters
164 self.support = self.module.params['support']
165 self.state = self.module.params['state']
166
167 # Optional parameters
168 self.name = self.module.params.get('name', [])
169 self.sra_ip = self.module.params.get('sra_ip', [])
170 self.sra_port = self.module.params.get('sra_port', [])
171
172 self.basic_checks()
173
174 # Varialbe to store some frequently used data
175 self.sra_status_detail = None
176
177 # logging setup
178 self.log_path = self.module.params['log_path']
179 log = get_logger(self.__class__.__name__, self.log_path)
180 self.log = log.info
181 self.changed = False
182 self.msg = ''
183
184 self.restapi = IBMSVCRestApi(
185 module=self.module,
186 clustername=self.module.params['clustername'],
187 domain=self.module.params['domain'],
188 username=self.module.params['username'],
189 password=self.module.params['password'],
190 validate_certs=self.module.params['validate_certs'],
191 log_path=self.log_path,
192 token=self.module.params['token']
193 )
194
195 def basic_checks(self):
196 self.filtered_params = dict(
197 filter(
198 lambda item: item[0] in ['name', 'sra_ip', 'sra_port'],
199 self.module.params.items()
200 )
201 )
202 if self.support == 'remote' and self.state == 'enabled':
203 if self.name and self.sra_ip and self.sra_port:
204 if len(self.name) == len(self.sra_ip) == len(self.sra_port):
205 if not all([all(self.name), all(self.sra_ip), all(self.sra_port)]):
206 missing_params = ', '.join([k for k, v in self.filtered_params.items() if not all(v)])
207 self.module.fail_json(
208 msg='{0} should not contain blank values'.format(missing_params)
209 )
210 else:
211 self.module.fail_json(
212 msg='Name, sra_ip and sra_port parameters should contain same number of arguments'
213 )
214 else:
215 missing_params = ', '.join([k for k, v in self.filtered_params.items() if not v])
216 self.module.fail_json(
217 msg='support is remote and state is enabled but following parameter missing: {0}'.format(missing_params)
218 )
219 elif self.support == 'remote' and self.state == 'disabled':
220 if self.sra_ip or self.sra_port:
221 invalid_params = ', '.join([k for k, v in self.filtered_params.items() if k in ['sra_ip', 'sra_port'] and v])
222 self.module.fail_json(
223 msg='{0} should not be passed when support=remote and state=disabled'.format(invalid_params)
224 )
225 elif self.support == 'onsite':
226 if self.name or self.sra_ip or self.sra_port:
227 invalid_params = ', '.join([k for k, v in self.filtered_params.items()])
228 self.module.fail_json(
229 msg='{0} should not be passed when support=onsite'.format(invalid_params)
230 )
231
232 def is_sra_enabled(self):
233 if self.sra_status_detail:
234 return self.sra_status_detail['status'] == 'enabled'
235
236 result = self.restapi.svc_obj_info(
237 cmd='lssra',
238 cmdopts=None,
239 cmdargs=None
240 )
241 self.sra_status_detail = result
242 return result['status'] == 'enabled'
243
244 def is_remote_support_enabled(self):
245 if self.sra_status_detail:
246 return self.sra_status_detail['remote_support_enabled'] == 'yes'
247
248 result = self.restapi.svc_obj_info(
249 cmd='lssra',
250 cmdopts=None,
251 cmdargs=None
252 )
253 return result['remote_support_enabled'] == 'yes'
254
255 def is_proxy_exist(self, obj_name):
256 obj = {}
257 result = self.restapi.svc_obj_info(
258 cmd='lssystemsupportcenter',
259 cmdopts=None,
260 cmdargs=[obj_name]
261 )
262
263 if isinstance(result, list):
264 for d in result:
265 obj.update(d)
266 else:
267 obj = result
268
269 return obj
270
271 def sra_probe(self):
272 if self.module.check_mode:
273 self.changed = True
274 return
275
276 message = ''
277 if (self.support == 'remote' and not self.is_remote_support_enabled()) \
278 or (self.support == 'onsite' and self.is_remote_support_enabled()):
279
280 message += 'SRA configuration cannot be updated right now. '
281
282 if any(self.add_proxy_details()):
283 message += 'Proxy server details cannot be updated when SRA is enabled. '
284
285 message += 'Please disable SRA and try to update.' if message else ''
286
287 self.msg = message if message else self.msg
288
289 return self.msg
290
291 def add_proxy_details(self):
292 existed = []
293 if self.support == 'remote':
294 cmd = 'mksystemsupportcenter'
295 cmdargs = []
296
297 for nm, ip, port in zip(self.name, self.sra_ip, self.sra_port):
298 if nm != 'None' and ip != 'None' and port != 'None':
299 if not self.is_proxy_exist(nm):
300 existed.append(True)
301 if not self.is_sra_enabled():
302 cmdopts = {
303 'name': nm,
304 'ip': ip,
305 'port': port,
306 'proxy': 'yes'
307 }
308 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
309 self.log('Proxy server(%s) details added', nm)
310 else:
311 self.log('Skipping, Proxy server(%s) already exist', nm)
312 else:
313 missing_params = ', '.join([k for k, v in self.filtered_params.items() if 'None' in v])
314 self.module.fail_json(
315 msg='support is remote and state is enabled but following parameter missing: {0}'.format(missing_params)
316 )
317
318 return existed
319
320 def remove_proxy_details(self):
321 if self.support == 'remote':
322 cmd = 'rmsystemsupportcenter'
323 cmdopts = {}
324
325 for nm in self.name:
326 if nm and nm != 'None':
327 if self.is_proxy_exist(nm):
328 cmdargs = [nm]
329 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
330 self.log('Proxy server(%s) details removed', nm)
331 else:
332 self.log('Proxy server(%s) does not exist', nm)
333 else:
334 self.module.fail_json(
335 msg='support is remote and state is disabled but following parameter is blank: name'
336 )
337
338 def enable_sra(self):
339 if self.module.check_mode:
340 self.changed = True
341 return
342
343 self.add_proxy_details()
344
345 cmd = 'chsra'
346 cmdopts = {}
347 cmdargs = ['-enable']
348
349 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
350
351 if self.support == 'remote':
352 cmdargs = ['-remotesupport', 'enable']
353 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
354
355 self.log('%s support assistance enabled', self.support.capitalize())
356
357 self.changed = True
358
359 def disable_sra(self):
360 if self.module.check_mode:
361 self.changed = True
362 return
363
364 cmd = 'chsra'
365 cmdopts = {}
366
367 if self.support == 'remote':
368 cmdargs = ['-remotesupport', 'disable']
369 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
370
371 cmdargs = ['-disable']
372 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
373 self.log('%s support assistance disabled', self.support.capitalize())
374
375 self.remove_proxy_details()
376 self.changed = True
377
378 def apply(self):
379 if self.is_sra_enabled():
380 if self.state == 'enabled':
381 if not self.sra_probe():
382 self.msg = 'Support remote assistance already enabled. '\
383 'No modifications done.'
384 else:
385 self.disable_sra()
386 self.msg = 'Support remote assistance disabled.'
387 else:
388 if self.state == 'disabled':
389 self.msg = 'Support remote assistance is already disabled.'
390 else:
391 self.enable_sra()
392 self.msg = 'Support remote assistance({0}) enabled.'.format(
393 self.support
394 )
395
396 if self.module.check_mode:
397 self.msg = 'skipping changes due to check mode.'
398
399 self.module.exit_json(msg=self.msg, changed=self.changed)
400
401
402 def main():
403 v = IBMSVCSupportRemoteAssistance()
404 try:
405 v.apply()
406 except Exception as e:
407 v.log('Exception in apply(): \n%s', format_exc())
408 v.module.fail_json(msg='Module failed. Error [%s].' % to_native(e))
409
410
411 if __name__ == '__main__':
412 main()