a
    !f27                     @   sP   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	 G dd de
ZdS )	zDefine API ManagedZones.    N)_rfc3339_to_datetime)NotFoundChangesResourceRecordSetc                   @   s   e Zd ZdZd-ddZedd Zedd Zed	d
 Z	edd Z
edd Zedd Zedd Zejdd Zedd Zejdd Zdd Zdd Zdd Zdd Zdd  Zd.d!d"Zd/d#d$Zd0d%d&Zd1d'd(Zd2d)d*Zd3d+d,ZdS )4ManagedZonea  ManagedZones are containers for DNS resource records.

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

    :type name: string
    :param name: the name of the zone

    :type dns_name: string or :class:`NoneType`
    :param dns_name: the DNS name of the zone.  If not passed, then calls
                     to :meth:`create` will fail.

    :type client: :class:`gcloud.dns.client.Client`
    :param client: A client which holds credentials and project configuration
                   for the zone (which requires a project).

    :type description: string or :class:`NoneType`
    :param description: the description for the zone.  If not passed, defaults
                        to the value of 'dns_name'.
    Nc                 C   s.   || _ || _|| _i | _|d u r$|}|| _d S )N)namedns_name_client_propertiesdescription)selfr	   r
   clientr    r   P/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/dns/zone.py__init__.   s    zManagedZone.__init__c                 C   sH   | d}| d}|du s$|du r,td| |||d}|| |S )a  Factory:  construct a zone given its API representation

        :type resource: dict
        :param resource: zone resource representation returned from the API

        :type client: :class:`gcloud.dns.client.Client`
        :param client: Client which holds credentials and project
                       configuration for the zone.

        :rtype: :class:`gcloud.dns.zone.ManagedZone`
        :returns: Zone parsed from ``resource``.
        r	   dnsNameNz@Resource lacks required identity information:["name"]["dnsName"])r   )getKeyError_set_properties)clsresourcer   r	   r
   zoner   r   r   from_api_repr7   s    


