| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2024, 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_node |
| 16 |
short_description: NetApp ONTAP Modify or Rename a node. |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: 2.7.0 |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
description: |
| 22 |
- Modify or Rename an ONTAP node. |
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name for the node |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
|
| 30 |
from_name: |
| 31 |
description: |
| 32 |
- The name of the node to be renamed. If I(name) already exists, no action will be performed. |
| 33 |
type: str |
| 34 |
|
| 35 |
location: |
| 36 |
description: |
| 37 |
- The location for the node |
| 38 |
type: str |
| 39 |
|
| 40 |
asset_tag: |
| 41 |
description: |
| 42 |
- The asset tag for the node, not supported by REST |
| 43 |
type: str |
| 44 |
|
| 45 |
''' |
| 46 |
|
| 47 |
EXAMPLES = """ |
| 48 |
- name: Modify node |
| 49 |
netapp.ontap.na_ontap_node: |
| 50 |
name: laurentncluster-2 |
| 51 |
location: SF1 |
| 52 |
asset_tag: mytag |
| 53 |
hostname: "{{ netapp_hostname }}" |
| 54 |
username: "{{ netapp_username }}" |
| 55 |
password: "{{ netapp_password }}" |
| 56 |
|
| 57 |
- name: Rename node |
| 58 |
netapp.ontap.na_ontap_node: |
| 59 |
hostname: "{{ netapp_hostname }}" |
| 60 |
username: "{{ netapp_username }}" |
| 61 |
password: "{{ netapp_password }}" |
| 62 |
from_name: laurentn-vsim1 |
| 63 |
name: laurentncluster-2 |
| 64 |
|
| 65 |
- name: Modify and rename node |
| 66 |
netapp.ontap.na_ontap_node: |
| 67 |
hostname: "{{ netapp_hostname }}" |
| 68 |
username: "{{ netapp_username }}" |
| 69 |
password: "{{ netapp_password }}" |
| 70 |
location: SF2 |
| 71 |
from_name: laurentn-vsim1 |
| 72 |
name: laurentncluster-2 |
| 73 |
""" |
| 74 |
|
| 75 |
RETURN = """ |
| 76 |
""" |
| 77 |
import traceback |
| 78 |
|
| 79 |
from ansible.module_utils.basic import AnsibleModule |
| 80 |
from ansible.module_utils._text import to_native |
| 81 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 82 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 83 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 84 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 85 |
import copy |
| 86 |
|
| 87 |
HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() |
| 88 |
|
| 89 |
|
| 90 |
class NetAppOntapNode(object): |
| 91 |
""" |
| 92 |
Rename and modify node |
| 93 |
""" |
| 94 |
|
| 95 |
def __init__(self): |
| 96 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 97 |
self.argument_spec.update(dict( |
| 98 |
name=dict(required=True, type='str'), |
| 99 |
from_name=dict(required=False, type='str'), |
| 100 |
location=dict(required=False, type='str'), |
| 101 |
asset_tag=dict(required=False, type='str'), |
| 102 |
)) |
| 103 |
|
| 104 |
self.module = AnsibleModule( |
| 105 |
argument_spec=self.argument_spec, |
| 106 |
supports_check_mode=True |
| 107 |
) |
| 108 |
|
| 109 |
self.na_helper = NetAppModule() |
| 110 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 111 |
self.rest_api = OntapRestAPI(self.module) |
| 112 |
|
| 113 |
# some attributes are not supported in REST implementation |
| 114 |
unsupported_rest_properties = ['asset_tag'] |
| 115 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties) |
| 116 |
if not self.use_rest: |
| 117 |
if HAS_NETAPP_LIB is False: |
| 118 |
self.module.fail_json(msg="the python NetApp-Lib module is required") |
| 119 |
else: |
| 120 |
self.cluster = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 121 |
return |
| 122 |
|
| 123 |
def update_node_details(self, uuid, modify): |
| 124 |
api = 'cluster/nodes/%s' % uuid |
| 125 |
data = {} |
| 126 |
if 'from_name' in self.parameters: |
| 127 |
data['name'] = self.parameters['name'] |
| 128 |
if 'location' in self.parameters: |
| 129 |
data['location'] = self.parameters['location'] |
| 130 |
if not data: |
| 131 |
self.module.fail_json(msg='Nothing to update in the modified attributes: %s' % modify) |
| 132 |
response, error = self.rest_api.patch(api, body=data) |
| 133 |
response, error = rrh.check_for_error_and_job_results(api, response, error, self.rest_api) |
| 134 |
if error: |
| 135 |
self.module.fail_json(msg='Error while modifying node details: %s' % error) |
| 136 |
|
| 137 |
def modify_node(self, modify=None, uuid=None): |
| 138 |
""" |
| 139 |
Modify an existing node |
| 140 |
:return: none |
| 141 |
""" |
| 142 |
if self.use_rest: |
| 143 |
self.update_node_details(uuid, modify) |
| 144 |
else: |
| 145 |
node_obj = netapp_utils.zapi.NaElement('system-node-modify') |
| 146 |
node_obj.add_new_child('node', self.parameters['name']) |
| 147 |
if 'location' in self.parameters: |
| 148 |
node_obj.add_new_child('node-location', self.parameters['location']) |
| 149 |
if 'asset_tag' in self.parameters: |
| 150 |
node_obj.add_new_child('node-asset-tag', self.parameters['asset_tag']) |
| 151 |
try: |
| 152 |
self.cluster.invoke_successfully(node_obj, True) |
| 153 |
except netapp_utils.zapi.NaApiError as error: |
| 154 |
self.module.fail_json(msg='Error modifying node: %s' % |
| 155 |
(to_native(error)), |
| 156 |
exception=traceback.format_exc()) |
| 157 |
|
| 158 |
def rename_node(self): |
| 159 |
""" |
| 160 |
Rename an existing node |
| 161 |
:return: none |
| 162 |
""" |
| 163 |
node_obj = netapp_utils.zapi.NaElement('system-node-rename') |
| 164 |
node_obj.add_new_child('node', self.parameters['from_name']) |
| 165 |
node_obj.add_new_child('new-name', self.parameters['name']) |
| 166 |
try: |
| 167 |
self.cluster.invoke_successfully(node_obj, True) |
| 168 |
except netapp_utils.zapi.NaApiError as error: |
| 169 |
self.module.fail_json(msg='Error renaming node: %s' % |
| 170 |
(to_native(error)), |
| 171 |
exception=traceback.format_exc()) |
| 172 |
|
| 173 |
def get_node(self, name): |
| 174 |
if self.use_rest: |
| 175 |
api = 'cluster/nodes' |
| 176 |
query = { |
| 177 |
'fields': 'name,uuid,location', |
| 178 |
'name': name |
| 179 |
} |
| 180 |
message, error = self.rest_api.get(api, query) |
| 181 |
node, error = rrh.check_for_0_or_1_records(api, message, error) |
| 182 |
if error: |
| 183 |
self.module.fail_json(msg='Error while fetching node details: %s' % error) |
| 184 |
if node: |
| 185 |
if 'location' not in message['records'][0]: |
| 186 |
node_location = '' |
| 187 |
else: |
| 188 |
node_location = message['records'][0]['location'] |
| 189 |
return dict( |
| 190 |
name=message['records'][0]['name'], |
| 191 |
uuid=message['records'][0]['uuid'], |
| 192 |
location=node_location) |
| 193 |
return None |
| 194 |
else: |
| 195 |
node_obj = netapp_utils.zapi.NaElement('system-node-get') |
| 196 |
node_obj.add_new_child('node', name) |
| 197 |
try: |
| 198 |
result = self.cluster.invoke_successfully(node_obj, True) |
| 199 |
except netapp_utils.zapi.NaApiError as error: |
| 200 |
if to_native(error.code) == "13115": |
| 201 |
# 13115 (EINVALIDINPUTERROR) if the node does not exist |
| 202 |
return None |
| 203 |
else: |
| 204 |
self.module.fail_json(msg=to_native( |
| 205 |
error), exception=traceback.format_exc()) |
| 206 |
attributes = result.get_child_by_name('attributes') |
| 207 |
if attributes is not None: |
| 208 |
node_info = attributes.get_child_by_name('node-details-info') |
| 209 |
node_location = node_info.get_child_content('node-location') |
| 210 |
node_location = node_location if node_location is not None else '' |
| 211 |
node_tag = node_info.get_child_content('node-tag') |
| 212 |
node_tag = node_tag if node_tag is not None else '' |
| 213 |
return dict( |
| 214 |
name=node_info['node'], |
| 215 |
location=node_location, |
| 216 |
asset_tag=node_tag) |
| 217 |
return None |
| 218 |
|
| 219 |
def apply(self): |
| 220 |
from_exists = None |
| 221 |
modify, modify_dict = None, None |
| 222 |
uuid = None |
| 223 |
current = self.get_node(self.parameters['name']) |
| 224 |
if current is None and 'from_name' in self.parameters: |
| 225 |
from_exists = self.get_node(self.parameters['from_name']) |
| 226 |
if from_exists is None: |
| 227 |
self.module.fail_json(msg='Node not found: %s' % self.parameters['from_name']) |
| 228 |
uuid = from_exists['uuid'] if 'uuid' in from_exists else None |
| 229 |
# since from_exists contains the node name, modify will at least contain the node name if a rename is required. |
| 230 |
modify = self.na_helper.get_modified_attributes(from_exists, self.parameters) |
| 231 |
elif current is not None: |
| 232 |
uuid = current['uuid'] if 'uuid' in current else None |
| 233 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 234 |
|
| 235 |
allowed_options = ['name', 'location'] |
| 236 |
if not self.use_rest: |
| 237 |
allowed_options.append('asset_tag') |
| 238 |
if modify: |
| 239 |
if any(x not in allowed_options for x in modify): |
| 240 |
self.module.fail_json(msg='Too many modified attributes found: %s, allowed: %s' % (modify, allowed_options)) |
| 241 |
modify_dict = copy.deepcopy(modify) |
| 242 |
if current is None and from_exists is None: |
| 243 |
msg = 'from_name: %s' % self.parameters.get('from_name') if 'from_name' in self.parameters \ |
| 244 |
else 'name: %s' % self.parameters['name'] |
| 245 |
self.module.fail_json(msg='Node not found: %s' % msg) |
| 246 |
if self.na_helper.changed: |
| 247 |
if not self.module.check_mode: |
| 248 |
if not self.use_rest: |
| 249 |
if 'name' in modify: |
| 250 |
self.rename_node() |
| 251 |
modify.pop('name') |
| 252 |
if modify: |
| 253 |
self.modify_node(modify, uuid) |
| 254 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modify_dict) |
| 255 |
self.module.exit_json(**result) |
| 256 |
|
| 257 |
|
| 258 |
def main(): |
| 259 |
""" |
| 260 |
Start, Stop and Enable node services. |
| 261 |
""" |
| 262 |
obj = NetAppOntapNode() |
| 263 |
obj.apply() |
| 264 |
|
| 265 |
|
| 266 |
if __name__ == '__main__': |
| 267 |
main() |