a
    !f6                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ e	dZ
e	dZi ZdZd	d
 ZdddZdd ZG dd deZG dd deZdS )z:User friendly container for Google Cloud Bigtable Cluster.    N)operations_pb2)instance_pb2)bigtable_instance_admin_pb2zd^projects/(?P<project>[^/]+)/instances/(?P<instance>[^/]+)/clusters/(?P<cluster_id>[a-z][-a-z0-9]*)$zk^operations/projects/([^/]+)/instances/([^/]+)/clusters/([a-z][-a-z0-9]*)/operations/(?P<operation_id>\d+)$   c                 C   s    t j| jj| jtj| jddS )a  Creates a protobuf request for a CreateCluster request.

    :type cluster: :class:`Cluster`
    :param cluster: The cluster to be created.

    :rtype: :class:`.messages_v2_pb2.CreateClusterRequest`
    :returns: The CreateCluster request object containing the cluster info.
    serve_nodes)parent
cluster_idcluster)messages_v2_pb2ZCreateClusterRequest	_instancenamer	   data_v2_pb2Clusterr   r
    r   X/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigtable/cluster.py_prepare_create_request+   s    	r   c                 C   s:   |dur$|| j kr$td|| j f t| j  }|| jS )a  Convert a serialized "google.protobuf.Any" value to actual type.

    :type any_val: :class:`google.protobuf.any_pb2.Any`
    :param any_val: A serialized protobuf value container.

    :type expected_type: str
    :param expected_type: (Optional) The type URL we expect ``any_val``
                          to have.

    :rtype: object
    :returns: The de-serialized object.
    :raises: :class:`ValueError <exceptions.ValueError>` if the
             ``expected_type`` does not match the ``type_url`` on the input.
    NzExpected type: %s, Received: %s)Ztype_url
ValueError_TYPE_URL_MAPZ
FromStringvalue)Zany_valexpected_typeZcontainer_classr   r   r   _parse_pb_any_to_native=   s    
r   c                 C   s2   t | j}|du r td| jt|d}|S )a  Processes a create protobuf response.

    :type operation_pb: :class:`google.longrunning.operations_pb2.Operation`
    :param operation_pb: The long-running operation response from a
                         Create/Update/Undelete cluster request.

    :rtype: tuple
    :returns: integer ID of the operation (``operation_id``).
    :raises: :class:`ValueError <exceptions.ValueError>` if the operation name
             doesn't match the :data:`_OPERATION_NAME_RE` regex.
    NzKOperation name was not in the expected format after a cluster modification.operation_id)_OPERATION_NAME_REmatchr   r   intgroup)operation_pbr   r   r   r   r   _process_operationS   s    r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )	Operationa  Representation of a Google API Long-Running Operation.

    In particular, these will be the result of operations on
    clusters using the Cloud Bigtable API.

    :type op_type: str
    :param op_type: The type of operation being performed. Expect
                    ``create``, ``update`` or ``undelete``.

    :type op_id: int
    :param op_id: The ID of the operation.

    :type cluster: :class:`Cluster`
    :param cluster: The cluster that created the operation.
    Nc                 C   s   || _ || _|| _d| _d S NF)op_typeop_id_cluster	_complete)selfr"   r#   r
   r   r   r   __init__z   s    zOperation.__init__c                 C   s@   t || jsdS |j| jko>|j| jko>|j| jko>|j| jkS r!   )
isinstance	__class__r"   r#   r$   r%   r&   otherr   r   r   __eq__   s    


zOperation.__eq__c                 C   s   |  | S Nr,   r*   r   r   r   __ne__   s    zOperation.__ne__c                 C   sd   | j rtdd| jj d| jf  }tj|d}| jjj}|j	
||j}|jr\d| _ dS dS dS )a  Check if the operation has finished.

        :rtype: bool
        :returns: A boolean indicating if the current operation has completed.
        :raises: :class:`ValueError <exceptions.ValueError>` if the operation
                 has already completed.
        zThe operation has completed.zoperations/z/operations/%dr   TFN)r%   r   r$   r   r#   r   ZGetOperationRequestr   _clientZ_operations_stubZGetOperationtimeout_secondsdone)r&   Zoperation_name
request_pbclientr   r   r   r   finished   s    


zOperation.finished)N)__name__
__module____qualname____doc__r'   r,   r/   r6   r   r   r   r   r    i   s
   
r    c                   @   st   e Zd ZdZefddZdd Zedd Zdd	 Z	e
d
d Zdd Zdd Zdd Zdd Zdd Zdd ZdS )r   a  Representation of a Google Cloud Bigtable Cluster.

    We can use a :class:`Cluster` to:

    * :meth:`reload` itself
    * :meth:`create` itself
    * :meth:`update` itself
    * :meth:`delete` itself
    * :meth:`undelete` itself

    .. note::

        For now, we leave out the ``default_storage_type`` (an enum)
        which if not sent will end up as :data:`.data_v2_pb2.STORAGE_SSD`.

    :type cluster_id: str
    :param cluster_id: The ID of the cluster.

    :type instance: :class:`.instance.Instance`
    :param instance: The instance where the cluster resides.

    :type serve_nodes: int
    :param serve_nodes: (Optional) The number of nodes in the cluster.
                        Defaults to :data:`DEFAULT_SERVE_NODES`.
    c                 C   s   || _ || _|| _d | _d S r-   )r	   r   r   location)r&   r	   instancer   r   r   r   r'      s    zCluster.__init__c                 C   s"   |j std|j | _ |j| _dS )zpRefresh self from the server-provided protobuf.

        Helper for :meth:`from_pb` and :meth:`reload`.
        z-Cluster protobuf does not contain serve_nodesN)r   r   r;   )r&   
