| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
''' |
| 10 |
na_ontap_net_port |
| 11 |
''' |
| 12 |
|
| 13 |
DOCUMENTATION = """ |
| 14 |
module: na_ontap_net_port |
| 15 |
short_description: NetApp ONTAP network ports. |
| 16 |
extends_documentation_fragment: |
| 17 |
- netapp.ontap.netapp.na_ontap |
| 18 |
version_added: 2.6.0 |
| 19 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 20 |
description: |
| 21 |
- Modify a ONTAP network port. |
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- Whether the specified net port should exist or not. |
| 26 |
choices: ['present'] |
| 27 |
type: str |
| 28 |
default: present |
| 29 |
node: |
| 30 |
description: |
| 31 |
- Specifies the name of node. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
ports: |
| 35 |
aliases: |
| 36 |
- port |
| 37 |
description: |
| 38 |
- Specifies the name of port(s). |
| 39 |
required: true |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
mtu: |
| 43 |
description: |
| 44 |
- Specifies the maximum transmission unit (MTU) reported by the port. |
| 45 |
- Not supported with REST. |
| 46 |
type: int |
| 47 |
autonegotiate_admin: |
| 48 |
description: |
| 49 |
- Enables or disables Ethernet auto-negotiation of speed, |
| 50 |
duplex and flow control. |
| 51 |
- Not supported with REST. |
| 52 |
type: bool |
| 53 |
duplex_admin: |
| 54 |
description: |
| 55 |
- Specifies the user preferred duplex setting of the port. |
| 56 |
- Valid values auto, half, full |
| 57 |
- Not supported with REST. |
| 58 |
type: str |
| 59 |
speed_admin: |
| 60 |
description: |
| 61 |
- Specifies the user preferred speed setting of the port. |
| 62 |
- Not supported with REST. |
| 63 |
type: str |
| 64 |
flowcontrol_admin: |
| 65 |
description: |
| 66 |
- Specifies the user preferred flow control setting of the port. |
| 67 |
- Supported with REST for 9.16.1 and later. |
| 68 |
choices: ['none', 'receive', 'send', 'full', 'pfc'] |
| 69 |
type: str |
| 70 |
ipspace: |
| 71 |
description: |
| 72 |
- Specifies the port's associated IPspace name. |
| 73 |
- The 'Cluster' ipspace is reserved for cluster ports. |
| 74 |
- use netapp.ontap.na_ontap_ports to modify ipspace with REST. |
| 75 |
type: str |
| 76 |
up_admin: |
| 77 |
description: |
| 78 |
- Enables or disables the port. |
| 79 |
type: bool |
| 80 |
version_added: 21.8.0 |
| 81 |
""" |
| 82 |
|
| 83 |
EXAMPLES = """ |
| 84 |
- name: Modify Net Port |
| 85 |
netapp.ontap.na_ontap_net_port: |
| 86 |
state: present |
| 87 |
username: "{{ netapp_username }}" |
| 88 |
password: "{{ netapp_password }}" |
| 89 |
hostname: "{{ netapp_hostname }}" |
| 90 |
node: "{{ node_name }}" |
| 91 |
ports: e0d,e0c |
| 92 |
autonegotiate_admin: true |
| 93 |
up_admin: true |
| 94 |
mtu: 1500 |
| 95 |
flowcontrol_admin: full |
| 96 |
ipspace: test_ipspace |
| 97 |
""" |
| 98 |
|
| 99 |
RETURN = """ |
| 100 |
|
| 101 |
""" |
| 102 |
import traceback |
| 103 |
|
| 104 |
from ansible.module_utils.basic import AnsibleModule |
| 105 |
from ansible.module_utils._text import to_native |
| 106 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 107 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 108 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 109 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 110 |
|
| 111 |
|
| 112 |
class NetAppOntapNetPort: |
| 113 |
""" |
| 114 |
Modify a Net port |
| 115 |
""" |
| 116 |
|
| 117 |
def __init__(self): |
| 118 |
""" |
| 119 |
Initialize the Ontap Net Port Class |
| 120 |
""" |
| 121 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 122 |
self.argument_spec.update(dict( |
| 123 |
state=dict(required=False, type='str', choices=['present'], default='present'), |
| 124 |
node=dict(required=True, type="str"), |
| 125 |
ports=dict(required=True, type='list', elements='str', aliases=['port']), |
| 126 |
mtu=dict(required=False, type="int", default=None), |
| 127 |
autonegotiate_admin=dict(required=False, type="bool", default=None), |
| 128 |
up_admin=dict(required=False, type="bool", default=None), |
| 129 |
duplex_admin=dict(required=False, type="str", default=None), |
| 130 |
speed_admin=dict(required=False, type="str", default=None), |
| 131 |
flowcontrol_admin=dict(required=False, type="str", choices=['none', 'receive', 'send', 'full', 'pfc'], default=None), |
| 132 |
ipspace=dict(required=False, type="str", default=None) |
| 133 |
)) |
| 134 |
|
| 135 |
self.module = AnsibleModule( |
| 136 |
argument_spec=self.argument_spec, |
| 137 |
supports_check_mode=True |
| 138 |
) |
| 139 |
|
| 140 |
self.na_helper = NetAppModule() |
| 141 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 142 |
|
| 143 |
# Set up Rest API |
| 144 |
self.rest_api = OntapRestAPI(self.module) |
| 145 |
unsupported_rest_properties = ['mtu', 'autonegotiate_admin', 'duplex_admin', 'speed_admin'] |
| 146 |
partially_supported_rest_properties = [['flowcontrol_admin', (9, 16, 1)]] |
| 147 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties, partially_supported_rest_properties) |
| 148 |
if not self.use_rest: |
| 149 |
if not netapp_utils.has_netapp_lib(): |
| 150 |
self.module.fail_json(msg="the python NetApp-Lib module is required") |
| 151 |
else: |
| 152 |
self.set_playbook_zapi_key_map() |
| 153 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 154 |
|
| 155 |
def set_playbook_zapi_key_map(self): |
| 156 |
self.na_helper.zapi_string_keys = { |
| 157 |
'duplex_admin': 'administrative-duplex', |
| 158 |
'speed_admin': 'administrative-speed', |
| 159 |
'flowcontrol_admin': 'administrative-flowcontrol', |
| 160 |
'ipspace': 'ipspace' |
| 161 |
} |
| 162 |
self.na_helper.zapi_bool_keys = { |
| 163 |
'up_admin': 'is-administrative-up', |
| 164 |
'autonegotiate_admin': 'is-administrative-auto-negotiate', |
| 165 |
} |
| 166 |
self.na_helper.zapi_int_keys = { |
| 167 |
'mtu': 'mtu', |
| 168 |
} |
| 169 |
|
| 170 |
def get_net_port(self, port): |
| 171 |
""" |
| 172 |
Return details about the net port |
| 173 |
:param: port: Name of the port |
| 174 |
:return: Dictionary with current state of the port. None if not found. |
| 175 |
:rtype: dict |
| 176 |
""" |
| 177 |
if self.use_rest: |
| 178 |
return self.get_net_port_rest(port) |
| 179 |
net_port_get = netapp_utils.zapi.NaElement('net-port-get-iter') |
| 180 |
attributes = { |
| 181 |
'query': { |
| 182 |
'net-port-info': { |
| 183 |
'node': self.parameters['node'], |
| 184 |
'port': port |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
net_port_get.translate_struct(attributes) |
| 189 |
|
| 190 |
try: |
| 191 |
result = self.server.invoke_successfully(net_port_get, True) |
| 192 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 193 |
port_info = result['attributes-list']['net-port-info'] |
| 194 |
port_details = dict() |
| 195 |
else: |
| 196 |
return None |
| 197 |
except netapp_utils.zapi.NaApiError as error: |
| 198 |
self.module.fail_json(msg='Error getting net ports for %s: %s' % (self.parameters['node'], to_native(error)), |
| 199 |
exception=traceback.format_exc()) |
| 200 |
|
| 201 |
for item_key, zapi_key in self.na_helper.zapi_bool_keys.items(): |
| 202 |
port_details[item_key] = self.na_helper.get_value_for_bool(from_zapi=True, value=port_info.get_child_content(zapi_key)) |
| 203 |
for item_key, zapi_key in self.na_helper.zapi_int_keys.items(): |
| 204 |
port_details[item_key] = self.na_helper.get_value_for_int(from_zapi=True, value=port_info.get_child_content(zapi_key)) |
| 205 |
for item_key, zapi_key in self.na_helper.zapi_string_keys.items(): |
| 206 |
port_details[item_key] = port_info.get_child_content(zapi_key) |
| 207 |
return port_details |
| 208 |
|
| 209 |
def get_net_port_rest(self, port): |
| 210 |
api = 'network/ethernet/ports' |
| 211 |
query = { |
| 212 |
'name': port, |
| 213 |
'node.name': self.parameters['node'], |
| 214 |
'fields': 'name,node,uuid,enabled,broadcast_domain.ipspace.name,' |
| 215 |
} |
| 216 |
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 16, 1): |
| 217 |
query['fields'] += 'flowcontrol_admin,' |
| 218 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 219 |
if error: |
| 220 |
self.module.fail_json(msg=error) |
| 221 |
if record: |
| 222 |
current = { |
| 223 |
'node': self.na_helper.safe_get(record, ['node', 'name']), |
| 224 |
'uuid': self.na_helper.safe_get(record, ['uuid']), |
| 225 |
'up_admin': self.na_helper.safe_get(record, ['enabled']), |
| 226 |
'flowcontrol_admin': self.na_helper.safe_get(record, ['flowcontrol_admin']), |
| 227 |
'ipspace': self.na_helper.safe_get(record, ['broadcast_domain', 'ipspace', 'name']), |
| 228 |
} |
| 229 |
return current |
| 230 |
return None |
| 231 |
|
| 232 |
def modify_net_port(self, port, modify): |
| 233 |
""" |
| 234 |
Modify a port |
| 235 |
|
| 236 |
:param port: Name of the port |
| 237 |
:param modify: dict with attributes to be modified |
| 238 |
:return: None |
| 239 |
""" |
| 240 |
if self.use_rest: |
| 241 |
return self.modify_net_port_rest(port, modify) |
| 242 |
|
| 243 |
def get_zapi_key_and_value(key, value): |
| 244 |
zapi_key = self.na_helper.zapi_string_keys.get(key) |
| 245 |
if zapi_key is not None: |
| 246 |
return zapi_key, value |
| 247 |
zapi_key = self.na_helper.zapi_bool_keys.get(key) |
| 248 |
if zapi_key is not None: |
| 249 |
return zapi_key, self.na_helper.get_value_for_bool(from_zapi=False, value=value) |
| 250 |
zapi_key = self.na_helper.zapi_int_keys.get(key) |
| 251 |
if zapi_key is not None: |
| 252 |
return zapi_key, self.na_helper.get_value_for_int(from_zapi=False, value=value) |
| 253 |
raise KeyError(key) |
| 254 |
|
| 255 |
port_modify = netapp_utils.zapi.NaElement('net-port-modify') |
| 256 |
port_attributes = {'node': self.parameters['node'], 'port': port} |
| 257 |
for key, value in modify.items(): |
| 258 |
zapi_key, value = get_zapi_key_and_value(key, value) |
| 259 |
port_attributes[zapi_key] = value |
| 260 |
port_modify.translate_struct(port_attributes) |
| 261 |
try: |
| 262 |
self.server.invoke_successfully(port_modify, enable_tunneling=True) |
| 263 |
except netapp_utils.zapi.NaApiError as error: |
| 264 |
self.module.fail_json(msg='Error modifying net ports for %s: %s' % (self.parameters['node'], to_native(error)), |
| 265 |
exception=traceback.format_exc()) |
| 266 |
|
| 267 |
def modify_net_port_rest(self, uuid, modify): |
| 268 |
""" |
| 269 |
Modify broadcast domain, ipspace and enable/disable port |
| 270 |
""" |
| 271 |
api = 'network/ethernet/ports' |
| 272 |
body = {} |
| 273 |
if 'up_admin' in modify: |
| 274 |
body['enabled'] = modify['up_admin'] |
| 275 |
if 'flowcontrol_admin' in modify: |
| 276 |
body['flowcontrol_admin'] = modify['flowcontrol_admin'] |
| 277 |
if 'ipspace' in modify: |
| 278 |
body['broadcast_domain.ipspace.name'] = modify['ipspace'] |
| 279 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid, body) |
| 280 |
if error: |
| 281 |
self.module.fail_json(msg=error) |
| 282 |
|
| 283 |
def apply(self): |
| 284 |
""" |
| 285 |
Run Module based on play book |
| 286 |
""" |
| 287 |
# Run the task for all ports in the list of 'ports' |
| 288 |
missing_ports = list() |
| 289 |
modified = dict() |
| 290 |
for port in self.parameters['ports']: |
| 291 |
current = self.get_net_port(port) |
| 292 |
if current is None: |
| 293 |
missing_ports.append(port) |
| 294 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 295 |
modified[port] = modify |
| 296 |
if modify and not self.module.check_mode: |
| 297 |
port = current['uuid'] if self.use_rest else port |
| 298 |
self.modify_net_port(port, modify) |
| 299 |
if missing_ports: |
| 300 |
plural, suffix = '', '.' |
| 301 |
if len(missing_ports) == len(self.parameters['ports']): |
| 302 |
suffix = ' - check node name.' |
| 303 |
if len(missing_ports) > 1: |
| 304 |
plural = 's' |
| 305 |
self.module.fail_json(changed=self.na_helper.changed, modify=modified, |
| 306 |
msg='Error: port%s: %s not found on node: %s%s' |
| 307 |
% (plural, ', '.join(missing_ports), self.parameters['node'], suffix)) |
| 308 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modified) |
| 309 |
self.module.exit_json(**result) |
| 310 |
|
| 311 |
|
| 312 |
def main(): |
| 313 |
""" |
| 314 |
Create the NetApp Ontap Net Port Object and modify it |
| 315 |
""" |
| 316 |
obj = NetAppOntapNetPort() |
| 317 |
obj.apply() |
| 318 |
|
| 319 |
|
| 320 |
if __name__ == '__main__': |
| 321 |
main() |