Debian Codegraph @

ansible_collections/dellemc/unity/plugins/modules/nasserver.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: nasserver
12 version_added: '1.1.0'
13 short_description: Manage NAS servers 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 NAS servers on Unity storage system includes get,
20 modification to the NAS servers.
21 options:
22 nas_server_id:
23 description:
24 - The ID of the NAS server.
25 - Either I(nas_server_name) or I(nas_server_id) is required to perform the task.
26 - The parameters I(nas_server_name) and I(nas_server_id) are mutually exclusive.
27 type: str
28 nas_server_name:
29 description:
30 - The Name of the NAS server.
31 - Either I(nas_server_name) or I(nas_server_id) is required to perform the task.
32 - The parameters I(nas_server_name) and I(nas_server_id) are mutually exclusive.
33 type: str
34 nas_server_new_name:
35 description:
36 - The new name of the NAS server.
37 - It can be mentioned during modification of the NAS server.
38 type: str
39 is_replication_destination:
40 description:
41 - It specifies whether the NAS server is a replication destination.
42 - It can be mentioned during modification of the NAS server.
43 type: bool
44 is_backup_only:
45 description:
46 - It specifies whether the NAS server is used as backup only.
47 - It can be mentioned during modification of the NAS server.
48 type: bool
49 is_multiprotocol_enabled:
50 description:
51 - This parameter indicates whether multiprotocol sharing mode is enabled.
52 - It can be mentioned during modification of the NAS server.
53 type: bool
54 allow_unmapped_user:
55 description:
56 - This flag is used to mandatorily disable access in case of any user
57 mapping failure.
58 - If C(true), then enable access in case of any user mapping failure.
59 - If C(false), then disable access in case of any user mapping failure.
60 - It can be mentioned during modification of the NAS server.
61 type: bool
62 default_windows_user:
63 description:
64 - Default windows user name used for granting access in the case of Unix
65 to Windows user mapping failure.
66 - It can be mentioned during modification of the NAS server.
67 type: str
68 default_unix_user:
69 description:
70 - Default Unix user name used for granting access in the case of Windows
71 to Unix user mapping failure.
72 - It can be mentioned during modification of the NAS server.
73 type: str
74 enable_windows_to_unix_username_mapping:
75 description:
76 - This parameter indicates whether a Unix to/from Windows user name
77 mapping is enabled.
78 - It can be mentioned during modification of the NAS server.
79 type: bool
80 is_packet_reflect_enabled:
81 description:
82 - If the packet has to be reflected, then this parameter
83 has to be set to C(true).
84 - It can be mentioned during modification of the NAS server.
85 type: bool
86 current_unix_directory_service:
87 description:
88 - This is the directory service used for querying identity information
89 for UNIX (such as UIDs, GIDs, net groups).
90 - It can be mentioned during modification of the NAS server.
91 type: str
92 choices: ["NONE", "NIS", "LOCAL", "LDAP", "LOCAL_THEN_NIS", "LOCAL_THEN_LDAP"]
93 replication_params:
94 description:
95 - Settings required for enabling replication.
96 type: dict
97 suboptions:
98 destination_nas_server_name:
99 description:
100 - Name of the destination nas server.
101 - Default value will be source nas server name prefixed by 'DR_'.
102 type: str
103 replication_mode:
104 description:
105 - The replication mode.
106 - This is mandatory to enable replication.
107 type: str
108 choices: ['asynchronous', 'manual']
109 rpo:
110 description:
111 - Maximum time to wait before the system syncs the source and destination LUNs.
112 - The I(rpo) option should be specified if the I(replication_mode) is C(asynchronous).
113 - The value should be in range of C(5) to C(1440).
114 type: int
115 replication_type:
116 description:
117 - Type of replication.
118 choices: ['local', 'remote']
119 type: str
120 remote_system:
121 description:
122 - Details of remote system to which the replication is being configured.
123 - The I(remote_system) option should be specified if the
124 I(replication_type) is C(remote).
125 type: dict
126 suboptions:
127 remote_system_host:
128 required: true
129 description:
130 - IP or FQDN for remote Unity unisphere Host.
131 type: str
132 remote_system_username:
133 type: str
134 required: true
135 description:
136 - User name of remote Unity unisphere Host.
137 remote_system_password:
138 type: str
139 required: true
140 description:
141 - Password of remote Unity unisphere Host.
142 remote_system_verifycert:
143 type: bool
144 default: true
145 description:
146 - Boolean variable to specify whether or not to validate SSL
147 certificate of remote Unity unisphere Host.
148 - C(true) - Indicates that the SSL certificate should be verified.
149 - C(false) - Indicates that the SSL certificate should not be
150 verified.
151 remote_system_port:
152 description:
153 - Port at which remote Unity unisphere is hosted.
154 type: int
155 default: 443
156 destination_pool_name:
157 description:
158 - Name of pool to allocate destination Luns.
159 - Mutually exclusive with I(destination_pool_id).
160 type: str
161 destination_pool_id:
162 description:
163 - Id of pool to allocate destination Luns.
164 - Mutually exclusive with I(destination_pool_name).
165 type: str
166 destination_sp:
167 description:
168 - Storage process of destination nas server
169 choices: ['SPA', 'SPB']
170 type: str
171 is_backup:
172 description:
173 - Indicates if the destination nas server is backup.
174 type: bool
175 replication_name:
176 description:
177 - User defined name for replication session.
178 type: str
179 new_replication_name:
180 description:
181 - Replication name to rename the session to.
182 type: str
183 replication_state:
184 description:
185 - State of the replication.
186 choices: ['enable', 'disable']
187 type: str
188 replication_reuse_resource:
189 description:
190 - This parameter indicates if existing NAS Server is to be used for replication.
191 type: bool
192 state:
193 description:
194 - Define the state of NAS server on the array.
195 - The value present indicates that NAS server should exist on the system after
196 the task is executed.
197 - In this release deletion of NAS server is not supported. Hence, if state is
198 set to C(absent) for any existing NAS server then error will be thrown.
199 - For any non-existing NAS server, if state is set to C(absent) then it will return None.
200 type: str
201 required: true
202 choices: ['present', 'absent']
203
204 notes:
205 - The I(check_mode) is not supported.
206 '''
207
208 EXAMPLES = r'''
209
210 - name: Get Details of NAS Server
211 nasserver:
212 unispherehost: "{{unispherehost}}"
213 username: "{{username}}"
214 password: "{{password}}"
215 validate_certs: "{{validate_certs}}"
216 nas_server_name: "{{nas_server_name}}"
217 state: "present"
218
219 - name: Modify Details of NAS Server
220 nasserver:
221 unispherehost: "{{unispherehost}}"
222 username: "{{username}}"
223 password: "{{password}}"
224 validate_certs: "{{validate_certs}}"
225 nas_server_name: "{{nas_server_name}}"
226 nas_server_new_name: "updated_sample_nas_server"
227 is_replication_destination: false
228 is_backup_only: false
229 is_multiprotocol_enabled: true
230 allow_unmapped_user: true
231 default_unix_user: "default_unix_sample_user"
232 default_windows_user: "default_windows_sample_user"
233 enable_windows_to_unix_username_mapping: true
234 current_unix_directory_service: "LDAP"
235 is_packet_reflect_enabled: true
236 state: "present"
237
238 - name: Enable replication for NAS Server on Local System
239 nasserver:
240 unispherehost: "{{unispherehost}}"
241 username: "{{username}}"
242 password: "{{password}}"
243 validate_certs: "{{validate_certs}}"
244 nas_server_id: "nas_10"
245 replication_reuse_resource: false
246 replication_params:
247 replication_name: "test_replication"
248 destination_nas_server_name: "destination_nas"
249 replication_mode: "asynchronous"
250 rpo: 60
251 replication_type: "local"
252 destination_pool_name: "Pool_Ansible_Neo_DND"
253 destination_sp: "SPA"
254 is_backup: true
255 replication_state: "enable"
256 state: "present"
257
258 - name: Enable replication for NAS Server on Remote System
259 nasserver:
260 unispherehost: "{{unispherehost}}"
261 username: "{{username}}"
262 password: "{{password}}"
263 validate_certs: "{{validate_certs}}"
264 nas_server_name: "dummy_nas"
265 replication_reuse_resource: false
266 replication_params:
267 replication_name: "test_replication"
268 destination_nas_server_name: "destination_nas"
269 replication_mode: "asynchronous"
270 rpo: 60
271 replication_type: "remote"
272 remote_system:
273 remote_system_host: '10.10.10.10'
274 remote_system_verifycert: false
275 remote_system_username: 'test1'
276 remote_system_password: 'test1!'
277 destination_pool_name: "fastVP_pool"
278 destination_sp: "SPA"
279 is_backup: true
280 replication_state: "enable"
281 state: "present"
282
283 - name: Enable replication for NAS Server on Remote System in existing NAS Server
284 nasserver:
285 unispherehost: "{{unispherehost}}"
286 username: "{{username}}"
287 password: "{{password}}"
288 validate_certs: "{{validate_certs}}"
289 nas_server_name: "dummy_nas"
290 replication_reuse_resource: true
291 replication_params:
292 destination_nas_server_name: "destination_nas"
293 replication_mode: "asynchronous"
294 rpo: 60
295 replication_type: "remote"
296 replication_name: "test_replication"
297 remote_system:
298 remote_system_host: '10.10.10.10'
299 remote_system_verifycert: false
300 remote_system_username: 'test1'
301 remote_system_password: 'test1!'
302 destination_pool_name: "fastVP_pool"
303 replication_state: "enable"
304 state: "present"
305
306 - name: Modify replication on the nasserver
307 nasserver:
308 unispherehost: "{{unispherehost}}"
309 username: "{{username}}"
310 password: "{{password}}"
311 validate_certs: "{{validate_certs}}"
312 nas_server_name: "dummy_nas"
313 replication_params:
314 replication_name: "test_repl"
315 new_replication_name: "test_repl_updated"
316 replication_mode: "asynchronous"
317 rpo: 50
318 replication_state: "enable"
319 state: "present"
320
321 - name: Disable replication on the nasserver
322 nasserver:
323 unispherehost: "{{unispherehost}}"
324 username: "{{username}}"
325 password: "{{password}}"
326 validate_certs: "{{validate_certs}}"
327 nas_server_name: "dummy_nas"
328 replication_state: "disable"
329 state: "present"
330
331 - name: Disable replication by specifying replication_name on the nasserver
332 nasserver:
333 unispherehost: "{{unispherehost}}"
334 username: "{{username}}"
335 password: "{{password}}"
336 validate_certs: "{{validate_certs}}"
337 nas_server_name: "dummy_nas"
338 replication_params:
339 replication_name: "test_replication"
340 replication_state: "disable"
341 state: "present"
342 '''
343
344 RETURN = r'''
345 changed:
346 description: Whether or not the resource has changed.
347 returned: always
348 type: bool
349 sample: true
350 nas_server_details:
351 description: The NAS server details.
352 type: dict
353 returned: When NAS server exists.
354 contains:
355 name:
356 description: Name of the NAS server.
357 type: str
358 id:
359 description: ID of the NAS server.
360 type: str
361 allow_unmapped_user:
362 description: Enable/disable access status in case of any user
363 mapping failure.
364 type: bool
365 current_unix_directory_service:
366 description: Directory service used for querying identity
367 information for UNIX (such as UIDs, GIDs, net groups).
368 type: str
369 default_unix_user:
370 description: Default Unix user name used for granting access
371 in the case of Windows to Unix user mapping failure.
372 type: str
373 default_windows_user:
374 description: Default windows user name used for granting
375 access in the case of Unix to Windows user mapping
376 failure.
377 type: str
378 is_backup_only:
379 description: Whether the NAS server is used as backup only.
380 type: bool
381 is_multi_protocol_enabled:
382 description: Indicates whether multiprotocol sharing mode is
383 enabled.
384 type: bool
385 is_packet_reflect_enabled:
386 description: If the packet reflect has to be enabled.
387 type: bool
388 is_replication_destination:
389 description: If the NAS server is a replication destination
390 then true.
391 type: bool
392 is_windows_to_unix_username_mapping_enabled:
393 description: Indicates whether a Unix to/from Windows user name
394 mapping is enabled.
395 type: bool
396 sample: {
397 "allow_unmapped_user": null,
398 "cifs_server": {
399 "UnityCifsServerList": [
400 {
401 "UnityCifsServer": {
402 "hash": 8761756885270,
403 "id": "cifs_34"
404 }
405 }
406 ]
407 },
408 "current_sp": {
409 "UnityStorageProcessor": {
410 "hash": 8761756885273,
411 "id": "spb"
412 }
413 },
414 "current_unix_directory_service": "NasServerUnixDirectoryServiceEnum.NIS",
415 "default_unix_user": null,
416 "default_windows_user": null,
417 "existed": true,
418 "file_dns_server": {
419 "UnityFileDnsServer": {
420 "hash": 8761756885441,
421 "id": "dns_12"
422 }
423 },
424 "file_interface": {
425 "UnityFileInterfaceList": [
426 {
427 "UnityFileInterface": {
428 "hash": 8761756889908,
429 "id": "if_37"
430 }
431 }
432 ]
433 },
434 "filesystems": null,
435 "hash": 8761757005084,
436 "health": {
437 "UnityHealth": {
438 "hash": 8761756867588
439 }
440 },
441 "home_sp": {
442 "UnityStorageProcessor": {
443 "hash": 8761756867618,
444 "id": "spb"
445 }
446 },
447 "id": "nas_10",
448 "is_backup_only": false,
449 "is_multi_protocol_enabled": false,
450 "is_packet_reflect_enabled": false,
451 "is_replication_destination": false,
452 "is_replication_enabled": true,
453 "is_windows_to_unix_username_mapping_enabled": null,
454 "name": "dummy_nas",
455 "pool": {
456 "UnityPool": {
457 "hash": 8761756885360,
458 "id": "pool_7"
459 }
460 },
461 "preferred_interface_settings": {
462 "UnityPreferredInterfaceSettings": {
463 "hash": 8761756885438,
464 "id": "preferred_if_10"
465 }
466 },
467 "replication_type": "ReplicationTypeEnum.REMOTE",
468 "size_allocated": 3489660928,
469 "tenant": null,
470 "virus_checker": {
471 "UnityVirusChecker": {
472 "hash": 8761756885426,
473 "id": "cava_10"
474 }
475 }
476 }
477 '''
478
479 from ansible.module_utils.basic import AnsibleModule
480 from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \
481 import utils
482 LOG = utils.get_logger('nasserver')
483
484 application_type = "Ansible/1.7.1"
485
486
487 class NASServer(object):
488 """Class with NAS Server operations"""
489
490 def __init__(self):
491 """ Define all parameters required by this module"""
492 self.module_params = utils.get_unity_management_host_parameters()
493 self.module_params.update(get_nasserver_parameters())
494
495 # initialize the ansible module
496 mut_ex_args = [['nas_server_name', 'nas_server_id']]
497 required_one_of = [['nas_server_name', 'nas_server_id']]
498
499 self.module = AnsibleModule(
500 argument_spec=self.module_params,
501 supports_check_mode=False,
502 mutually_exclusive=mut_ex_args,
503 required_one_of=required_one_of
504 )
505 utils.ensure_required_libs(self.module)
506
507 # result is a dictionary that contains changed status and
508 # nas server details
509 self.result = {"changed": False,
510 'nas_server_details': {}}
511
512 self.unity_conn = utils.get_unity_unisphere_connection(
513 self.module.params, application_type)
514 self.nas_server_conn_obj = utils.nas_server.UnityNasServer(
515 self.unity_conn)
516 LOG.info('Connection established with the Unity Array')
517
518 def get_current_uds_enum(self, current_uds):
519 """
520 Get the enum of the Offline Availability parameter.
521 :param current_uds: Current Unix Directory Service string
522 :return: current_uds enum
523 """
524 if current_uds in \
525 utils.NasServerUnixDirectoryServiceEnum.__members__:
526 return utils.NasServerUnixDirectoryServiceEnum[current_uds]
527 else:
528 error_msg = "Invalid value {0} for Current Unix Directory" \
529 " Service provided".format(current_uds)
530 LOG.error(error_msg)
531 self.module.fail_json(msg=error_msg)
532
533 def get_nas_server(self, nas_server_name, nas_server_id):
534 """
535 Get the NAS Server Object using NAME/ID of the NAS Server.
536 :param nas_server_name: Name of the NAS Server
537 :param nas_server_id: ID of the NAS Server
538 :return: NAS Server object.
539 """
540 nas_server = nas_server_name if nas_server_name else nas_server_id
541 try:
542 obj_nas = self.unity_conn.get_nas_server(_id=nas_server_id,
543 name=nas_server_name)
544 if nas_server_id and obj_nas and not obj_nas.existed:
545 # if obj_nas is not None and existed is observed as False,
546 # then None will be returned.
547 LOG.error("NAS Server object does not exist"
548 " with ID: %s ", nas_server_id)
549 return None
550 return obj_nas
551 except utils.HttpError as e:
552 if e.http_status == 401:
553 cred_err = "Incorrect username or password , {0}".format(
554 e.message)
555 self.module.fail_json(msg=cred_err)
556 else:
557 err_msg = "Failed to get details of NAS Server" \
558 " {0} with error {1}".format(nas_server, str(e))
559 LOG.error(err_msg)
560 self.module.fail_json(msg=err_msg)
561
562 except utils.UnityResourceNotFoundError as e:
563 err_msg = "Failed to get details of NAS Server" \
564 " {0} with error {1}".format(nas_server, str(e))
565 LOG.error(err_msg)
566 return None
567
568 except Exception as e:
569 nas_server = nas_server_name if nas_server_name \
570 else nas_server_id
571 err_msg = "Failed to get nas server details {0} with" \
572 " error {1}".format(nas_server, str(e))
573 LOG.error(err_msg)
574 self.module.fail_json(msg=err_msg)
575
576 def to_update(self, nas_server_obj, current_uds):
577 LOG.info("Checking Whether the parameters are modified or not.")
578
579 # Checking all parameters individually because the nas obj return
580 # names are different compared to ansible parameter names.
581
582 # Current Unix Directory Service
583 if current_uds is not None and \
584 current_uds != nas_server_obj.current_unix_directory_service:
585 return True
586
587 # Rename NAS Server
588 if self.module.params['nas_server_new_name'] is not None and \
589 self.module.params['nas_server_new_name'] != \
590 nas_server_obj.name:
591 return True
592
593 # Is Replication Destination
594 if self.module.params["is_replication_destination"] is not None and \
595 (nas_server_obj.is_replication_destination is None or
596 self.module.params["is_replication_destination"] !=
597 nas_server_obj.is_replication_destination):
598 return True
599
600 # Is Multiprotocol Enabled
601 if self.module.params["is_multiprotocol_enabled"] is not None and \
602 (nas_server_obj.is_multi_protocol_enabled is None or
603 self.module.params["is_multiprotocol_enabled"] !=
604 nas_server_obj.is_multi_protocol_enabled):
605 return True
606
607 # Is Back Up Enabled
608 if self.module.params["is_backup_only"] is not None and \
609 (nas_server_obj.is_backup_only is None or
610 self.module.params["is_backup_only"] !=
611 nas_server_obj.is_backup_only):
612 return True
613
614 # Is Packet Reflect Enabled
615 if self.module.params["is_packet_reflect_enabled"] is not None and \
616 (nas_server_obj.is_packet_reflect_enabled is None or
617 self.module.params["is_packet_reflect_enabled"] !=
618 nas_server_obj.is_packet_reflect_enabled):
619 return True
620
621 # Allow Unmapped User
622 if self.module.params["allow_unmapped_user"] is not None and \
623 (nas_server_obj.allow_unmapped_user is None or
624 self.module.params["allow_unmapped_user"] !=
625 nas_server_obj.allow_unmapped_user):
626 return True
627
628 # Enable Windows To Unix User Mapping Flag
629 nas_win_flag = \
630 nas_server_obj.is_windows_to_unix_username_mapping_enabled
631 input_win_flag = \
632 self.module.params["enable_windows_to_unix_username_mapping"]
633 if input_win_flag is not None and \
634 (nas_win_flag is None or nas_win_flag != input_win_flag):
635 return True
636
637 # Default Windows User
638 if self.module.params["default_windows_user"] is not None and \
639 (nas_server_obj.default_windows_user is None or
640 self.module.params["default_windows_user"] !=
641 nas_server_obj.default_windows_user):
642 return True
643
644 # Default Unix User
645 if self.module.params["default_unix_user"] is not None and \
646 (nas_server_obj.default_unix_user is None or
647 self.module.params["default_unix_user"] !=
648 nas_server_obj.default_unix_user):
649 return True
650
651 return False
652
653 def update_nas_server(self, nas_server_obj, new_name=None,
654 default_unix_user=None, default_windows_user=None,
655 is_rep_dest=None, is_multiprotocol_enabled=None,
656 allow_unmapped_user=None, is_backup_only=None,
657 is_packet_reflect_enabled=None, current_uds=None,
658 enable_win_to_unix_name_map=None):
659 """
660 The Details of the NAS Server will be updated in the function.
661 """
662 try:
663 nas_server_obj.modify(
664 name=new_name,
665 is_replication_destination=is_rep_dest,
666 is_backup_only=is_backup_only,
667 is_multi_protocol_enabled=is_multiprotocol_enabled,
668 default_unix_user=default_unix_user,
669 default_windows_user=default_windows_user,
670 allow_unmapped_user=allow_unmapped_user,
671 is_packet_reflect_enabled=is_packet_reflect_enabled,
672 enable_windows_to_unix_username=enable_win_to_unix_name_map,
673 current_unix_directory_service=current_uds)
674
675 except Exception as e:
676 error_msg = "Failed to Update parameters of NAS Server" \
677 " %s with error %s" % (nas_server_obj.name, str(e))
678 LOG.error(error_msg)
679 self.module.fail_json(msg=error_msg)
680
681 def modify_replication_session(self, nas_server_obj, repl_session, replication_params):
682 """ Modify the replication session
683 :param: nas_server_obj: NAS server object
684 :param: repl_session: Replication session to be modified
685 :param: replication_params: Module input params
686 :return: True if modification is successful
687 """
688 try:
689 LOG.info("Modifying replication session of nas server %s", nas_server_obj.name)
690 modify_payload = {}
691 if replication_params['replication_mode'] and \
692 replication_params['replication_mode'] == 'manual':
693 rpo = -1
694 elif replication_params['rpo']:
695 rpo = replication_params['rpo']
696 name = repl_session.name
697 if replication_params['new_replication_name'] and \
698 name != replication_params['new_replication_name']:
699 name = replication_params['new_replication_name']
700
701 if repl_session.name != name:
702 modify_payload['name'] = name
703 if ((replication_params['replication_mode'] or replication_params['rpo']) and
704 repl_session.max_time_out_of_sync != rpo):
705 modify_payload['max_time_out_of_sync'] = rpo
706
707 if modify_payload:
708 repl_session.modify(**modify_payload)
709 return True
710
711 return False
712 except Exception as e:
713 errormsg = "Modifying replication session failed with error %s", e
714 LOG.error(errormsg)
715 self.module.fail_json(msg=errormsg)
716
717 def enable_replication(self, nas_server_obj, replication, replication_reuse_resource):
718 """ Enable replication on NAS Server
719 :param: nas_server_obj: NAS Server object.
720 :param: replication: Dict which has all the replication parameter values.
721 :return: True if replication is enabled else False.
722 """
723 try:
724 # Validate replication params
725 self.validate_nas_server_replication_params(replication)
726 self.update_replication_params(replication, replication_reuse_resource)
727
728 repl_session = \
729 self.get_replication_session_on_filter(nas_server_obj, replication, "modify")
730 if repl_session:
731 return self.modify_replication_session(nas_server_obj, repl_session, replication)
732
733 self.validate_create_replication_params(replication)
734 replication_args_list = get_replication_args_list(replication)
735
736 # Get remote system
737 if 'replication_type' in replication and replication['replication_type'] == 'remote':
738 self.get_remote_system(replication, replication_args_list)
739
740 # Form parameters when replication_reuse_resource is False
741 if not replication_reuse_resource:
742 update_replication_arg_list(replication, replication_args_list, nas_server_obj)
743 nas_server_obj.replicate_with_dst_resource_provisioning(**replication_args_list)
744 else:
745 replication_args_list['dst_nas_server_id'] = replication['destination_nas_server_id']
746 nas_server_obj.replicate(**replication_args_list)
747 return True
748
749 if 'replication_type' in replication and replication['replication_type'] == 'local':
750 update_replication_arg_list(replication, replication_args_list, nas_server_obj)
751 nas_server_obj.replicate_with_dst_resource_provisioning(**replication_args_list)
752 return True
753
754 except Exception as e:
755 errormsg = "Enabling replication to the nas server %s failed " \
756 "with error %s" % (nas_server_obj.name, str(e))
757 LOG.error(errormsg)
758 self.module.fail_json(msg=errormsg)
759
760 def disable_replication(self, obj_nas, replication_params):
761 """ Remove replication from the nas server
762 :param: replication_params: Module input params
763 :param: obj_nas: NAS Server object
764 :return: True if disabling replication is successful
765 """
766 try:
767 LOG.info(("Disabling replication on the nas server %s", obj_nas.name))
768 if replication_params:
769 self.update_replication_params(replication_params, False)
770 repl_session = \
771 self.get_replication_session_on_filter(obj_nas, replication_params, "delete")
772 if repl_session:
773 repl_session.delete()
774 return True
775 return False
776 except Exception as e:
777 errormsg = "Disabling replication on the nas server %s failed " \
778 "with error %s" % (obj_nas.name, str(e))
779 LOG.error(errormsg)
780 self.module.fail_json(msg=errormsg)
781
782 def get_replication_session_on_filter(self, obj_nas, replication_params, action):
783 """ Retrieves replication session on nas server
784 :param: obj_nas: NAS server object
785 :param: replication_params: Module input params
786 :param: action: Specifies action as modify or delete
787 :return: Replication session based on filter
788 """
789 if replication_params and replication_params['remote_system']:
790 repl_session = \
791 self.get_replication_session(obj_nas, filter_key="remote_system_name",
792 replication_params=replication_params)
793 elif replication_params and replication_params['replication_name']:
794 repl_session = \
795 self.get_replication_session(obj_nas, filter_key="name",
796 name=replication_params['replication_name'])
797 else:
798 repl_session = self.get_replication_session(obj_nas, action=action)
799 if repl_session and action and replication_params and \
800 replication_params['replication_type'] == 'local' and \
801 repl_session.remote_system.name != self.unity_conn.name:
802 return None
803 return repl_session
804
805 def get_replication_session(self, obj_nas, filter_key=None, replication_params=None, name=None, action=None):
806 """ Retrieves the replication sessions configured for the nas server
807 :param: obj_nas: NAS server object
808 :param: filter_key: Key to filter replication sessions
809 :param: replication_params: Module input params
810 :param: name: Replication session name
811 :param: action: Specifies modify or delete action on replication session
812 :return: Replication session details
813 """
814 try:
815 repl_session = self.unity_conn.get_replication_session(src_resource_id=obj_nas.id)
816 if not filter_key and repl_session:
817 if len(repl_session) > 1:
818 if action:
819 error_msg = 'There are multiple replication sessions for the nas server.'\
820 ' Please specify replication_name in replication_params to %s.' % action
821 self.module.fail_json(msg=error_msg)
822 return repl_session
823 return repl_session[0]
824 for session in repl_session:
825 if filter_key == 'remote_system_name' and \
826 session.remote_system.name == replication_params['remote_system_name']:
827 return session
828 if filter_key == 'name' and session.name == name:
829 return session
830 return None
831 except Exception as e:
832 errormsg = "Retrieving replication session on the nas server failed " \
833 "with error %s", str(e)
834 LOG.error(errormsg)
835 self.module.fail_json(msg=errormsg)
836
837 def get_remote_system(self, replication, replication_args_list):
838 remote_system_name = replication['remote_system_name']
839 remote_system_list = self.unity_conn.get_remote_system()
840 for remote_system in remote_system_list:
841 if remote_system.name == remote_system_name:
842 replication_args_list['remote_system'] = remote_system
843 break
844 if 'remote_system' not in replication_args_list.keys():
845 errormsg = "Remote system %s is not found" % (remote_system_name)
846 LOG.error(errormsg)
847 self.module.fail_json(msg=errormsg)
848
849 def update_replication_params(self, replication, replication_reuse_resource):
850 """ Update replication dict with remote system information
851 :param: replication: Dict which has all the replication parameter values
852 :return: Updated replication Dict
853 """
854 try:
855 if 'replication_type' in replication and replication['replication_type'] == 'remote':
856 connection_params = {
857 'unispherehost': replication['remote_system']['remote_system_host'],
858 'username': replication['remote_system']['remote_system_username'],
859 'password': replication['remote_system']['remote_system_password'],
860 'validate_certs': replication['remote_system']['remote_system_verifycert'],
861 'port': replication['remote_system']['remote_system_port']
862 }
863 remote_system_conn = utils.get_unity_unisphere_connection(
864 connection_params, application_type)
865 replication['remote_system_name'] = remote_system_conn.name
866 if replication['destination_pool_name'] is not None:
867 pool_object = remote_system_conn.get_pool(name=replication['destination_pool_name'])
868 replication['destination_pool_id'] = pool_object.id
869 if replication['destination_nas_server_name'] is not None and replication_reuse_resource:
870 nas_object = remote_system_conn.get_nas_server(name=replication['destination_nas_server_name'])
871 replication['destination_nas_server_id'] = nas_object.id
872 else:
873 replication['remote_system_name'] = self.unity_conn.name
874 if replication['destination_pool_name'] is not None:
875 pool_object = self.unity_conn.get_pool(name=replication['destination_pool_name'])
876 replication['destination_pool_id'] = pool_object.id
877 except Exception as e:
878 errormsg = "Updating replication params failed with error %s" % str(e)
879 LOG.error(errormsg)
880 self.module.fail_json(msg=errormsg)
881
882 def validate_rpo(self, replication):
883 if 'replication_mode' in replication and replication['replication_mode'] == 'asynchronous' \
884 and replication['rpo'] is None:
885 errormsg = "rpo is required together with 'asynchronous' replication_mode."
886 LOG.error(errormsg)
887 self.module.fail_json(msg=errormsg)
888
889 if (replication['rpo'] and (replication['rpo'] < 5 or replication['rpo'] > 1440)) \
890 and (replication['replication_mode'] and replication['replication_mode'] != 'manual' or
891 not replication['replication_mode'] and replication['rpo'] != -1):
892 errormsg = "rpo value should be in range of 5 to 1440"
893 LOG.error(errormsg)
894 self.module.fail_json(msg=errormsg)
895
896 def validate_nas_server_replication_params(self, replication):
897 """ Validate NAS server replication params
898 :param: replication: Dict which has all the replication parameter values
899 """
900
901 # Valdiate replication
902 if replication is None:
903 errormsg = "Please specify replication_params to enable replication."
904 LOG.error(errormsg)
905 self.module.fail_json(msg=errormsg)
906 else:
907 # validate destination pool info
908 if replication['destination_pool_id'] is not None and replication['destination_pool_name'] is not None:
909 errormsg = "'destination_pool_id' and 'destination_pool_name' is mutually exclusive."
910 LOG.error(errormsg)
911 self.module.fail_json(msg=errormsg)
912
913 # Validate replication mode
914 self.validate_rpo(replication)
915 # Validate replication type
916 if replication['replication_type'] == 'remote' and replication['remote_system'] is None:
917 errormsg = "Remote_system is required together with 'remote' replication_type"
918 LOG.error(errormsg)
919 self.module.fail_json(msg=errormsg)
920
921 # Validate destination NAS server name
922 if 'destination_nas_name' in replication and replication['destination_nas_name'] is not None:
923 dst_nas_server_name_length = len(replication['destination_nas_name'])
924 if dst_nas_server_name_length == 0 or dst_nas_server_name_length > 95:
925 errormsg = "destination_nas_name value should be in range of 1 to 95"
926 LOG.error(errormsg)
927 self.module.fail_json(msg=errormsg)
928
929 def validate_create_replication_params(self, replication):
930 ''' Validate replication params '''
931 if replication['destination_pool_id'] is None and replication['destination_pool_name'] is None:
932 errormsg = "Either 'destination_pool_id' or 'destination_pool_name' is required."
933 LOG.error(errormsg)
934 self.module.fail_json(msg=errormsg)
935
936 keys = ['replication_mode', 'replication_type']
937 for key in keys:
938 if replication[key] is None:
939 errormsg = "Please specify %s to enable replication." % key
940 LOG.error(errormsg)
941 self.module.fail_json(msg=errormsg)
942
943 def perform_module_operation(self):
944 """
945 Perform different actions on NAS Server based on user parameters
946 chosen in playbook
947 """
948 state = self.module.params['state']
949 nas_server_name = self.module.params['nas_server_name']
950 nas_server_id = self.module.params['nas_server_id']
951 nas_server_new_name = self.module.params['nas_server_new_name']
952 default_unix_user = self.module.params['default_unix_user']
953 default_windows_user = self.module.params['default_windows_user']
954
955 is_replication_destination = \
956 self.module.params['is_replication_destination']
957 is_multiprotocol_enabled = \
958 self.module.params['is_multiprotocol_enabled']
959 allow_unmapped_user = self.module.params['allow_unmapped_user']
960 enable_windows_to_unix_username_mapping = \
961 self.module.params['enable_windows_to_unix_username_mapping']
962
963 is_backup_only = self.module.params['is_backup_only']
964 is_packet_reflect_enabled = \
965 self.module.params['is_packet_reflect_enabled']
966
967 current_uds = self.module.params['current_unix_directory_service']
968 replication = self.module.params['replication_params']
969 replication_state = self.module.params['replication_state']
970 replication_reuse_resource = self.module.params['replication_reuse_resource']
971 # Get the enum for the corresponding offline_availability
972 if current_uds:
973 current_uds = \
974 self.get_current_uds_enum(current_uds)
975
976 changed = False
977
978 if replication and replication_state is None:
979 self.module.fail_json(msg="Please specify replication_state along with replication_params")
980
981 '''
982 Get details of NAS Server.
983 '''
984 nas_server_obj = None
985 if nas_server_name or nas_server_id:
986 nas_server_obj = self.get_nas_server(nas_server_name,
987 nas_server_id)
988
989 # As creation is not supported and if NAS Server does not exist
990 # along with state as present, then error will be thrown.
991 if not nas_server_obj and state == "present":
992 msg = "NAS Server Resource not found. Please enter a valid " \
993 "Name/ID to get or modify the parameters of nas server."
994 LOG.error(msg)
995 self.module.fail_json(msg=msg)
996
997 '''
998 Update the parameters of NAS Server
999 '''
1000 if nas_server_obj and state == "present":
1001 update_flag = self.to_update(nas_server_obj, current_uds)
1002 if update_flag:
1003 self.update_nas_server(
1004 nas_server_obj, nas_server_new_name, default_unix_user,
1005 default_windows_user, is_replication_destination,
1006 is_multiprotocol_enabled, allow_unmapped_user,
1007 is_backup_only, is_packet_reflect_enabled,
1008 current_uds, enable_windows_to_unix_username_mapping)
1009 changed = True
1010
1011 # As deletion is not supported and if NAS Server exists along with
1012 # state as absent, then error will be thrown.
1013 if nas_server_obj and state == 'absent':
1014 self.module.fail_json(msg="Deletion of NAS Server is "
1015 "currently not supported.")
1016
1017 if state == 'present' and nas_server_obj and replication_state is not None:
1018 if replication_state == 'enable':
1019 changed = self.enable_replication(nas_server_obj, replication, replication_reuse_resource)
1020 else:
1021 changed = self.disable_replication(nas_server_obj, replication)
1022
1023 '''
1024 Update the changed state and NAS Server details
1025 '''
1026 nas_server_details = None
1027 if nas_server_obj:
1028 nas_server_details = self.get_nas_server(
1029 None, nas_server_obj.id)._get_properties()
1030
1031 self.result["changed"] = changed
1032 self.result["nas_server_details"] = nas_server_details
1033 self.module.exit_json(**self.result)
1034
1035
1036 def get_nasserver_parameters():
1037 """
1038 This method provides parameters required for the ansible NAS Server
1039 modules on Unity
1040 """
1041
1042 return dict(
1043 nas_server_name=dict(), nas_server_id=dict(),
1044 nas_server_new_name=dict(),
1045 default_unix_user=dict(),
1046 default_windows_user=dict(),
1047 current_unix_directory_service=dict(
1048 choices=["NIS", "LDAP", "LOCAL_THEN_NIS",
1049 "LOCAL_THEN_LDAP", "NONE", "LOCAL"]),
1050 is_replication_destination=dict(type='bool'),
1051 is_backup_only=dict(type='bool'),
1052 is_multiprotocol_enabled=dict(type='bool'),
1053 allow_unmapped_user=dict(type='bool'),
1054 enable_windows_to_unix_username_mapping=dict(type='bool'),
1055 is_packet_reflect_enabled=dict(type='bool'),
1056 replication_params=dict(type='dict', options=dict(
1057 destination_nas_server_name=dict(type='str'),
1058 replication_mode=dict(type='str', choices=['asynchronous', 'manual']),
1059 rpo=dict(type='int'),
1060 replication_type=dict(type='str', choices=['local', 'remote']),
1061 remote_system=dict(type='dict',
1062 options=dict(
1063 remote_system_host=dict(type='str', required=True, no_log=True),
1064 remote_system_verifycert=dict(type='bool', required=False,
1065 default=True),
1066 remote_system_username=dict(type='str', required=True),
1067 remote_system_password=dict(type='str', required=True, no_log=True),
1068 remote_system_port=dict(type='int', required=False, default=443, no_log=True)
1069 )),
1070 destination_pool_name=dict(type='str'),
1071 destination_pool_id=dict(type='str'),
1072 destination_sp=dict(type='str', choices=['SPA', 'SPB']),
1073 is_backup=dict(type='bool'),
1074 replication_name=dict(type='str'),
1075 new_replication_name=dict(type='str')
1076 )),
1077 replication_reuse_resource=dict(type='bool'),
1078 replication_state=dict(type='str', choices=['enable', 'disable']),
1079 state=dict(required=True, choices=['present', 'absent'], type='str')
1080 )
1081
1082
1083 def get_sp_enum(destination_sp):
1084 """Getting correct enum values for Storage Processor
1085 :param: destination_sp: Storage Processor to be used in Destination NAS Server.
1086 :return: enum value for Storage Processor.
1087 """
1088 if utils.NodeEnum[destination_sp]:
1089 destination_sp_enum = utils.NodeEnum[destination_sp]
1090 return destination_sp_enum
1091
1092
1093 def get_replication_args_list(replication_params):
1094 """Returns the replication args for payload"""
1095 replication_args_list = {}
1096
1097 if replication_params['replication_name']:
1098 replication_args_list['replication_name'] = replication_params['replication_name']
1099 if 'replication_mode' in replication_params and \
1100 replication_params['replication_mode'] == 'asynchronous':
1101 replication_args_list['max_time_out_of_sync'] = replication_params['rpo']
1102 else:
1103 replication_args_list['max_time_out_of_sync'] = -1
1104
1105 return replication_args_list
1106
1107
1108 def update_replication_arg_list(replication, replication_args_list, nas_server_obj):
1109 """ Update replication arg list
1110 :param: replication: Dict which has all the replication parameter values
1111 :param: replication_args_list: the existing list which should be updated
1112 :param: nas_server_obj: NAS Server object on which replication is to be enabled
1113 :return: Updated replication_args_list
1114 """
1115 if 'destination_sp' in replication and replication['destination_sp']:
1116 dst_sp_enum = get_sp_enum(replication['destination_sp'])
1117 replication_args_list['dst_sp'] = dst_sp_enum
1118
1119 replication_args_list['dst_pool_id'] = replication['destination_pool_id']
1120
1121 if 'is_backup' in replication and replication['is_backup']:
1122 replication_args_list['is_backup_only'] = replication['is_backup']
1123
1124 if replication['replication_type'] == 'local':
1125 replication_args_list['dst_nas_server_name'] = "DR_" + nas_server_obj.name
1126 if 'destination_nas_server_name' in replication and replication['destination_nas_server_name'] is not None:
1127 replication_args_list['dst_nas_server_name'] = replication['destination_nas_server_name']
1128 else:
1129 if replication['destination_nas_server_name'] is None:
1130 replication_args_list['dst_nas_server_name'] = nas_server_obj.name
1131
1132
1133 def main():
1134 """ Create Unity NAS Server object and perform action on it
1135 based on user input from playbook"""
1136 obj = NASServer()
1137 obj.perform_module_operation()
1138
1139
1140 if __name__ == '__main__':
1141 main()