Debian Codegraph @

ansible_collections/dellemc/unity/plugins/modules/smbshare.py

raw symbols SCIP
1 #!/usr/bin/python
2 # Copyright: (c) 2020-2025, Dell Technologies
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import (absolute_import, division, print_function)
6
7 __metaclass__ = type
8
9 DOCUMENTATION = r'''
10 ---
11 module: smbshare
12 version_added: '1.1.0'
13 short_description: Manage SMB shares on Unity storage system
14 extends_documentation_fragment:
15 - dellemc.unity.unity
16 author:
17 - P Srinivas Rao (@srinivas-rao5) <[email protected]>
18 description:
19 - Managing SMB Shares on Unity storage system includes create, get,
20 modify, and delete the smb shares.
21 options:
22 share_name:
23 description:
24 - Name of the SMB share.
25 - Required during creation of the SMB share.
26 - For all other operations either I(share_name) or I(share_id) is required.
27 type: str
28 share_id:
29 description:
30 - ID of the SMB share.
31 - Should not be specified during creation. Id is auto generated.
32 - For all other operations either I(share_name) or I(share_id) is required.
33 - If I(share_id) is used then no need to pass nas_server/filesystem/snapshot/path.
34 type: str
35 path:
36 description:
37 - Local path to the file system/Snapshot or any existing sub-folder of
38 the file system/Snapshot that is shared over the network.
39 - Path is relative to the root of the filesystem.
40 - Required for creation of the SMB share.
41 type: str
42 filesystem_id:
43 description:
44 - The ID of the File System.
45 - Either I(filesystem_name) or I(filesystem_id) is required for creation of the SMB share for filesystem.
46 - If I(filesystem_name) is specified, then I(nas_server_name)/I(nas_server_id) is required to
47 uniquely identify the filesystem.
48 - Options I(filesystem_name) and I(filesystem_id) are mutually exclusive parameters.
49 type: str
50 snapshot_id:
51 description:
52 - The ID of the Filesystem Snapshot.
53 - Either I(snapshot_name) or I(snapshot_id) is required for creation of the SMB share for a snapshot.
54 - If I(snapshot_name) is specified, then I(nas_server_name)/I(nas_server_id) is required to
55 uniquely identify the snapshot.
56 - Options I(snapshot_name) and I(snapshot_id) are mutually exclusive parameters.
57 type: str
58 nas_server_id:
59 description:
60 - The ID of the NAS Server.
61 - It is not required if I(share_id) is used.
62 type: str
63 filesystem_name:
64 description:
65 - The Name of the File System.
66 - Either I(filesystem_name) or I(filesystem_id) is required for creation of the SMB share for filesystem.
67 - If I(filesystem_name) is specified, then I(nas_server_name)/I(nas_server_id) is required to
68 uniquely identify the filesystem.
69 - Options I(filesystem_name) and I(filesytem_id) are mutually exclusive parameters.
70 type: str
71 snapshot_name:
72 description:
73 - The Name of the Filesystem Snapshot.
74 - Either I(snapshot_name) or I(snapshot_id) is required for creation of the SMB share for a snapshot.
75 - If I(snapshot_name) is specified, then I(nas_server_name)/I(nas_server_id) is required to
76 uniquely identify the snapshot.
77 - Options I(snapshot_name) and I(snapshot_id) are mutually exclusive parameters.
78 type: str
79 nas_server_name:
80 description:
81 - The Name of the NAS Server.
82 - It is not required if I(share_id) is used.
83 - Options I(nas_server_name) and I(nas_server_id) are mutually exclusive parameters.
84 type: str
85 description:
86 description:
87 - Description for the SMB share.
88 - Optional parameter when creating a share.
89 - To modify, pass the new value in description field.
90 type: str
91 is_abe_enabled:
92 description:
93 - Indicates whether Access-based Enumeration (ABE) for SMB share is enabled.
94 - During creation, if not mentioned then default is C(false).
95 type: bool
96 is_branch_cache_enabled:
97 description:
98 - Indicates whether Branch Cache optimization for SMB share is enabled.
99 - During creation, if not mentioned then default is C(false).
100 type: bool
101 is_continuous_availability_enabled:
102 description:
103 - Indicates whether continuous availability for SMB 3.0 is enabled.
104 - During creation, if not mentioned then default is C(false).
105 type: bool
106 is_encryption_enabled:
107 description:
108 - Indicates whether encryption for SMB 3.0 is enabled at the shared folder level.
109 - During creation, if not mentioned then default is C(false).
110 type: bool
111 offline_availability:
112 description:
113 - Defines valid states of Offline Availability.
114 - C(MANUAL)- Only specified files will be available offline.
115 - C(DOCUMENTS)- All files that users open will be available offline.
116 - C(PROGRAMS)- Program will preferably run from the offline cache even when
117 connected to the network. All files that users open will be available offline.
118 - C(NONE)- Prevents clients from storing documents and programs in offline cache.
119 type: str
120 choices: ["MANUAL","DOCUMENTS","PROGRAMS","NONE"]
121 umask:
122 description:
123 - The default UNIX umask for new files created on the SMB Share.
124 type: str
125 state:
126 description:
127 - Define whether the SMB share should exist or not.
128 - Value C(present) indicates that the share should exist on the system.
129 - Value C(absent) indicates that the share should not exist on the system.
130 type: str
131 required: true
132 choices: ['absent', 'present']
133 notes:
134 - When ID/Name of the filesystem/snapshot is passed then I(nas_server) is not required.
135 If passed, then filesystem/snapshot should exist for the mentioned I(nas_server),
136 else the task will fail.
137 - The I(check_mode) is not supported.
138 '''
139
140 EXAMPLES = r'''
141 - name: Create SMB share for a filesystem
142 smbshare:
143 unispherehost: "{{unispherehost}}"
144 username: "{{username}}"
145 password: "{{password}}"
146 validate_certs: "{{validate_certs}}"
147 share_name: "sample_smb_share"
148 filesystem_name: "sample_fs"
149 nas_server_id: "NAS_11"
150 path: "/sample_fs"
151 description: "Sample SMB share created"
152 is_abe_enabled: true
153 is_branch_cache_enabled: true
154 offline_availability: "DOCUMENTS"
155 is_continuous_availability_enabled: true
156 is_encryption_enabled: true
157 umask: "777"
158 state: "present"
159 - name: Modify Attributes of SMB share for a filesystem
160 smbshare:
161 unispherehost: "{{unispherehost}}"
162 username: "{{username}}"
163 password: "{{password}}"
164 validate_certs: "{{validate_certs}}"
165 share_name: "sample_smb_share"
166 nas_server_name: "sample_nas_server"
167 description: "Sample SMB share attributes updated"
168 is_abe_enabled: false
169 is_branch_cache_enabled: false
170 offline_availability: "MANUAL"
171 is_continuous_availability_enabled: "false"
172 is_encryption_enabled: "false"
173 umask: "022"
174 state: "present"
175 - name: Create SMB share for a snapshot
176 smbshare:
177 unispherehost: "{{unispherehost}}"
178 username: "{{username}}"
179 password: "{{password}}"
180 validate_certs: "{{validate_certs}}"
181 share_name: "sample_snap_smb_share"
182 snapshot_name: "sample_snapshot"
183 nas_server_id: "NAS_11"
184 path: "/sample_snapshot"
185 description: "Sample SMB share created for snapshot"
186 is_abe_enabled: true
187 is_branch_cache_enabled: true
188 is_continuous_availability_enabled: true
189 is_encryption_enabled: true
190 umask: "777"
191 state: "present"
192 - name: Modify Attributes of SMB share for a snapshot
193 smbshare:
194 unispherehost: "{{unispherehost}}"
195 username: "{{username}}"
196 password: "{{password}}"
197 validate_certs: "{{validate_certs}}"
198 share_name: "sample_snap_smb_share"
199 snapshot_name: "sample_snapshot"
200 description: "Sample SMB share attributes updated for snapshot"
201 is_abe_enabled: false
202 is_branch_cache_enabled: false
203 offline_availability: "MANUAL"
204 is_continuous_availability_enabled: "false"
205 is_encryption_enabled: "false"
206 umask: "022"
207 state: "present"
208 - name: Get details of SMB share
209 smbshare:
210 unispherehost: "{{unispherehost}}"
211 username: "{{username}}"
212 password: "{{password}}"
213 validate_certs: "{{validate_certs}}"
214 share_id: "{{smb_share_id}}"
215 state: "present"
216 - name: Delete SMB share
217 smbshare:
218 unispherehost: "{{unispherehost}}"
219 username: "{{username}}"
220 password: "{{password}}"
221 validate_certs: "{{validate_certs}}"
222 share_id: "{{smb_share_id}}"
223 state: "absent"
224 '''
225
226 RETURN = r'''
227 changed:
228 description: Whether or not the resource has changed.
229 returned: always
230 type: bool
231 sample: true
232 smb_share_details:
233 description: The SMB share details.
234 type: dict
235 returned: When share exists.
236 contains:
237 id:
238 description: The ID of the SMB share.
239 type: str
240 name:
241 description: Name of the SMB share.
242 type: str
243 sample: "sample_smb_share"
244 filesystem_id:
245 description: The ID of the Filesystem.
246 type: str
247 filesystem_name:
248 description: The Name of the filesystem
249 type: str
250 snapshot_id:
251 description: The ID of the Snapshot.
252 type: str
253 snapshot_name:
254 description: The Name of the Snapshot.
255 type: str
256 nas_server_id:
257 description: The ID of the nas_server.
258 type: str
259 nas_server_name:
260 description: The Name of the nas_server.
261 type: str
262 description:
263 description: Additional information about the share.
264 type: str
265 sample: This share is created for demo purpose only.
266 is_abe_enabled:
267 description: Whether Access Based enumeration is enforced or not.
268 type: bool
269 sample: false
270 is_branch_cache_enabled:
271 description: Whether branch cache is enabled or not.
272 type: bool
273 sample: false
274 is_continuous_availability_enabled:
275 description: Whether the share will be available continuously or not.
276 type: bool
277 sample: false
278 is_encryption_enabled:
279 description: Whether encryption is enabled or not.
280 type: bool
281 sample: false
282 umask:
283 description: Unix mask for the SMB share.
284 type: str
285 sample: {
286 "creation_time": "2022-03-17 11:56:54.867000+00:00",
287 "description": "",
288 "existed": true,
289 "export_paths": [
290 "\\\\multi-prot-pie.extreme1.com\\multi-prot-hui",
291 "\\\\10.230.24.26\\multi-prot-hui"
292 ],
293 "filesystem": {
294 "UnityFileSystem": {
295 "hash": 8748426746492
296 }
297 },
298 "filesystem_id": "fs_140",
299 "filesystem_name": "multi-prot-hui",
300 "hash": 8748426746588,
301 "id": "SMBShare_20",
302 "is_abe_enabled": false,
303 "is_ace_enabled": false,
304 "is_branch_cache_enabled": false,
305 "is_continuous_availability_enabled": false,
306 "is_dfs_enabled": false,
307 "is_encryption_enabled": false,
308 "is_read_only": null,
309 "modified_time": "2022-03-17 11:56:54.867000+00:00",
310 "name": "multi-prot-hui",
311 "nas_server_id": "nas_5",
312 "nas_server_name": "multi-prot",
313 "offline_availability": "CifsShareOfflineAvailabilityEnum.NONE",
314 "path": "/",
315 "snap": null,
316 "type": "CIFSTypeEnum.CIFS_SHARE",
317 "umask": "022"
318 }
319 '''
320
321 from ansible.module_utils.basic import AnsibleModule
322 from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \
323 import utils
324
325 LOG = utils.get_logger('smbshare')
326
327 application_type = "Ansible/1.7.1"
328
329
330 class SMBShare(object):
331 """Class with SMB Share operations"""
332
333 def __init__(self):
334 """ Define all parameters required by this module"""
335 self.module_params = utils.get_unity_management_host_parameters()
336 self.module_params.update(get_smb_share_parameters())
337
338 # initialize the ansible module
339 mut_ex_args = [['share_name', 'share_id'],
340 ['nas_server_name', 'nas_server_id'],
341 ['filesystem_name', 'snapshot_name',
342 'filesystem_id', 'snapshot_id'],
343 ['share_id', 'nas_server_name'],
344 ['share_id', 'nas_server_id'],
345 ['share_id', 'filesystem_name'],
346 ['share_id', 'filesystem_id'],
347 ['share_id', 'path'],
348 ['share_id', 'snapshot_name'],
349 ['share_id', 'snapshot_id']]
350 required_one_of = [['share_id', 'share_name']]
351
352 self.module = AnsibleModule(
353 argument_spec=self.module_params,
354 supports_check_mode=False,
355 mutually_exclusive=mut_ex_args,
356 required_one_of=required_one_of
357 )
358 utils.ensure_required_libs(self.module)
359
360 # result is a dictionary that contains changed status and
361 # snapshot details
362 self.result = {"changed": False,
363 'smb_share_details': {}}
364
365 self.unity_conn = utils.get_unity_unisphere_connection(
366 self.module.params, application_type)
367 self.smb_share_conn_obj = utils.cifs_share.UnityCifsShare(
368 self.unity_conn)
369 LOG.info('Connection established with the Unity Array')
370
371 def get_offline_availability_enum(self, offline_availability):
372 """
373 Get the enum of the Offline Availability parameter.
374 :param offline_availability: The offline_availability string
375 :return: offline_availability enum
376 """
377 if offline_availability in \
378 utils.CifsShareOfflineAvailabilityEnum.__members__:
379 return utils.CifsShareOfflineAvailabilityEnum[
380 offline_availability]
381 else:
382 error_msg = "Invalid value {0} for offline availability" \
383 " provided".format(offline_availability)
384 LOG.error(error_msg)
385 self.module.fail_json(msg=error_msg)
386
387 def get_smb_share_obj(self, share_id=None, share_name=None,
388 filesystem_obj=None, snap_obj=None, nas_obj=None):
389 """Get SMB share details"""
390 msg = "Failed to get details of SMB Share {0} with error {1} "
391 smb_share = share_name if share_name else share_id
392 try:
393 if share_id:
394 obj_smb = self.unity_conn.get_cifs_share(_id=share_id)
395 if obj_smb and obj_smb.existed:
396 LOG.info("Successfully got the SMB share "
397 "object %s ", obj_smb)
398 return obj_smb
399
400 elif share_name is not None and filesystem_obj:
401 # There might be a case where SMB share with same name exists
402 # for different nas server. Hence, filesystem_obj is passed
403 # along with share name to get a unique resource.
404 return self.unity_conn.get_cifs_share(
405 name=share_name, filesystem=filesystem_obj)
406
407 elif share_name is not None and snap_obj:
408 # There might be a case where SMB share with same name exists
409 # for different nas server. Hence, snap_obj is passed
410 # along with share name to get a unique resource.
411 return self.unity_conn.get_cifs_share(
412 name=share_name, snap=snap_obj)
413
414 # This elif is addressing scenario where nas server details is
415 # passed and neither filesystem nor snapshot details are passed.
416 elif share_name is not None and nas_obj:
417 # Multiple smb shares can be received, as only name is passed
418 smb_share_obj = self.unity_conn.get_cifs_share(
419 name=share_name)
420
421 # Checking if instance or list of instance is returned.
422 if isinstance(smb_share_obj,
423 utils.cifs_share.UnityCifsShareList):
424 LOG.info("Multiple SMB share with same name found.")
425 smb_share_obj_list = smb_share_obj
426
427 for smb_share in smb_share_obj_list:
428 if smb_share.filesystem.nas_server == nas_obj:
429 return smb_share
430
431 msg = "No SMB share found with the given NAS Server." \
432 " Please provide correct share name and" \
433 " nas server details."
434 return None
435
436 # Below statements will execute when there is only single
437 # smb share returned.
438 if smb_share_obj.filesystem.nas_server == nas_obj:
439 return smb_share_obj
440 msg = "No SMB share found with the given NAS Server." \
441 " Please provide correct share name and" \
442 " nas server details."
443 return None
444
445 else:
446 self.module.fail_json(
447 msg="Share Name is Passed. Please enter Filesystem/"
448 "Snapshot/NAS Server Resource along with share_name"
449 " to get the details of the SMB share")
450
451 except utils.HttpError as e:
452 if e.http_status == 401:
453 cred_err = "Incorrect username or password , {0}".format(
454 e.message)
455 self.module.fail_json(msg=cred_err)
456 else:
457 err_msg = msg.format(smb_share, str(e))
458 LOG.error(err_msg)
459 self.module.fail_json(msg=err_msg)
460
461 except utils.UnityResourceNotFoundError as e:
462 err_msg = msg.format(smb_share, str(e))
463 LOG.error(err_msg)
464 return None
465
466 except Exception as e:
467 err_msg = msg.format(smb_share, str(e))
468 LOG.error(err_msg)
469 self.module.fail_json(msg=err_msg)
470
471 def create_smb_share(self, share_name, path, filesystem_obj=None,
472 snapshot_obj=None, description=None,
473 is_abe_enabled=None, is_branch_cache_enabled=None,
474 is_continuous_availability_enabled=None,
475 is_encryption_enabled=None,
476 offline_availability=None, umask=None):
477 """
478 Create SMB Share
479 :return: SMB Share Object if successful, else error.
480 """
481 if path is None or path == "":
482 self.module.fail_json(msg="Please enter a valid path."
483 " Empty string or None provided.")
484 if not filesystem_obj and not snapshot_obj:
485 self.module.fail_json(msg="Either Filesystem or Snapshot "
486 "Resource's Name/ID is required to"
487 " Create a SMB share")
488 try:
489 if filesystem_obj:
490 return self.smb_share_conn_obj.create(
491 cli=self.unity_conn._cli, name=share_name,
492 fs=filesystem_obj, path=path,
493 is_encryption_enabled=is_encryption_enabled,
494 is_con_avail_enabled=is_continuous_availability_enabled,
495 is_abe_enabled=is_abe_enabled,
496 is_branch_cache_enabled=is_branch_cache_enabled,
497 umask=umask, description=description,
498 offline_availability=offline_availability)
499 else:
500 return self.smb_share_conn_obj.create_from_snap(
501 cli=self.unity_conn._cli, name=share_name,
502 snap=snapshot_obj, path=path,
503 is_encryption_enabled=is_encryption_enabled,
504 is_con_avail_enabled=is_continuous_availability_enabled,
505 is_abe_enabled=is_abe_enabled,
506 is_branch_cache_enabled=is_branch_cache_enabled,
507 umask=umask, description=description,
508 offline_availability=offline_availability)
509
510 except Exception as e:
511 error_msg = "Failed to create SMB share" \
512 " %s with error %s" % (share_name, str(e))
513 LOG.error(error_msg)
514 self.module.fail_json(msg=error_msg)
515
516 def get_filesystem(self, filesystem_id=None, filesystem_name=None,
517 nas_server_obj=None):
518 """
519 Get the Filesystem Object.
520 :param filesystem_id: ID of the Filesystem.
521 :param filesystem_name: Name of the filesystem.
522 :param nas_server_obj: NAS Server object.
523 :return: Object of the filesystem.
524 """
525 try:
526 if filesystem_id:
527 obj_fs = self.unity_conn.get_filesystem(_id=filesystem_id)
528 if obj_fs and obj_fs.existed:
529 LOG.info("Successfully got the filesystem "
530 "object %s ", obj_fs)
531 return obj_fs
532 else:
533 return self.unity_conn.get_filesystem(
534 name=filesystem_name, nas_server=nas_server_obj)
535 return None
536 except Exception as e:
537 filesystem = filesystem_name if filesystem_name \
538 else filesystem_id
539 err_msg = "Failed to get filesystem details {0} with" \
540 " error {1}".format(filesystem, str(e))
541 LOG.error(err_msg)
542 self.module.fail_json(msg=err_msg)
543
544 def get_snapshot(self, snapshot_name, snapshot_id):
545 """
546 Get the Snapshot Object.
547 :param snapshot_id: ID of the Snapshot.
548 :param snapshot_name: Name of the Snapshot
549 :return: Object of the filesystem.
550 """
551 try:
552 obj_snap = self.unity_conn.get_snap(_id=snapshot_id,
553 name=snapshot_name)
554 if snapshot_id and obj_snap and not obj_snap.existed:
555 LOG.info("Snapshot object does not exist %s ", obj_snap)
556 return None
557 return obj_snap
558 except Exception as e:
559 snapshot = snapshot_name if snapshot_name else snapshot_id
560 err_msg = "Failed to get filesystem snapshots details {0} with" \
561 " error {1}".format(snapshot, str(e))
562 LOG.error(err_msg)
563 self.module.fail_json(msg=err_msg)
564
565 def get_nas_server(self, nas_server_name, nas_server_id):
566 """
567 Get the NAS Server Object using NAME/ID of the NAS Server.
568 :param nas_server_name: Name of the NAS Server
569 :param nas_server_id: ID of the NAS Server
570 :return: NAS Server object.
571 """
572 nas_server = nas_server_name if nas_server_name else nas_server_id
573 try:
574 obj_nas = self.unity_conn.get_nas_server(_id=nas_server_id,
575 name=nas_server_name)
576 if nas_server_id and obj_nas and not obj_nas.existed:
577 LOG.info("NAS Server object does not exist %s ", obj_nas)
578 return None
579 return obj_nas
580 except utils.HttpError as e:
581 if e.http_status == 401:
582 cred_err = "Incorrect username or password , {0}".format(
583 e.message)
584 self.module.fail_json(msg=cred_err)
585 else:
586 err_msg = "Failed to get details of NAS Server" \
587 " {0} with error {1}".format(nas_server, str(e))
588 LOG.error(err_msg)
589 self.module.fail_json(msg=err_msg)
590 except Exception as e:
591 nas_server = nas_server_name if nas_server_name \
592 else nas_server_id
593 err_msg = "Failed to get nas server details {0} with" \
594 " error {1}".format(nas_server, str(e))
595 LOG.error(err_msg)
596 self.module.fail_json(msg=err_msg)
597
598 def delete_smb_share(self, smb_share_obj):
599 """
600 Delete SMB share if exists, else thrown error.
601 """
602 try:
603 smb_share_obj.delete()
604 except Exception as e:
605 error_msg = "Failed to Delete SMB share" \
606 " %s with error %s" % (smb_share_obj.name, str(e))
607 LOG.error(error_msg)
608 self.module.fail_json(msg=error_msg)
609
610 def to_update(self, smb_share_obj):
611 LOG.info("Checking Whether the parameters are modified or not.")
612
613 offline_availability = self.module.params['offline_availability']
614 # Get the enum for the corresponding offline_availability
615 if offline_availability:
616 offline_availability = \
617 self.get_offline_availability_enum(offline_availability)
618 if offline_availability is not None and \
619 offline_availability != smb_share_obj.offline_availability:
620 return True
621
622 smb_share_dict = smb_share_obj._get_properties()
623 params_list = ['is_abe_enabled', 'is_branch_cache_enabled',
624 'is_continuous_availability_enabled',
625 'is_encryption_enabled', 'description', 'umask']
626 for param in params_list:
627 if self.module.params[param] is not None and \
628 self.module.params[param] != smb_share_dict[param]:
629 return True
630 return False
631
632 def update_smb_share(self, smb_share_obj, is_encryption_enabled=None,
633 is_continuous_availability_enabled=None,
634 is_abe_enabled=None,
635 is_branch_cache_enabled=None,
636 umask=None, description=None,
637 offline_availability=None):
638 """
639 The Details of the SMB share will be updated in the function.
640 """
641 try:
642 smb_share_obj.modify(
643 is_encryption_enabled=is_encryption_enabled,
644 is_con_avail_enabled=is_continuous_availability_enabled,
645 is_abe_enabled=is_abe_enabled,
646 is_branch_cache_enabled=is_branch_cache_enabled,
647 umask=umask, description=description,
648 offline_availability=offline_availability)
649
650 except Exception as e:
651 error_msg = "Failed to Update parameters of SMB share" \
652 " %s with error %s" % (smb_share_obj.name, str(e))
653 LOG.error(error_msg)
654 self.module.fail_json(msg=error_msg)
655
656 def perform_module_operation(self):
657 """
658 Perform different actions on SMB share based on user parameters
659 chosen in playbook
660 """
661 state = self.module.params['state']
662 share_name = self.module.params['share_name']
663 filesystem_name = self.module.params['filesystem_name']
664 snapshot_name = self.module.params['snapshot_name']
665 nas_server_name = self.module.params['nas_server_name']
666 share_id = self.module.params['share_id']
667 filesystem_id = self.module.params['filesystem_id']
668 snapshot_id = self.module.params['snapshot_id']
669 nas_server_id = self.module.params['nas_server_id']
670 path = self.module.params['path']
671
672 description = self.module.params['description']
673 is_branch_cache_enabled = \
674 self.module.params['is_branch_cache_enabled']
675 is_continuous_availability_enabled = \
676 self.module.params['is_continuous_availability_enabled']
677 is_encryption_enabled = self.module.params['is_encryption_enabled']
678 is_abe_enabled = self.module.params['is_abe_enabled']
679 umask = self.module.params['umask']
680
681 offline_availability = self.module.params['offline_availability']
682 # Get the enum for the corresponding offline_availability
683 if offline_availability:
684 offline_availability = \
685 self.get_offline_availability_enum(offline_availability)
686
687 changed = False
688 '''
689 Validate parameters.
690 '''
691 if share_id is not None and \
692 (share_id == "" or len(share_id.split()) == 0):
693 self.module.fail_json(msg="Invalid share id provided."
694 " Please enter a valid share ID.")
695
696 '''
697 Get details of NAS Server, if entered.
698 '''
699 nas_server_obj = None
700 if nas_server_name or nas_server_id:
701 nas_server_obj = self.get_nas_server(nas_server_name,
702 nas_server_id)
703 if nas_server_obj:
704 msg = "NAS Server Object:" \
705 " {0}".format(nas_server_obj._get_properties())
706 LOG.info(msg)
707 else:
708 msg = "NAS Server Resource not fetched."
709 LOG.info(msg)
710
711 '''
712 Get details of Filesystem, if entered.
713 '''
714 filesystem_obj = None
715 if filesystem_id:
716 filesystem_obj = self.get_filesystem(filesystem_id)
717 if filesystem_name:
718 # nas_server_obj is required to uniquely identify filesystem
719 # resource. If neither nas_server_name nor nas_server_id
720 # is passed along with filesystem_name then error is thrown.
721 if not nas_server_obj:
722 self.module.fail_json(msg="nas_server_id/nas_server_name is "
723 "required when filesystem_name is "
724 "passed")
725 filesystem_obj = self.get_filesystem(
726 None, filesystem_name, nas_server_obj)
727 if filesystem_obj:
728 msg = "Filesystem Object:" \
729 " {0}".format(filesystem_obj._get_properties())
730 LOG.info(msg)
731 # Checking if filesystem supports SMB protocol or not.
732 if filesystem_obj and \
733 filesystem_obj.supported_protocols.name == "NFS":
734 self.module.fail_json(msg="Cannot perform SMB share operations "
735 "as file system supports only NFS "
736 "protocol. Please enter a valid "
737 "Filesystem having supported protocol"
738 " as SMB or Multiprotocol.")
739 '''
740 Get details of Snapshot, if entered.
741 '''
742 snapshot_obj = None
743 if snapshot_id or snapshot_name:
744 # Snapshot Name and Snapshot ID both are unique across array.
745 # Hence no need to mention nas server details
746 snapshot_obj = self.get_snapshot(snapshot_name, snapshot_id)
747 if snapshot_obj:
748 msg = "Snapshot Object:" \
749 " {0}".format(snapshot_obj._get_properties())
750 LOG.info(msg)
751 else:
752 msg = "Snapshot Resource not fetched."
753 LOG.info(msg)
754
755 '''
756 Get the Details of the SMB Share
757 '''
758 smb_share_obj = self.get_smb_share_obj(
759 share_id, share_name, filesystem_obj, snapshot_obj,
760 nas_server_obj)
761 if smb_share_obj:
762 msg = "SMB Share Object:" \
763 " {0}".format(smb_share_obj._get_properties())
764 LOG.info(msg)
765 elif state == 'present' and share_id:
766 msg = "Unable to fetch SMB Share Resource. " \
767 "Incorrect SMB share id provided. " \
768 "Please enter a correct share id."
769 LOG.error(msg)
770 self.module.fail_json(msg=msg)
771
772 '''
773 Creation of SMB Share
774 '''
775 if state == "present" and not smb_share_obj:
776 smb_share_obj = self.create_smb_share(
777 share_name, path, filesystem_obj, snapshot_obj, description,
778 is_abe_enabled, is_branch_cache_enabled,
779 is_continuous_availability_enabled, is_encryption_enabled,
780 offline_availability, umask)
781 changed = True
782
783 '''
784 Update the SMB share details
785 '''
786 if state == "present" and smb_share_obj:
787 LOG.info("Modify the details of the SMB share.")
788 update_flag = self.to_update(smb_share_obj)
789 msg = "Update Flag: {0}".format(str(update_flag))
790 LOG.info(msg)
791 if update_flag:
792 self.update_smb_share(smb_share_obj, is_encryption_enabled,
793 is_continuous_availability_enabled,
794 is_abe_enabled,
795 is_branch_cache_enabled,
796 umask, description,
797 offline_availability)
798 changed = True
799
800 '''
801 Delete the SMB share details
802 '''
803 if state == "absent" and smb_share_obj:
804 self.delete_smb_share(smb_share_obj)
805 changed = True
806
807 '''
808 Update the changed state and SMB share details
809 '''
810
811 self.result["changed"] = changed
812 smb_details = self.prepare_output_dict(state, share_id, share_name,
813 filesystem_obj, snapshot_obj,
814 nas_server_obj)
815 self.result["smb_share_details"] = smb_details
816 self.module.exit_json(**self.result)
817
818 def prepare_output_dict(self, state, share_id, share_name,
819 filesystem_obj, snapshot_obj, nas_server_obj):
820 smb_share_details = None
821 smb_share_obj = None
822 if state == 'present':
823 smb_share_obj = self.get_smb_share_obj(
824 share_id, share_name, filesystem_obj,
825 snapshot_obj, nas_server_obj)
826 smb_share_details = smb_share_obj._get_properties()
827 if smb_share_details:
828 # Get Snapshot NAME and ID if SMB share exists for Snapshot
829 if smb_share_obj.type.name == "CIFS_SNAPSHOT":
830 smb_share_details['snapshot_name'] = smb_share_obj.snap.name
831 smb_share_details['snapshot_id'] = smb_share_obj.snap.id
832
833 # Get Filesystem NAME and ID
834 smb_share_details['filesystem_name'] = \
835 smb_share_obj.filesystem.name
836 smb_share_details['filesystem_id'] = smb_share_obj.filesystem.id
837
838 # Get NAS server NAME and ID
839 smb_share_details['nas_server_name'] = \
840 smb_share_obj.filesystem.nas_server.name
841 smb_share_details['nas_server_id'] = \
842 smb_share_obj.filesystem.nas_server.id
843 return smb_share_details
844
845
846 def get_smb_share_parameters():
847 """
848 This method provides parameters required for the ansible smb share
849 modules on Unity
850 """
851
852 return dict(
853 share_name=dict(), share_id=dict(),
854 filesystem_name=dict(), filesystem_id=dict(),
855 snapshot_name=dict(), snapshot_id=dict(),
856 nas_server_name=dict(), nas_server_id=dict(),
857 path=dict(no_log=True), umask=dict(), description=dict(),
858 offline_availability=dict(
859 choices=["MANUAL", "DOCUMENTS", "PROGRAMS", "NONE"]),
860 is_abe_enabled=dict(type='bool'),
861 is_branch_cache_enabled=dict(type='bool'),
862 is_continuous_availability_enabled=dict(type='bool'),
863 is_encryption_enabled=dict(type='bool'),
864 state=dict(required=True, choices=['present', 'absent'], type='str')
865 )
866
867
868 def main():
869 """ Create Unity SMB share object and perform action on it
870 based on user input from playbook"""
871 obj = SMBShare()
872 obj.perform_module_operation()
873
874
875 if __name__ == '__main__':
876 main()