Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (C) 2021 IBM CORPORATION
5 # Author(s): Sreshtant Bohidar <[email protected]>
6 # Sandip Gulab Rajbanshi <[email protected]>
7 #
8 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
9
10 from __future__ import absolute_import, division, print_function
11 __metaclass__ = type
12
13 DOCUMENTATION = '''
14 ---
15 module: ibm_svc_manage_callhome
16 short_description: This module manages Call Home feature configuration on IBM Storage Virtualize
17 family systems
18 description:
19 - Ansible interface to manage cloud and email Call Home feature.
20 version_added: "1.7.0"
21 options:
22 state:
23 description:
24 - Enables or updates (C(enabled)) or disables (C(disabled)) Call Home feature.
25 choices: [ enabled, disabled ]
26 required: true
27 type: str
28 clustername:
29 description:
30 - The hostname or management IP of the Storage Virtualize system.
31 type: str
32 required: true
33 domain:
34 description:
35 - Domain for the Storage Virtualize system.
36 - Valid when hostname is used for the parameter I(clustername).
37 type: str
38 username:
39 description:
40 - REST API username for the Storage Virtualize system.
41 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
42 type: str
43 password:
44 description:
45 - REST API password for the Storage Virtualize system.
46 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
47 type: str
48 token:
49 description:
50 - The authentication token to verify a user on the Storage Virtualize system.
51 - To generate a token, use the ibm_svc_auth module.
52 type: str
53 callhome_type:
54 description:
55 - Specifies the transmission type.
56 choices: [ 'cloud services', 'email', 'both' ]
57 required: True
58 type: str
59 proxy_type:
60 description:
61 - Specifies the proxy type.
62 - Required when I(state=enabled), to create or modify Call Home feature.
63 - Proxy gets deleted for I(proxy_type=no_proxy).
64 - The parameter is mandatory when I(callhome_type='cloud services')) or I(callhome_type='both').
65 choices: [ open_proxy, basic_authentication, certificate, no_proxy ]
66 type: str
67 proxy_url:
68 description:
69 - Specifies the proxy server URL with a protocol prefix in fully qualified domain name format.
70 - Applies when I(state=enabled) and I(proxy_type=open_proxy) or I(proxy_type=basic_authentication).
71 type: str
72 proxy_port:
73 description:
74 - Specifies the proxy server port number.
75 The value must be in the range 1 - 65535.
76 - Applies when I(state=enabled) and I(proxy_type=open_proxy) or I(proxy_type=basic_authentication).
77 type: int
78 proxy_username:
79 description:
80 - Specifies the proxy's username.
81 - Applies when I(state=enabled) and I(proxy_type=basic_authentication).
82 type: str
83 proxy_password:
84 description:
85 - Specifies the proxy's password.
86 - Applies when I(state=enabled) and I(proxy_type=basic_authentication).
87 type: str
88 sslcert:
89 description:
90 - Specifies the file path of proxy's certificate.
91 - Applies when I(state=enabled) and I(proxy_type=certificate).
92 type: str
93 company_name:
94 description:
95 - Specifies the user's organization as it should appear in Call Home email.
96 - Required when I(state=enabled).
97 type: str
98 address:
99 description:
100 - Specifies the first line of the user's address as it should appear in Call Home email.
101 - Required when I(state=enabled).
102 type: str
103 city:
104 description:
105 - Specifies the user's city as it should appear in Call Home email.
106 - Required when I(state=enabled).
107 type: str
108 province:
109 description:
110 - Specifies the user's state or province as it should appear in Call Home email.
111 - Required when I(state=enabled).
112 type: str
113 postalcode:
114 description:
115 - Specifies the user's zip code or postal code as it should appear in Call Home email.
116 - Required when I(state=enabled).
117 type: str
118 country:
119 description:
120 - Specifies the country in which the machine resides as it should appear in Call Home email.
121 - Required when I(state=enabled).
122 type: str
123 location:
124 description:
125 - Specifies the physical location of the system that has reported the error.
126 - Required when I(state=enabled).
127 type: str
128 contact_name:
129 description:
130 - Specifies the name of the person receiving the email.
131 - Required when I(state=enabled).
132 type: str
133 contact_email:
134 description:
135 - Specifies the email of the person.
136 - Required when I(state=enabled).
137 type: str
138 phonenumber_primary:
139 description:
140 - Specifies the primary contact telephone number.
141 - Required when I(state=enabled).
142 type: str
143 phonenumber_secondary:
144 description:
145 - Specifies the secondary contact telephone number.
146 - Required when I(state=enabled).
147 type: str
148 serverIP:
149 description:
150 - Specifies the IP address of the email server.
151 - Required when I(state=enabled) and I(callhome_type=email) or I(callhome_type=both).
152 type: str
153 serverPort:
154 description:
155 - Specifies the port number of the email server.
156 - The value must be in the range 1 - 65535.
157 - Required when I(state=enabled) and I(callhome_type=email) or I(callhome_type=both).
158 type: int
159 inventory:
160 description:
161 - Specifies whether the recipient mentioned in parameter I(contact_email) receives inventory email notifications.
162 - Applies when I(state=enabled).
163 If unspecified, default value 'off' will be used.
164 choices: ['on', 'off']
165 type: str
166 invemailinterval:
167 description:
168 - Specifies the interval at which inventory emails are sent to the configured email recipients.
169 - The interval is measured in days. The value must be in the range 0 - 15.
170 - Setting the value to '0' turns off the inventory email notification function.
171 Valid if I(inventory) is set to 'on'.
172 type: int
173 enhancedcallhome:
174 description:
175 - Specifies that the Call Home function is to send enhanced reports to the support center.
176 - Applies when I(state=enabled).
177 - If unspecified, default value 'off' will be used.
178 choices: ['on', 'off']
179 type: str
180 censorcallhome:
181 description:
182 - Specifies that sensitive data is deleted from the enhanced Call Home data.
183 - Applies when I(state=enabled).
184 - If unspecified, default value 'off' will be used.
185 choices: ['on', 'off']
186 type: str
187 validate_certs:
188 description:
189 - Validates certification.
190 default: false
191 type: bool
192 log_path:
193 description:
194 - Path of debug log file.
195 type: str
196 author:
197 - Sreshtant Bohidar(@Sreshtant-Bohidar)
198 - Sandip Gulab Rajbanshi (@Sandip-Rajbanshi)
199 notes:
200 - This module supports C(check_mode).
201 '''
202
203 EXAMPLES = '''
204 - name: Configure callhome with both email and cloud
205 ibm.storage_virtualize.ibm_svc_manage_callhome:
206 clustername: "{{ clustername }}"
207 username: "{{ username }}"
208 password: "{{ password }}"
209 log_path: "/tmp/playbook.debug"
210 state: "enabled"
211 callhome_type: "both"
212 address: "{{ address }}"
213 city: "{{ city }}"
214 company_name: "{{ company_name }}"
215 contact_email: "{{ contact_email }}"
216 contact_name: "{{ contact_name }}"
217 country: "{{ country }}"
218 location: "{{ location }}"
219 phonenumber_primary: "{{ primary_phonenumber }}"
220 postalcode: "{{ postal_code }}"
221 province: "{{ province }}"
222 proxy_type: "{{ proxy_type }}"
223 proxy_url: "{{ proxy_url }}"
224 proxy_port: "{{ proxy_port }}"
225 serverIP: "{{ server_ip }}"
226 serverPort: "{{ server_port }}"
227 inventory: "on"
228 invemailinterval: 1
229 enhancedcallhome: "on"
230 censorcallhome: "on"
231
232 - name: Configure callhome with cloud
233 ibm.storage_virtualize.ibm_svc_manage_callhome:
234 clustername: "{{ clustername }}"
235 username: "{{ username }}"
236 password: "{{ password }}"
237 log_path: "/tmp/playbook.debug"
238 state: "enabled"
239 callhome_type: "cloud services"
240 province: "{{ province }}"
241 proxy_type: "{{ proxy_type }}"
242 proxy_url: "{{ proxy_url }}"
243 proxy_port: "{{ proxy_port }}"
244
245 - name: Configure callhome with email
246 ibm.storage_virtualize.ibm_svc_manage_callhome:
247 clustername: "{{ clustername }}"
248 username: "{{ username }}"
249 password: "{{ password }}"
250 log_path: "/tmp/playbook.debug"
251 state: "enabled"
252 callhome_type: "email"
253 contact_email: "{{ contact_email }}"
254 serverIP: "{{ server_ip }}"
255 serverPort: "{{ server_port }}"
256 '''
257
258 RETURN = '''#'''
259
260 from traceback import format_exc
261 from ansible.module_utils.basic import AnsibleModule
262 from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import IBMSVCRestApi, svc_argument_spec, get_logger
263 from ansible.module_utils._text import to_native
264 import time
265
266
267 class IBMSVCCallhome(object):
268 def __init__(self):
269 argument_spec = svc_argument_spec()
270 argument_spec.update(
271 dict(
272 state=dict(type='str', required=True, choices=['enabled', 'disabled']),
273 callhome_type=dict(type='str', required=True, choices=['cloud services', 'email', 'both']),
274 proxy_type=dict(type='str', choices=['open_proxy', 'basic_authentication', 'certificate', 'no_proxy']),
275 proxy_url=dict(type='str'),
276 proxy_port=dict(type='int'),
277 proxy_username=dict(type='str'),
278 proxy_password=dict(type='str', no_log=True),
279 sslcert=dict(type='str'),
280 company_name=dict(type='str'),
281 address=dict(type='str'),
282 city=dict(type='str'),
283 province=dict(type='str'),
284 postalcode=dict(type='str'),
285 country=dict(type='str'),
286 location=dict(type='str'),
287 contact_name=dict(type='str'),
288 contact_email=dict(type='str'),
289 phonenumber_primary=dict(type='str'),
290 phonenumber_secondary=dict(type='str'),
291 serverIP=dict(type='str'),
292 serverPort=dict(type='int'),
293 inventory=dict(type='str', choices=['on', 'off']),
294 invemailinterval=dict(type='int'),
295 enhancedcallhome=dict(type='str', choices=['on', 'off']),
296 censorcallhome=dict(type='str', choices=['on', 'off'])
297 )
298 )
299
300 self.module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
301
302 # logging setup
303 log_path = self.module.params['log_path']
304 log = get_logger(self.__class__.__name__, log_path)
305 self.log = log.info
306
307 # Required
308 self.state = self.module.params['state']
309 self.callhome_type = self.module.params['callhome_type']
310
311 # Optional
312 self.company_name = self.module.params['company_name']
313 self.address = self.module.params['address']
314 self.city = self.module.params['city']
315 self.province = self.module.params['province']
316 self.postalcode = self.module.params['postalcode']
317 self.country = self.module.params['country']
318 self.location = self.module.params['location']
319 self.contact_name = self.module.params['contact_name']
320 self.contact_email = self.module.params['contact_email']
321 self.phonenumber_primary = self.module.params['phonenumber_primary']
322 self.proxy_type = self.module.params.get('proxy_type', False)
323 self.proxy_url = self.module.params.get('proxy_url', False)
324 self.proxy_port = self.module.params.get('proxy_port', False)
325 self.proxy_username = self.module.params.get('proxy_username', False)
326 self.proxy_password = self.module.params.get('proxy_password', False)
327 self.sslcert = self.module.params.get('sslcert', False)
328 self.phonenumber_secondary = self.module.params.get('phonenumber_secondary', False)
329 self.serverIP = self.module.params.get('serverIP', False)
330 self.serverPort = self.module.params.get('serverPort', False)
331 self.inventory = self.module.params.get('inventory', False)
332 self.invemailinterval = self.module.params.get('invemailinterval', False)
333 self.enhancedcallhome = self.module.params.get('enhancedcallhome', False)
334 self.censorcallhome = self.module.params.get('censorcallhome', False)
335
336 # creating an instance of IBMSVCRestApi
337 self.restapi = IBMSVCRestApi(
338 module=self.module,
339 clustername=self.module.params['clustername'],
340 domain=self.module.params['domain'],
341 username=self.module.params['username'],
342 password=self.module.params['password'],
343 validate_certs=self.module.params['validate_certs'],
344 log_path=log_path,
345 token=self.module.params['token']
346 )
347
348 def basic_checks(self):
349 # setting the default value if unspecified
350 if not self.inventory:
351 self.inventory = 'off'
352 if not self.enhancedcallhome:
353 self.enhancedcallhome = 'off'
354 if not self.censorcallhome:
355 self.censorcallhome = 'off'
356 # perform some basic handling for few parameters
357 if self.inventory == 'on':
358 if not self.invemailinterval:
359 self.module.fail_json(msg="Parameter [invemailinterval] should be configured to use [inventory]")
360 if self.invemailinterval:
361 if self.inventory == 'off':
362 self.module.fail_json(msg="The parameter [inventory] should be configured with 'on' while setting [invemailinterval]")
363 if self.invemailinterval not in range(1, 16):
364 self.module.fail_json(msg="Parameter [invemailinterval] supported range is 0 to 15")
365 if isinstance(self.serverPort, int):
366 if self.serverPort not in range(1, 65536):
367 self.module.fail_json(msg="Parameter [serverPort] must be in range[1-65535]")
368 if isinstance(self.proxy_port, int):
369 if self.proxy_port not in range(1, 65536):
370 self.module.fail_json(msg="Parameter [proxy_port] must be in range[1-65535]")
371 if not self.state:
372 self.module.fail_json(msg="Missing mandatory parameter: state")
373 if not self.callhome_type:
374 self.module.fail_json(msg="Missing mandatory parameter: callhome_type")
375 if self.state == "enabled" and self.callhome_type in ["cloud services", "both"]:
376 if not self.proxy_type:
377 self.module.fail_json(msg="Parameter [proxy_type] required when callhome_type=cloud services or both")
378 if self.proxy_type == 'open_proxy' and (not self.proxy_url or not self.proxy_port):
379 self.module.fail_json(msg="Parameters [proxy_url, proxy_port] required when proxy_type=open_proxy")
380 if self.proxy_type == 'basic_authentication' and (not self.proxy_url or not self.proxy_port or not self.proxy_username or not self.proxy_password):
381 self.module.fail_json(msg="Parameters [proxy_url, proxy_port, proxy_username, proxy_password] required when proxy_type=basic_authentication")
382 if self.proxy_type == 'certificate' and (not self.proxy_url or not self.proxy_port or not self.sslcert):
383 self.module.fail_json(msg="Parameters [proxy_url, proxy_port, sslcert] required when proxy_type=certificate")
384 if self.state == 'enabled' and self.callhome_type in ["email", "both"]:
385 parameters = {
386 'contact_email': self.contact_email,
387 'serverIP': self.serverIP,
388 'serverPort': self.serverPort
389 }
390 parameter_not_provided = []
391 for parameter in parameters:
392 if not parameters[parameter]:
393 parameter_not_provided.append(parameter)
394 if parameter_not_provided:
395 self.module.fail_json(msg="Parameters {0} are required when state is 'enabled' and"
396 " callhome_type is email or both".format(parameter_not_provided))
397
398 # function to fetch lssystem data
399 def get_system_data(self):
400 return self.restapi.svc_obj_info('lssystem', cmdopts=None, cmdargs=None)
401
402 # function to probe lssystem data
403 def probe_system(self, data):
404 modify = {}
405 if self.invemailinterval:
406 if self.invemailinterval != data['inventory_mail_interval']:
407 modify['invemailinterval'] = self.invemailinterval
408 if self.enhancedcallhome:
409 if self.enhancedcallhome != data['enhanced_callhome']:
410 modify['enhancedcallhome'] = self.enhancedcallhome
411 if self.censorcallhome:
412 if self.censorcallhome != data['censor_callhome']:
413 modify['censorcallhome'] = self.censorcallhome
414 return modify
415
416 # function to execute chsystem commands
417 def update_system(self, modify):
418 command = 'chsystem'
419 command_options = modify
420 cmdargs = None
421 self.restapi.svc_run_command(command, command_options, cmdargs)
422 self.log("Chsystem commands executed.")
423
424 # function to fetch existing email user
425 def get_existing_email_user_data(self):
426 data = {}
427 email_data = self.restapi.svc_obj_info(cmd='lsemailuser', cmdopts=None, cmdargs=None)
428 for item in email_data:
429 if item['address'] == self.contact_email:
430 data = item
431 return data
432
433 # function to check if email server exists or not
434 def check_email_server_exists(self):
435 status = False
436 data = self.restapi.svc_obj_info(cmd='lsemailserver', cmdopts=None, cmdargs=None)
437 for item in data:
438 if item['IP_address'] == self.serverIP and int(item['port']) == self.serverPort:
439 status = True
440 break
441 return status
442
443 # function to check if email user exists or not
444 def check_email_user_exists(self):
445 temp = {}
446 data = self.restapi.svc_obj_info(cmd='lsemailuser', cmdopts=None, cmdargs=None)
447 for item in data:
448 if item['address'] == self.contact_email:
449 temp = item
450 break
451 return temp
452
453 # function to create an email server
454 def create_email_server(self):
455 if self.module.check_mode:
456 self.changed = True
457 return
458 self.log("Creating email server '%s:%s'.", self.serverIP, self.serverPort)
459 command = 'mkemailserver'
460 command_options = {
461 'ip': self.serverIP,
462 'port': self.serverPort,
463 }
464 cmdargs = None
465 result = self.restapi.svc_run_command(command, command_options, cmdargs)
466 if 'message' in result:
467 self.changed = True
468 self.log("create email server result message '%s'", (result['message']))
469 else:
470 self.module.fail_json(
471 msg="Failed to create email server [%s:%s]" % (self.serverIP, self.serverPort)
472 )
473
474 # function to update email user
475 def update_email_user(self, data, id):
476 command = "chemailuser"
477 command_options = data
478 cmdargs = [id]
479 self.restapi.svc_run_command(command, command_options, cmdargs)
480 self.log('Email user updated successfully.')
481
482 # function to manage support email user
483 def manage_support_email_user(self):
484 if self.module.check_mode:
485 self.changed = True
486 return
487 support_email = {}
488 selected_email_id = ''
489 t = -1 * ((time.timezone / 60) / 60)
490 if t >= -8 and t <= -4:
491 # for US timezone, [email protected] is used
492 selected_email_id = '[email protected]'
493 else:
494 # for ROW, [email protected] is used
495 selected_email_id = '[email protected]'
496 existing_user = self.restapi.svc_obj_info('lsemailuser', cmdopts=None, cmdargs=None)
497 if existing_user:
498 for user in existing_user:
499 if user['user_type'] == 'support':
500 support_email = user
501 if not support_email:
502 self.log("Creating support email user '%s'.", selected_email_id)
503 command = 'mkemailuser'
504 command_options = {
505 'address': selected_email_id,
506 'usertype': 'support',
507 'info': 'off',
508 'warning': 'off',
509 }
510 if self.inventory:
511 command_options['inventory'] = self.inventory
512 cmdargs = None
513 result = self.restapi.svc_run_command(command, command_options, cmdargs)
514 if 'message' in result:
515 self.changed = True
516 self.log("create support email user result message '%s'", (result['message']))
517 else:
518 self.module.fail_json(
519 msg="Failed to support create email user [%s]" % (self.contact_email)
520 )
521 else:
522 modify = {}
523 if support_email['address'] != selected_email_id:
524 modify['address'] = selected_email_id
525 if self.inventory:
526 if support_email['inventory'] != self.inventory:
527 modify['inventory'] = self.inventory
528 if modify:
529 self.restapi.svc_run_command(
530 'chemailuser',
531 modify,
532 [support_email['id']]
533 )
534 self.log("Updated support user successfully.")
535
536 # function to create an email user
537 def create_email_user(self):
538 if self.module.check_mode:
539 self.changed = True
540 return
541 self.log("Creating email user '%s'.", self.contact_email)
542 command = 'mkemailuser'
543 command_options = {
544 'address': self.contact_email,
545 'usertype': 'local',
546 }
547 if self.inventory:
548 command_options['inventory'] = self.inventory
549 cmdargs = None
550 result = self.restapi.svc_run_command(command, command_options, cmdargs)
551 if 'message' in result:
552 self.changed = True
553 self.log("Create email user result message '%s'.", (result['message']))
554 else:
555 self.module.fail_json(
556 msg="Failed to create email user [%s]" % (self.contact_email)
557 )
558
559 # function to enable email callhome
560 def enable_email_callhome(self):
561 if self.module.check_mode:
562 self.changed = True
563 return
564 command = "startemail"
565 command_options = {}
566 cmdargs = None
567 self.restapi.svc_run_command(command, command_options, cmdargs)
568 self.log("Email callhome enabled.")
569
570 # function to disable email callhome
571 def disable_email_callhome(self):
572 if self.module.check_mode:
573 self.changed = True
574 return
575 command = "stopemail"
576 command_options = {}
577 cmdargs = None
578 self.restapi.svc_run_command(command, command_options, cmdargs)
579 self.log("Email callhome disabled.")
580
581 # function to update email data
582 def update_email_data(self):
583
584 if self.module.check_mode:
585 self.changed = True
586 return
587 command = "chemail"
588 command_options = {}
589 if self.contact_email:
590 command_options['reply'] = self.contact_email
591 if self.contact_name:
592 command_options['contact'] = self.contact_name
593 if self.phonenumber_primary:
594 command_options['primary'] = self.phonenumber_primary
595 if self.phonenumber_secondary:
596 command_options['alternate'] = self.phonenumber_secondary
597 if self.location:
598 command_options['location'] = self.location
599 if self.company_name:
600 command_options['organization'] = self.company_name
601 if self.address:
602 command_options['address'] = self.address
603 if self.city:
604 command_options['city'] = self.city
605 if self.province:
606 command_options['state'] = self.province
607 if self.postalcode:
608 command_options['zip'] = self.postalcode
609 if self.country:
610 command_options['country'] = self.country
611 cmdargs = None
612 if command_options:
613 self.restapi.svc_run_command(command, command_options, cmdargs)
614 self.log("Email data successfully updated.")
615
616 # function for checking if proxy server exists
617 def get_existing_proxy(self):
618 data = {}
619 data = self.restapi.svc_obj_info(cmd='lsproxy', cmdopts=None, cmdargs=None)
620 return data
621
622 # function for removing a proxy
623 def remove_proxy(self):
624 if self.module.check_mode:
625 self.changed = True
626 return
627 command = 'rmproxy'
628 command_options = None
629 cmdargs = None
630 self.restapi.svc_run_command(command, command_options, cmdargs)
631 self.log('Proxy removed successfully.')
632
633 # function for creating a proxy
634 def create_proxy(self):
635 if self.module.check_mode:
636 self.changed = True
637 return
638 command = 'mkproxy'
639 command_options = {}
640 if self.proxy_type == 'open_proxy':
641 if self.proxy_url:
642 command_options['url'] = self.proxy_url
643 if self.proxy_port:
644 command_options['port'] = self.proxy_port
645 elif self.proxy_type == 'basic_authentication':
646 if self.proxy_url:
647 command_options['url'] = self.proxy_url
648 if self.proxy_port:
649 command_options['port'] = self.proxy_port
650 if self.proxy_username:
651 command_options['username'] = self.proxy_username
652 if self.proxy_password:
653 command_options['password'] = self.proxy_password
654 elif self.proxy_type == 'certificate':
655 if self.proxy_url:
656 command_options['url'] = self.proxy_url
657 if self.proxy_port:
658 command_options['port'] = self.proxy_port
659 if self.sslcert:
660 command_options['sslcert'] = self.sslcert
661
662 cmdargs = None
663 self.restapi.svc_run_command(command, command_options, cmdargs)
664 self.log("Proxy created successfully.")
665
666 # function for probing existing proxy data
667 def probe_proxy(self, data):
668 modify = {}
669 if self.proxy_type == 'open_proxy':
670 if self.proxy_url:
671 if self.proxy_url != data['url']:
672 modify['url'] = self.proxy_url
673 if self.proxy_port:
674 if int(self.proxy_port) != int(data['port']):
675 modify['port'] = self.proxy_port
676 elif self.proxy_type == 'basic_authentication':
677 if self.proxy_url:
678 if self.proxy_url != data['url']:
679 modify['url'] = self.proxy_url
680 if self.proxy_port:
681 if self.proxy_port != int(data['port']):
682 modify['port'] = self.proxy_port
683 if self.proxy_username:
684 if self.proxy_username != data['username']:
685 modify['username'] = self.proxy_username
686 if self.proxy_password:
687 modify['password'] = self.proxy_password
688 elif self.proxy_type == 'certificate':
689 if self.proxy_url:
690 if self.proxy_url != data['url']:
691 modify['url'] = self.proxy_url
692 if self.proxy_port:
693 if self.proxy_port != int(data['port']):
694 modify['port'] = self.proxy_port
695 if self.sslcert:
696 modify['sslcert'] = self.sslcert
697 return modify
698
699 # function for updating a proxy
700 def update_proxy(self, data):
701 if self.module.check_mode:
702 self.changed = True
703 return
704 command = 'chproxy'
705 command_options = data
706 cmdargs = None
707 self.restapi.svc_run_command(command, command_options, cmdargs)
708 self.log('Proxy updated successfully.')
709
710 # function for fetching existing cloud callhome data
711 def get_existing_cloud_callhome_data(self):
712 data = {}
713 command = 'lscloudcallhome'
714 command_options = None
715 cmdargs = None
716 data = self.restapi.svc_obj_info(command, command_options, cmdargs)
717 return data
718
719 # function for enabling cloud callhome
720 def enable_cloud_callhome(self):
721 if self.module.check_mode:
722 self.changed = True
723 return
724 command = 'chcloudcallhome'
725 command_options = {
726 'enable': True
727 }
728 cmdargs = None
729 self.restapi.svc_run_command(command, command_options, cmdargs)
730 self.changed = True
731 self.log('Cloud callhome enabled.')
732
733 # function for doing connection test for cloud callhome
734 def test_connection_cloud_callhome(self):
735 if self.module.check_mode:
736 self.changed = True
737 return
738 command = 'sendcloudcallhome'
739 command_options = {
740 'connectiontest': True
741 }
742 self.restapi.svc_run_command(command, command_options, None)
743 self.changed = True
744 self.log('Cloud callhome connection tested.')
745 # the connection testing can take some time to complete.
746 time.sleep(3)
747
748 # function for managing proxy server
749 def manage_proxy_server(self):
750 proxy_data = self.get_existing_proxy()
751 if proxy_data['enabled'] == 'no':
752 if self.proxy_type == 'no_proxy':
753 self.log('Proxy already disabled.')
754 else:
755 self.create_proxy()
756 self.changed = True
757 elif proxy_data['enabled'] == 'yes':
758 if self.proxy_type == 'no_proxy':
759 self.remove_proxy()
760 self.changed = True
761 else:
762 modify = self.probe_proxy(proxy_data)
763 if modify:
764 self.update_proxy(modify)
765 self.changed = True
766
767 # function for disabling cloud callhome
768 def disable_cloud_callhome(self):
769 if self.module.check_mode:
770 self.changed = True
771 return
772 command = 'chcloudcallhome'
773 command_options = {
774 'disable': True
775 }
776 cmdargs = None
777 self.restapi.svc_run_command(command, command_options, cmdargs)
778 self.log('Cloud callhome disabled.')
779
780 # function to initiate callhome with cloud
781 def initiate_cloud_callhome(self):
782 msg = ''
783 attempts = 0
784 limit_reached = False
785 active_status = False
786 # manage proxy server
787 self.manage_proxy_server()
788 # update email data
789 self.update_email_data()
790 # manage cloud callhome
791 lsdata = self.get_existing_cloud_callhome_data()
792 if lsdata['status'] == 'enabled':
793 # perform connection test
794 self.test_connection_cloud_callhome()
795 else:
796 self.enable_cloud_callhome()
797 # cloud callhome takes some time to get enabled.
798 while not active_status:
799 attempts += 1
800 if attempts > 10:
801 limit_reached = True
802 break
803 lsdata = self.get_existing_cloud_callhome_data()
804 if lsdata['status'] == 'enabled':
805 active_status = True
806 time.sleep(2)
807 if limit_reached:
808 # the module will exit without performing connection test.
809 msg = "Callhome with Cloud is enabled. Please check connection to proxy."
810 self.changed = True
811 return msg
812 if active_status:
813 # perform connection test
814 self.test_connection_cloud_callhome()
815 msg = "Callhome with Cloud enabled successfully."
816 self.changed = True
817 return msg
818
819 # function to initiate callhome with email notifications
820 def initiate_email_callhome(self):
821 msg = ''
822 # manage email server
823 email_server_exists = self.check_email_server_exists()
824 if email_server_exists:
825 self.log("Email server already exists.")
826 else:
827 self.create_email_server()
828 self.changed = True
829 # manage support email user
830 self.manage_support_email_user()
831 # manage local email user
832 email_user_exists = self.check_email_user_exists()
833 if email_user_exists:
834 email_user_modify = {}
835 if email_user_exists['inventory'] != self.inventory:
836 email_user_modify['inventory'] = self.inventory
837 if email_user_modify:
838 self.update_email_user(email_user_modify, email_user_exists['id'])
839 else:
840 self.create_email_user()
841 # manage email data
842 self.update_email_data()
843 # enable email callhome
844 self.enable_email_callhome()
845 msg = "Callhome with email enabled successfully."
846 self.changed = True
847 return msg
848
849 def apply(self):
850 self.changed = False
851 msg = None
852 self.basic_checks()
853 if self.state == 'enabled':
854 # enable cloud callhome
855 if self.callhome_type == 'cloud services':
856 msg = self.initiate_cloud_callhome()
857 # enable email callhome
858 elif self.callhome_type == 'email':
859 msg = self.initiate_email_callhome()
860 # enable both cloud and email callhome
861 elif self.callhome_type == 'both':
862 temp_msg = ''
863 temp_msg += self.initiate_cloud_callhome()
864 temp_msg += ' ' + self.initiate_email_callhome()
865 if temp_msg:
866 msg = temp_msg
867 # manage chsystem parameters
868 system_data = self.get_system_data()
869 system_modify = self.probe_system(system_data)
870 if system_modify:
871 self.update_system(system_modify)
872 elif self.state == 'disabled':
873 if self.callhome_type == 'cloud services':
874 cloud_callhome_data = self.get_existing_cloud_callhome_data()
875 if cloud_callhome_data['status'] == 'disabled':
876 msg = "Callhome with cloud already disabled."
877 elif cloud_callhome_data['status'] == 'enabled':
878 self.disable_cloud_callhome()
879 msg = "Callhome with cloud disabled successfully."
880 self.changed = True
881 elif self.callhome_type == 'email':
882 self.disable_email_callhome()
883 msg = "Callhome with email disabled successfully."
884 self.changed = True
885 elif self.callhome_type == 'both':
886 # disable email callhome
887 self.disable_email_callhome()
888 msg = "Callhome with email disabled successfully."
889 self.changed = True
890 # disable cloud callhome
891 cloud_callhome_data = self.get_existing_cloud_callhome_data()
892 if cloud_callhome_data['status'] == 'disabled':
893 msg += " Callhome with cloud already disabled."
894 elif cloud_callhome_data['status'] == 'enabled':
895 self.disable_cloud_callhome()
896 msg += " Callhome with cloud disabled successfully."
897 self.changed = True
898 self.module.exit_json(msg=msg, changed=self.changed)
899
900
901 def main():
902 v = IBMSVCCallhome()
903 try:
904 v.apply()
905 except Exception as e:
906 v.log("Exception in apply(): \n%s", format_exc())
907 v.module.fail_json(msg="Module failed. Error [%s]." % to_native(e))
908
909
910 if __name__ == '__main__':
911 main()