
    VhpJ                         d dl mZmZmZ dZdZdZd dlZd dlm	Z	 d dl
mZmZ eZdZ G d	 d
e      Z G d de      Zd Zd ZddZd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionav  
module: nxos_devicealias
short_description: Configuration of device alias for Cisco NXOS MDS Switches.
description:
- Configuration of device alias for Cisco MDS NXOS.
version_added: 1.0.0
author:
- Suhas Bharadwaj (@srbharadwaj) (subharad@cisco.com)
notes:
- Tested against Cisco MDS NX-OS 8.4(1)
options:
  distribute:
    description:
    - Enable/Disable device-alias distribution
    type: bool
  mode:
    description:
    - Mode of devices-alias, basic or enhanced
    choices:
    - basic
    - enhanced
    type: str
  da:
    description:
    - List of device-alias to be added or removed
    type: list
    elements: dict
    suboptions:
      name:
        description:
        - Name of the device-alias to be added or removed
        required: true
        type: str
      pwwn:
        description:
        - pwwn to which the name needs to be associated with
        type: str
      remove:
        description:
        - Removes the device-alias if set to True
        type: bool
        default: false
  rename:
    description:
    - List of device-alias to be renamed
    type: list
    elements: dict
    suboptions:
      old_name:
        description:
        - Old name of the device-alias that needs to be renamed
        required: true
        type: str
      new_name:
        description:
        - New name of the device-alias
        required: true
        type: str
a  
- name: Test that device alias module works
  cisco.nxos.nxos_devicealias:
    da:
      - name: test1_add
        pwwn: 56:2:22:11:22:88:11:67
      - name: test2_add
        pwwn: 65:22:22:11:22:22:11:d
      - name: dev1
        remove: true
      - name: dev2
        remove: true
    distribute: true
    mode: enhanced
    rename:
      - new_name: bcd
        old_name: abc
      - new_name: bcd1
        old_name: abc1
aF  
commands:
  description: commands sent to the device
  returned: always
  type: list
  sample:
    - terminal dont-ask
    - device-alias database
    - device-alias name somename pwwn 10:00:00:00:89:a1:01:03
    - device-alias name somename1 pwwn 10:00:00:00:89:a1:02:03
    - device-alias commit
    - no terminal dont-ask
N)AnsibleModule)load_configrun_commands)-_$^c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	showDeviceAliasStatusz#docstring for showDeviceAliasStatusc                 \    || _         d| _        d| _        d| _        | j	                          y )N F)module
distributemodelockedupdateselfr   s     o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_devicealias.py__init__zshowDeviceAliasStatus.__init__z   s'    	    c                 8    t        || j                        d   }|S Nr   execute_show_commandr   r   cmdoutputs      r   execute_show_cmdz&showDeviceAliasStatus.execute_show_cmd       %c4;;7:r   c                 R   d}| j                  |      j                  d      }|D ]  }d|v r5|j                  d      d   j                         j                         | _        d|v r5|j                  d      d   j                         j                         | _        d|v szd	| _         y )
Nzshow device-alias status
zFabric Distribution:   ModezMode:LockedT)r"   splitstriplowerr   r   r   )r   commandr!   os       r   r   zshowDeviceAliasStatus.update   s    ,&&w/55d; 	#A$)"#''#,q/"7"7"9"?"?"A{GGG,Q/557==?	1}"	#r   c                     | j                   S N)r   r   s    r   isLockedzshowDeviceAliasStatus.isLocked   s    {{r   c                     | j                   S r0   )r   r1   s    r   getDistributez#showDeviceAliasStatus.getDistribute   s    r   c                     | j                   S r0   )r   r1   s    r   getModezshowDeviceAliasStatus.getMode   s    yyr   N)
__name__
__module____qualname____doc__r   r"   r   r2   r4   r6    r   r   r   r   w   s#    -	#r   r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)showDeviceAliasDatabasez%docstring for showDeviceAliasDatabasec                 @    || _         i | _        | j                          y r0   )r   da_dictr   r   s     r   r   z showDeviceAliasDatabase.__init__   s    r   c                 8    t        || j                        d   }|S r   r   r   s      r   r"   z(showDeviceAliasDatabase.execute_show_cmd   r#   r   c                     d}| j                  |      }|j                  d      | _        | j                  D ]:  }d|v s|j                         j                         }|d   | j                  |d   <   < y )Nzshow device-alias databaser%   zdevice-alias      )r"   r*   da_listr+   r?   )r   r-   r!   eachlinesvs        r   r   zshowDeviceAliasDatabase.update   sm    .&&w/||D) 	,H)^^%++-&(eRU#	,r   c                 :    || j                   j                         v S r0   r?   keysr   names     r   isNameInDaDatabasez*showDeviceAliasDatabase.isNameInDaDatabase   s    t||((***r   c           	          dj                  |j                  d      D cg c]   }t        |      dk(  rdt        |      z   n|" c}      }|| j                  j                         v S c c}w Nr&   r'   0)joinr*   lenstrr?   values)r   pwwnepnewpwwns       r   isPwwnInDaDatabasez*showDeviceAliasDatabase.isPwwnInDaDatabase   s]    ((=AZZ_Mrc"glS3r7]2M
 $,,--/// Ns   %A'c           	          dj                  |j                  d      D cg c]   }t        |      dk(  rdt        |      z   n|" c}      }|| j                  j                         v r|| j                  |   k(  ryyc c}w )Nr&   r'   rO   TF)rP   r*   rQ   rR   r?   rI   )r   rK   rT   rU   rV   s        r   isNamePwwnPresentInDatabasez3showDeviceAliasDatabase.isNamePwwnPresentInDatabase   sq    ((=AZZ_Mrc"glS3r7]2M
 4<<$$&&$,,t,, Ns   %A;c                 Z    || j                   j                         v r| j                   |   S y r0   rH   rJ   s     r   getPwwnByNamez%showDeviceAliasDatabase.getPwwnByName   s)    4<<$$&&<<%%r   c           	          dj                  |j                  d      D cg c]   }t        |      dk(  rdt        |      z   n|" c}      }| j                  j                         D ]  \  }}||k(  s|c S  y c c}w rN   )rP   r*   rQ   rR   r?   items)r   rT   rU   rV   nps         r   getNameByPwwnz%showDeviceAliasDatabase.getNameByPwwn   sv    ((=AZZ_Mrc"glS3r7]2M
 LL&&( 	DAqG|	  Ns   %A7N)r7   r8   r9   r:   r   r"   r   rL   rW   rY   r[   r`   r;   r   r   r=   r=      s-    /
