
    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 d dlmc mc mc mc mZ  G d de      ZddZd	 Zd
 Zd Zy)    )absolute_importdivisionprint_function)	urlencode)open_url)jsonNc                       e Zd ZdZd Zd Zy)Responsez=
    Create a response object to mimic that of requests.
    c                 .    d | _         d | _        d | _        y N)contentstatus_codestderrselfs    q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/module_utils/memset.py__init__zResponse.__init__   s        c                 @    t        j                  | j                        S r   )r   loadsr   r   s    r   r   zResponse.json    s    zz$,,''r   N)__name__
__module____qualname____doc__r   r    r   r   r
   r
      s    
(r   r
   c                    t               }|t               }n|j                         }d}d}t        |      }ddi}d}dj	                  ||      }		 t        |	||dd| 	      }
|
j                         j                  d
      |_        |
j                         |_
        ||j                         }|||fS # t        j                  $ r}	 |j                  }n# t        $ r d}Y nw xY wd}|j                         j                  d      |_        ||_
        |j                  @dj	                  |j                  |j                         d   |j                         d         }n4dj	                  |j                         d   |j                         d         }Y d}~d}~wt        j                   $ r=}d}dj	                  t#        |            }dj	                  |      |_        Y d}~Id}~ww xY w)z
    Generic function which returns results back to calling function.

    Requires an API key and an API method to assemble the API URL.
    Returns response text to be analysed.
    NFzContent-Typez!application/x-www-form-urlencodedzhttps://api.memset.com/v1/json/z{0}{1}/POSTT)dataheadersmethodforce_basic_authurl_usernamezutf-8utf8z.Memset API returned a {0} response ({1}, {2}).
error_typeerrorz(Memset API returned an error ({0}, {1}).zAn URLError occurred ({0}).z{0})r
   dictcopyr   formatr   readdecoder   getcoder   urllib_error	HTTPErrorcodeAttributeErrorr   URLErrortyper   )api_key
api_methodpayloadresponse
has_failedmsgr   r   api_uri_baseapi_urirespe	errorcodes                r   memset_api_callr=   $   s    zH &,,. J
CWDBCG4L  z:G,dGF]apwx99;--g6#||~( {mmosH$$- !! 	I 	I	 
668??62(+BKKHL`L`bjbobobqr~b  BJ  BO  BO  BQ  RY  BZ  [C<EEhmmoVbFcemereretu|e}~C   ,
+44T!W=..+,sD   A
B1 1G(CFC FC  B0FG(+2G##G(c                 f    d}| j                   dv r | j                         D ]  }|d   |k(  sd} |S )zB
    Returns true if domain already exists, and false if not.
    F      domainTr   r   )r   rB   existszone_domains       r   check_zone_domainrF   [   sE     F:%99; 	K8$.	 Mr   c                     d}d}| j                   dv r*| j                         D ]  }|d   |k(  s|dz  } |dk(  rd}||fS )z@
    Returns true if zone already exists, and false if not.
    r   Fr?   nickname   TrC   )r   namecounterrD   zones        r   
check_zonerM   i   s_     GF:%IIK 	DJ4'1	 a<F7?r   c                     d}d\  }}g }|D ]  }|d   | k(  s|j                  |d          ! t        |      }|dk(  rd}n|dk(  r|d   }d}n	|dkD  rd	}d
}||||fS )z:
    Returns the zone's id if it exists and is unique
    F)NNrH   idr   zNo matching zone foundrI   TNzCZone ID could not be returned as duplicate zone names were detected)appendlen)	zone_namecurrent_zoneszone_existszone_idr7   	zone_listrL   rK   s           r   get_zone_idrW   z   s     KLGSI )
y(T$Z() )nG!|&	AA,	1SWg--r   r   )
__future__r   r   r   r1   __metaclass__+ansible.module_utils.six.moves.urllib.parser   ansible.module_utils.urlsr   ansible.module_utils.basicr   +ansible.module_utils.six.moves.urllib.errormodule_utilssixmovesurllibr%   r,   objectr
   r=   rF   rM   rW   r   r   r   <module>rc      sF    C B A . + B B B(v (4%n".r   