| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2026, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_ontap_export_policy |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
module: na_ontap_export_policy |
| 16 |
short_description: NetApp ONTAP manage export-policy |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: 2.6.0 |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
description: |
| 22 |
- Create or destroy or rename export-policies on ONTAP |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- Whether the specified export policy should exist or not. |
| 27 |
choices: ['present', 'absent'] |
| 28 |
type: str |
| 29 |
default: present |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the export-policy to manage. |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
from_name: |
| 36 |
description: |
| 37 |
- The name of the export-policy to be renamed. |
| 38 |
type: str |
| 39 |
version_added: 2.7.0 |
| 40 |
vserver: |
| 41 |
required: true |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- Name of the vserver to use. |
| 45 |
lambda_config: |
| 46 |
description: |
| 47 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 48 |
- These option and suboptions are only supported with REST. |
| 49 |
type: dict |
| 50 |
version_added: 23.5.0 |
| 51 |
suboptions: |
| 52 |
function_name: |
| 53 |
description: |
| 54 |
- The name of the AWS Lambda function to invoke. |
| 55 |
type: str |
| 56 |
required: true |
| 57 |
aws_region: |
| 58 |
description: |
| 59 |
- The name of the AWS region. |
| 60 |
type: str |
| 61 |
required: true |
| 62 |
aws_profile: |
| 63 |
description: |
| 64 |
- The name of the AWS profile to use for authentication. |
| 65 |
type: str |
| 66 |
|
| 67 |
notes: |
| 68 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 69 |
''' |
| 70 |
|
| 71 |
EXAMPLES = """ |
| 72 |
- name: Create Export Policy |
| 73 |
netapp.ontap.na_ontap_export_policy: |
| 74 |
state: present |
| 75 |
name: ansiblePolicyName |
| 76 |
vserver: vs_hack |
| 77 |
hostname: "{{ netapp_hostname }}" |
| 78 |
username: "{{ netapp_username }}" |
| 79 |
password: "{{ netapp_password }}" |
| 80 |
|
| 81 |
- name: Rename Export Policy |
| 82 |
netapp.ontap.na_ontap_export_policy: |
| 83 |
state: present |
| 84 |
from_name: ansiblePolicyName |
| 85 |
vserver: vs_hack |
| 86 |
name: newPolicyName |
| 87 |
hostname: "{{ netapp_hostname }}" |
| 88 |
username: "{{ netapp_username }}" |
| 89 |
password: "{{ netapp_password }}" |
| 90 |
|
| 91 |
- name: Delete Export Policy |
| 92 |
netapp.ontap.na_ontap_export_policy: |
| 93 |
state: absent |
| 94 |
name: ansiblePolicyName |
| 95 |
vserver: vs_hack |
| 96 |
hostname: "{{ netapp_hostname }}" |
| 97 |
username: "{{ netapp_username }}" |
| 98 |
password: "{{ netapp_password }}" |
| 99 |
""" |
| 100 |
|
| 101 |
RETURN = """ |
| 102 |
""" |
| 103 |
|
| 104 |
import traceback |
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
from ansible.module_utils._text import to_native |
| 107 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 108 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 109 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 110 |
|
| 111 |
HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() |
| 112 |
|
| 113 |
|
| 114 |
class NetAppONTAPExportPolicy(): |
| 115 |
""" |
| 116 |
Class with export policy methods |
| 117 |
""" |
| 118 |
|
| 119 |
def __init__(self): |
| 120 |
self.use_rest = False |
| 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', 'absent'], default='present'), |
| 124 |
name=dict(required=True, type='str'), |
| 125 |
from_name=dict(required=False, type='str', default=None), |
| 126 |
vserver=dict(required=True, type='str') |
| 127 |
)) |
| 128 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 129 |
self.module = AnsibleModule( |
| 130 |
argument_spec=self.argument_spec, |
| 131 |
supports_check_mode=True, |
| 132 |
required_if=[('use_lambda', True, ('lambda_config',))] |
| 133 |
) |
| 134 |
|
| 135 |
self.na_helper = NetAppModule() |
| 136 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 137 |
|
| 138 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 139 |
if self.parameters.get('use_lambda') and not self.rest_api.is_rest(): |
| 140 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 141 |
if self.rest_api.is_rest(): |
| 142 |
self.use_rest = True |
| 143 |
elif HAS_NETAPP_LIB is False: |
| 144 |
self.module.fail_json(msg="the python NetApp-Lib module is required") |
| 145 |
else: |
| 146 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 147 |
|
| 148 |
def get_export_policy(self, name=None): |
| 149 |
""" |
| 150 |
Return details about the export-policy |
| 151 |
:param: |
| 152 |
name : Name of the export-policy |
| 153 |
:return: Details about the export-policy. None if not found. |
| 154 |
:rtype: dict |
| 155 |
""" |
| 156 |
if name is None: |
| 157 |
name = self.parameters['name'] |
| 158 |
if self.use_rest: |
| 159 |
return self.get_export_policy_rest(name) |
| 160 |
else: |
| 161 |
export_policy_iter = netapp_utils.zapi.NaElement('export-policy-get-iter') |
| 162 |
export_policy_info = netapp_utils.zapi.NaElement('export-policy-info') |
| 163 |
export_policy_info.add_new_child('policy-name', name) |
| 164 |
export_policy_info.add_new_child('vserver', self.parameters['vserver']) |
| 165 |
query = netapp_utils.zapi.NaElement('query') |
| 166 |
query.add_child_elem(export_policy_info) |
| 167 |
export_policy_iter.add_child_elem(query) |
| 168 |
result = self.server.invoke_successfully(export_policy_iter, True) |
| 169 |
return_value = None |
| 170 |
# check if query returns the expected export-policy |
| 171 |
if result.get_child_by_name('num-records') and \ |
| 172 |
int(result.get_child_content('num-records')) == 1: |
| 173 |
export_policy = result.get_child_by_name('attributes-list').get_child_by_name( |
| 174 |
'export-policy-info').get_child_by_name('policy-name') |
| 175 |
return_value = { |
| 176 |
'policy-name': export_policy |
| 177 |
} |
| 178 |
return return_value |
| 179 |
|
| 180 |
def create_export_policy(self): |
| 181 |
""" |
| 182 |
Creates an export policy |
| 183 |
""" |
| 184 |
if self.use_rest: |
| 185 |
return self.create_export_policy_rest() |
| 186 |
export_policy_create = netapp_utils.zapi.NaElement.create_node_with_children( |
| 187 |
'export-policy-create', **{'policy-name': self.parameters['name']}) |
| 188 |
try: |
| 189 |
self.server.invoke_successfully(export_policy_create, |
| 190 |
enable_tunneling=True) |
| 191 |
except netapp_utils.zapi.NaApiError as error: |
| 192 |
self.module.fail_json(msg='Error on creating export-policy %s: %s' |
| 193 |
% (self.parameters['name'], to_native(error)), |
| 194 |
exception=traceback.format_exc()) |
| 195 |
|
| 196 |
def delete_export_policy(self, current): |
| 197 |
""" |
| 198 |
Delete export-policy |
| 199 |
""" |
| 200 |
if self.use_rest: |
| 201 |
return self.delete_export_policy_rest(current) |
| 202 |
export_policy_delete = netapp_utils.zapi.NaElement.create_node_with_children( |
| 203 |
'export-policy-destroy', **{'policy-name': self.parameters['name'], }) |
| 204 |
try: |
| 205 |
self.server.invoke_successfully(export_policy_delete, |
| 206 |
enable_tunneling=True) |
| 207 |
except netapp_utils.zapi.NaApiError as error: |
| 208 |
self.module.fail_json(msg='Error on deleting export-policy %s: %s' |
| 209 |
% (self.parameters['name'], |
| 210 |
to_native(error)), exception=traceback.format_exc()) |
| 211 |
|
| 212 |
def rename_export_policy(self): |
| 213 |
""" |
| 214 |
Rename the export-policy. |
| 215 |
""" |
| 216 |
export_policy_rename = netapp_utils.zapi.NaElement.create_node_with_children( |
| 217 |
'export-policy-rename', **{'policy-name': self.parameters['from_name'], |
| 218 |
'new-policy-name': self.parameters['name']}) |
| 219 |
try: |
| 220 |
self.server.invoke_successfully(export_policy_rename, |
| 221 |
enable_tunneling=True) |
| 222 |
except netapp_utils.zapi.NaApiError as error: |
| 223 |
self.module.fail_json(msg='Error on renaming export-policy %s:%s' |
| 224 |
% (self.parameters['name'], to_native(error)), |
| 225 |
exception=traceback.format_exc()) |
| 226 |
|
| 227 |
def get_export_policy_rest(self, name): |
| 228 |
options = {'fields': 'name,id', |
| 229 |
'svm.name': self.parameters['vserver'], |
| 230 |
'name': name} |
| 231 |
api = 'protocols/nfs/export-policies/' |
| 232 |
record, error = rest_generic.get_one_record(self.rest_api, api, options) |
| 233 |
if error: |
| 234 |
self.module.fail_json(msg="Error on fetching export policy: %s" % error) |
| 235 |
if record: |
| 236 |
return { |
| 237 |
'name': record['name'], |
| 238 |
'id': record['id'] |
| 239 |
} |
| 240 |
else: |
| 241 |
return record |
| 242 |
|
| 243 |
def create_export_policy_rest(self): |
| 244 |
params = {'name': self.parameters['name'], |
| 245 |
'svm.name': self.parameters['vserver']} |
| 246 |
api = 'protocols/nfs/export-policies' |
| 247 |
dummy, error = rest_generic.post_async(self.rest_api, api, params) |
| 248 |
if error is not None: |
| 249 |
self.module.fail_json(msg="Error on creating export policy: %s" % error) |
| 250 |
|
| 251 |
def delete_export_policy_rest(self, current): |
| 252 |
policy_id = current['id'] |
| 253 |
api = 'protocols/nfs/export-policies' |
| 254 |
dummy, error = rest_generic.delete_async(self.rest_api, api, policy_id) |
| 255 |
if error is not None: |
| 256 |
self.module.fail_json(msg=" Error on deleting export policy: %s" % error) |
| 257 |
|
| 258 |
def rename_export_policy_rest(self, current): |
| 259 |
policy_id = current['id'] |
| 260 |
params = {'name': self.parameters['name']} |
| 261 |
api = 'protocols/nfs/export-policies' |
| 262 |
dummy, error = rest_generic.patch_async(self.rest_api, api, policy_id, params) |
| 263 |
if error is not None: |
| 264 |
self.module.fail_json(msg="Error on renaming export policy: %s" % error) |
| 265 |
|
| 266 |
def apply(self): |
| 267 |
""" |
| 268 |
Apply action to export-policy |
| 269 |
""" |
| 270 |
modify, rename = None, None |
| 271 |
current = self.get_export_policy() |
| 272 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 273 |
if cd_action == 'create' and self.parameters.get('from_name'): |
| 274 |
current = self.get_export_policy(self.parameters['from_name']) |
| 275 |
if current is None: |
| 276 |
self.module.fail_json( |
| 277 |
msg="Error renaming: export policy %s does not exist" % self.parameters['from_name']) |
| 278 |
rename = True |
| 279 |
|
| 280 |
if self.na_helper.changed and not self.module.check_mode: |
| 281 |
if rename: |
| 282 |
modify = {'name': self.parameters['name']} |
| 283 |
if self.use_rest: |
| 284 |
self.rename_export_policy_rest(current) |
| 285 |
else: |
| 286 |
self.rename_export_policy() |
| 287 |
elif cd_action == 'create': |
| 288 |
self.create_export_policy() |
| 289 |
elif cd_action == 'delete': |
| 290 |
self.delete_export_policy(current) |
| 291 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 292 |
self.module.exit_json(**result) |
| 293 |
|
| 294 |
|
| 295 |
def main(): |
| 296 |
""" |
| 297 |
Execute action |
| 298 |
""" |
| 299 |
export_policy = NetAppONTAPExportPolicy() |
| 300 |
export_policy.apply() |
| 301 |
|
| 302 |
|
| 303 |
if __name__ == '__main__': |
| 304 |
main() |