
    Vh#                         d dl mZmZmZ eZ	 d dlmZ d dl
mZ d dlmZmZ d dlmZ d dlmZ d Zd Zd	 Zd
 Zd Z G d de      Zy# e$ r	 d dl	mZ Y Bw xY w)    )absolute_importdivisionprint_function)CertificateError)to_text)
ConnectionConnectionError)	iteritems)	urlencodec                 *   	 i }| j                   D ]I  }d| j                   |   v s| j                   |   d   (| j                  |   8| j                  |   ||<   K |S # t        $ r+}| j                  dj	                  |             Y d}~yd}~ww xY w)aw  
    Get the valid fields that should be passed to the REST API as urlencoded
    data so long as the argument specification to the module follows the
    convention:
        1) name field is Required to be passed as data to REST API
        2) all module argspec items that should be passed to data are not
            Required by the module and are set to default=None
    defaultNzHInvalid data type provided for splunk module_util.parse_splunk_args: {0}msg)argument_specparams	TypeError	fail_jsonformat)modulesplunk_dataargspeces       i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/splunk/es/plugins/module_utils/splunk.pyparse_splunk_argsr      s    
++ 	>GV11'::((1)<DMM'*6'-}}W'=G$	>  
Zaa 	 	
 	

s'   "A A A A 	B'!BBc                 @    |D ]  }|| v s| j                  |        | S N)pop)payload_dictremove_key_listeach_keys      r   !remove_get_keys_from_payload_dictr!   0   s/    # '|#X&'     c                     i }t        |      D ]W  \  }}|| v s| j                  |      s(| j                  |      dk(  s| j                  |      du sD| j                  |      ||<   Y |S )a  The fn to convert the api returned params to module params
    :param module_params: Module params
    :param key_transform: Dict with module equivalent API params
    :rtype: A dict
    :returns: dict with module prams transformed having API expected params
    r   Fr
   getr   )module_paramskey_transformobjkvs        r   map_params_to_objr+   7   sx     C-( *1a M$5$5a$8A$=ARARSTAUY^A^"&&q)CF	*
 Jr"   c                     i }t        |      D ]W  \  }}|| v s| j                  |      s(| j                  |      dk(  s| j                  |      du sD| j                  |      ||<   Y |S )zThe fn to convert the module params to api return params
    :param module_return_params: API returned response params
    :param key_transform: Module params
    :rtype: A dict
    :returns: dict with api returned value to module param value
    r   Fr$   )module_return_paramsr'   tempr)   r*   s        r   map_obj_to_paramsr/   H   sy     D-( 21$$ $$Q'#''*a/#''*e3*..q1DG2 Kr"   c                 Z    |j                         D ]  \  }}| j                  ||        | S r   )items
