ó
Y[c           @   sq   d  Z  d d l Z d d l m Z m Z d d l m Z d d l m Z e j	 e
  Z d e f d     YZ d S(   s;   Common code for DNS Authenticator Plugins built on Lexicon.i˙˙˙˙N(   t	   HTTPErrort   RequestException(   t   errors(   t
   dns_commont   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/dist-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/dist-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/dist-packages/certbot/plugins/dns_common_lexicon.pyt   del_txt_record&   s    	#c         C   sÎ   t  j |  } x | D] } y% | |  j j d <|  j j   d SWq t k
 rw } |  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.
        R   NsA   Unable to determine zone identifier for {0} using zone names: {1}(   R   t   base_domain_name_guessesR   t   optionst   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/dist-packages/certbot/plugins/dns_common_lexicon.pyR   ;   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/dist-packages/certbot/plugins/dns_common_lexicon.pyR   Z   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#   (    (    sF   /usr/lib/python2.7/dist-packages/certbot/plugins/dns_common_lexicon.pyR!   ^   s    (	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R!   (    (    (    sF   /usr/lib/python2.7/dist-packages/certbot/plugins/dns_common_lexicon.pyR      s   					(   R)   t   loggingt   requests.exceptionsR    R   t   certbotR   t   certbot.pluginsR   t	   getLoggerR'   R   t   objectR   (    (    (    sF   /usr/lib/python2.7/dist-packages/certbot/plugins/dns_common_lexicon.pyt   <module>   s   