| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: NetApp, Inc |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
module: na_ontap_cifs_unix_symlink_mapping |
| 12 |
short_description: NetApp ONTAP module to manage UNIX symbolic link mapping for CIFS clients. |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap_rest |
| 15 |
version_added: '22.9.0' |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
description: |
| 18 |
- Create/ modify/ delete a UNIX symbolic link mapping for a CIFS client. |
| 19 |
options: |
| 20 |
state: |
| 21 |
description: |
| 22 |
- Whether the specified symlink mapping should exist or not. |
| 23 |
choices: ['present', 'absent'] |
| 24 |
type: str |
| 25 |
default: present |
| 26 |
|
| 27 |
vserver: |
| 28 |
description: |
| 29 |
- Name of the vserver to use. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
|
| 33 |
unix_path: |
| 34 |
description: |
| 35 |
- Specifies the UNIX path prefix to be matched for the mapping. |
| 36 |
- It must begin and end with a forward slash (/). |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
|
| 40 |
share_name: |
| 41 |
description: |
| 42 |
- Specifies the CIFS share name on the destination CIFS server to which the UNIX symbolic link is pointing. |
| 43 |
type: str |
| 44 |
|
| 45 |
cifs_server: |
| 46 |
description: |
| 47 |
- Specifies the destination CIFS server (DNS name, IP address, or NetBIOS name). |
| 48 |
- This field is mandatory if the locality of the symbolic link is 'widelink'. |
| 49 |
type: str |
| 50 |
|
| 51 |
cifs_path: |
| 52 |
description: |
| 53 |
- Specifies the CIFS path on the destination to which the symbolic link maps. |
| 54 |
- Note that this value is specified by using a UNIX-style path. It must begin and end with a forward slash (/). |
| 55 |
type: str |
| 56 |
|
| 57 |
locality: |
| 58 |
description: |
| 59 |
- Specifies whether the CIFS symbolic link is a local link or wide link. The default setting is local. |
| 60 |
- The following values are supported |
| 61 |
local - Local symbolic link maps only to the same CIFS share. |
| 62 |
widelink - Wide symbolic link maps to any CIFS share on the network. |
| 63 |
type: str |
| 64 |
choices: ['local', 'widelink'] |
| 65 |
default: 'local' |
| 66 |
|
| 67 |
home_directory: |
| 68 |
description: |
| 69 |
- Specify if the destination share is a home directory. The default value is false. |
| 70 |
type: bool |
| 71 |
default: False |
| 72 |
|
| 73 |
lambda_config: |
| 74 |
description: |
| 75 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 76 |
- These option and suboptions are only supported with REST. |
| 77 |
type: dict |
| 78 |
version_added: 23.4.0 |
| 79 |
suboptions: |
| 80 |
function_name: |
| 81 |
description: |
| 82 |
- The name of the AWS Lambda function to invoke. |
| 83 |
type: str |
| 84 |
required: true |
| 85 |
aws_region: |
| 86 |
description: |
| 87 |
- The name of the AWS region. |
| 88 |
type: str |
| 89 |
required: true |
| 90 |
aws_profile: |
| 91 |
description: |
| 92 |
- The name of the AWS profile to use for authentication. |
| 93 |
type: str |
| 94 |
|
| 95 |
notes: |
| 96 |
- Only supported with REST and requires ONTAP 9.6 or later. |
| 97 |
- Supports AWS Lambda proxy functionality. See README for example usage. |
| 98 |
|
| 99 |
""" |
| 100 |
|
| 101 |
EXAMPLES = """ |
| 102 |
- name: Create a UNIX symlink mapping for CIFS share |
| 103 |
netapp.ontap.na_ontap_cifs_unix_symlink_mapping: |
| 104 |
state: present |
| 105 |
vserver: "{{ svm }}" |
| 106 |
unix_path: "/example1/" |
| 107 |
share_name: "share1" |
| 108 |
cifs_path: "/path1/test_dir/" |
| 109 |
cifs_server: "CIFS" |
| 110 |
hostname: "{{ netapp_hostname }}" |
| 111 |
username: "{{ netapp_username }}" |
| 112 |
password: "{{ netapp_password }}" |
| 113 |
https: true |
| 114 |
validate_certs: "{{ validate_certs }}" |
| 115 |
|
| 116 |
- name: Update a specific UNIX symlink mapping for a SVM |
| 117 |
netapp.ontap.na_ontap_cifs_unix_symlink_mapping: |
| 118 |
state: present |
| 119 |
vserver: "{{ svm }}" |
| 120 |
unix_path: "/example1/" |
| 121 |
share_name: "share2" |
| 122 |
cifs_path: "/path2/test_dir/" |
| 123 |
cifs_server: "CIFS" |
| 124 |
locality: "widelink" |
| 125 |
hostname: "{{ netapp_hostname }}" |
| 126 |
username: "{{ netapp_username }}" |
| 127 |
password: "{{ netapp_password }}" |
| 128 |
https: true |
| 129 |
validate_certs: "{{ validate_certs }}" |
| 130 |
|
| 131 |
- name: Remove a specific UNIX symlink mapping for a SVM |
| 132 |
netapp.ontap.na_ontap_cifs_unix_symlink_mapping: |
| 133 |
state: absent |
| 134 |
vserver: "{{ svm }}" |
| 135 |
unix_path: "/example1/" |
| 136 |
hostname: "{{ netapp_hostname }}" |
| 137 |
username: "{{ netapp_username }}" |
| 138 |
password: "{{ netapp_password }}" |
| 139 |
https: true |
| 140 |
validate_certs: "{{ validate_certs }}" |
| 141 |
""" |
| 142 |
|
| 143 |
RETURN = """ |
| 144 |
""" |
| 145 |
|
| 146 |
import traceback |
| 147 |
from ansible.module_utils.basic import AnsibleModule |
| 148 |
from ansible.module_utils._text import to_native |
| 149 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 150 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 151 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 152 |
|
| 153 |
|
| 154 |
class NetAppOntapCifsUnixSymlink: |
| 155 |
def __init__(self): |
| 156 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 157 |
self.argument_spec.update(dict( |
| 158 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 159 |
vserver=dict(required=True, type='str'), |
| 160 |
unix_path=dict(required=True, type='str'), |
| 161 |
share_name=dict(required=False, type='str'), |
| 162 |
cifs_path=dict(required=False, type='str'), |
| 163 |
cifs_server=dict(required=False, type='str'), |
| 164 |
locality=dict(required=False, type='str', choices=['local', 'widelink'], default='local'), |
| 165 |
home_directory=dict(required=False, type='bool', default=False) |
| 166 |
)) |
| 167 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 168 |
|
| 169 |
self.module = AnsibleModule( |
| 170 |
argument_spec=self.argument_spec, |
| 171 |
required_if=[ |
| 172 |
('state', 'present', ['share_name', 'cifs_path']), |
| 173 |
('use_lambda', True, ['lambda_config']), |
| 174 |
('locality', 'widelink', ['cifs_server']), |
| 175 |
], |
| 176 |
supports_check_mode=True |
| 177 |
) |
| 178 |
self.svm_uuid = None |
| 179 |
self.na_helper = NetAppModule(self.module) |
| 180 |
self.parameters = self.na_helper.check_and_set_parameters(self.module) |
| 181 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 182 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_cifs_unix_symlink_mapping:', 9, 6) |
| 183 |
|
| 184 |
@staticmethod |
| 185 |
def validate_path(path): |
| 186 |
if not path.startswith('/'): |
| 187 |
path = "/%s" % path |
| 188 |
if not path.endswith('/'): |
| 189 |
path = "%s/" % path |
| 190 |
return path |
| 191 |
|
| 192 |
@staticmethod |
| 193 |
def encode_path(path): |
| 194 |
return path.replace('/', '%2F') |
| 195 |
|
| 196 |
def get_symlink_mapping_rest(self): |
| 197 |
""" |
| 198 |
Retrieves a specific UNIX symbolink mapping for a SVM |
| 199 |
""" |
| 200 |
api = 'protocols/cifs/unix-symlink-mapping' |
| 201 |
query = {'svm.name': self.parameters.get('vserver'), |
| 202 |
'unix_path': self.parameters['unix_path'], |
| 203 |
'fields': 'svm.uuid,' |
| 204 |
'unix_path,' |
| 205 |
'target.share,' |
| 206 |
'target.path,'} |
| 207 |
if self.parameters.get('cifs_server') is not None: |
| 208 |
query['fields'] += 'target.server,' |
| 209 |
if self.parameters.get('locality') is not None: |
| 210 |
query['fields'] += 'target.locality,' |
| 211 |
if self.parameters.get('home_directory') is not None: |
| 212 |
query['fields'] += 'target.home_directory,' |
| 213 |
|
| 214 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 215 |
if error: |
| 216 |
self.module.fail_json(msg='Error while fetching cifs unix symlink mapping: %s' % to_native(error), |
| 217 |
exception=traceback.format_exc()) |
| 218 |
if record: |
| 219 |
self.svm_uuid = self.na_helper.safe_get(record, ['svm', 'uuid']) |
| 220 |
return self.format_record(record) |
| 221 |
return None |
| 222 |
|
| 223 |
def format_record(self, record): |
| 224 |
return { |
| 225 |
'unix_path': record.get('unix_path'), |
| 226 |
'share_name': self.na_helper.safe_get(record, ['target', 'share']), |
| 227 |
'cifs_path': self.na_helper.safe_get(record, ['target', 'path']), |
| 228 |
'cifs_server': self.na_helper.safe_get(record, ['target', 'server']), |
| 229 |
'locality': self.na_helper.safe_get(record, ['target', 'locality']), |
| 230 |
'home_directory': self.na_helper.safe_get(record, ['target', 'home_directory']) |
| 231 |
} |
| 232 |
|
| 233 |
def create_symlink_mapping_rest(self): |
| 234 |
""" |
| 235 |
Creates a UNIX symbolink mapping for CIFS share |
| 236 |
""" |
| 237 |
api = 'protocols/cifs/unix-symlink-mapping' |
| 238 |
body = { |
| 239 |
'svm.name': self.parameters['vserver'], |
| 240 |
'unix_path': self.parameters['unix_path'], |
| 241 |
'target': { |
| 242 |
'share': self.parameters['share_name'], |
| 243 |
'path': self.parameters['cifs_path'] |
| 244 |
} |
| 245 |
} |
| 246 |
if 'cifs_server' in self.parameters: |
| 247 |
body['target.server'] = self.parameters['cifs_server'] |
| 248 |
if 'locality' in self.parameters: |
| 249 |
body['target.locality'] = self.parameters['locality'] |
| 250 |
if 'home_directory' in self.parameters: |
| 251 |
body['target.home_directory'] = self.parameters['home_directory'] |
| 252 |
|
| 253 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 254 |
if error is not None: |
| 255 |
self.module.fail_json(msg='Error while creating cifs unix symlink mapping: %s' % to_native(error), |
| 256 |
exception=traceback.format_exc()) |
| 257 |
|
| 258 |
def modify_symlink_mapping_rest(self, modify): |
| 259 |
""" |
| 260 |
Updates a specific UNIX symbolink mapping for a SVM |
| 261 |
""" |
| 262 |
api = 'protocols/cifs/unix-symlink-mapping/%s/%s' % (self.svm_uuid, self.encode_path(self.parameters['unix_path'])) |
| 263 |
body = {'target': {}} |
| 264 |
for key, option in [ |
| 265 |
('share', 'share_name'), |
| 266 |
('path', 'cifs_path'), |
| 267 |
('server', 'cifs_server'), |
| 268 |
('locality', 'locality'), |
| 269 |
('home_directory', 'home_directory'), |
| 270 |
]: |
| 271 |
if modify.get(option) is not None: |
| 272 |
body['target'][key] = modify[option] |
| 273 |
|
| 274 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid_or_name=None, body=body) |
| 275 |
if error: |
| 276 |
self.module.fail_json(msg='Error while modifying cifs unix symlink mapping: %s.' % to_native(error), |
| 277 |
exception=traceback.format_exc()) |
| 278 |
|
| 279 |
def delete_symlink_mapping_rest(self): |
| 280 |
""" |
| 281 |
Removes a specific UNIX symbolink mapping for a SVM |
| 282 |
""" |
| 283 |
api = 'protocols/cifs/unix-symlink-mapping/%s/%s' % (self.svm_uuid, self.encode_path(self.parameters['unix_path'])) |
| 284 |
dummy, error = rest_generic.delete_async(self.rest_api, api, uuid=None) |
| 285 |
if error is not None: |
| 286 |
self.module.fail_json(msg='Error while deleting cifs unix symlink mapping: %s' % to_native(error)) |
| 287 |
|
| 288 |
def apply(self): |
| 289 |
# validate leading and trailing forward slashes in unix_path & cifs_path |
| 290 |
for option in ['unix_path', 'cifs_path']: |
| 291 |
if self.parameters.get(option) is not None: |
| 292 |
self.parameters[option] = self.validate_path(self.parameters[option]) |
| 293 |
|
| 294 |
current = self.get_symlink_mapping_rest() |
| 295 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 296 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 297 |
if self.na_helper.changed and not self.module.check_mode: |
| 298 |
if cd_action == 'create': |
| 299 |
self.create_symlink_mapping_rest() |
| 300 |
elif cd_action == 'delete': |
| 301 |
self.delete_symlink_mapping_rest() |
| 302 |
elif modify: |
| 303 |
self.modify_symlink_mapping_rest(modify) |
| 304 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 305 |
self.module.exit_json(**result) |
| 306 |
|
| 307 |
|
| 308 |
def main(): |
| 309 |
symlink_mapping = NetAppOntapCifsUnixSymlink() |
| 310 |
symlink_mapping.apply() |
| 311 |
|
| 312 |
|
| 313 |
if __name__ == '__main__': |
| 314 |
main() |