| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# Copyright (c) 2020, Laurent Nicolas <[email protected]> |
| 8 |
# All rights reserved. |
| 9 |
# |
| 10 |
# Redistribution and use in source and binary forms, with or without modification, |
| 11 |
# are permitted provided that the following conditions are met: |
| 12 |
# |
| 13 |
# * Redistributions of source code must retain the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer. |
| 15 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
# this list of conditions and the following disclaimer in the documentation |
| 17 |
# and/or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 27 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
''' Support class for NetApp ansible modules |
| 30 |
|
| 31 |
Provides accesss to SVM (vserver) resources using ZAPI calls |
| 32 |
''' |
| 33 |
|
| 34 |
from __future__ import (absolute_import, division, print_function) |
| 35 |
__metaclass__ = type |
| 36 |
|
| 37 |
import traceback |
| 38 |
|
| 39 |
from ansible.module_utils._text import to_native |
| 40 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 41 |
|
| 42 |
|
| 43 |
def get_vserver(svm_cx, vserver_name): |
| 44 |
""" |
| 45 |
Return vserver information. |
| 46 |
|
| 47 |
:return: |
| 48 |
vserver object if vserver found |
| 49 |
None if vserver is not found |
| 50 |
:rtype: object/None |
| 51 |
""" |
| 52 |
vserver_info = netapp_utils.zapi.NaElement('vserver-get-iter') |
| 53 |
query_details = netapp_utils.zapi.NaElement.create_node_with_children( |
| 54 |
'vserver-info', **{'vserver-name': vserver_name}) |
| 55 |
|
| 56 |
query = netapp_utils.zapi.NaElement('query') |
| 57 |
query.add_child_elem(query_details) |
| 58 |
vserver_info.add_child_elem(query) |
| 59 |
|
| 60 |
result = svm_cx.invoke_successfully(vserver_info, enable_tunneling=False) |
| 61 |
vserver_details = None |
| 62 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 63 |
attributes_list = result.get_child_by_name('attributes-list') |
| 64 |
vserver_info = attributes_list.get_child_by_name('vserver-info') |
| 65 |
aggr_list = [] |
| 66 |
# vserver aggr-list can be empty by default |
| 67 |
get_list = vserver_info.get_child_by_name('aggr-list') |
| 68 |
if get_list is not None: |
| 69 |
aggregates = get_list.get_children() |
| 70 |
aggr_list.extend(aggr.get_content() for aggr in aggregates) |
| 71 |
protocols = [] |
| 72 |
# allowed-protocols is not empty for data SVM, but is for node SVM |
| 73 |
allowed_protocols = vserver_info.get_child_by_name('allowed-protocols') |
| 74 |
if allowed_protocols is not None: |
| 75 |
get_protocols = allowed_protocols.get_children() |
| 76 |
protocols.extend(protocol.get_content() for protocol in get_protocols) |
| 77 |
vserver_details = {'name': vserver_info.get_child_content('vserver-name'), |
| 78 |
'root_volume': vserver_info.get_child_content('root-volume'), |
| 79 |
'root_volume_aggregate': vserver_info.get_child_content('root-volume-aggregate'), |
| 80 |
'root_volume_security_style': vserver_info.get_child_content('root-volume-security-style'), |
| 81 |
'subtype': vserver_info.get_child_content('vserver-subtype'), |
| 82 |
'aggr_list': aggr_list, |
| 83 |
'language': vserver_info.get_child_content('language'), |
| 84 |
'quota_policy': vserver_info.get_child_content('quota-policy'), |
| 85 |
'snapshot_policy': vserver_info.get_child_content('snapshot-policy'), |
| 86 |
'allowed_protocols': protocols, |
| 87 |
'ipspace': vserver_info.get_child_content('ipspace'), |
| 88 |
'comment': vserver_info.get_child_content('comment'), |
| 89 |
'max_volumes': vserver_info.get_child_content('max-volumes')} |
| 90 |
|
| 91 |
return vserver_details |
| 92 |
|
| 93 |
|
| 94 |
def modify_vserver(svm_cx, module, name, modify, parameters=None): |
| 95 |
''' |
| 96 |
Modify vserver. |
| 97 |
:param name: vserver name |
| 98 |
:param modify: list of modify attributes |
| 99 |
:param parameters: customer original inputs |
| 100 |
modify only contains the difference between the customer inputs and current |
| 101 |
for some attributes, it may be safer to apply the original inputs |
| 102 |
''' |
| 103 |
if parameters is None: |
| 104 |
parameters = modify |
| 105 |
|
| 106 |
vserver_modify = netapp_utils.zapi.NaElement('vserver-modify') |
| 107 |
vserver_modify.add_new_child('vserver-name', name) |
| 108 |
for attribute in modify: |
| 109 |
if attribute == 'comment': |
| 110 |
vserver_modify.add_new_child('comment', parameters['comment']) |
| 111 |
if attribute == 'language': |
| 112 |
vserver_modify.add_new_child('language', parameters['language']) |
| 113 |
if attribute == 'quota_policy': |
| 114 |
vserver_modify.add_new_child('quota-policy', parameters['quota_policy']) |
| 115 |
if attribute == 'snapshot_policy': |
| 116 |
vserver_modify.add_new_child('snapshot-policy', parameters['snapshot_policy']) |
| 117 |
if attribute == 'max_volumes': |
| 118 |
vserver_modify.add_new_child('max-volumes', parameters['max_volumes']) |
| 119 |
if attribute == 'allowed_protocols': |
| 120 |
allowed_protocols = netapp_utils.zapi.NaElement('allowed-protocols') |
| 121 |
for protocol in parameters['allowed_protocols']: |
| 122 |
allowed_protocols.add_new_child('protocol', protocol) |
| 123 |
vserver_modify.add_child_elem(allowed_protocols) |
| 124 |
if attribute == 'aggr_list': |
| 125 |
aggregates = netapp_utils.zapi.NaElement('aggr-list') |
| 126 |
for aggr in parameters['aggr_list']: |
| 127 |
aggregates.add_new_child('aggr-name', aggr) |
| 128 |
vserver_modify.add_child_elem(aggregates) |
| 129 |
try: |
| 130 |
svm_cx.invoke_successfully(vserver_modify, enable_tunneling=False) |
| 131 |
except netapp_utils.zapi.NaApiError as exc: |
| 132 |
module.fail_json(msg='Error modifying SVM %s: %s' % (name, to_native(exc)), |
| 133 |
exception=traceback.format_exc()) |