Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2020-2025, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 '''
7 na_ontap_autosupport_invoke
8 '''
9
10 from __future__ import absolute_import, division, print_function
11
12 __metaclass__ = type
13
14 ANSIBLE_METADATA = {
15 'metadata_version': '1.1',
16 'status': ['preview'],
17 'supported_by': 'certified'
18 }
19
20 DOCUMENTATION = '''
21
22 module: na_ontap_autosupport_invoke
23 author: NetApp Ansible Team (@carchi8py) <[email protected]>
24 short_description: NetApp ONTAP send AutoSupport message
25 extends_documentation_fragment:
26 - netapp.ontap.netapp.na_ontap
27 version_added: '20.4.0'
28 description:
29 - Send an AutoSupport message from a node
30
31 options:
32
33 name:
34 description:
35 - The name of the node to send the message to.
36 - Not specifying this option invokes AutoSupport on all nodes in the cluster.
37 type: str
38
39 autosupport_message:
40 description:
41 - Text sent in the subject line of the AutoSupport message.
42 - message is deprecated and will be removed to avoid a conflict with an Ansible internal variable.
43 type: str
44 aliases:
45 - message
46 version_added: 20.8.0
47
48 type:
49 description:
50 - Type of AutoSupport Collection to Issue.
51 choices: ['test', 'performance', 'all']
52 default: 'all'
53 type: str
54
55 uri:
56 description:
57 - send the AutoSupport message to the destination you specify instead of the configured destination.
58 type: str
59
60 '''
61
62 EXAMPLES = '''
63 - name: Send message
64 netapp.ontap.na_ontap_autosupport_invoke:
65 name: node1
66 autosupport_message: invoked test autosupport rest
67 uri: http://1.2.3.4/delivery_uri
68 type: test
69 hostname: "{{ netapp_hostname }}"
70 username: "{{ netapp_username }}"
71 password: "{{ netapp_password }}"
72 '''
73
74 RETURN = '''
75 '''
76
77 import traceback
78
79 from ansible.module_utils.basic import AnsibleModule
80 from ansible.module_utils._text import to_native
81 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
82 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
83 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
84
85 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
86
87
88 class NetAppONTAPasupInvoke(object):
89 ''' send ASUP message '''
90 def __init__(self):
91 self.use_rest = False
92 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
93 self.argument_spec.update(dict(
94 name=dict(required=False, type='str'),
95 autosupport_message=dict(required=False, type='str', aliases=["message"]),
96 type=dict(required=False, choices=[
97 'test', 'performance', 'all'], default='all'),
98 uri=dict(required=False, type='str')
99 ))
100
101 self.module = AnsibleModule(
102 argument_spec=self.argument_spec,
103 supports_check_mode=True
104 )
105 self.na_helper = NetAppModule()
106 self.parameters = self.na_helper.set_parameters(self.module.params)
107 if 'message' in self.parameters:
108 self.module.warn('Error: "message" option conflicts with Ansible internal variable - please use "autosupport_message".')
109
110 # REST API should be used for ONTAP 9.6 or higher.
111 self.rest_api = OntapRestAPI(self.module)
112 if self.rest_api.is_rest():
113 self.use_rest = True
114 else:
115 if not netapp_utils.has_netapp_lib():
116 self.module.fail_json(msg=netapp_utils.netapp_lib_is_required())
117 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)
118
119 def get_nodes(self):
120 nodes = []
121 node_obj = netapp_utils.zapi.NaElement('system-node-get-iter')
122 desired_attributes = netapp_utils.zapi.NaElement('desired-attributes')
123 node_details_info = netapp_utils.zapi.NaElement('node-details-info')
124 node_details_info.add_new_child('node', '')
125 desired_attributes.add_child_elem(node_details_info)
126 node_obj.add_child_elem(desired_attributes)
127 try:
128 result = self.server.invoke_successfully(node_obj, True)
129 except netapp_utils.zapi.NaApiError as error:
130 self.module.fail_json(msg=to_native(error), exception=traceback.format_exc())
131 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) > 0:
132 node_info = result.get_child_by_name('attributes-list')
133 if node_info is not None:
134 nodes = [node_details.get_child_content('node') for node_details in node_info.get_children()]
135 return nodes
136
137 def send_zapi_message(self, params, node_name):
138 params['node-name'] = node_name
139 send_message = netapp_utils.zapi.NaElement.create_node_with_children('autosupport-invoke', **params)
140 try:
141 self.server.invoke_successfully(send_message, enable_tunneling=False)
142 except netapp_utils.zapi.NaApiError as error:
143 self.module.fail_json(msg="Error on sending autosupport message to node %s: %s."
144 % (node_name, to_native(error)),
145 exception=traceback.format_exc())
146
147 def send_message(self):
148 params = {}
149 if self.parameters.get('autosupport_message'):
150 params['message'] = self.parameters['autosupport_message']
151 if self.parameters.get('type'):
152 params['type'] = self.parameters['type']
153 if self.parameters.get('uri'):
154 params['uri'] = self.parameters['uri']
155
156 if self.use_rest:
157 if self.parameters.get('name'):
158 params['node.name'] = self.parameters['name']
159 node_name = params['node.name']
160 else:
161 node_name = '*'
162 api = 'support/autosupport/messages'
163 dummy, error = self.rest_api.post(api, params)
164 if error is not None:
165 self.module.fail_json(msg="Error on sending autosupport message to node %s: %s."
166 % (node_name, error))
167 else:
168 if self.parameters.get('name'):
169 node_names = [self.parameters['name']]
170 else:
171 # simulate REST behavior by sending to all nodes in the cluster
172 node_names = self.get_nodes()
173 for name in node_names:
174 self.send_zapi_message(params, name)
175
176 def apply(self):
177 if not self.module.check_mode:
178 self.send_message()
179 self.module.exit_json(changed=True)
180
181
182 def main():
183 message = NetAppONTAPasupInvoke()
184 message.apply()
185
186
187 if __name__ == '__main__':
188 main()