zManagedZone.from_api_reprc                 C   s   | j jS )ztProject bound to the zone.

        :rtype: string
        :returns: the project (derived from the client).
        )r   projectr   r   r   r   r   N   s    zManagedZone.projectc                 C   s   d| j | jf S )z|URL path for the zone's APIs.

        :rtype: string
        :returns: the path based on project and dataste name.
        z/projects/%s/managedZones/%s)r   r	   r   r   r   r   pathW   s    zManagedZone.pathc                 C   s   | j dS )zDatetime at which the zone was created.

        :rtype: ``datetime.datetime``, or ``NoneType``
        :returns: the creation time (None until set from the server).
        creationTimer   r   r   r   r   r   created`   s    zManagedZone.createdc                 C   s   | j dS )zDatetime at which the zone was created.

        :rtype: list of strings, or ``NoneType``.
        :returns: the assigned name servers (None until set from the server).
        ZnameServersr   r   r   r   r   name_serversi   s    zManagedZone.name_serversc                 C   s   | j dS )zID for the zone resource.

        :rtype: string, or ``NoneType``
        :returns: the ID (None until set from the server).
        idr   r   r   r   r   zone_idr   s    zManagedZone.zone_idc                 C   s   | j dS )zDescription of the zone.

        :rtype: string, or ``NoneType``
        :returns: The description as set by the user, or None (the default).
        r   r   r   r   r   r   r   {   s    zManagedZone.descriptionc                 C   s*   t |tjs|durtd|| jd< dS )zUpdate description of the zone.

        :type value: string, or ``NoneType``
        :param value: new description

        :raises: ValueError for invalid value types.
        NPass a string, or Noner   
isinstancesixstring_types
ValueErrorr   r   valuer   r   r   r      s    	c                 C   s   | j dS )a:  Named set of DNS name servers that all host the same ManagedZones.

        Most users will leave this blank.

        See:
        https://cloud.google.com/dns/api/v1/managedZones#nameServerSet

        :rtype: string, or ``NoneType``
        :returns: The name as set by the user, or None (the default).
        nameServerSetr   r   r   r   r   name_server_set   s    zManagedZone.name_server_setc                 C   s*   t |tjs|durtd|| jd< dS )zUpdate named set of DNS name servers.

        :type value: string, or ``NoneType``
        :param value: new title

        :raises: ValueError for invalid value types.
        Nr$   r,   r%   r*   r   r   r   r-      s    	c                 C   s   t ||||| dS )a  Construct a resource record set bound to this zone.

        :type name: string
        :param name: Name of the record set.

        :type record_type: string
        :param record_type: RR type

        :type ttl: integer
        :param ttl: TTL for the RR, in seconds

        :type rrdatas: list of string
        :param rrdatas: resource data for the RR

        :rtype: :class:`gcloud.dns.resource_record_set.ResourceRecordSet`
        :returns: a new ``ResourceRecordSet`` instance
        r   r   )r   r	   Zrecord_typettlZrrdatasr   r   r   resource_record_set   s    zManagedZone.resource_record_setc                 C   s
   t | dS )zConstruct a change set bound to this zone.

        :rtype: :class:`gcloud.dns.changes.Changes`
        :returns: a new ``Changes`` instance
        r.   r   r   r   r   r   changes   s    zManagedZone.changesc                 C   s   |du r| 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   r   r   r   r   r   _require_client   s    
zManagedZone._require_clientc                 C   sH   | j   | }|dd| _d|v r8t|d |d< | j | dS )zUpdate properties from resource in body of ``api_response``

        :type api_response: httplib2.Response
        :param api_response: response returned from an API call
        r   Nr   )r   clearcopypopr
   r   update)r   api_responsecleanedr   r   r   r      s    
zManagedZone._set_propertiesc                 C   sJ   d| j i}| jdur| j|d< | jdur2| j|d< | jdurF| j|d< |S )z1Generate a resource for ``create`` or ``update``.r	   Nr   r   r,   )r	   r
   r   r-   )r   r   r   r   r   _build_resource   s    





zManagedZone._build_resourcec                 C   s:   |  |}d| jf }|jjd||  d}| | dS )aV  API call:  create the zone via a PUT request

        See:
        https://cloud.google.com/dns/api/v1/managedZones/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.
        z/projects/%s/managedZonesPOST)methodr   dataN)r3   r   
connectionapi_requestr:   r   )r   r   r   r8   r   r   r   create   s    


zManagedZone.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 zone via a GET request

        See
        https://cloud.google.com/dns/api/v1/managedZones/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 managed zone.
        GETfieldsr"   r<   r   Zquery_paramsFTN)r3   r>   r?   r   r   r2   r   r   r   exists  s    

zManagedZone.existsc                 C   s*   |  |}|jjd| jd}| | dS )aZ  API call:  refresh zone properties via a GET request

        See
        https://cloud.google.com/dns/api/v1/managedZones/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.
        rA   r<   r   N)r3   r>   r?   r   r   )r   r   r8   r   r   r   reload  s
    

zManagedZone.reloadc                 C   s    |  |}|jjd| jd dS )aY  API call:  delete the zone via a DELETE request

        See:
        https://cloud.google.com/dns/api/v1/managedZones/delete

        :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.
        DELETErE   N)r3   r>   r?   r   r2   r   r   r   delete,  s    

zManagedZone.deletec           	         sx   i }|dur||d< |dur$||d< d j  jf } |}|j}|jd||d} fdd|d	 D }||d
fS )a>  List resource record sets for this zone.

        See:
        https://cloud.google.com/dns/api/v1/resourceRecordSets/list

        :type max_results: int
        :param max_results: maximum number of zones to return, If not
                            passed, defaults to a value set by the API.

        :type page_token: string
        :param page_token: opaque marker for the next "page" of zones. If
                           not passed, the API will return the first page of
                           zones.

        :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: tuple, (list, str)
        :returns: list of
                  :class:`gcloud.dns.resource_record_set.ResourceRecordSet`,
                  plus a "next page token" string:  if the token is not None,
                  indicates that more zones can be retrieved with another
                  call (pass that value as ``page_token``).
        N
maxResults	pageTokenz#/projects/%s/managedZones/%s/rrsetsrA   rC   c                    s   g | ]}t | qS r   )r   r   .0r   r   r   r   
<listcomp>a  s   z9ManagedZone.list_resource_record_sets.<locals>.<listcomp>ZrrsetsnextPageTokenr   r	   r3   r>   r?   r   	r   Zmax_resultsZ
page_tokenr   paramsr   connrespZzonesr   r   r   list_resource_record_sets9  s    

z%ManagedZone.list_resource_record_setsc           	         sx   i }|dur||d< |dur$||d< d j  jf } |}|j}|jd||d} fdd|d	 D }||d
fS )a5  List change sets for this zone.

        See:
        https://cloud.google.com/dns/api/v1/resourceRecordSets/list

        :type max_results: int
        :param max_results: maximum number of zones to return, If not
                            passed, defaults to a value set by the API.

        :type page_token: string
        :param page_token: opaque marker for the next "page" of zones. If
                           not passed, the API will return the first page of
                           zones.

        :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: tuple, (list, str)
        :returns: list of
                  :class:`gcloud.dns.resource_record_set.ResourceRecordSet`,
                  plus a "next page token" string:  if the token is not None,
                  indicates that more zones can be retrieved with another
                  call (pass that value as ``page_token``).
        NrI   rJ   z$/projects/%s/managedZones/%s/changesrA   rC   c                    s   g | ]}t | qS r   )r   r   rK   r   r   r   rM     s   z,ManagedZone.list_changes.<locals>.<listcomp>r1   rN   rO   rP   r   r   r   list_changese  s    

zManagedZone.list_changes)NNN)N)N)N)N)NNN)NNN)__name__
__module____qualname____doc__r   classmethodr   propertyr   r   r    r!   r#   r   setterr-   r0   r1   r3   r   r:   r@   rD   rF   rH   rT   rU   r   r   r   r   r      sF   
	













  
,r   )rY   r'   Zgcloud._helpersr   Zgcloud.exceptionsr   Zgcloud.dns.changesr   Zgcloud.dns.resource_record_setr   objectr   r   r   r   r   <module>   s   