
    Vh@              
           d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZmZmZ dd	d
ddddddd	ZdddddddddZd Zd Zd Zd Zd Zd Zd Zed k(  r e        yy)!    )absolute_importdivisionprint_functiona
  
module: nxos_vpc
extends_documentation_fragment:
- cisco.nxos.nxos
short_description: Manages global VPC configuration
description:
- Manages global VPC configuration
version_added: 1.0.0
author:
- Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
- Unsupported for Cisco MDS
- The feature vpc must be enabled before this module can be used
- If not using management vrf, vrf must be globally on the device before using in
  the pkl config
- Although source IP isn't required on the command line it is required when using
  this module.  The PKL VRF must also be configured prior to using this module.
- Both pkl_src and pkl_dest are needed when changing PKL VRF.
options:
  domain:
    description:
    - VPC domain
    required: true
    type: str
  role_priority:
    description:
    - Role priority for device. Remember lower is better.
    type: str
  system_priority:
    description:
    - System priority device.  Remember they must match between peers.
    type: str
  pkl_src:
    description:
    - Source IP address used for peer keepalive link
    type: str
  pkl_dest:
    description:
    - Destination (remote) IP address used for peer keepalive link
    - pkl_dest is required whenever pkl options are used.
    type: str
  pkl_vrf:
    description:
    - VRF used for peer keepalive link
    - The VRF must exist on the device before using pkl_vrf.
    - "(Note) 'default' is an overloaded term: Default vrf context for pkl_vrf is
      'management'; 'pkl_vrf: default' refers to the literal 'default' rib."
    type: str
  peer_gw:
    description:
    - Enables/Disables peer gateway
    type: bool
  peer_sw:
    description:
    - Enables/Disables peer-switch
    type: bool
  auto_recovery:
    description:
    - Enables/Disables auto recovery on platforms that support disable
    - timers are not modifiable with this attribute
    - mutually exclusive with auto_recovery_reload_delay
    type: bool
  auto_recovery_reload_delay:
    description:
    - Manages auto-recovery reload-delay timer in seconds
    - mutually exclusive with auto_recovery
    type: str
  delay_restore:
    description:
    - manages delay restore command and config value in seconds
    type: str
  delay_restore_interface_vlan:
    description:
    - manages delay restore interface-vlan command and config value in seconds
    - not supported on all platforms
    type: str
  delay_restore_orphan_port:
    description:
    - manages delay restore orphan-port command and config value in seconds
    - not supported on all platforms
    type: str
  state:
    description:
    - Manages desired state of the resource
    default: present
    choices:
    - present
    - absent
    type: str
a  
- name: configure a simple asn
  cisco.nxos.nxos_vpc:
    domain: 100
    role_priority: 1000
    system_priority: 2000
    pkl_dest: 192.168.100.4
    pkl_src: 10.1.100.20
    peer_gw: true
    peer_sw: true
    auto_recovery: true

- name: configure
  cisco.nxos.nxos_vpc:
    domain: 100
    role_priority: 32667
    system_priority: 2000
    peer_gw: true
    peer_sw: true
    pkl_src: 10.1.100.2
    pkl_dest: 192.168.100.4
    auto_recovery: true

- name: Configure VPC with delay restore and existing keepalive VRF
  cisco.nxos.nxos_vpc:
    domain: 10
    role_priority: 28672
    system_priority: 2000
    delay_restore: 180
    peer_gw: true
    peer_sw: true
    pkl_src: 1.1.1.2
    pkl_dest: 1.1.1.1
    pkl_vrf: vpckeepalive
    auto_recovery: true
a  
commands:
    description: commands sent to the device
    returned: always
    type: list
    sample: ["vpc domain 100",
            "peer-keepalive destination 192.168.100.4 source 10.1.100.20 vrf management",
            "auto-recovery", "peer-gateway","peer-switch"]
N)AnsibleModule)
get_configload_configrun_commandszrole priority {role_priority}z!system-priority {system_priority}zdelay restore {delay_restore}z;delay restore interface-vlan {delay_restore_interface_vlan}z5delay restore orphan-port {delay_restore_orphan_port}z{peer_sw} peer-switchz{peer_gw} peer-gatewayz{auto_recovery} auto-recoveryz7auto-recovery reload-delay {auto_recovery_reload_delay})	role_prioritysystem_prioritydelay_restoredelay_restore_interface_vlandelay_restore_orphan_portpeer_swpeer_gwauto_recoveryauto_recovery_reload_delay6010032667F   )r   r   r   r
   r   r   r   r   c                 ~    g }| D ]5  }t        |t              r|j                  |       %|j                  |       7 |S N)
