
    Vh8                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZmZ d	 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: eos_eapi
author: Peter Sprygada (@privateip)
short_description: Manage and configure Arista EOS eAPI.
requirements:
- EOS v4.12 or greater
description:
- Use to enable or disable eAPI access, and set the port and state of http, https,
  local_http and unix-socket servers.
- When enabling eAPI access the default is to enable HTTP on port 80, enable HTTPS
  on port 443, disable local HTTP, and disable Unix socket server. Use the options
  listed below to override the default configuration.
- Requires EOS v4.12 or greater.
version_added: 1.0.0
options:
  http:
    description:
    - The C(http) argument controls the operating state of the HTTP transport protocol
      when eAPI is present in the running-config. When the value is set to True, the
      HTTP protocol is enabled and when the value is set to False, the HTTP protocol
      is disabled. By default, when eAPI is first configured, the HTTP protocol is
      disabled.
    type: bool
    aliases:
    - enable_http
  http_port:
    description:
    - Configures the HTTP port that will listen for connections when the HTTP transport
      protocol is enabled.  This argument accepts integer values in the valid range
      of 1 to 65535.
    type: int
  https:
    description:
    - The C(https) argument controls the operating state of the HTTPS transport protocol
      when eAPI is present in the running-config. When the value is set to True, the
      HTTPS protocol is enabled and when the value is set to False, the HTTPS protocol
      is disabled. By default, when eAPI is first configured, the HTTPS protocol is
      enabled.
    type: bool
    aliases:
    - enable_https
  https_port:
    description:
    - Configures the HTTP port that will listen for connections when the HTTP transport
      protocol is enabled.  This argument accepts integer values in the valid range
      of 1 to 65535.
    type: int
  local_http:
    description:
    - The C(local_http) argument controls the operating state of the local HTTP transport
      protocol when eAPI is present in the running-config.  When the value is set
      to True, the HTTP protocol is enabled and restricted to connections from localhost
      only.  When the value is set to False, the HTTP local protocol is disabled.
    - Note is value is independent of the C(http) argument
    type: bool
    aliases:
    - enable_local_http
  local_http_port:
    description:
    - Configures the HTTP port that will listen for connections when the HTTP transport
      protocol is enabled.  This argument accepts integer values in the valid range
      of 1 to 65535.
    type: int
  socket:
    description:
    - The C(socket) argument controls the operating state of the UNIX Domain Socket
      used to receive eAPI requests.  When the value of this argument is set to True,
      the UDS will listen for eAPI requests.  When the value is set to False, the
      UDS will not be available to handle requests.  By default when eAPI is first
      configured, the UDS is disabled.
    type: bool
    aliases:
    - enable_socket
  timeout:
    description:
    - The time (in seconds) to wait for the eAPI configuration to be reflected in
      the running-config.
    type: int
    default: 30
  vrf:
    description:
    - The C(vrf) argument will configure eAPI to listen for connections in the specified
      VRF.  By default, eAPI transports will listen for connections in the global
      table.  This value requires the VRF to already be created otherwise the task
      will fail.
    default: default
    type: str
  config:
    description:
    - The module, by default, will connect to the remote device and retrieve the current
      running-config to use as a base for comparing against the contents of source.  There
      are times when it is not desirable to have the task get the current running-config
      for every task in a playbook.  The I(config) argument allows the implementer
      to pass in the configuration to use as the base config for comparison.
    type: str
  state:
    description:
    - The C(state) argument controls the operational state of eAPI on the remote device.  When
      this argument is set to C(started), eAPI is enabled to receive requests and
      when this argument is C(stopped), eAPI is disabled and will not receive requests.
    type: str
    default: started
    choices:
    - started
    - stopped
a  
- name: Enable eAPI access with default configuration
  arista.eos.eos_eapi:
    state: started

- name: Enable eAPI with no HTTP, HTTPS at port 9443, local HTTP at port 80, and socket
    enabled
  arista.eos.eos_eapi:
    state: started
    http: false
    https_port: 9443
    local_http: true
    local_http_port: 80
    socket: true