setdefault)configdefaultsr)   r*   s       r   set_defaultsr5   Z   s1       1!Q Mr"   c                   b    e Zd Z	 	 	 	 	 	 	 ddZddZd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd Zy)SplunkRequestNc                 x   || _         |r,t        | j                   j                        | _        d| _        n3|r1|| _        	 | j                  j                  d       || _         d| _        |i | _        n|| _        || _        |g | _	        n|| _	        | j                  j                  d       y # t        $ r  w xY w)NTzsplunk.es.splunkFvalidate_certs)r   r   _socket_path
connectionlegacyload_platform_pluginsr	   keymapoverridenot_rest_data_keysappend)selfr   headersaction_moduler;   r>   r@   r?   s           r   __init__zSplunkRequest.__init__d   s      ))A)ABDODK(DO556HI+# >DK DK ! %&(D#&8D#&&'78) # s    )B. .B9c                    	 | j                   j                  |||      \  }}|dk(  r.t        d      t        |      v st        d      t        |      v ri S |dk\  r|dk  s,| j                  j	                  dj                  ||             |S # t        $ r5}| j                  j	                  d	j                  |             Y d }~y d }~wt        $ r5}| j                  j	                  d
j                  |             Y d }~y d }~wt        $ rK}	 | j                  j	                  dj                  |             n# t        $ r Y nw xY wY d }~y Y d }~y d }~ww xY w)Npayloadi  zObject not foundzCould not find object   i,  z2Splunk httpapi returned error {0} with message {1}r   zconnection error occurred: {0}zcertificate error occurred: {0}zcertificate not found: {0})
r;   send_requestr   r   r   r   r	   r   
ValueErrorAttributeError)rB   methodurirH   coderesponser   s          r   _httpapi_error_handlez#SplunkRequest._httpapi_error_handle   sh   %	!__99 : ND( s{-.'(2CCw+HX&H' ICKD3J%%LSS  &  O 	KK!!4;;A> "     	KK!!5<<Q? "    	%%4;;A> &  " 		sT   AB 7B 	E+CE+DE+D<;E<	EEEEEc                 *     | j                   d|fi |S )NGETrQ   rB   urlkwargss      r   r%   zSplunkRequest.get       )t))%???r"   c                 *     | j                   d|fi |S )NPUTrT   rU   s      r   putzSplunkRequest.put   rX   r"   c                 *     | j                   d|fi |S )NPOSTrT   rU   s      r   postzSplunkRequest.post   s    )t))&#@@@r"   c                 *     | j                   d|fi |S )NDELETErT   rU   s      r   deletezSplunkRequest.delete   s    )t))(CB6BBr"   c                 f   	 i }| j                   r|s| j                  j                  }|D ]C  }||   	|| j                  vs|| j                  v r||   || j                  |   <   <||   ||<   E |S # t
        $ r5}| j                  j                  dj                  |             Y d}~yd}~ww xY w)a@  
        Get the valid fields that should be passed to the REST API as urlencoded
        data so long as the argument specification to the module follows the
        convention:
            - the key to the argspec item does not start with splunk_
            - the key does not exist in the not_data_keys list
        Nzinvalid data type provided: {0}r   )r<   r   r   r@   r>   r   r   r   )rB   r3   r   paramr   s        r   get_datazSplunkRequest.get_data   s    	K{{6++ ;5M.EAXAX4X+:@-DKK$67-3E]E*;  	KK!!5<<Q? "  	s"   0A2 A2 /A2 2	B0;+B++B0c                 6    t        | j                  |            S r   )r   rd   )rB   r3   s     r   get_urlencoded_dataz!SplunkRequest.get_urlencoded_data   s    v.//r"   c                 B    | j                  dj                  |            S )z:
        GET attributes of a monitor by rest path
        /{0}?output_mode=json)r%   r   rB   	rest_paths     r   get_by_pathzSplunkRequest.get_by_path   s    
 xx/66yABBr"   c                 B    | j                  dj                  |            S )z=
        DELETE attributes of a monitor by rest path
        rh   )ra   r   ri   s     r   delete_by_pathzSplunkRequest.delete_by_path   s    
 {{299)DEEr"   c                     || j                   r| j                  |      }| j                  dj                  |      |      S )zP
        Create or Update a file/directory monitor data input in Splunk
        rh   rG   )r?   rf   r^   r   )rB   rj   datas      r   create_updatezSplunkRequest.create_update   sG     ++D1Dyy#**95  
 	
r"   )NNNNNNTr   )__name__
__module____qualname__rE   rQ   r%   r[   r^   ra   rd   rf   rk   rm   rp    r"   r   r7   r7   `   sZ     
 19f&P@@AC40CF
r"   r7   N)
__future__r   r   r   type__metaclass__sslr   ImportErrorbackports.ssl_match_hostnameansible.module_utils._textr   ansible.module_utils.connectionr   r	   ansible.module_utils.sixr
   +ansible.module_utils.six.moves.urllib.parser   r   r!   r+   r/   r5   objectr7   rt   r"   r   <module>r      sg    A @ >$ / G . A
6"$a
F a
i  >=>s   A	 	AA