isinstancelistextendappend)command_listsflat_command_listcommands      g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_vpc.pyflatten_listr"      sE      .gt$$$W-$$W-	.
     c                     	 t        | dg      d   }|d   d   }g }|r2|D ]-  }|j                  t	        |d   j                                      / |S # t        t        f$ r g cY S w xY w)Nzshow vrf all | jsonr   	TABLE_vrfROW_vrfvrf_name)r	   KeyErrorAttributeErrorr   strlower)modulebody	vrf_tablevrf_listeachs        r!   get_vrf_listr1      s    F%:$;<Q?%i0	 H 	;DOOCZ 0 6 6 89:	; O n% 	s   A A&%A&c                    d}t        | dg      d   }|d   d   d   d   }t        j                  d|      rd}|S t        j                  d	|      r:t        | d
g      d   }|j                  dd      }t        j                  d|      rd}|S )NFzshow inventory | jsonr   	TABLE_invROW_inv	productidN7KTN9Kzshow hardware | jsonkickstart_ver_str z	7.0\(3\)F)r	   researchget)r,   autodatapidvers        r!   get_auto_recovery_defaultrA      s    D!8 9:1=D
{
I
&q
)+
6C	yy K 
63	F%;$<=a@hh*B/99\3'DKr#   c                    t        | dg      d   }|rt        |d         }n(t        | dg      d   }|r|j                         d   }nd}i }|dk7  rt        | dg      }|r||d	<   t        j                         D ]  }t        j                  |      ||<    t        |       |d
<   |j                  d      }|D ]G  }d|v r|j                         }|d   |d<   d|v r|j                         }|d   |d<   t        j                  d|      r|j                         }|d   |d<   d|v r|j                         }|d   |d<   d|v r|j                         }|d   |d<   d|v r#d|v rdnd|d
<   |j                         }|d   |d<   d|v rd|v rdnd|d<   d|v rd|d<   d |v st        j                  d!|      }	|	s|	j                         j                         D ](  }
|	j                  |
      s|	j                  |
      ||
