
    Vh!8                         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lmZ d	 Zd
 Z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_nxapi
extends_documentation_fragment:
- cisco.nxos.nxos
author: Peter Sprygada (@privateip)
short_description: Manage NXAPI configuration on an NXOS device.
notes:
- Limited Support for Cisco MDS
description:
- Configures the NXAPI feature on devices running Cisco NXOS.  The NXAPI feature is
  absent from the configuration by default.  Since this module manages the NXAPI feature
  it only supports the use of the C(Cli) transport.
version_added: 1.0.0
options:
  http_port:
    description:
    - Configure the port with which the HTTP server will listen on for requests.  By
      default, NXAPI will bind the HTTP service to the standard HTTP port 80.  This
      argument accepts valid port values in the range of 1 to 65535.
    required: false
    default: 80
    type: int
  http:
    description:
    - Controls the operating state of the HTTP protocol as one of the underlying transports
      for NXAPI.  By default, NXAPI will enable the HTTP transport when the feature
      is first configured.  To disable the use of the HTTP transport, set the value
      of this argument to False.
    required: false
    default: true
    type: bool
    aliases:
    - enable_http
  https_port:
    description:
    - Configure the port with which the HTTPS server will listen on for requests.  By
      default, NXAPI will bind the HTTPS service to the standard HTTPS port 443.  This
      argument accepts valid port values in the range of 1 to 65535.
    required: false
    default: 443
    type: int
  https:
    description:
    - Controls the operating state of the HTTPS protocol as one of the underlying
      transports for NXAPI.  By default, NXAPI will disable the HTTPS transport when
      the feature is first configured.  To enable the use of the HTTPS transport,
      set the value of this argument to True.
    required: false
    default: false
    type: bool
    aliases:
    - enable_https
  sandbox:
    description:
    - The NXAPI feature provides a web base UI for developers for entering commands.  This
      feature is initially disabled when the NXAPI feature is configured for the first
      time.  When the C(sandbox) argument is set to True, the developer sandbox URL
      will accept requests and when the value is set to False, the sandbox URL is
      unavailable. This is supported on NX-OS 7K series.
    required: false
    type: bool
    aliases:
    - enable_sandbox
  state:
    description:
    - The C(state) argument controls whether or not the NXAPI feature is configured
      on the remote device.  When the value is C(present) the NXAPI feature configuration
      is present in the device running-config.  When the values is C(absent) the feature
      configuration is removed from the running-config.
    choices:
    - present
    - absent
    required: false
    default: present
    type: str
  ssl_strong_ciphers:
    description:
    - Controls the use of whether strong or weak ciphers are configured. By default,
      this feature is disabled and weak ciphers are configured.  To enable the use
      of strong ciphers, set the value of this argument to True.
    required: false
    default: false
    type: bool
  tlsv1_0:
    description:
    - Controls the use of the Transport Layer Security version 1.0 is configured.  By
      default, this feature is enabled.  To disable the use of TLSV1.0, set the value
      of this argument to True.
    required: false
    default: true
    type: bool
  tlsv1_1:
    description:
    - Controls the use of the Transport Layer Security version 1.1 is configured.  By
      default, this feature is disabled.  To enable the use of TLSV1.1, set the value
      of this argument to True.
    required: false
    default: false
    type: bool
  tlsv1_2:
    description:
    - Controls the use of the Transport Layer Security version 1.2 is configured.  By
      default, this feature is disabled.  To enable the use of TLSV1.2, set the value
      of this argument to True.
    required: false
    default: false
    type: bool
an  
- name: Enable NXAPI access with default configuration
  cisco.nxos.nxos_nxapi:
    state: present

- name: Enable NXAPI with no HTTP, HTTPS at port 9443 and sandbox disabled
  cisco.nxos.nxos_nxapi:
    enable_http: false
    https_port: 9443
    https: true
    enable_sandbox: false

- name: remove NXAPI configuration
  cisco.nxos.nxos_nxapi:
    state: absent
z
updates:
  description:
    - Returns the list of commands that need to be pushed into the remote
      device to satisfy the arguments
  returned: always
  type: list
  sample: ['no feature nxapi']
