U
    W+dg?                     @   sD   d Z ddlZddlmZ ddlmZ ddlmZ G dd deZ	dS )<       N)TooManyRecordsException)ResourceRecordSets)Statusc                   @   s   e Zd ZdZdd Zdd Zdd Zd5d	d
Zd6ddZd7ddZ	d8ddZ
d9ddZd:ddZd;ddZd<ddZd=ddZd>dd Zd?d!d"Zd@d#d$ZdAd%d&ZdBd'd(ZdCd)d*ZdDd+d,ZdEd-d.Zd/d0 Zd1d2 Zd3d4 ZdS )FZonez
    A Route53 Zone.

    :ivar route53connection: A :class:`boto.route53.connection.Route53Connection` connection
    :ivar id: The ID of the hosted zone
    c                 C   sD   || _ |D ]4}|dkr*|d dd| _q
| | ||  q
d S )NZIdz/hostedzone/ )route53connectionreplaceid__setattr__lower)selfr   Z	zone_dictkey r   5/tmp/pip-unpacked-wheel-dlxw5sjy/boto/route53/zone.py__init__(   s
    zZone.__init__c                 C   s
   d| j  S )Nz	<Zone:%s>)namer   r   r   r   __repr__0   s    zZone.__repr__c                 C   s   |  }|d d S )z
        Commit a set of changes and return the ChangeInfo portion of
        the response.

        :type changes: ResourceRecordSets
        :param changes: changes to be committed
        Z ChangeResourceRecordSetsResponseZ
ChangeInfo)commit)r   changesresponser   r   r   _commit3   s    zZone._commitr   c              	   C   s   d}d}	|dk	rNz t |d  |d }|d }W n   |d }	|d }Y nX |jd||||||	d}
t|tttfkr|D ]}|
| q|n
|
| dS )aw  
        Add a CREATE change record to an existing ResourceRecordSets

        :type changes: ResourceRecordSets
        :param changes: change set to append to

        :type name: str
        :param name: The name of the resource record you want to
            perform the action on.

        :type resource_type: str
        :param resource_type: The DNS record type

        :param value: Appropriate value for resource_type

        :type ttl: int
        :param ttl: The resource record cache time to live (TTL), in seconds.

        :type identifier: tuple
        :param identifier: A tuple for setting WRR or LBR attributes.  Valid
           forms are:

           * (str, int): WRR record [e.g. ('foo',10)]
           * (str, str): LBR record [e.g. ('foo','us-east-1')

        :type comment: str
        :param comment: A comment that will be stored with the change.
        N   r   ZCREATE)
identifierweightregion)intZ
add_changetypelisttuplesetZ	add_value)r   r   resource_typer   valuettlr   commentr   r   Zchangerecordr   r   r   _new_record>   s&     zZone._new_recordr   Nc              	   C   s8   t | j| j|}| ||||||| t| j| |S )z
        Add a new record to this Zone.  See _new_record for parameter
        documentation.  Returns a Status object.
        )r   r   r
   r'   r   r   )r   r"   r   r#   r$   r   r%   r   r   r   r   
add_recordo   s
    zZone.add_recordc              	   C   sZ   |pt }t|}t| j| j|}|d| | ||j|j|||| t	| j| 
|S )a
  
        Update an existing record in this Zone.  Returns a Status object.

        :type old_record: ResourceRecord
        :param old_record: A ResourceRecord (e.g. returned by find_records)

        See _new_record for additional parameter documentation.
        DELETE)default_ttlcopyr   r   r
   add_change_recordr'   r   r   r   r   )r   
old_record	new_valuenew_ttlnew_identifierr%   r&   r   r   r   r   update_recordz   s    

   zZone.update_recordc                 C   sX   t | j| j|}t|tttfkr:|D ]}|d| q&n|d| t| j| 	|S )a8  
        Delete one or more records from this Zone.  Returns a Status object.

        :param record: A ResourceRecord (e.g. returned by
           find_records) or list, tuple, or set of ResourceRecords.

        :type comment: str
        :param comment: A comment that will be stored with the change.
        r)   )
r   r   r
   r   r   r    r!   r,   r   r   )r   r&   r%   r   rr   r   r   delete_record   s    
zZone.delete_recordc                 C   s6   |pt }| j|}| j|}| jd|||||dS )z
        Add a new CNAME record to this Zone.  See _new_record for
        parameter documentation.  Returns a Status object.
        CNAMEr"   r   r#   r$   r   r%   r*   r   _make_qualifiedr(   r   r   r#   r$   r   r%   r   r   r   	add_cname   s    zZone.add_cnamec                 C   s*   |pt }| j|}| jd|||||dS )z
        Add a new A record to this Zone.  See _new_record for
        parameter documentation.  Returns a Status object.
        Ar5   r6   r8   r   r   r   add_a   s    z
Zone.add_ac                 C   s*   |pt }| j|}| jd|||||dS )z
        Add a new MX record to this Zone.  See _new_record for
        parameter documentation.  Returns a Status object.
        MXr5   r6   )r   r   recordsr$   r   r%   r   r   r   add_mx   s    zZone.add_mxr   Fc           
         sB  | j |}| j j| j||d}g }|D ](}|j|krL|j|krL|| q( qRq(dd dk	rzt d   d W n    d Y nX dk	r fdd|D }dk	rȇ fdd|D }|st||krd||f }	|	d7 }	|	d		d
