Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_ems_config.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: NetApp, Inc
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 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: na_ontap_ems_config
12 short_description: NetApp ONTAP module to modify EMS configuration.
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap_rest
15 version_added: '22.8.0'
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Configure event notification and logging for the cluster.
19 options:
20 state:
21 description:
22 - modify EMS configuration, only present is supported.
23 choices: ['present']
24 type: str
25 default: present
26 mail_from:
27 description:
28 - The email address that the event notification system uses as the "From" address for email notifications.
29 type: str
30 required: false
31 mail_server:
32 description:
33 - The name or IP address of the SMTP server that the event notification system uses to send email notification of events.
34 type: str
35 required: false
36 proxy_url:
37 description:
38 - HTTP or HTTPS proxy server URL used by rest-api type EMS notification destinations if your organization uses a proxy.
39 type: str
40 required: false
41 proxy_user:
42 description:
43 - User name for the HTTP or HTTPS proxy server if authentication is required.
44 type: str
45 required: false
46 proxy_password:
47 description:
48 - Password for HTTP or HTTPS proxy.
49 type: str
50 required: false
51 pubsub_enabled:
52 description:
53 - Indicates whether or not events are published to the Publish/Subscribe messaging broker.
54 - Requires ONTAP 9.10 or later.
55 type: bool
56 required: false
57
58 notes:
59 - Only supported with REST and requires ONTAP 9.6 or later.
60 - Module is not idempotent when proxy_password is set.
61 """
62
63 EXAMPLES = """
64 - name: Modify EMS mail config
65 netapp.ontap.na_ontap_ems_config:
66 state: present
67 mail_from: [email protected]
68 mail_server: mail.mycompany.com
69 pubsub_enabled: true
70 hostname: "{{ netapp_hostname }}"
71 username: "{{ netapp_username }}"
72 password: "{{ netapp_password }}"
73 https: true
74 validate_certs: "{{ validate_certs }}"
75
76 - name: Modify EMS proxy config
77 netapp.ontap.na_ontap_ems_config:
78 state: present
79 proxy_url: http://proxy.example.com:8080
80 pubsub_enabled: true
81 proxy_user: admin
82 proxy_password: password
83 hostname: "{{ netapp_hostname }}"
84 username: "{{ netapp_username }}"
85 password: "{{ netapp_password }}"
86 https: true
87 validate_certs: "{{ validate_certs }}"
88 """
89
90 RETURN = """
91 """
92
93 import traceback
94 from ansible.module_utils.basic import AnsibleModule
95 from ansible.module_utils._text import to_native
96 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
97 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
98 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
99
100
101 class NetAppOntapEmsConfig:
102 def __init__(self):
103 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
104 self.argument_spec.update(dict(
105 state=dict(required=False, type='str', choices=['present'], default='present'),
106 mail_from=dict(required=False, type='str'),
107 mail_server=dict(required=False, type='str'),
108 proxy_url=dict(required=False, type='str'),
109 proxy_user=dict(required=False, type='str'),
110 proxy_password=dict(required=False, type='str', no_log=True),
111 pubsub_enabled=dict(required=False, type='bool')
112 ))
113 self.module = AnsibleModule(
114 argument_spec=self.argument_spec,
115 supports_check_mode=True
116 )
117 self.uuid = None
118 self.na_helper = NetAppModule(self.module)
119 self.parameters = self.na_helper.check_and_set_parameters(self.module)
120 self.rest_api = netapp_utils.OntapRestAPI(self.module)
121 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_ems_config:', 9, 6)
122 self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, None, [['pubsub_enabled', (9, 10, 1)]])
123
124 def get_ems_config_rest(self):
125 """Get EMS config details"""
126 fields = 'mail_from,mail_server,proxy_url,proxy_user'
127 if 'pubsub_enabled' in self.parameters and self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 10, 1):
128 fields += ',pubsub_enabled'
129 record, error = rest_generic.get_one_record(self.rest_api, 'support/ems', None, fields)
130 if error:
131 self.module.fail_json(msg="Error fetching EMS config: %s" % to_native(error), exception=traceback.format_exc())
132 if record:
133 return {
134 'mail_from': record.get('mail_from'),
135 'mail_server': record.get('mail_server'),
136 'proxy_url': record.get('proxy_url'),
137 'proxy_user': record.get('proxy_user'),
138 'pubsub_enabled': record.get('pubsub_enabled')
139 }
140 return None
141
142 def modify_ems_config_rest(self, modify):
143 """Modify EMS config"""
144 dummy, error = rest_generic.patch_async(self.rest_api, 'support/ems', None, modify)
145 if error:
146 self.module.fail_json(msg='Error modifying EMS config: %s.' % to_native(error), exception=traceback.format_exc())
147
148 def check_proxy_url(self, current):
149 # GET return the proxy url, if configured, along with port number
150 # based on the existing config, append port numnber to input url to
151 # maintain idempotency while modifying config
152 port = None
153 if current.get('proxy_url') is not None:
154 # strip trailing '/' and extract the port no
155 port = current['proxy_url'].rstrip('/').split(':')[-1]
156 pos = self.parameters['proxy_url'].rstrip('/').rfind(':')
157 if self.parameters['proxy_url'][pos + 1] == '/':
158 # port is not mentioned in input proxy URL
159 # if port is present in current url configured then add to the input url
160 if port is not None and port != '':
161 self.parameters['proxy_url'] = "%s:%s" % (self.parameters['proxy_url'].rstrip('/'), port)
162
163 def apply(self):
164 current = self.get_ems_config_rest()
165 if self.parameters.get('proxy_url') not in [None, '']:
166 self.check_proxy_url(current)
167 modify = self.na_helper.get_modified_attributes(current, self.parameters)
168
169 password_changed = False
170 if self.parameters.get('proxy_password') not in [None, '']:
171 modify['proxy_password'] = self.parameters['proxy_password']
172 self.module.warn('Module is not idempotent when proxy_password is set.')
173 password_changed = True
174 if (self.na_helper.changed or password_changed) and not self.module.check_mode:
175 self.modify_ems_config_rest(modify)
176 result = netapp_utils.generate_result(changed=self.na_helper.changed | password_changed, modify=modify)
177 self.module.exit_json(**result)
178
179
180 def main():
181 ems_config = NetAppOntapEmsConfig()
182 ems_config.apply()
183
184
185 if __name__ == '__main__':
186 main()