,+0r   r=   c                     | j                  d      }t        |      dk7  ry|D ]4  }t        |      dkD  st        |      dk  r yt        d |D              r4 y y)Nr&      FrC   r'   c              3   @   K   | ]  }|t         j                  v   y wr0   )string	hexdigits).0cs     r   	<genexpr>zisPwwnValid.<locals>.<genexpr>   s     @Q1(((@s   T)r*   rQ   all)rT   	pwwnspliteachpwwnsplits      r   isPwwnValidrl      s_    

3I
9~" }!S%7!%;@-@@	
 r   c                     | d   j                         syt        |       dkD  ry| D ]  }|j                         r|t        vs y y)Nr   F@   T)isalpharQ   isalnumVALID_DA_CHARS)rK   	characters     r   isNameValidrs      sK    7??
4y2~ 	  "y'F r   c                 .    d}| |dg}t        ||      }|S )Ntext)r-   r!   )r   )r-   r   command_typer!   commandsouts         r   r   r      s%    F#v67H
vx
(CJr   c                 ~    g }| D ]5  }t        |t              r|j                  |       %|j                  |       7 |S r0   )
isinstancelistextendappend)command_listsflat_command_listr-   s      r   flatten_listr      sE      .gt$$$W-$$W-	.
 r   c                     t        t        dd      t        d      t        dd            } t        t        dd      t        dd      	      }t        t        d      t        dd