N)AnsibleModule)get_capabilitiesload_configrun_commands)Versionc                    |j                  dd      }|dk(  r| j                  d       |d   d   }d|vr!| j                  d   r| j                  d	       | j                  d
   }|dk(  r!d| j                  d
<   |j                  d       n%|dk(  r d| j                  d
<   |j                  d       dD ]B  }| j                  |   d| j                  |   cxk  rdk  r,n | j                  d|z         D |S )Nnetwork_apinxapiz)module not supported over nxapi transport)msgdevice_infonetwork_os_platform7KsandboxzEsandbox or enable_sandbox is supported on NX-OS 7K series of switchesstatestartedpresentzhstate=started is deprecated and will be removed in a a future release.  Please use state=present insteadstoppedabsentzgstate=stopped is deprecated and will be removed in a a future release.  Please use state=absent instead)	http_port
https_port   i  z%s must be between 1 and 65535)get	fail_jsonparamsappend)modulewarningscapabilitiesr   os_platformr   keys          i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_nxapi.py
check_argsr%      s   ""=':KgHI}-.CDK;6==#;W 	 	
 MM'"E	!*gB	
 
)	!)gA	

 + M==)c*3e3  %E%K LM
 O    c                 n    t               }t               }d }d }|j                  d      }	|	r0|	j                  d      }|r|d d }|	j                  d      }|r|d d } fd}
 |
d      r d   dk(  rdgS |j                  d	       n
 d   dk(  r|S d
D ]|  }|dz   } |
|      r6 j                  |      du r	d|z  ||<   nd|d j                  |      ||<    |
|      sO j                  |      du scd|d j                  |      ||<   ~  |
d      rd|d<    d   sd|d   z  |d<   |r|r|dk(  s|dk(  rt	        |      dk\  r |
d      rd|d<    d   du rd|d<   d}d}ddddj                         D ]\  \  }} |
|      r( j                  |      du s"dj                  ||g      }6j                  |      du sJdj                  ||g      }^ t        |      d kD  r+d!dj                  ||g      z  |d"<   n|j                  d#       |j                  |j                                |S )$Nr   network_os_version   r   c                 t    j                  |       d uxr# j                  |       j                  |       k7  S )N)r   )xhavewants    r$   needs_updatez)map_obj_to_commands.<locals>.needs_update   s0    xx{$&GDHHQK488A;,FGr&   r   r   no feature nxapizfeature nxapi)httphttps_portFzno nxapi %sznxapi z port Tr   nxapi sandboxzno %sN9KN3Kz9.2ssl_strong_ciphersznxapi ssl ciphers weakzno nxapi ssl ciphers weak zTLSv1.2zTLSv1.1TLSv1)tlsv1_2tlsv1_1tlsv1_0 r   znxapi ssl protocols%sssl_protocolszos_version and/or os_platform keys from platform capabilities are not available.  Any NXAPI SSL optional arguments will be ignored)
listdictr   r   r
   itemsjoinlenextendvalues)r-   r,   r   r    r!   send_commandscommandsr"   
os_versionr   r.   	parameter
port_paramhave_ssl_protocolswant_ssl_protocolsr#   values   ``               r$   map_obj_to_commandsrM      s   FMvHKJ""=1K __%9:
#BQJ!oo&;<%bq/KH G=H$&''_-	g(	"& 	(
	"xx	"e+&3i&?# , HHZ('#
 
#(;t(C$#HY" I-I")HY,?"?HYz5 K5$8gj>QUZ>Z011I-.,-55PH12!#!#$$" eg		O
U
  $xx},-0XX7I56Q-R*XXc]d*),3Eu2M)N&	O %&*,CHH02DEF-) 	?	
 *+r&   c                     dg}d }|D ]E  }t        j                  || t         j                        }|s+t        |j	                  d            } n |d u|dS )Nznxapi http port (\d+)r   )r0   r   researchMintgroup)datahttp_resr   regexmatchs        r$   
