| 1 |
#!/usr/bin/python |
| 2 |
''' na_ontap_igroup |
| 3 |
|
| 4 |
(c) 2018-2026, NetApp, Inc |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
''' |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
|
| 14 |
module: na_ontap_igroup |
| 15 |
short_description: NetApp ONTAP iSCSI or FC igroup configuration |
| 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 |
|
| 21 |
description: |
| 22 |
- Create/Delete/Rename Igroups and Modify initiators belonging to an igroup |
| 23 |
|
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Whether the specified Igroup should exist or not. |
| 28 |
choices: ['present', 'absent'] |
| 29 |
type: str |
| 30 |
default: present |
| 31 |
|
| 32 |
name: |
| 33 |
description: |
| 34 |
- The name of the igroup to manage. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
|
| 38 |
initiator_group_type: |
| 39 |
description: |
| 40 |
- Type of the initiator group. |
| 41 |
- Required when C(state=present). |
| 42 |
choices: ['fcp', 'iscsi', 'mixed'] |
| 43 |
type: str |
| 44 |
aliases: ['protocol'] |
| 45 |
|
| 46 |
from_name: |
| 47 |
description: |
| 48 |
- Name of igroup to rename to name. |
| 49 |
version_added: 2.7.0 |
| 50 |
type: str |
| 51 |
|
| 52 |
os_type: |
| 53 |
description: |
| 54 |
- OS type of the initiators within the group. |
| 55 |
type: str |
| 56 |
aliases: ['ostype'] |
| 57 |
|
| 58 |
igroups: |
| 59 |
description: |
| 60 |
- List of igroups to be mapped to the igroup. |
| 61 |
- For a modify operation, this list replaces the existing igroups, or existing initiators. |
| 62 |
- This module does not add or remove specific igroup(s) in an igroup. |
| 63 |
- Mutually exclusive with initiator_names (initiators) and initiator_objects. |
| 64 |
- Requires ONTAP 9.9 or newer. |
| 65 |
type: list |
| 66 |
elements: str |
| 67 |
version_added: 21.3.0 |
| 68 |
|
| 69 |
initiator_names: |
| 70 |
description: |
| 71 |
- List of initiators to be mapped to the igroup. |
| 72 |
- WWPN, WWPN Alias, or iSCSI name of Initiator to add or remove. |
| 73 |
- For a modify operation, this list replaces the existing initiators, or existing igroups. |
| 74 |
- This module does not add or remove specific initiator(s) in an igroup. |
| 75 |
- Mutually exclusive with igroups and initiator_objects. |
| 76 |
- This serves the same purpose as initiator_objects, but without the comment suboption. |
| 77 |
aliases: |
| 78 |
- initiator |
| 79 |
- initiators |
| 80 |
type: list |
| 81 |
elements: str |
| 82 |
version_added: 21.4.0 |
| 83 |
|
| 84 |
initiator_objects: |
| 85 |
description: |
| 86 |
- List of initiators to be mapped to the igroup, with an optional comment field. |
| 87 |
- WWPN, WWPN Alias, or iSCSI name of Initiator to add or remove. |
| 88 |
- For a modify operation, this list replaces the existing initiators, or existing igroups. |
| 89 |
- This module does not add or remove specific initiator(s) in an igroup. |
| 90 |
- Mutually exclusive with initiator_names (initiators) and igroups. |
| 91 |
- Requires ONTAP 9.9 with REST support. |
| 92 |
type: list |
| 93 |
elements: dict |
| 94 |
suboptions: |
| 95 |
name: |
| 96 |
description: name of the initiator. |
| 97 |
type: str |
| 98 |
required: true |
| 99 |
comment: |
| 100 |
description: a more descriptive comment as the WWPN can be quite opaque. |
| 101 |
type: str |
| 102 |
version_added: 21.4.0 |
| 103 |
|
| 104 |
bind_portset: |
| 105 |
description: |
| 106 |
- Name of a current portset to bind to the newly created igroup. |
| 107 |
type: str |
| 108 |
|
| 109 |
force_remove_initiator: |
| 110 |
description: |
| 111 |
- Forcibly remove the initiator even if there are existing LUNs mapped to this initiator group. |
| 112 |
- This parameter should be used with caution. |
| 113 |
type: bool |
| 114 |
default: false |
| 115 |
aliases: ['allow_delete_while_mapped'] |
| 116 |
|
| 117 |
vserver: |
| 118 |
description: |
| 119 |
- The name of the vserver to use. |
| 120 |
required: true |
| 121 |
type: str |
| 122 |
|
| 123 |
lambda_config: |
| 124 |
description: |
| 125 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 126 |
- These option and suboptions are only supported with REST. |
| 127 |
type: dict |
| 128 |
version_added: 23.4.0 |
| 129 |
suboptions: |
| 130 |
function_name: |
| 131 |
description: |
| 132 |
- The name of the AWS Lambda function to invoke. |
| 133 |
type: str |
| 134 |
required: true |
| 135 |
aws_region: |
| 136 |
description: |
| 137 |
- The name of the AWS region. |
| 138 |
type: str |
| 139 |
required: true |
| 140 |
aws_profile: |
| 141 |
description: |
| 142 |
- The name of the AWS profile to use for authentication. |
| 143 |
type: str |
| 144 |
|
| 145 |
notes: |
| 146 |
- supports check mode. |
| 147 |
- supports ZAPI and REST. |
| 148 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 149 |
''' |
| 150 |
|
| 151 |
EXAMPLES = ''' |
| 152 |
- name: Create iSCSI Igroup |
| 153 |
netapp.ontap.na_ontap_igroup: |
| 154 |
state: present |
| 155 |
name: ansibleIgroup3 |
| 156 |
initiator_group_type: iscsi |
| 157 |
os_type: linux |
| 158 |
initiator_names: iqn.1994-05.com.redhat:scspa0395855001.rtp.openenglab.netapp.com,abc.com:redhat.com |
| 159 |
vserver: ansibleVServer |
| 160 |
hostname: "{{ netapp_hostname }}" |
| 161 |
username: "{{ netapp_username }}" |
| 162 |
password: "{{ netapp_password }}" |
| 163 |
|
| 164 |
- name: Create iSCSI Igroup - ONTAP 9.9 |
| 165 |
netapp.ontap.na_ontap_igroup: |
| 166 |
state: present |
| 167 |
name: ansibleIgroup3 |
| 168 |
initiator_group_type: iscsi |
| 169 |
os_type: linux |
| 170 |
initiator_objects: |
| 171 |
- name: iqn.1994-05.com.redhat:scspa0395855001.rtp.openenglab.netapp.com |
| 172 |
comment: for test only |
| 173 |
- name: abc.com:redhat.com |
| 174 |
vserver: ansibleVServer |
| 175 |
hostname: "{{ netapp_hostname }}" |
| 176 |
username: "{{ netapp_username }}" |
| 177 |
password: "{{ netapp_password }}" |
| 178 |
|
| 179 |
- name: Create FC Igroup |
| 180 |
netapp.ontap.na_ontap_igroup: |
| 181 |
state: present |
| 182 |
name: ansibleIgroup4 |
| 183 |
initiator_group_type: fcp |
| 184 |
os_type: linux |
| 185 |
initiator_names: 20:00:00:50:56:9f:19:82 |
| 186 |
vserver: ansibleVServer |
| 187 |
hostname: "{{ netapp_hostname }}" |
| 188 |
username: "{{ netapp_username }}" |
| 189 |
password: "{{ netapp_password }}" |
| 190 |
|
| 191 |
- name: Rename Igroup |
| 192 |
netapp.ontap.na_ontap_igroup: |
| 193 |
state: present |
| 194 |
from_name: ansibleIgroup3 |
| 195 |
name: testexamplenewname |
| 196 |
initiator_group_type: iscsi |
| 197 |
os_type: linux |
| 198 |
initiator_names: iqn.1994-05.com.redhat:scspa0395855001.rtp.openenglab.netapp.com |
| 199 |
vserver: ansibleVServer |
| 200 |
hostname: "{{ netapp_hostname }}" |
| 201 |
username: "{{ netapp_username }}" |
| 202 |
password: "{{ netapp_password }}" |
| 203 |
|
| 204 |
- name: Modify Igroup Initiators (replaces exisiting initiator_names) |
| 205 |
netapp.ontap.na_ontap_igroup: |
| 206 |
state: present |
| 207 |
name: ansibleIgroup3 |
| 208 |
initiator_group_type: iscsi |
| 209 |
os_type: linux |
| 210 |
initiator: iqn.1994-05.com.redhat:scspa0395855001.rtp.openenglab.netapp.com |
| 211 |
vserver: ansibleVServer |
| 212 |
hostname: "{{ netapp_hostname }}" |
| 213 |
username: "{{ netapp_username }}" |
| 214 |
password: "{{ netapp_password }}" |
| 215 |
|
| 216 |
- name: Delete Igroup |
| 217 |
netapp.ontap.na_ontap_igroup: |
| 218 |
state: absent |
| 219 |
name: ansibleIgroup3 |
| 220 |
vserver: ansibleVServer |
| 221 |
hostname: "{{ netapp_hostname }}" |
| 222 |
username: "{{ netapp_username }}" |
| 223 |
password: "{{ netapp_password }}" |
| 224 |
''' |
| 225 |
|
| 226 |
RETURN = ''' |
| 227 |
''' |
| 228 |
|
| 229 |
import traceback |
| 230 |
|
| 231 |
from ansible.module_utils.basic import AnsibleModule |
| 232 |
from ansible.module_utils._text import to_native |
| 233 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 234 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 235 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 236 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 237 |
|
| 238 |
|
| 239 |
class NetAppOntapIgroup: |
| 240 |
"""Create/Delete/Rename Igroups and Modify initiators list""" |
| 241 |
def __init__(self): |
| 242 |
|
| 243 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 244 |
self.argument_spec.update(dict( |
| 245 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 246 |
name=dict(required=True, type='str'), |
| 247 |
from_name=dict(required=False, type='str', default=None), |
| 248 |
os_type=dict(required=False, type='str', aliases=['ostype']), |
| 249 |
igroups=dict(required=False, type='list', elements='str'), |
| 250 |
initiator_group_type=dict(required=False, type='str', |
| 251 |
choices=['fcp', 'iscsi', 'mixed'], |
| 252 |
aliases=['protocol']), |
| 253 |
initiator_names=dict(required=False, type='list', elements='str', aliases=['initiator', 'initiators']), |
| 254 |
initiator_objects=dict(required=False, type='list', elements='dict', options=dict( |
| 255 |
name=dict(required=True, type='str'), |
| 256 |
comment=dict(type='str'), |
| 257 |
)), |
| 258 |
vserver=dict(required=True, type='str'), |
| 259 |
force_remove_initiator=dict(required=False, type='bool', default=False, aliases=['allow_delete_while_mapped']), |
| 260 |
bind_portset=dict(required=False, type='str') |
| 261 |
)) |
| 262 |
|
| 263 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 264 |
|
| 265 |
self.module = AnsibleModule( |
| 266 |
argument_spec=self.argument_spec, |
| 267 |
supports_check_mode=True, |
| 268 |
mutually_exclusive=[('igroups', 'initiator_names'), ('igroups', 'initiator_objects'), ('initiator_objects', 'initiator_names'), ], |
| 269 |
required_if=[ |
| 270 |
['use_lambda', True, ('lambda_config',)] |
| 271 |
], |
| 272 |
) |
| 273 |
|
| 274 |
self.na_helper = NetAppModule() |
| 275 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 276 |
self.rest_modify_zapi_to_rest = dict( |
| 277 |
# initiator_group_type (protocol) cannot be changed after create |
| 278 |
bind_portset='portset', |
| 279 |
name='name', |
| 280 |
os_type='os_type' |
| 281 |
) |
| 282 |
|
| 283 |
self.rest_api = OntapRestAPI(self.module) |
| 284 |
self.use_rest = self.rest_api.is_rest() |
| 285 |
|
| 286 |
# use the new structure, a list of dict with name/comment as keys. |
| 287 |
if self.parameters.get('initiator_names') is not None: |
| 288 |
self.parameters['initiator_objects'] = [ |
| 289 |
dict(name=initiator, comment=None) |
| 290 |
for initiator in self.parameters['initiator_names']] |
| 291 |
|
| 292 |
if self.parameters.get('initiator_objects') is not None: |
| 293 |
ontap_99_option = 'comment' |
| 294 |
if not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9) and\ |
| 295 |
any(x[ontap_99_option] is not None for x in self.parameters['initiator_objects']): |
| 296 |
msg = 'Error: in initiator_objects: %s' % self.rest_api.options_require_ontap_version(ontap_99_option, version='9.9', use_rest=self.use_rest) |
| 297 |
self.module.fail_json(msg=msg) |
| 298 |
# sanitize WWNs and convert to lowercase for idempotency |
| 299 |
self.parameters['initiator_objects'] = [ |
| 300 |
dict(name=self.na_helper.sanitize_wwn(initiator['name']), comment=initiator['comment']) |
| 301 |
for initiator in self.parameters['initiator_objects']] |
| 302 |
# keep a list of names as it is convenient for add and remove computations |
| 303 |
self.parameters['initiator_names'] = [initiator['name'] for initiator in self.parameters['initiator_objects']] |
| 304 |
|
| 305 |
def too_old_for_rest(minimum_generation, minimum_major): |
| 306 |
return self.use_rest and not self.rest_api.meets_rest_minimum_version(self.use_rest, minimum_generation, minimum_major, 0) |
| 307 |
|
| 308 |
ontap_99_options = ['bind_portset'] |
| 309 |
if too_old_for_rest(9, 9) and any(x in self.parameters for x in ontap_99_options): |
| 310 |
self.module.warn('Warning: falling back to ZAPI: %s' % self.rest_api.options_require_ontap_version(ontap_99_options, version='9.9')) |
| 311 |
self.use_rest = False |
| 312 |
|
| 313 |
ontap_99_options = ['igroups'] |
| 314 |
if not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9, 1) and any(x in self.parameters for x in ontap_99_options): |
| 315 |
self.module.fail_json(msg='Error: %s' % self.rest_api.options_require_ontap_version(ontap_99_options, version='9.9.1')) |
| 316 |
|
| 317 |
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9, 1): |
| 318 |
if 'igroups' in self.parameters: |
| 319 |
# we may need to remove existing initiators |
| 320 |
self.parameters['initiator_names'] = list() |
| 321 |
elif 'initiator_names' in self.parameters: |
| 322 |
# we may need to remove existing igroups |
| 323 |
self.parameters['igroups'] = list() |
| 324 |
|
| 325 |
if not self.use_rest: |
| 326 |
if self.parameters.get('use_lambda'): |
| 327 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 328 |
if not netapp_utils.has_netapp_lib(): |
| 329 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 330 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 331 |
|
| 332 |
def fail_on_error(self, error, stack=False): |
| 333 |
if error is None: |
| 334 |
return |
| 335 |
elements = dict(msg="Error: %s" % error) |
| 336 |
if stack: |
| 337 |
elements['stack'] = traceback.format_stack() |
| 338 |
self.module.fail_json(**elements) |
| 339 |
|
| 340 |
def get_igroup_rest(self, name): |
| 341 |
api = "protocols/san/igroups" |
| 342 |
fields = 'name,uuid,svm,initiators,os_type,protocol' |
| 343 |
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9, 1): |
| 344 |
fields += ',igroups' |
| 345 |
query = dict(name=name, fields=fields) |
| 346 |
query['svm.name'] = self.parameters['vserver'] |
| 347 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 348 |
self.fail_on_error(error) |
| 349 |
if record: |
| 350 |
try: |
| 351 |
igroup_details = dict( |
| 352 |
name=record['name'], |
| 353 |
uuid=record['uuid'], |
| 354 |
vserver=record['svm']['name'], |
| 355 |
os_type=record['os_type'], |
| 356 |
initiator_group_type=record['protocol'], |
| 357 |
name_to_uuid=dict() |
| 358 |
) |
| 359 |
except KeyError as exc: |
| 360 |
self.module.fail_json(msg='Error: unexpected igroup body: %s, KeyError on %s' % (str(record), str(exc))) |
| 361 |
igroup_details['name_to_key'] = {} |
| 362 |
for attr in ('igroups', 'initiators'): |
| 363 |
option = 'initiator_names' if attr == 'initiators' else attr |
| 364 |
if attr in record: |
| 365 |
igroup_details[option] = [item['name'] for item in record[attr]] |
| 366 |
if attr == 'initiators': |
| 367 |
igroup_details['initiator_objects'] = [dict(name=item['name'], comment=item.get('comment')) for item in record[attr]] |
| 368 |
# for initiators, there is no uuid, so we're using name as the key |
| 369 |
igroup_details['name_to_uuid'][option] = dict((item['name'], item.get('uuid', item['name'])) for item in record[attr]) |
| 370 |
else: |
| 371 |
igroup_details[option] = [] |
| 372 |
igroup_details['name_to_uuid'][option] = {} |
| 373 |
return igroup_details |
| 374 |
return None |
| 375 |
|
| 376 |
def get_igroup(self, name): |
| 377 |
""" |
| 378 |
Return details about the igroup |
| 379 |
:param: |
| 380 |
name : Name of the igroup |
| 381 |
|
| 382 |
:return: Details about the igroup. None if not found. |
| 383 |
:rtype: dict |
| 384 |
""" |
| 385 |
if self.use_rest: |
| 386 |
return self.get_igroup_rest(name) |
| 387 |
|
| 388 |
igroup_info = netapp_utils.zapi.NaElement('igroup-get-iter') |
| 389 |
attributes = dict(query={'initiator-group-info': {'initiator-group-name': name, |
| 390 |
'vserver': self.parameters['vserver']}}) |
| 391 |
igroup_info.translate_struct(attributes) |
| 392 |
current = None |
| 393 |
|
| 394 |
try: |
| 395 |
result = self.server.invoke_successfully(igroup_info, True) |
| 396 |
except netapp_utils.zapi.NaApiError as error: |
| 397 |
self.module.fail_json(msg='Error fetching igroup info %s: %s' % (self.parameters['name'], to_native(error)), |
| 398 |
exception=traceback.format_exc()) |
| 399 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 400 |
igroup_info = result.get_child_by_name('attributes-list') |
| 401 |
initiator_group_info = igroup_info.get_child_by_name('initiator-group-info') |
| 402 |
initiator_names = [] |
| 403 |
initiator_objects = [] |
| 404 |
if initiator_group_info.get_child_by_name('initiators'): |
| 405 |
current_initiators = initiator_group_info['initiators'].get_children() |
| 406 |
initiator_names = [initiator['initiator-name'] for initiator in current_initiators] |
| 407 |
initiator_objects = [dict(name=initiator['initiator-name'], comment=None) for initiator in current_initiators] |
| 408 |
current = { |
| 409 |
'initiator_names': initiator_names, |
| 410 |
'initiator_objects': initiator_objects, |
| 411 |
# place holder, not used for ZAPI |
| 412 |
'name_to_uuid': dict(initiator_names=dict()) |
| 413 |
} |
| 414 |
zapi_to_params = { |
| 415 |
'vserver': 'vserver', |
| 416 |
'initiator-group-os-type': 'os_type', |
| 417 |
'initiator-group-portset-name': 'bind_portset', |
| 418 |
'initiator-group-type': 'initiator_group_type' |
| 419 |
} |
| 420 |
for attr in zapi_to_params: |
| 421 |
value = igroup_info.get_child_content(attr) |
| 422 |
if value is not None: |
| 423 |
current[zapi_to_params[attr]] = value |
| 424 |
return current |
| 425 |
|
| 426 |
def check_option_is_valid(self, option): |
| 427 |
if self.use_rest and option in ('igroups', 'initiator_names'): |
| 428 |
return |
| 429 |
if option == 'initiator_names': |
| 430 |
return |
| 431 |
raise KeyError('check_option_is_valid: option=%s' % option) |
| 432 |
|
| 433 |
@staticmethod |
| 434 |
def get_rest_name_for_option(option): |
| 435 |
if option == 'initiator_names': |
| 436 |
return 'initiators' |
| 437 |
if option == 'igroups': |
| 438 |
return option |
| 439 |
raise KeyError('get_rest_name_for_option: option=%s' % option) |
| 440 |
|
| 441 |
def add_initiators_or_igroups_rest(self, uuid, option, names): |
| 442 |
self.check_option_is_valid(option) |
| 443 |
api = "protocols/san/igroups/%s/%s" % (uuid, self.get_rest_name_for_option(option)) |
| 444 |
if option == 'initiator_names' and self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9, 1): |
| 445 |
in_objects = self.parameters['initiator_objects'] |
| 446 |
records = [self.na_helper.filter_out_none_entries(item) for item in in_objects if item['name'] in names] |
| 447 |
else: |
| 448 |
records = [dict(name=name) for name in names] |
| 449 |
body = dict(records=records) |
| 450 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 451 |
self.fail_on_error(error) |
| 452 |
|
| 453 |
def modify_initiators_rest(self, uuid, initiator_objects): |
| 454 |
for initiator in initiator_objects: |
| 455 |
if 'comment' in initiator: |
| 456 |
api = "protocols/san/igroups/%s/initiators" % uuid |
| 457 |
body = dict(comment=initiator['comment']) |
| 458 |
dummy, error = rest_generic.patch_async(self.rest_api, api, initiator['name'], body) |
| 459 |
self.fail_on_error(error) |
| 460 |
|
| 461 |
def add_initiators_or_igroups(self, uuid, option, current_names): |
| 462 |
""" |
| 463 |
Add the list of desired initiators to igroup unless they are already set |
| 464 |
:return: None |
| 465 |
""" |
| 466 |
self.check_option_is_valid(option) |
| 467 |
# don't add if initiator_names/igroups is empty string |
| 468 |
if self.parameters.get(option) == [''] or self.parameters.get(option) is None: |
| 469 |
return |
| 470 |
names_to_add = [name for name in self.parameters[option] if name not in current_names] |
| 471 |
if self.use_rest and names_to_add: |
| 472 |
self.add_initiators_or_igroups_rest(uuid, option, names_to_add) |
| 473 |
else: |
| 474 |
for name in names_to_add: |
| 475 |
self.modify_initiator(name, 'igroup-add') |
| 476 |
|
| 477 |
def delete_initiator_or_igroup_rest(self, uuid, option, name_or_uuid): |
| 478 |
self.check_option_is_valid(option) |
| 479 |
api = "protocols/san/igroups/%s/%s" % (uuid, self.get_rest_name_for_option(option)) |
| 480 |
query = {'allow_delete_while_mapped': True} if self.parameters['force_remove_initiator'] else None |
| 481 |
dummy, error = rest_generic.delete_async(self.rest_api, api, name_or_uuid, query=query) |
| 482 |
self.fail_on_error(error) |
| 483 |
|
| 484 |
def remove_initiators_or_igroups(self, uuid, option, current_names, mapping): |
| 485 |
""" |
| 486 |
Removes current names from igroup unless they are still desired |
| 487 |
:return: None |
| 488 |
""" |
| 489 |
self.check_option_is_valid(option) |
| 490 |
for name in current_names: |
| 491 |
if name not in self.parameters.get(option, list()): |
| 492 |
if self.use_rest: |
| 493 |
self.delete_initiator_or_igroup_rest(uuid, option, mapping[name]) |
| 494 |
else: |
| 495 |
self.modify_initiator(name, 'igroup-remove') |
| 496 |
|
| 497 |
def modify_initiator(self, initiator, zapi): |
| 498 |
""" |
| 499 |
Add or remove an initiator to/from an igroup |
| 500 |
""" |
| 501 |
options = {'initiator-group-name': self.parameters['name'], |
| 502 |
'initiator': initiator} |
| 503 |
if zapi == 'igroup-remove' and self.parameters.get('force_remove_initiator'): |
| 504 |
options['force'] = 'true' |
| 505 |
|
| 506 |
igroup_modify = netapp_utils.zapi.NaElement.create_node_with_children(zapi, **options) |
| 507 |
|
| 508 |
try: |
| 509 |
self.server.invoke_successfully(igroup_modify, enable_tunneling=True) |
| 510 |
except netapp_utils.zapi.NaApiError as error: |
| 511 |
self.module.fail_json(msg='Error modifying igroup initiator %s: %s' % (self.parameters['name'], |
| 512 |
to_native(error)), |
| 513 |
exception=traceback.format_exc()) |
| 514 |
|
| 515 |
def create_igroup_rest(self): |
| 516 |
api = "protocols/san/igroups" |
| 517 |
body = dict( |
| 518 |
name=self.parameters['name'], |
| 519 |
os_type=self.parameters['os_type']) |
| 520 |
body['svm'] = dict(name=self.parameters['vserver']) |
| 521 |
mapping = dict( |
| 522 |
initiator_group_type='protocol', |
| 523 |
bind_portset='portset', |
| 524 |
igroups='igroups', |
| 525 |
initiator_objects='initiators' |
| 526 |
) |
| 527 |
for option in mapping: |
| 528 |
value = self.parameters.get(option) |
| 529 |
if value is not None: |
| 530 |
if option in ('igroups', 'initiator_objects'): |
| 531 |
# we may have an empty list, ignore it |
| 532 |
if option == 'initiator_objects': |
| 533 |
value = [self.na_helper.filter_out_none_entries(item) for item in value] if value else None |
| 534 |
else: |
| 535 |
value = [dict(name=name) for name in value] if value else None |
| 536 |
if value is not None: |
| 537 |
body[mapping[option]] = value |
| 538 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 539 |
self.fail_on_error(error) |
| 540 |
|
| 541 |
def create_igroup(self): |
| 542 |
""" |
| 543 |
Create the igroup. |
| 544 |
""" |
| 545 |
if self.use_rest: |
| 546 |
return self.create_igroup_rest() |
| 547 |
|
| 548 |
options = {'initiator-group-name': self.parameters['name']} |
| 549 |
if self.parameters.get('os_type') is not None: |
| 550 |
options['os-type'] = self.parameters['os_type'] |
| 551 |
if self.parameters.get('initiator_group_type') is not None: |
| 552 |
options['initiator-group-type'] = self.parameters['initiator_group_type'] |
| 553 |
if self.parameters.get('bind_portset') is not None: |
| 554 |
options['bind-portset'] = self.parameters['bind_portset'] |
| 555 |
|
| 556 |
igroup_create = netapp_utils.zapi.NaElement.create_node_with_children( |
| 557 |
'igroup-create', **options) |
| 558 |
|
| 559 |
try: |
| 560 |
self.server.invoke_successfully(igroup_create, |
| 561 |
enable_tunneling=True) |
| 562 |
except netapp_utils.zapi.NaApiError as error: |
| 563 |
self.module.fail_json(msg='Error provisioning igroup %s: %s' % (self.parameters['name'], to_native(error)), |
| 564 |
exception=traceback.format_exc()) |
| 565 |
self.add_initiators_or_igroups(None, 'initiator_names', []) |
| 566 |
|
| 567 |
@staticmethod |
| 568 |
def change_in_initiator_comments(modify, current): |
| 569 |
|
| 570 |
if 'initiator_objects' not in current: |
| 571 |
return list() |
| 572 |
|
| 573 |
# for python 2.6 |
| 574 |
comments = dict((item['name'], item['comment']) for item in current['initiator_objects']) |
| 575 |
|
| 576 |
def has_changed_comment(item): |
| 577 |
return item['name'] in comments and item['comment'] is not None and item['comment'] != comments[item['name']] |
| 578 |
|
| 579 |
return [item for item in modify['initiator_objects'] if has_changed_comment(item)] |
| 580 |
|
| 581 |
def modify_igroup_rest(self, uuid, modify): |
| 582 |
api = "protocols/san/igroups" |
| 583 |
body = dict() |
| 584 |
for option in modify: |
| 585 |
if option not in self.rest_modify_zapi_to_rest: |
| 586 |
self.module.fail_json(msg='Error: modifying %s is not supported in REST' % option) |
| 587 |
body[self.rest_modify_zapi_to_rest[option]] = modify[option] |
| 588 |
if body: |
| 589 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid, body) |
| 590 |
self.fail_on_error(error) |
| 591 |
|
| 592 |
def delete_igroup_rest(self, uuid): |
| 593 |
api = "protocols/san/igroups" |
| 594 |
query = {'allow_delete_while_mapped': True} if self.parameters['force_remove_initiator'] else None |
| 595 |
dummy, error = rest_generic.delete_async(self.rest_api, api, uuid, query=query) |
| 596 |
self.fail_on_error(error) |
| 597 |
|
| 598 |
def delete_igroup(self, uuid): |
| 599 |
""" |
| 600 |
Delete the igroup. |
| 601 |
""" |
| 602 |
if self.use_rest: |
| 603 |
return self.delete_igroup_rest(uuid) |
| 604 |
|
| 605 |
igroup_delete = netapp_utils.zapi.NaElement.create_node_with_children( |
| 606 |
'igroup-destroy', **{'initiator-group-name': self.parameters['name'], |
| 607 |
'force': 'true' if self.parameters['force_remove_initiator'] else 'false'}) |
| 608 |
|
| 609 |
try: |
| 610 |
self.server.invoke_successfully(igroup_delete, |
| 611 |
enable_tunneling=True) |
| 612 |
except netapp_utils.zapi.NaApiError as error: |
| 613 |
self.module.fail_json(msg='Error deleting igroup %s: %s' % (self.parameters['name'], to_native(error)), |
| 614 |
exception=traceback.format_exc()) |
| 615 |
|
| 616 |
def rename_igroup(self): |
| 617 |
""" |
| 618 |
Rename the igroup. |
| 619 |
""" |
| 620 |
if self.use_rest: |
| 621 |
self.module.fail_json(msg='Internal error, should not call rename, but use modify') |
| 622 |
|
| 623 |
igroup_rename = netapp_utils.zapi.NaElement.create_node_with_children( |
| 624 |
'igroup-rename', **{'initiator-group-name': self.parameters['from_name'], |
| 625 |
'initiator-group-new-name': str(self.parameters['name'])}) |
| 626 |
try: |
| 627 |
self.server.invoke_successfully(igroup_rename, |
| 628 |
enable_tunneling=True) |
| 629 |
except netapp_utils.zapi.NaApiError as error: |
| 630 |
self.module.fail_json(msg='Error renaming igroup %s: %s' % (self.parameters['name'], to_native(error)), |
| 631 |
exception=traceback.format_exc()) |
| 632 |
|
| 633 |
def report_error_in_modify(self, modify, context): |
| 634 |
if modify: |
| 635 |
if len(modify) > 1: |
| 636 |
tag = 'any of ' |
| 637 |
else: |
| 638 |
tag = '' |
| 639 |
self.module.fail_json(msg='Error: modifying %s %s is not supported in %s' % (tag, str(modify), context)) |
| 640 |
|
| 641 |
def validate_modify(self, modify): |
| 642 |
"""Identify options that cannot be modified for REST or ZAPI |
| 643 |
""" |
| 644 |
if not modify: |
| 645 |
return |
| 646 |
modify_local = dict(modify) |
| 647 |
modify_local.pop('igroups', None) |
| 648 |
modify_local.pop('initiator_names', None) |
| 649 |
modify_local.pop('initiator_objects', None) |
| 650 |
if not self.use_rest: |
| 651 |
self.report_error_in_modify(modify_local, 'ZAPI') |
| 652 |
return |
| 653 |
for option in modify: |
| 654 |
if option in self.rest_modify_zapi_to_rest: |
| 655 |
modify_local.pop(option) |
| 656 |
self.report_error_in_modify(modify_local, 'REST') |
| 657 |
|
| 658 |
def is_rename_action(self, cd_action, current): |
| 659 |
old = self.get_igroup(self.parameters['from_name']) |
| 660 |
rename = self.na_helper.is_rename_action(old, current) |
| 661 |
if rename is None: |
| 662 |
self.module.fail_json(msg='Error: igroup with from_name=%s not found' % self.parameters.get('from_name')) |
| 663 |
if rename: |
| 664 |
current = old |
| 665 |
cd_action = None |
| 666 |
return cd_action, rename, current |
| 667 |
|
| 668 |
def modify_igroup(self, uuid, current, modify): |
| 669 |
for attr in ('igroups', 'initiator_names'): |
| 670 |
if attr in current: |
| 671 |
# we need to remove everything first |
| 672 |
self.remove_initiators_or_igroups(uuid, attr, current[attr], current['name_to_uuid'][attr]) |
| 673 |
for attr in ('igroups', 'initiator_names'): |
| 674 |
if attr in current: |
| 675 |
self.add_initiators_or_igroups(uuid, attr, current[attr]) |
| 676 |
modify.pop(attr, None) |
| 677 |
if 'initiator_objects' in modify: |
| 678 |
if self.use_rest: |
| 679 |
# comments are not supported in ZAPI, we already checked for that in validate changes |
| 680 |
changed_initiator_objects = self.change_in_initiator_comments(modify, current) |
| 681 |
self.modify_initiators_rest(uuid, changed_initiator_objects) |
| 682 |
modify.pop('initiator_objects',) |
| 683 |
if modify: |
| 684 |
# validate_modify ensured modify is empty with ZAPI |
| 685 |
self.modify_igroup_rest(uuid, modify) |
| 686 |
|
| 687 |
def apply(self): |
| 688 |
uuid = None |
| 689 |
rename, modify = None, None |
| 690 |
current = self.get_igroup(self.parameters['name']) |
| 691 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 692 |
if cd_action == 'create' and self.parameters.get('from_name'): |
| 693 |
cd_action, rename, current = self.is_rename_action(cd_action, current) |
| 694 |
if cd_action is None and self.parameters['state'] == 'present': |
| 695 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 696 |
# a change in name is handled in rename for ZAPI, but REST can use modify |
| 697 |
if self.use_rest: |
| 698 |
rename = False |
| 699 |
else: |
| 700 |
modify.pop('name', None) |
| 701 |
if current and self.use_rest: |
| 702 |
uuid = current['uuid'] |
| 703 |
if cd_action == 'create' and self.use_rest and 'os_type' not in self.parameters: |
| 704 |
self.module.fail_json(msg='Error: os_type is a required parameter when creating an igroup with REST') |
| 705 |
saved_modify = str(modify) |
| 706 |
self.validate_modify(modify) |
| 707 |
|
| 708 |
if self.na_helper.changed and not self.module.check_mode: |
| 709 |
if rename: |
| 710 |
self.rename_igroup() |
| 711 |
elif cd_action == 'create': |
| 712 |
self.create_igroup() |
| 713 |
elif cd_action == 'delete': |
| 714 |
self.delete_igroup(uuid) |
| 715 |
if modify: |
| 716 |
self.modify_igroup(uuid, current, modify) |
| 717 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify=saved_modify) |
| 718 |
self.module.exit_json(**result) |
| 719 |
|
| 720 |
|
| 721 |
def main(): |
| 722 |
obj = NetAppOntapIgroup() |
| 723 |
obj.apply() |
| 724 |
|
| 725 |
|
| 726 |
if __name__ == '__main__': |
| 727 |
main() |