| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021-2025, 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': ['preview'], |
| 12 |
'supported_by': 'certified'} |
| 13 |
|
| 14 |
DOCUMENTATION = """ |
| 15 |
module: na_ontap_storage_auto_giveback |
| 16 |
short_description: Enables or disables NetApp ONTAP storage auto giveback for a specified node |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: '21.3.0' |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
description: |
| 22 |
- Enable or disable storage auto giveback |
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Specifies the node name to enable or disable storage auto giveback on. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
|
| 30 |
auto_giveback_enabled: |
| 31 |
description: |
| 32 |
- specifies whether auto give back should be enabled or disabled |
| 33 |
required: true |
| 34 |
type: bool |
| 35 |
|
| 36 |
auto_giveback_after_panic_enabled: |
| 37 |
description: |
| 38 |
- specifies whether auto give back on panic should be enabled or disabled |
| 39 |
type: bool |
| 40 |
|
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = """ |
| 44 |
- name: Enable storage auto giveback |
| 45 |
netapp.ontap.na_ontap_storage_auto_giveback: |
| 46 |
name: node1 |
| 47 |
auto_giveback_enabled: true |
| 48 |
auto_giveback_after_panic_enabled: true |
| 49 |
hostname: "{{ netapp_hostname }}" |
| 50 |
username: "{{ netapp_username }}" |
| 51 |
password: "{{ netapp_password }}" |
| 52 |
|
| 53 |
- name: Disable storage auto giveback |
| 54 |
netapp.ontap.na_ontap_storage_auto_giveback: |
| 55 |
name: node1 |
| 56 |
auto_giveback_enabled: false |
| 57 |
auto_giveback_after_panic_enabled: false |
| 58 |
hostname: "{{ netapp_hostname }}" |
| 59 |
username: "{{ netapp_username }}" |
| 60 |
password: "{{ netapp_password }}" |
| 61 |
""" |
| 62 |
|
| 63 |
RETURN = """ |
| 64 |
""" |
| 65 |
|
| 66 |
import traceback |
| 67 |
from ansible.module_utils.basic import AnsibleModule |
| 68 |
from ansible.module_utils._text import to_native |
| 69 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 70 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 71 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 72 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 73 |
|
| 74 |
|
| 75 |
class NetAppOntapStorageAutoGiveback(object): |
| 76 |
""" |
| 77 |
Enable or disable storage failover for a specified node |
| 78 |
""" |
| 79 |
def __init__(self): |
| 80 |
""" |
| 81 |
Initialize the ONTAP Storage auto giveback class |
| 82 |
""" |
| 83 |
|
| 84 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 85 |
self.argument_spec.update(dict( |
| 86 |
name=dict(required=True, type='str'), |
| 87 |
auto_giveback_enabled=dict(required=True, type='bool'), |
| 88 |
auto_giveback_after_panic_enabled=dict(required=False, type='bool') |
| 89 |
)) |
| 90 |
|
| 91 |
self.module = AnsibleModule( |
| 92 |
argument_spec=self.argument_spec, |
| 93 |
supports_check_mode=True |
| 94 |
) |
| 95 |
|
| 96 |
# set up variables |
| 97 |
self.na_helper = NetAppModule() |
| 98 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 99 |
|
| 100 |
self.rest_api = OntapRestAPI(self.module) |
| 101 |
self.use_rest = self.rest_api.is_rest() |
| 102 |
|
| 103 |
if not self.use_rest: |
| 104 |
if not netapp_utils.has_netapp_lib(): |
| 105 |
self.module.fail_json(msg='The python NetApp-Lib module is required') |
| 106 |
else: |
| 107 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 108 |
|
| 109 |
def get_storage_auto_giveback(self): |
| 110 |
""" |
| 111 |
get the storage failover giveback options for a given node |
| 112 |
:return: dict for options |
| 113 |
""" |
| 114 |
return_value = None |
| 115 |
|
| 116 |
if self.use_rest: |
| 117 |
|
| 118 |
api = "private/cli/storage/failover" |
| 119 |
query = { |
| 120 |
'fields': 'node,auto_giveback,auto_giveback_after_panic', |
| 121 |
'node': self.parameters['name'], |
| 122 |
} |
| 123 |
message, error = self.rest_api.get(api, query) |
| 124 |
records, error = rrh.check_for_0_or_1_records(api, message, error) |
| 125 |
|
| 126 |
if error is None and records is not None: |
| 127 |
return_value = { |
| 128 |
'name': message['records'][0]['node'], |
| 129 |
'auto_giveback_enabled': message['records'][0].get('auto_giveback'), |
| 130 |
'auto_giveback_after_panic_enabled': message['records'][0].get('auto_giveback_after_panic') |
| 131 |
} |
| 132 |
|
| 133 |
if error: |
| 134 |
self.module.fail_json(msg=error) |
| 135 |
|
| 136 |
if not records: |
| 137 |
error = "REST API did not return failover options for node %s" % (self.parameters['name']) |
| 138 |
self.module.fail_json(msg=error) |
| 139 |
|
| 140 |
else: |
| 141 |
|
| 142 |
storage_auto_giveback_get_iter = netapp_utils.zapi.NaElement('cf-get-iter') |
| 143 |
|
| 144 |
try: |
| 145 |
result = self.server.invoke_successfully(storage_auto_giveback_get_iter, True) |
| 146 |
|
| 147 |
except netapp_utils.zapi.NaApiError as error: |
| 148 |
self.module.fail_json(msg='Error getting auto giveback info for node %s: %s' % (self.parameters['name'], to_native(error)), |
| 149 |
exception=traceback.format_exc()) |
| 150 |
|
| 151 |
if result.get_child_by_name('attributes-list'): |
| 152 |
attributes_list = result.get_child_by_name('attributes-list') |
| 153 |
for storage_failover_info_attributes in attributes_list.get_children(): |
| 154 |
|
| 155 |
sfo_node_info = storage_failover_info_attributes.get_child_by_name('sfo-node-info') |
| 156 |
node_related_info = sfo_node_info.get_child_by_name('node-related-info') |
| 157 |
|
| 158 |
if node_related_info.get_child_content('node') == self.parameters['name']: |
| 159 |
|
| 160 |
sfo_options_info = storage_failover_info_attributes.get_child_by_name('sfo-options-info') |
| 161 |
options_related_info = sfo_options_info.get_child_by_name('options-related-info') |
| 162 |
sfo_giveback_options_info = options_related_info.get_child_by_name('sfo-giveback-options-info') |
| 163 |
giveback_options = sfo_giveback_options_info.get_child_by_name('giveback-options') |
| 164 |
|
| 165 |
return_value = { |
| 166 |
'name': node_related_info.get_child_content('node'), |
| 167 |
'auto_giveback_enabled': self.na_helper.get_value_for_bool( |
| 168 |
True, options_related_info.get_child_content('auto-giveback-enabled')), |
| 169 |
'auto_giveback_after_panic_enabled': self.na_helper.get_value_for_bool( |
| 170 |
True, giveback_options.get_child_content('auto-giveback-after-panic-enabled')), |
| 171 |
} |
| 172 |
break |
| 173 |
|
| 174 |
return return_value |
| 175 |
|
| 176 |
def modify_storage_auto_giveback(self): |
| 177 |
""" |
| 178 |
Modifies storage failover giveback options for a specified node |
| 179 |
""" |
| 180 |
if self.use_rest: |
| 181 |
api = "private/cli/storage/failover" |
| 182 |
body = dict() |
| 183 |
query = { |
| 184 |
'node': self.parameters['name'] |
| 185 |
} |
| 186 |
|
| 187 |
body['auto_giveback'] = self.parameters['auto_giveback_enabled'] |
| 188 |
if 'auto_giveback_after_panic_enabled' in self.parameters: |
| 189 |
body['auto_giveback_after_panic'] = self.parameters['auto_giveback_after_panic_enabled'] |
| 190 |
|
| 191 |
dummy, error = self.rest_api.patch(api, body, query) |
| 192 |
if error: |
| 193 |
self.module.fail_json(msg=error) |
| 194 |
|
| 195 |
else: |
| 196 |
|
| 197 |
storage_auto_giveback_enable = netapp_utils.zapi.NaElement('cf-modify-iter') |
| 198 |
attributes_info = netapp_utils.zapi.NaElement('options-related-info-modify') |
| 199 |
query_info = netapp_utils.zapi.NaElement('options-related-info-modify') |
| 200 |
|
| 201 |
attributes_info.add_new_child('node', self.parameters['name']) |
| 202 |
attributes_info.add_new_child('auto-giveback-enabled', self.na_helper.get_value_for_bool( |
| 203 |
from_zapi=False, value=self.parameters['auto_giveback_enabled'])) |
| 204 |
|
| 205 |
if 'auto_giveback_after_panic_enabled' in self.parameters: |
| 206 |
sfo_give_back_options_info_modify = netapp_utils.zapi.NaElement('sfo-giveback-options-info-modify') |
| 207 |
give_back_options_modify = netapp_utils.zapi.NaElement('giveback-options-modify') |
| 208 |
give_back_options_modify.add_new_child('auto-giveback-after-panic-enabled', self.na_helper.get_value_for_bool( |
| 209 |
from_zapi=False, value=self.parameters['auto_giveback_after_panic_enabled'])) |
| 210 |
sfo_give_back_options_info_modify.add_child_elem(give_back_options_modify) |
| 211 |
attributes_info.add_child_elem(sfo_give_back_options_info_modify) |
| 212 |
|
| 213 |
query = netapp_utils.zapi.NaElement('query') |
| 214 |
attributes = netapp_utils.zapi.NaElement("attributes") |
| 215 |
query.add_child_elem(query_info) |
| 216 |
attributes.add_child_elem(attributes_info) |
| 217 |
|
| 218 |
storage_auto_giveback_enable.add_child_elem(query) |
| 219 |
storage_auto_giveback_enable.add_child_elem(attributes) |
| 220 |
|
| 221 |
try: |
| 222 |
self.server.invoke_successfully(storage_auto_giveback_enable, True) |
| 223 |
except netapp_utils.zapi.NaApiError as error: |
| 224 |
self.module.fail_json(msg='Error modifying auto giveback for node %s: %s' % (self.parameters['name'], to_native(error)), |
| 225 |
exception=traceback.format_exc()) |
| 226 |
|
| 227 |
def apply(self): |
| 228 |
current = self.get_storage_auto_giveback() |
| 229 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 230 |
|
| 231 |
if self.na_helper.changed: |
| 232 |
if not self.module.check_mode: |
| 233 |
self.modify_storage_auto_giveback() |
| 234 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modify) |
| 235 |
self.module.exit_json(**result) |
| 236 |
|
| 237 |
|
| 238 |
def main(): |
| 239 |
""" |
| 240 |
Enables or disables NetApp ONTAP storage auto giveback for a specified node |
| 241 |
""" |
| 242 |
obj = NetAppOntapStorageAutoGiveback() |
| 243 |
obj.apply() |
| 244 |
|
| 245 |
|
| 246 |
if __name__ == '__main__': |
| 247 |
main() |