dg      t        dd|       t        dd|            }t        |d      }t               }t               }t               }ddi}|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|
|
D ]  }|d   }|d   }|d   }||j	                         }|r'|!|j                  dt        |      z   dz          t        |      sW|j                  dt        |      z   dz   dz   d j                  t        D cg c]  }d!j                  |       c}      z          t        |      r|j                  d"t        |      z   d#z           ||D ]  }|d$   }|d%   }t        |      sW|j                  dt        |      z   dz   dz   d j                  t        D cg c]  }d!j                  |       c}      z          t        |      r{|j                  dt        |      z   dz   dz   d j                  t        D cg c]  }d!j                  |       c}      z           t        |      }|j                         }|j                         }|j                         r|j                  d&       g }|u|r:|d'k(  r#|j!                  d(       |j!                  d)       nK|j!                  d*       n9|d+k(  r#|j!                  d,       |j!                  d-       n|j!                  d.       t#        |      }|r#||z   }|j$                  rnd|d<   t'        ||       g }|	x|	dk(  r:|d
k(  r#|j!                  d/       |j!                  d0       nK|j!                  d1       n9|dk(  r#|j!                  d2       |j!                  d3       n|j!                  d4       |r@|r|j!                  d5       d6g|z   d7gz   }n"| |d+k(  r|j!                  d5       d6g|z   d7gz   }t#        |      }|r#||z   }|j$                  rnd|d<   t'        ||       g }t)        |      }|
Ag }g }|
D ]C  }|d   }|d   }|d   }||j	                         }|rL|j+                  |      r&|j!                  d8|z          |j!                  |       ^|j!                  |d9z          s|j-                  ||      r|j!                  |d:z   |z   d;z          |j+                  |      r+|j                  |d<z   |j/                  |      z   d=z          |j1                  |      r,|j                  |d>z   |j3                  |      z   d=z          |j!                  d?|z   d@z   |z          |j!                  |       F t5        |      dAk7  st5        |      dAk7  rFdBg|z   }|r|j!                  d5       d6g|z   d7gz   }n"| |d+k(  r|j!                  d5       d6g|z   d7gz   }t#        |      }|r||z   }|j$                  rnsd|d<   t'        ||       t5        |      dAk7  r#|j!                  dCdDj                  |      z          t5        |      dAk7  r#|j!                  dEdDj                  |      z          g }||D ]}  }|d$   }|d%   }|j+                  |      r|j                  d||dFz   |z   dGz   H       |j+                  |      r|j!                  dI|z   dJz   |z          g|j                  d||dKz   H        t5        |      dAk7  rFdBg|z   }|r|j!                  d5       d6g|z   d7gz   }n"| |d+k(  r|j!                  d5       d6g|z   d7gz   }t#        |      }|r#||z   }|j$                  rnd|d<   t'        ||       |j$                  r|j7                  d|dLH       ||dM<   ||dN<   ||dO<    |j6                  dPi | y c c}w c c}w c c}w )QNTrR   )requiredtype)r   boolF)r   default)rK   rT   remove)old_namenew_nameenhancedbasic)r   choicesr{   dict)r   elementsoptions)r   r   darename)argument_specsupports_check_modechangedr   r   r   r   rK   rT   r   zThis device alias name zS which needs to be added, does not have pwwn specified. Please specify a valid pwwn)msgzThis pwwn name is invalid : z<. Note that name cannot be more than 64 alphanumeric chars, zDit must start with a letter, and can only contain these characters: z, z'{0}'zThis pwwn is invalid : z$. Please check that its a valid pwwnr   r   zCdevice-alias has acquired lock on the switch. Hence cannot procced.disabledzdevice-alias distributez8device-alias distribute changed from disabled to enabledzOdevice-alias distribute remains unchanged. current distribution mode is enabledenabledzno device-alias distributez8device-alias distribute changed from enabled to disabledzPdevice-alias distribute remains unchanged. current distribution mode is disabledzno device-alias mode enhancedz0device-alias mode changed from enhanced to basicz:device-alias mode remains unchanged. current mode is basiczdevice-alias mode enhancedz0device-alias mode changed from basic to enhancedz=device-alias mode remains unchanged. current mode is enhancedzdevice-alias commitzterminal dont-askzno terminal dont-askzno device-alias name zZ - This device alias name is not in switch device-alias database, hence cannot be removed.z : ze - This device alias name,pwwn is already in switch device-alias database, hence nothing to configurezk - This device alias name is already present in switch device-alias database but assigned to another pwwn (z) hence cannot be addedzk - This device alias pwwn is already present in switch device-alias database but assigned to another name (zdevice-alias name z pwwn r   zdevice-alias databasez(the required device-alias were removed. ,z&the required device-alias were added. zU - this name is already present in the device-alias database, hence we cannot rename z with this one)r   rw   r   zdevice-alias rename  zQ - this name is not present in the device-alias database, hence we cannot rename.z'Check Mode: No cmds issued to the hostsmessagesrw   warningsr;   )r   r   r{   paramsr,   	fail_jsonrR   rs   rP   rq   formatrl   r   r4   r6   r2   r}   r   
check_moder   r=   rL   rY   r[   rW   r`   rQ   	exit_json)element_specelement_spec_renamer   r   r   r   commands_to_executeresultr   r   r   r   eachdictrK   rT   r   rg   oldnamenewnameshDAStausObjdmrw   cmdsshDADatabaseObjda_remove_listda_add_lists                              r   mainr      s	   4e,u/L t%0t%0
 V$uz7&;<VflC&:MN	M DQFvHvH&F|,J== D	t	B]]8$F 
~ 	HF#DF#Dh'Fzz|<$$5d)$op % 
 #4($$:d)$XY aa ))$O1W^^A%6$OP	Q %  #4($$5d)$@A % -	6  	Hz*Gz*Gw'  6'l#TU ]] iiN Kq!2 KL	M !  w'  6'l#TU ]] iiN Kq!2 KL	M ! 	* )0L""$AAU 	 	

 HJ  9:N e
 I~  <=N f !D1D8 $F9% H7?J ?@ RSP G| <= RSS OO12+,x7;Q:RRH!a9n 56/08;?U>VV!D1D8 $F9% H-f5O	~ *	1HF#DF#Dh'Fzz|"55d;OO$;d$BC"))$/OOvw
 #>>tTJOO  BB '99$?(( $ L!L-;;DA!B 8!8 )  );;DA(( $ L!L-;;DA!B 8!8 )  !(<t(Ch(NQU(UV#**40U*	1X {q C$71$</08;H 56/08;?U>VV%!y.OO$9: 34x?CYBZZHH%"5"<  $(y!FD)~&!+OOBSXXnE]] {#q(OO@388KCXX
 H 	Hz*Gz*G11':  !!mn '' !  11': 6 @3 F PQ  !!ij ! 	, x=A/08;H 56/08;?U>VV%!y.OO$9: 34x?CYBZZHH%"5"<  $(y!FD) (9 	 	
 "F:,F:!F:FvA %P$ !L !Ls   `4'`9
`>__main__)cli_show)
__future__r   r   r   DOCUMENTATIONEXAMPLESRETURNrd   ansible.module_utils.basicr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r   __metaclass__rq   objectr   r=   rl   rs   r   r   r   r7   r;   r   r   <module>r      s    A @:x*
  4 % F  F5f 5p		up	 zF r   