- name: Shutdown eAPI access
  arista.eos.eos_eapi:
    state: stopped
a,  
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - management api http-commands
    - protocol http port 81
    - no protocol https
urls:
  description: Hash of URL endpoints eAPI is listening on per interface
  returned: when eAPI is started
  type: dict
  sample: {'Management1': ['http://172.26.10.1:80']}
session_name:
  description: The EOS config session name used to load the configuration
  returned: when changed is True
  type: str
  sample: ansible_1479315771
N)AnsibleModule)	iteritems)load_configrun_commandsc                 D    d| cxk  rdk  sn |j                  d       y y N   i  z%http_port must be between 1 and 65535msg	fail_jsonvaluemodules     g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/arista/eos/plugins/modules/eos_eapi.pyvalidate_http_portr      &    DE     c                 D    d| cxk  rdk  sn |j                  d       y y r   r   r   s     r   validate_https_portr      r   r   c                 D    d| cxk  rdk  sn |j                  d       y y r   r   r   s     r   validate_local_http_portr      r   r   c                 b   t        |dg      }g }|d   j                         j                         dd  }|D ]L  }|st        j                  d|j                               }t        |      dkD  s9|j                  |d          N |j                  d       | |vr|j                  d| z         y y )	Nzshow vrfr      z\s{2,}   defaultz(vrf `%s` is not configured on the systemr   )r	   strip
splitlinesresplitlenappendr   )r   r   outconfigured_vrfslineslinesplitted_lines          r   validate_vrfr+      s    
v
|
,COFLLN%%'+E 5DJJL9}!""=#345 9%O#:UB 	 	
 $r   c                    t               | \  fd}fd}t         |d       |d      f      rDd   du r	 |d       n4d   du rd   dv r|j                  d       nd   xs d	} |d
|z         t         |d       |d      f      rDd   du r	 |d       n4d   du rd   dv r|j                  d       nd   xs d} |d|z         t         |d       |d      f      rDd   du r	 |d       n4d   du rd   dv r|j                  d       nd   xs d} |d|z         t         |d       |d      f      rd   du r	 |d       n |d        |d      r!d   dk(  r	 |d       nd   dk(  r |d        |d      r0 |d d   z         d   dk(  r
 |d       S d   dk(  r |d       S )!Nc                 t    j                  |       d uxr# j                  |       j                  |       k7  S )N)get)xhavewants    r   needs_updatez)map_obj_to_commands.<locals>.needs_update   s0    xx{$&GDHHQK488A;,FGr   c                 T    dvrj                  dd       j                  |        y )Nzmanagement api http-commandsr   )insertr%   )cmdcommandss    r   addz map_obj_to_commands.<locals>.add   s&    )9OOA=>r   http	http_portFzno protocol http)FNz=protocol http is not enabled, not configuring http port valueP   zprotocol http port %shttps
https_portzno protocol httpsz?protocol https is not enabled, not configuring https port valuei  zprotocol https port %s
local_httplocal_http_portzno protocol http localhostzIprotocol local_http is not enabled, not configuring local_http port valuei  zprotocol http localhost port %ssocketzno protocol unix-socketzprotocol unix-socketstatestoppedshutdownstartedzno shutdownvrfzvrf %s)listanyr%   )	updatesr   warningsr2   r7   portr6   r0   r1   s	         @@@r   map_obj_to_commandsrJ      s4   vHJD$H
 L ,{";<=<5 "#F|u$f)FS K(.B+d23
L!<#=>?=E!#$G}%$w-=*HU L)0S,t34
L&5F(GHI&,-L!U*tL/A F 0 _ -.6$5<=
L"L$:;<>U")*&'G=I%
O']i'EHtE{"# =I%
O O ']i'Or   c                     | d   d   ryy)Nr   enabledrC   rA    )datas    r   parse_staterO   "  s    Awyr   c                     t        | dg      }|d   d   d   |d   d   d   |d   d   d   |d   d   d   |d   d   d   |d   d   d   |d   d   d   |d   d	   xs d
t        |      d	S )N(show management api http-commands | jsonr   
httpServer
configuredrI   httpsServerlocalHttpServerunixSocketServerrD   r   	r8   r9   r;   r<   r=   r>   r?   rD   r@   )r	   rO   )r   r&   s     r   map_config_to_objrX   )  s    
v JK
LCA|$\2VL)&1Q&|4!f]+F3!f./=q6"34V<a&+,\:1ve})	S!
 
