a
    !f"                     @   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 )zDefine API ResourceRecordSets.    N)_rfc3339_to_datetime)NotFound)ResourceRecordSetc                   @   s   e Zd ZdZdd Zedd Zdd Zedd	 Z	ed
d Z
e
jdd Z
edd Zedd Zedd Zedd Zdd Zdd Zdd Zdd Zd$ddZd%d d!Zd&d"d#ZdS )'Changesa?  Changes are bundled additions / deletions of DNS resource records.

    Changes are owned by a :class:`gcloud.dns.zone.ManagedZone` instance.

    See:
    https://cloud.google.com/dns/api/v1/changes

    :type zone: :class:`gcloud.dns.zone.ManagedZone`
    :param zone: A zone which holds one or more record sets.
    c                 C   s   || _ i | _d | _| _d S )N )zone_properties
_additions
_deletions)selfr   r   r   S/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/dns/changes.py__init__$   s    zChanges.__init__c                 C   s   | |d}| | |S )a  Factory:  construct a change set given its API representation

        :type resource: dict
        :param resource: change set representation returned from the API

        :type zone: :class:`gcloud.dns.zone.ManagedZone`
        :param zone: A zone which holds zero or more change sets.

        :rtype: :class:`gcloud.dns.changes.Changes`
        :returns: RRS parsed from ``resource``.
        )r   )_set_properties)clsresourcer   changesr   r   r   from_api_repr)   s    

zChanges.from_api_reprc                    sR   |  }t fdd|ddD  _t fdd|ddD  _| _dS )zHelper method for :meth:`from_api_repr`, :meth:`create`, etc.

        :type resource: dict
        :param resource: change set representation returned from the API
        c                    s   g | ]}t | jqS r   r   r   r   .0Z	added_resr   r   r   
<listcomp>A   s   z+Changes._set_properties.<locals>.<listcomp>	additionsr   c                    s   g | ]}t | jqS r   r   r   r   r   r   r   D   s   	deletionsN)copytuplepopr	   r
   r   )r   r   r   r   r   r   :   s    



zChanges._set_propertiesc                 C   s   d| j j| j j| jf S )zURL path for change set APIs.

        :rtype: string
        :returns: the path based on project, zone, and change set names.
        z'/projects/%s/managedZones/%s/changes/%s)r   projectnamer   r   r   r   pathI   s    zChanges.pathc                 C   s   | j dS )zName of the change set.

        :rtype: string or ``NoneType``
        :returns: Name, as set by the back-end, or None.
        idr   getr   r   r   r   r   S   s    zChanges.namec                 C   s"   t |tjstd|| jd< dS )zvUpdate name of the change set.

        :type value: string
        :param value: New name for the changeset.
        zPass a stringr    N)
isinstancesixstring_types
ValueErrorr   )r   valuer   r   r   r   \   s    c                 C   s   | j dS )zStatus of the change set.

        :rtype: string or ``NoneType``
        :returns: Status, as set by the back-end, or None.
        statusr!   r   r   r   r   r(   g   s    zChanges.statusc                 C   s    | j d}|durt|S dS )zTime when the change set was started.

        :rtype: ``datetime.datetime`` or ``NoneType``
        :returns: Time, as set by the back-end, or None.
        Z	startTimeN)r   r"   r   )r   Zstampr   r   r   startedp   s    zChanges.startedc                 C   s   | j S )zResource record sets to be added to the zone.

        :rtype: sequence of
                :class:`gcloud.dns.resource_record_set.ResourceRecordSet`.
        :returns: record sets appended via :meth:`add_record_set`
        )r	   r   r   r   r   r   {   s    zChanges.additionsc                 C   s   | j S )zResource record sets to be deleted from the zone.

        :rtype: sequence of
                :class:`gcloud.dns.resource_record_set.ResourceRecordSet`.
        :returns: record sets appended via :meth:`delete_record_set`
        )r
   r   r   r   r   r      s    zChanges.deletionsc                 C   s&   t |tstd|  j|f7  _dS )a(  Append a record set to the 'additions' for the change set.

        :type record_set:
            :class:`gcloud.dns.resource_record_set.ResourceRecordSet`
        :param record_set: the record set to append

        :raises: ``ValueError`` if ``record_set`` is not of the required type.
        Pass a ResourceRecordSetN)r#   r   r&   r	   r   Z
record_setr   r   r   add_record_set   s    	
zChanges.add_record_setc                 C   s&   t |tstd|  j|f7  _dS )a(  Append a record set to the 'deletions' for the change set.

        :type record_set:
            :class:`gcloud.dns.resource_record_set.ResourceRecordSet`
        :param record_set: the record set to append

        :raises: ``ValueError`` if ``record_set`` is not of the required type.
        r*   N)r#   r   r&   r
   r+   r   r   r   delete_record_set   s    	
zChanges.delete_record_setc                 C   s   |du r| j j}|S )av  Check client or verify over-ride.

        :type client: :class:`gcloud.dns.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current zone.

        :rtype: :class:`gcloud.dns.client.Client`
        :returns: The client passed in or the currently bound client.
        N)r   Z_clientr   clientr   r   r   _require_client   s    
zChanges._require_clientc                 C   s*   dd | j D }dd | jD }||dS )z#Generate a resource for ``create``.c                 S   s&   g | ]}|j |jt|j|jd qS )r   typettlrrdatasr   Zrecord_typestrr3   r4   )r   addedr   r   r   r      s   z+Changes._build_resource.<locals>.<listcomp>c                 S   s&   g | ]}|j |jt|j|jd qS r1   r5   )r   Zdeletedr   r   r   r      s   r   r   r8   )r   r   r   r   r   r   _build_resource   s    zChanges._build_resourceNc                 C   sf   t | jdkr$t | jdkr$td| |}d| jj| jjf }|jj	d|| 
 d}| | dS )aX  API call:  create the change set via a POST request

        See:
        https://cloud.google.com/dns/api/v1/changes/create

        :type client: :class:`gcloud.dns.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current zone.
        r   zNo record sets added or deletedz$/projects/%s/managedZones/%s/changesPOST)methodr   dataN)lenr   r   r&   r0   r   r   r   
connectionapi_requestr9   r   )r   r/   r   api_responser   r   r   create   s    


zChanges.createc                 C   sD   |  |}z|jjd| jddid W n ty:   Y dS 0 dS dS )a  API call:  test for the existence of the change set via a GET request

        See
        https://cloud.google.com/dns/api/v1/changes/get

        :type client: :class:`gcloud.dns.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current zone.

        :rtype: bool
        :returns: Boolean indicating existence of the changes.
        GETfieldsr    )r;   r   Zquery_paramsFTN)r0   r>   r?   r   r   r.   r   r   r   exists   s    

zChanges.existsc                 C   s*   |  |}|jjd| jd}| | dS )aU  API call:  refresh zone properties via a GET request

        See
        https://cloud.google.com/dns/api/v1/changes/get

        :type client: :class:`gcloud.dns.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current zone.
        rB   )r;   r   N)r0   r>   r?   r   r   )r   r/   r@   r   r   r   reload   s
    

zChanges.reload)N)N)N)__name__
__module____qualname____doc__r   classmethodr   r   propertyr   r   setterr(   r)   r   r   r,   r-   r0   r9   rA   rD   rE   r   r   r   r   r      s4   

	






	
	

r   )
rI   r$   Zgcloud._helpersr   Zgcloud.exceptionsr   Zgcloud.dns.resource_record_setr   objectr   r   r   r   r   <module>   s
   