parse_httprY      s^    ()HI 		%rtt,EKKN+I	 T)	BBr&   c                     dg}d }|D ]E  }t        j                  || t         j                        }|s+t        |j	                  d            } n |d u|dS )Nznxapi https port (\d+)r   )r1   r   rO   )rU   	https_resr   rW   rX   s        r$   parse_httpsr\   -  s^    *+IJ 		%rtt,U[[^,J	  t+:FFr&   c                     | j                  d      D cg c]  }t        j                  d|      s| }}d}|r
|d   dk(  rd}d|iS c c}w )N
z.*sandbox.*Fr   r3   Tr   )splitrP   rQ   )rU   itemr   rL   s       r$   parse_sandboxra   :  sT     $

4 0TBIInd4StTGTE71:0u	 Us
   AAc                     dg}d }|D ]<  }t        j                  || t         j                        }|s+|j                  d      } n d|dk(  iS )Nz(\w+) nxapi ssl ciphers weakr   r6   no)rP   rQ   rR   rT   )rU   ciphers_resrL   rW   rX   s        r$   parse_ssl_strong_ciphersre   B  sX    23KE 		%rtt,KKNE	 !%4-00r&   c                     t        j                  d| t         j                        d u}t        j                  d| t         j                        d u}t        j                  d| t         j                        d u}|||dS )Nz(?<!\S)TLSv1(?!\S)z(?<!\S)TLSv1.1(?!\S)z(?<!\S)TLSv1.2(?!\S))r;   r:   r9   )rP   rQ   rR   )rU   r;   r:   r9   s       r$   parse_ssl_protocolsrg   O  sc    ii-tRTT:$FGii/rtt<DHGii/rtt<DHG7wGGr&   c                    t        | dgd      d   }t        j                  d|t        j                        }|s|dk(  rddiS t	        |      j                         }dd	i}|j                  t        |             |j                  t        |             |j                  t        |             |j                  t        |             |j                  t        |             |S )
Nzshow run all | inc nxapiF)check_rcr   r/   r7   r   r   r   )r	   rP   rQ   rR   strstripupdaterY   r\   ra   re   rg   )r   outrX   objs       r$   map_config_to_objro   W  s    
v :;e
LQ
OCII)35E r	""
c(..
CI
CJJz#JJ{3 JJ}S!"JJ',-JJ"3'(Jr&   c                 $   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d	   | j                   d
   d
}|S )Nr0   r   r1   r   r   r   r6   r;   r:   r9   
r0   r   r1   r   r   r   r6   r;   r:   r9   )r   )r   rn   s     r$   map_params_to_objrr   l  s    f%]];/w'mmL1==+w'$mm,@A==+==+==+C Jr&   c                  n   t        t        dgdd      t        dd      t        dgdd	      t        dd
      t        dgd      t        dddg      t        dd	      t        dd      t        dd	      t        dd	      
      } t        | d      }t               }d}|dz  }|dz  }|j                  |dd       t	        |      }t        |||       t        |      }t        |      }t        |||||      }d	||d}|r|j                  st        ||       d|d<    |j                  di | y)z%main entry point for module executionenable_httpboolT)aliasestypedefaultrS   P   )rw   rx   enable_httpsFi  enable_sandbox)rv   rw   r   r   )rx   choicesrq   )argument_specsupports_check_modezBModule nxos_nxapi currently defaults to configure 'http port 80'. z:Default behavior is changing to configure 'https port 443'zM when params 'http, http_port, https, https_port' are not set in the playbookz
2022-06-01z
cisco.nxos)r   datecollection_name)changedr    rF   r   N )r?   r   r>   	deprecater   r%   rr   ro   rM   
check_moder   	exit_json)	r}   r   r    warning_msgr!   r-   r,   rF   results	            r$   mainr   }  sB   =/EE2.N+&%HUC0./f=9y(.CDVU;&$/&%0&%0M DQFvHVKOOKbbK
<V#F+Lvx.V$DV$D"4vxNHH(KF  ) yFvr&   __main__)
__future__r   r   r   rw   __metaclass__DOCUMENTATIONEXAMPLESRETURNrP   ansible.module_utils.basicr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r	   Lansible_collections.cisco.nxos.plugins.module_utils.network.nxos.utils.utilsr
   r%   rM   rY   r\   ra   re   rg   ro   rr   r   __name__r   r&   r$   <module>r      s   $ A @ kZ"
 
 4 
 aDPf
C
G
1H*"'T zF r&   