
    Vh                         d dl mZmZmZ eZdZd dlZd dlZd dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d	Z G d
 de      Zd Zy)    )absolute_importdivisionprint_functiona  
---
name: sonic
short_description: HttpApi Plugin for devices supporting Restconf SONIC API
description:
  - This HttpApi plugin provides methods to connect to Restconf SONIC API endpoints.
version_added: 1.0.0
options:
  root_path:
    type: str
    description:
      - Specifies the location of the Restconf root.
    default: '/restconf'
    vars:
      - name: ansible_httpapi_restconf_root
N)to_text)ConnectionError)	HTTPError)HttpApiBase)to_listzapplication/yang-data+jsonc                   ,    e Zd Zd Zd ZddZd Zd Zy)HttpApic                    |rt        j                  |      }dj                  | j                  d      j	                  d      |j                  dd      j                  d      g      }|j                  d      xs t        |j                  d      xs t        d}| j                  j                  ||||j                  d      	      \  }}t        |||      S )
N/	root_pathpath content_typeaccept)zContent-TypeAcceptmethod)headersr   )jsondumpsjoin
get_optionrstripgetlstripCONTENT_TYPE
connectionsendhandle_response)selfdatamessage_kwargsr   r   responseresponse_datas          r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/enterprise_sonic/plugins/httpapi/sonic.pysend_requestzHttpApi.send_request6   s    ::d#Dxx5<<SA>CUCUV\^`CaChChilCmno +..~>N,$((2Bl
 #'//"6"6tT7[i[m[mnv[w"6"x-xGG    c                 *    | j                  |d d      S )Nr   )r   r#   r   )r(   )r"   commands     r'   r   zHttpApi.getD   s      gD GGr)   c                 h   |t        d      t               }t        |      D ]&  }	  | j                  di |}|j                  |       ( |S # t        $ rZ}|r8|j                  d      dk(  r$t        j                  dt        |            ri i g}nt	        t        |d            Y d}~sd}~ww xY w)	zISend a list of http requests to remote device and return results
        N'requests' value is requiredr   r   z[nN]ot [fF]ound.*code': 404surrogate_then_replace)errors )
ValueErrorlistr
   r(   r   r   researchstrr   append)r"   requestssuppr_ntf_excp	responsesreqr%   excs          r'   edit_configzHttpApi.edit_configG   s     ;<<F	8$ 		'CY,4,,3s3 X&		'  # Y!cggh&75&@RYYOlnqrunvEw "BxH)'#>V*WXX Ys   A	B1AB,,B1c                     |t        d      t        |      D ]  }	  | j                  di |} y# t        $ r}t	        j
                  d       Y d}~<d}~ww xY w)zPSend a list of http requests to remote device and allow time for reboot
        Nr-   i,  r0   )r1   r
   r(   	Exceptiontimesleep)r"   r7   r:   r%   r;   s        r'   edit_config_rebootzHttpApi.edit_config_rebootZ   sc     ;<<8$ 	 C ,4,,3s3	    

3 s   2	AAAc                 D    i }g |d<   d|d<   t        j                  |      S )Nrpc
sonic_restnetwork_api)r   r   )r"   results     r'   get_capabilitieszHttpApi.get_capabilitiesf   s)    u ,}zz&!!r)   N)T)__name__
__module____qualname__r(   r   r<   rA   rG   r0   r)   r'   r   r   5   s    HH&
 "r)   r   c                    |j                         }	 |sd}n$t        j                  |j                  d            }t        | t              r|rnd|v r!|d   d   }dj                  d |D              }n|}|j                  d| j                  i       |j                  d|i       t        || j                  	      t        t        |       | j                  	      | j                         |fS # t        $ r Y w xY w)
Nr   zutf-8r/   error
c              3   &   K   | ]	  }|d      yw)zerror-messageNr0   ).0rL   s     r'   	<genexpr>z"handle_response.<locals>.<genexpr>|   s     'S5o(>'Ss   coderequest_data)rQ   )readr   loadsdecoder1   
isinstancer   r   updaterQ   r   r   getcode)r%   r&   rR   r/   
error_texts        r'   r!   r!   n   s    !&&(MM JJ}';';G'DEM (I&=(&x09!YY'SF'ST
*
w67=>!*8==AAgh/hmmDD},,  s   )C- -	C98C9)
__future__r   r   r   type__metaclass__DOCUMENTATIONr   r?   r3   ansible.module_utils._textr   ansible.module_utils.connectionr   +ansible.module_utils.six.moves.urllib.errorr   ansible.plugins.httpapir	   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr
   r   r   r!   r0   r)   r'   <module>rc      sK   & C B"   	 . ; A / c+6"k 6"r-r)   