<   * J |S )"Nzshow vpc | jsonr   zvpc-domain-idzshow run vpc | inc domain   znot configuredzvpc all)flagsdomainr   
zrole priorityr
   zsystem-priorityr   zdelay restore \d+r   zdelay restore interface-vlanr   zdelay restore orphan-portr   zauto-recoveryzno FTr   peer-gatewayr   peer-switchr   zpeer-keepalive destinationz^destination (?P<pkl_dest>[\d.]+)(?:.* source (?P<pkl_src>[\d.]+))*(?:.* vrf (?P<pkl_vrf>\S+))*)r	   r*   splitr   PARAM_TO_DEFAULT_KEYMAPkeysr<   rA   r:   r;   	groupdictgroup)r,   r-   rE   vpcrunkeyvpc_listr0   linempkls              r!   get_vpcrV      sI   !2 34Q7DT/*+F%@$AB1EZZ\!_F%F
C!!	{3"CM.335 <266s;C<#<V#DC yyH  #8"d*::<D+/8C($,::<D-1"XC)*99148::<D+/8C(1T9::<D:>r(C67.$6::<D7;BxC34"d*49TM5tC(::<D8<RC45!T).3tmUC	N D(%)C	N/47		8 	A #$;;=#5#5#7 8C wws|+,773<C8C#8H Jr#   c                    |j                         D cg c]  }|j                  d      s| }}|D ]S  }| j                  j                  |      }|j                  |      r0|r|||   k(  r	||   ||<   C|F||   sL||   ||<   U yc c}w )a  peer-keepalive dependency checking.
    1. 'destination' is required with all pkl configs.
    2. If delta has optional pkl keywords present, then all optional pkl
       keywords in existing must be added to delta, otherwise the device cli
       will remove those values when the new config string is issued.
    3. The desired behavior for this set of properties is to merge changes;
       therefore if an optional pkl property exists on the device but not
       in the playbook, then that existing property should be retained.
    Example:
      CLI:       peer-keepalive dest 10.1.1.1 source 10.1.1.2 vrf orange
      Playbook:  {pkl_dest: 10.1.1.1, pkl_vrf: blue}
      Result:    peer-keepalive dest 10.1.1.1 source 10.1.1.2 vrf blue
    rU   N)rL   
startswithparamsr<   )r,   deltaexistingipkl_existingrU   params          r!   pkl_dependenciesr_   +  s      (}}F!!,,u2EAFLF 
+!!#&yy~(3-/ &c]c
8C=%c]c

+ Gs
   B	B	c                    t        |      }|j                  d      }g }d|v rU dj                  di |}d|v r| dj                  di |z  }d|v r| dj                  di |z  }|j                  |       d|v r|j                  d      sd	|d<   nd
|d<   d|v r|j                  d      sd	|d<   nd
|d<   d|v r|j                  d      sd	|d<   nd
|d<   |D ]u  }t        j                  |      }| |j                  di |j                         }d|v r|j                  d       d|v r|j                  d       |j                  |       w |s|r!|j                  ddj                  |             |S )NrE   pkl_destz%peer-keepalive destination {pkl_dest}pkl_srcz source {pkl_src}pkl_vrfz vrf {pkl_vrf}r   nor9   r   r   rH   terminal dont-askrI   r   zvpc domain {0} )dictr<   formatr   CONFIG_ARGSstripinsert)	r,   rO   rE   r[   domain_onlycommandspkl_commandr^   r    s	            r!   get_commands_to_config_vpcro   G  s   
s)C''(#KHSD=DDKsK5.55<<<K2+229S99K$#ww'#'C #%C Cwwy!!C	NC	NCwwy!!C	NC	N %//%($gnn+s+113G( 34' 34OOG$% ;+226:;Or#   c                  H   t        t        dd      t        dd      t        dd      t        d      t        d      t        d      t        dd      t        dd      t        dd      t        dd      t        dd      t        dd      t        dd      t        ddgd	      
      } dg}t        | |d      }t               }d|d}|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        |||||	|
|||||||      }|	sA|r|j	                  d       n,|
r*|
dk7  r|j	                  d        n|j	                  d!       |
r-|
j                         t        |      vr|j	                  d"       t        d# |j                         D              }t        |      }g }|dk(  ri }|j                         D ]U  \  }}t        |      j                         d$k(  r|dk7  rt        j                  |      }|j                  |      |k7  sQ|||<   W |rt        |||       t        ||||      }|j                  |       nS|dk(  rN|rL||d   k7  r|j	                  d%       n1|j                  d&       |j                  d'j                  |             t!        |      }||d(<   |r2d|d)<   |j"                  s!t%        ||       d*|v r|j'                  d+        |j(                  d,i | y )-NTr*   )requiredtypeF)rq   boolabsentpresent)choicesdefault)rE   r
   r   rb   ra   rc   r   r   r   r   r   r   r   state)r   r   )argument_specmutually_exclusivesupports_check_mode)changedwarningsrE   r
   r   rb   ra   rc   r   r   r   r   r   r   r   rx   )rE   r
   r   rb   ra   rc   r   r   r   r   r   r   r   z=dest IP for peer-keepalive is required when src IP is present)msg
managementzCdest and src IP for peer-keepalive are required when vrf is presentz:dest IP for peer-keepalive is required when vrf is presentzeThe VRF you are trying to use for the peer keepalive link is not on device yet. Add it first, please.c              3   0   K   | ]  \  }}|	||f  y wr   rf   ).0kvs      r!   	<genexpr>zmain.<locals>.<genexpr>  s     Etq!q}QFEs   
	rw   zCYou are trying to remove a domain that does not exist on the devicere   zno vpc domain {0}rm   r|   	configurer   rf   )rg   r   r   rY   	fail_jsonr+   r1   itemsrV   r*   rK   r<   r_   ro   r   rh   r"   
check_moder   pop	exit_json)ry   rz   r,   r}   resultsrE   r
   r   rb   ra   rc   r   r   r   r   r   r   r   rx   argsproposedr[   rm   rZ   rQ   valuer    cmdss                               r!   mainr   u  s   T.E6e%8e$u%e$e&1e&1E7#'U#CE6%)5u%E"&E"BHi0)DM" JJ#- F vHX6G]]8$FMM/2Mmm$56OmmI&G}}Z(HmmI&GmmI&GmmI&GMM/2M!'/K!LMM/2M#)==1O#P  &.I JMM'"E#'##=#%A";D  !cd,&  ` !    W !  ==?,v"66"  
 Etzz|EEHvHH	"..* 	#JC5z!Y.3)3C/33C8||C E)"c
	# VUH50QGOOG$	(	(++  ` !   34 3 : :6 BC!DGJ!	  %d"Fwr#   __main__)
__future__r   r   r   rr   __metaclass__DOCUMENTATIONEXAMPLESRETURNr:   ansible.module_utils.basicr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r	   ri   rK   r"   r1   rA   rV   r_   ro   r   __name__rf   r#   r!   <module>r      s   $ A @ [z#J
 
 4  5:4$a!X&'4"[
 $(!$"%	  8v+8+\s l zF r#   