ó
Á£ô_c           @   sâ   d  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 d l	 m
 Z
 d d	 l m Z y d d
 l m Z Wn e k
 r¯ d Z n Xe j e  Z d e f d     YZ d   Z d S(   s;   Common code for DNS Authenticator Plugins built on Lexicon.iÿÿÿÿN(   t	   HTTPError(   t   RequestException(   t   Any(   t   Dict(   t   Union(   t   errors(   t
   dns_common(   t   ConfigResolvert   LexiconClientc           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   sI   
    Encapsulates all communication with a DNS provider via Lexicon.
    c         C   s   d  |  _ d  S(   N(   t   Nonet   provider(   t   self(    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyt   __init__   s    c         C   sx   |  j  |  y# |  j j d d d | d |  WnA t k
 rs } t j d | d t t j d j	 |    n Xd S(	   s§  
        Add a TXT record using the supplied information.

        :param str domain: The domain to use to look up the managed zone.
        :param str record_name: The record name (typically beginning with '_acme-challenge.').
        :param str record_content: The record content (typically the challenge validation).
        :raises errors.PluginError: if an error occurs communicating with the DNS Provider API
        t   typet   TXTt   namet   contents'   Encountered error adding TXT record: %st   exc_infos   Error adding TXT record: {0}N(
   t   _find_domain_idR
   t   create_recordR   t   loggert   debugt   TrueR   t   PluginErrort   format(   R   t   domaint   record_namet   record_contentt   e(    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyt   add_txt_record"   s    	#c         C   s   y |  j  |  Wn- t j k
 r@ } t j d | d t d SXy# |  j j d d d | d |  Wn) t k
 r } t j d | d t n Xd S(	   s«  
        Delete a TXT record using the supplied information.

        :param str domain: The domain to use to look up the managed zone.
        :param str record_name: The record name (typically beginning with '_acme-challenge.').
        :param str record_content: The record content (typically the challenge validation).
        :raises errors.PluginError: if an error occurs communicating with the DNS Provider  API
        s7   Encountered error finding domain_id during deletion: %sR   NR   R   R   R   s)   Encountered error deleting TXT record: %s(	   R   R   R   R   R   R   R
   t   delete_recordR   (   R   R   R   R   R   (    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyt   del_txt_record3   s    	#c         C   sï   t  j |  } xŸ | D]¶ } yF t |  j d  rD | |  j j d <n | |  j _ |  j j   d SWq t k
 r } |  j | |  } | rÌ |  qÌ q t	 k
 rË } |  j
 | |  } | rÌ |  qÌ q Xq Wt j d j | |    d S(   sÆ   
        Find the domain_id for a given domain.

        :param str domain: The domain for which to find the domain_id.
        :raises errors.PluginError: if the domain_id cannot be found.
        t   optionsR   NsA   Unable to determine zone identifier for {0} using zone names: {1}(   R   t   base_domain_name_guessest   hasattrR
   R    R   t   authenticateR    t   _handle_http_errort	   Exceptiont   _handle_general_errorR   R   R   (   R   R   t   domain_name_guessest   domain_nameR   t   result(    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyR   H   s$    c         C   s   t  j d j | |   S(   Ns/   Error determining zone identifier for {0}: {1}.(   R   R   R   (   R   R   R(   (    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyR$   l   s    c         C   s2   t  |  j d  s. t j d j | |   Sd  S(   Ns   No domain founds9   Unexpected error determining zone identifier for {0}: {1}(   t   strt
   startswithR   R   R   R	   (   R   R   R(   (    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyR&   p   s    (	   t   __name__t
   __module__t   __doc__R   R   R   R   R$   R&   (    (    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyR      s   				$	c         C   si   i |  d 6} | j  |  t s0 | j  |  n5 i  } | j  |  | | |  <t   j |  j   } | S(   so  
    Convenient function to build a Lexicon 2.x/3.x config object.
    :param str lexicon_provider_name: the name of the lexicon provider to use
    :param dict lexicon_options: options specific to lexicon
    :param dict provider_options: options specific to provider
    :return: configuration to apply to the provider
    :rtype: ConfigurationResolver or dict
    t   provider_name(   t   updateR   t	   with_dictt   with_env(   t   lexicon_provider_namet   lexicon_optionst   provider_optionst   configt   provider_config(    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyt   build_lexicon_configw   s    

(   R.   t   loggingt   requests.exceptionsR    R   t   acme.magic_typingR   R   R   t   certbotR   t   certbot.pluginsR   t   lexicon.configR   t   ImportErrorR	   t	   getLoggerR,   R   t   objectR   R8   (    (    (    sF   /usr/lib/python2.7/site-packages/certbot/plugins/dns_common_lexicon.pyt   <module>   s   
]