d |D 7 }	t
|	n,t|dkr$|S t|dkr:|d S dS dS )a  
        Search this Zone for records that match given parameters.
        Returns None if no results, a ResourceRecord if one result, or
        a ResourceRecordSets if more than one result.

        :type name: str
        :param name: The name of the records should match this parameter

        :type type: str
        :param type: The type of the records should match this parameter

        :type desired: int
        :param desired: The number of desired results.  If the number of
           matching records in the Zone exceeds the value of this parameter,
           throw TooManyRecordsException

        :type all: Boolean
        :param all: If true return all records that match name, type, and
          identifier parameters

        :type identifier: Tuple
        :param identifier: A tuple specifying WRR or LBR attributes.  Valid
           forms are:

           * (str, int): WRR record [e.g. ('foo',10)]
           * (str, str): LBR record [e.g. ('foo','us-east-1')

        )r   r   Nr   c                    s(   g | ] }|j kr|j d  kr|qS r   )r   r   .0r2   )r   r   r   r   
<listcomp>   s     
z%Zone.find_records.<locals>.<listcomp>c                    s(   g | ] }|j kr|j d  kr|qS r?   )r   r   r@   )r   r   r   r   rB     s     
zSearch: name %s type %sz
Found: z, c                 S   s"   g | ]}d |j |j| f qS )z%s %s %s)r   r   Zto_printr@   r   r   r   rB     s   r   )r   r7   get_all_rrsetsr
   r   r   appendr   lenjoinr   )
r   r   r   Zdesiredallr   Zreturnedresultsr2   messager   )r   r   r   r   find_records   sB    

zZone.find_recordsc                 C   s   | j |d|dS )z
        Search this Zone for CNAME records that match name.

        Returns a ResourceRecord.

        If there is more than one match return all as a
        ResourceRecordSets if all is True, otherwise throws
        TooManyRecordsException.
        r4   rG   rJ   r   r   rG   r   r   r   	get_cname  s    
zZone.get_cnamec                 C   s   | j |d|dS )z
        Search this Zone for A records that match name.

        Returns a ResourceRecord.

        If there is more than one match return all as a
        ResourceRecordSets if all is True, otherwise throws
        TooManyRecordsException.
        r:   rK   rL   rM   r   r   r   get_a  s    
z
Zone.get_ac                 C   s   | j |d|dS )z
        Search this Zone for MX records that match name.

        Returns a ResourceRecord.

        If there is more than one match return all as a
        ResourceRecordSets if all is True, otherwise throws
        TooManyRecordsException.
        r<   rK   rL   rM   r   r   r   get_mx*  s    
zZone.get_mxc                 C   s@   | j |}| j |}| |}|p*|j}| j|||||dS )z
        Update the given CNAME record in this Zone to a new value, ttl,
        and identifier.  Returns a Status object.

        Will throw TooManyRecordsException is name, value does not match
        a single record.
        r.   r/   r0   r%   )r   r7   rN   r$   r1   r   r   r#   r$   r   r%   r-   r   r   r   update_cname6  s    

zZone.update_cnamec                 C   s4   | j |}| |}|p|j}| j|||||dS )z
        Update the given A record in this Zone to a new value, ttl,
        and identifier.  Returns a Status object.

        Will throw TooManyRecordsException is name, value does not match
        a single record.
        rQ   )r   r7   rO   r$   r1   rR   r   r   r   update_aH  s    

zZone.update_ac                 C   s@   | j |}| j |}| |}|p*|j}| j|||||dS )z
        Update the given MX record in this Zone to a new value, ttl,
        and identifier.  Returns a Status object.

        Will throw TooManyRecordsException is name, value does not match
        a single record.
        rQ   )r   r7   rP   r$   r1   rR   r   r   r   	update_mxY  s    

zZone.update_mxc                 C   s(   | j |}| j|d||d}| |S )z
        Delete a CNAME record matching name and identifier from
        this Zone.  Returns a Status object.

        If there is more than one match delete all matching records if
        all is True, otherwise throws TooManyRecordsException.
        r4   r   rG   r   r7   rJ   r3   r   r   r   rG   r&   r   r   r   delete_cnamek  s
    
zZone.delete_cnamec                 C   s(   | j |}| j|d||d}| |S )z
        Delete an A record matching name and identifier from this
        Zone.  Returns a Status object.

        If there is more than one match delete all matching records if
        all is True, otherwise throws TooManyRecordsException.
        r:   rV   rW   rX   r   r   r   delete_ax  s
    
zZone.delete_ac                 C   s(   | j |}| j|d||d}| |S )z
        Delete an MX record matching name and identifier from this
        Zone.  Returns a Status object.

        If there is more than one match delete all matching records if
        all is True, otherwise throws TooManyRecordsException.
        r<   rV   rW   rX   r   r   r   	delete_mx  s
    
zZone.delete_mxc                 C   s   | j | jS )zS
        Return a ResourceRecordsSets for all of the records in this zone.
        )r   rC   r
   r   r   r   r   get_records  s    zZone.get_recordsc                 C   s   | j | j dS )z>
        Request that this zone be deleted by Amazon.
        N)r   Zdelete_hosted_zoner
   r   r   r   r   delete  s    zZone.deletec                 C   s    |  | jd}|dk	r|j}|S )z+ Get the list of nameservers for this zone.ZNSN)rJ   r   Zresource_records)r   nsr   r   r   get_nameservers  s    zZone.get_nameservers)r   )r   Nr   )NNr   )r   )NNr   )NNr   )NNr   )r   FN)F)F)F)NNr   )NNr   )NNr   )NF)NF)NF)__name__
__module____qualname____doc__r   r   r   r'   r(   r1   r3   r9   r;   r>   rJ   rN   rO   rP   rS   rT   rU   rY   rZ   r[   r\   r]   r_   r   r   r   r   r   !   s<    
1  
    





I








r   )
r*   r+   Zboto.exceptionr   Zboto.route53.recordr   Zboto.route53.statusr   objectr   r   r   r   r   <module>   s
   