r   c           
         | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d	   d
	}t        |      D ]0  \  }}|s	t               j                  d|z        }|s( |||        2 |S )Nr8   r9   r;   r<   r=   r>   r?   rD   r@   rW   zvalidate_%s)paramsr   globalsr.   )r   objkeyr   	validators        r   map_params_to_objr_   8  s    f%]];/w'mmL1mmL1!==):;--)}}U#w'
C  n )
U	mc&9:I%(	) Jr   c                 p   | \  }}g d}|j                   d   }|j                   d   }|rt        |dg      }t        |      D ]=  \  }}	|	\  }
}||
   ||
   |d   |   d   k(  s"||= &|dk(  r|d   |   d   r7||= ;||= ? t        j                  d       |dz  }|dk(  r|j                  d	
       |ry y )N))r8   rR   )r;   rT   )r=   rU   )r?   rV   timeoutr@   rQ   r   runningrA   r   z1timeout expired before eapi running state changedr   )rZ   r	   	enumeratetimesleepr   )rG   r   r1   r0   invalid_statera   r@   r&   indexitemwant_keyeapi_keys               r   verify_staterk   N  s    JD$M mmI&GMM'"E
78
 %]3 		)KE4!%HhH~)>SVH%5i%@@%e,)#1vh'	2%e,!%(		) 	

11a<G  % r   c                 P   t        | dg      }t        t                     }|d   d   D ]u  }|j                  dd      \  }}t        |      j	                         }||d   vrt               |d   |<   |d   |   j                  t        |      j	                                w ||d<   y )	NrQ   )eos_eapi_urlsr   urls:r   rm   ansible_facts)r	   dictr#   strr    rE   r%   )r   resultr&   factseachintfurlr]   s           r   collect_factsrx   r  s    
v JK
LCtv&EAv =JJsA&	c$iooeO,,*.&E/"3'os#**3s8>>+;<= $F?r   c                  ,   t        t        dgd      t        d      t        dgd      t        d      t        dgd      t        d      t        dgd      t        dd	
      t        d      t               t        dddg            } t        | d      }ddi}t               }|j                  d   r|j	                  d       t        |      }t        |      }t        ||f||      }||d<   |rg|j                   }t        |||      }|j                  d      r"|j                  rd|j                  d      i|d<   |j                  d      |d<   d|d<   |d   rt        ||f|       t        ||       |r||d<    |j                  di | y) z%main entry point for module executionenable_httpbool)aliasestypeint)r}   enable_httpsenable_local_httpenable_socket   )r}   r   r   )r   rC   rA   )r   choices)r8   r9   r;   r<   r=   r>   r?   ra   rD   configr@   T)argument_specsupports_check_modechangedFr   z;config parameter is no longer necessary and will be ignoredr6   )commitdiffpreparedsessionsession_namerH   NrM   )rq   r   rE   rZ   r%   r_   rX   rJ   
check_moder   r.   _diffrk   rx   	exit_json)	r   r   rs   rH   r1   r0   r6   r   responses	            r   mainr   ~  s   =/7E"N+&9U#!4 5FC%(_-F;%,#v9y).DEM # F
 FvH}}XI	
 V$DV$D"D$<BH!F:&&&vx?<<FLL((,,v*>?F6N!)i!8~ yidD\6*&&!%zFvr   __main__)
__future__r   r   r   r}   __metaclass__DOCUMENTATIONEXAMPLESRETURNr"   rd   ansible.module_utils.basicr   ansible.module_utils.sixr   Cansible_collections.arista.eos.plugins.module_utils.network.eos.eosr   r	   r   r   r   r+   rJ   rO   rX   r_   rk   rx   r   __name__rM   r   r   <module>r      s   $ A @ iV(
( 
  4 .F
F
F

$GT,!H	$3l zF r   