cluster_pbr   r   r   _update_from_pb   s    zCluster._update_from_pbc                 C   sp   t |j}|du r td|j|d|jjkr:td|d|jkrRtd| |d|}|| |S )a  Creates a cluster instance from a protobuf.

        :type cluster_pb: :class:`instance_pb2.Cluster`
        :param cluster_pb: A cluster protobuf object.

        :type instance: :class:`.instance.Instance>`
        :param instance: The instance that owns the cluster.

        :rtype: :class:`Cluster`
        :returns: The cluster parsed from the protobuf response.
        :raises:
            :class:`ValueError <exceptions.ValueError>` if the cluster
            name does not match
            ``projects/{project}/instances/{instance}/clusters/{cluster_id}``
            or if the parsed project ID does not match the project ID
            on the client.
        Nz5Cluster protobuf name was not in the expected format.projectzAProject ID on cluster does not match the project ID on the clientr<   zCInstance ID on cluster does not match the instance ID on the clientr	   )	_CLUSTER_NAME_REr   r   r   r   r1   r?   Zinstance_idr>   )clsr=   r<   r   resultr   r   r   from_pb   s    
zCluster.from_pbc                 C   s   | j  }| j| j|| jdS )zMake a copy of this cluster.

        Copies the local data stored as simple types and copies the client
        attached to this instance.

        :rtype: :class:`.Cluster`
        :returns: A copy of the current cluster.
        r   )r   copyr)   r	   r   )r&   Znew_instancer   r   r   rD      s    	

zCluster.copyc                 C   s   | j jd | j S )ao  Cluster name used in requests.

        .. note::
          This property will not change if ``_instance`` and ``cluster_id``
          do not, but the return value is not cached.

        The cluster name is of the form

            ``"projects/{project}/instances/{instance}/clusters/{cluster_id}"``

        :rtype: str
        :returns: The cluster name.
        z
/clusters/)r   r   r	   )r&   r   r   r   r      s    zCluster.namec                 C   s(   t || jsdS |j| jko&|j| jkS r!   )r(   r)   r	   r   r*   r   r   r   r,     s
    
zCluster.__eq__c                 C   s   |  | S r-   r.   r*   r   r   r   r/     s    zCluster.__ne__c                 C   s4   t j| jd}| jjj|| jjj}| | dS )z%Reload the metadata for this cluster.r0   N)	r   ZGetClusterRequestr   r   r1   _instance_stubZ
GetClusterr2   r>   )r&   r4   r=   r   r   r   reload   s
    

zCluster.reloadc                 C   s6   t | }| jjj|| jjj}t|}td|| dS )a:  Create this cluster.

        .. note::

            Uses the ``project``, ``instance`` and ``cluster_id`` on the
            current :class:`Cluster` in addition to the ``serve_nodes``.
            To change them before creating, reset the values via

            .. code:: python

                cluster.serve_nodes = 8
                cluster.cluster_id = 'i-changed-my-mind'

            before calling :meth:`create`.

        :rtype: :class:`Operation`
        :returns: The long-running operation corresponding to the
                  create operation.
        creater   )r   r   r1   rE   ZCreateClusterr2   r   r    r&   r4   r   r#   r   r   r   rG   +  s    

zCluster.createc                 C   s@   t j| j| jd}| jjj|| jjj}t	|}t
d|| dS )a  Update this cluster.

        .. note::

            Updates the ``serve_nodes``. If you'd like to
            change them before updating, reset the values via

            .. code:: python

                cluster.serve_nodes = 8

            before calling :meth:`update`.

        :rtype: :class:`Operation`
        :returns: The long-running operation corresponding to the
                  update operation.
        )r   r   updater   )r   r   r   r   r   r1   rE   ZUpdateClusterr2   r   r    rH   r   r   r   rI   G  s    

zCluster.updatec                 C   s*   t j| jd}| jjj|| jjj dS )a:  Delete this cluster.

        Marks a cluster and all of its tables for permanent deletion in 7 days.

        Immediately upon completion of the request:

        * Billing will cease for all of the cluster's reserved resources.
        * The cluster's ``delete_time`` field will be set 7 days in the future.

        Soon afterward:

        * All tables within the cluster will become unavailable.

        Prior to the cluster's ``delete_time``:

        * The cluster can be recovered with a call to ``UndeleteCluster``.
        * All other attempts to modify or delete the cluster will be rejected.

        At the cluster's ``delete_time``:

        * The cluster and **all of its tables** will immediately and
          irrevocably disappear from the API, and their data will be
          permanently deleted.
        r0   N)r   ZDeleteClusterRequestr   r   r1   rE   ZDeleteClusterr2   )r&   r4   r   r   r   deleted  s    

zCluster.deleteN)r7   r8   r9   r:   DEFAULT_SERVE_NODESr'   r>   classmethodrC   rD   propertyr   r,   r/   rF   rG   rI   rJ   r   r   r   r   r      s   


!
r   )N)r:   reZgoogle.longrunningr   Zgcloud.bigtable._generatedr   r   r   r   compiler@   r   r   rK   r   r   r   objectr    r   r   r   r   r   <module>   s   


<