
    	]j&P                         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	 ddl
mZ ddlmZ  G d	 d
ej                        Z G d de      Z G d de      Z G d de      ZddZd Zd Zd Zy)z/Interact with Cloud Logging via JSON-over-HTTP.    N)page_iterator)_http)__version__)entry_from_resource)Metric)Sinkc                   4     e Zd ZdZded fd
ZdZ	 dZ xZS )
Connectionzhttps://logging.googleapis.comN)client_infoapi_endpointc                    t         t        |   ||       || _        t        | j
                  _        t        | j
                  _        y)a,  A connection to Google Cloud Logging via the JSON REST API.

        Args:
            client (google.cloud.logging_v2.cliet.Client):
                The client that owns the current connection.
            client_info (Optional[google.api_core.client_info.ClientInfo]):
                Instance used to generate user agent.
            client_options (Optional[google.api_core.client_options.ClientOptions]):
                Client options used to set user options
                on the client. API Endpoint should be set through client_options.
        N)superr
   __init__API_BASE_URLr   _client_infogapic_versionclient_library_version)selfclientr   r   	__class__s       G/root/env/lib/python3.12/site-packages/google/cloud/logging_v2/_http.pyr   zConnection.__init__   s:     	j$(=(*5'3>0    v2z"{api_base_url}/{api_version}{path})__name__
__module____qualname__DEFAULT_API_ENDPOINTr   API_VERSIONAPI_URL_TEMPLATE__classcell__)r   s   @r   r
   r
      s'    ;.2AU ?" KF;:r   r
   c                   D    e Zd ZdZd ZdddddddZddddddd	Zd
 Zy)_LoggingAPIa?  Helper mapping logging-related APIs.

    See
    https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries
    https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs

    :type client: :class:`~google.cloud.logging.client.Client`
    :param client: The client used to make API requests.
    c                 H    || _         |j                  j                  | _        y N_client_connectionapi_requestr   r   s     r   r   z_LoggingAPI.__init__B       !--99r   N)filter_order_bymax_results	page_size
page_tokenc          	          d|i}|||d<   |||d<   |||d<   d}i }	t        j                  t        |	      }
t        j                  | j
                  | j
                  j                  j                  ||
d||      }d	|_        t        ||      S )
a#  Return a page of log entry resources.

        Args:
            resource_names (Sequence[str]): Names of one or more parent resources
                from which to retrieve log entries:

                ::

                    "projects/[PROJECT_ID]"
                    "organizations/[ORGANIZATION_ID]"
                    "billingAccounts/[BILLING_ACCOUNT_ID]"
                    "folders/[FOLDER_ID]"

            filter_ (str): a filter expression. See
                https://cloud.google.com/logging/docs/view/advanced_filters
            order_by (str) One of :data:`~logging_v2.ASCENDING`
                or :data:`~logging_v2.DESCENDING`.
            max_results (Optional[int]):
                Optional. The maximum number of entries to return.
                Non-positive values are treated as 0. If None, uses API defaults.
            page_size (int): number of entries to fetch in each API call. Although
                requests are paged internally, logs are returned by the generator
                one at a time. If not passed, defaults to a value set by the API.
            page_token (str): opaque marker for the starting "page" of entries. If not
                passed, the API will return the first page of entries.
        Returns:
            Generator[~logging_v2.LogEntry]
        resourceNamesfilterorderBypageSizez/entries:list)loggersentriesr   r(   pathitem_to_value	items_keyr/   extra_paramsPOST)
	functoolspartial_item_to_entryr   HTTPIteratorr&   r'   r(   _HTTP_METHOD_entries_pager)r   resource_namesr+   r,   r-   r.   r/   r;   r8   r5   r9   iterators               r   list_entriesz_LoggingAPI.list_entriesF   s    L (8%,L"&.L# '0L$ !)).'J --<<00<<'!%
 !'h44r   TF)logger_nameresourcelabelspartial_successdry_runc                t    t        |      ||d}|||d<   |||d<   |||d<   | j                  dd|       y)	a  Log an entry resource via a POST request

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/write

        Args:
            entries (Sequence[Mapping[str, ...]]): sequence of mappings representing
                the log entry resources to log.
            logger_name (Optional[str]): name of default logger to which to log the entries;
                individual entries may override.
            resource(Optional[Mapping[str, ...]]): default resource to associate with entries;
                individual entries may override.
            labels (Optional[Mapping[str, ...]]): default labels to associate with entries;
                individual entries may override.
            partial_success (Optional[bool]): Whether valid entries should be written even if
                some other entries fail due to INVALID_ARGUMENT or
                PERMISSION_DENIED errors. If any entry is not written, then
                the response status is the error associated with one of the
                failed entries and the response includes error details keyed
                by the entries' zero-based index in the ``entries.write``
                method.
            dry_run (Optional[bool]):
                If true, the request should expect normal response,
                but the entries won't be persisted nor exported.
                Useful for checking whether the logging API endpoints are working
                properly before sending valuable data.
        )r6   partialSuccessrJ   NlogNamerG   rH   r<   z/entries:writemethodr8   data)listr(   )r   r6   rF   rG   rH   rI   rJ   rP   s           r   write_entriesz_LoggingAPI.write_entries   s_    L G}-
 ")DO'D#DN-=DIr   c                 4    d| }| j                  d|       y)aq  Delete all entries in a logger.

        Args:
            logger_name (str):  The resource name of the log to delete:

                ::

                    "projects/[PROJECT_ID]/logs/[LOG_ID]"
                    "organizations/[ORGANIZATION_ID]/logs/[LOG_ID]"
                    "billingAccounts/[BILLING_ACCOUNT_ID]/logs/[LOG_ID]"
                    "folders/[FOLDER_ID]/logs/[LOG_ID]"

                ``[LOG_ID]`` must be URL-encoded. For example,
                ``"projects/my-project-id/logs/syslog"``,
                ``"organizations/1234567890/logs/cloudresourcemanager.googleapis.com%2Factivity"``.
        /DELETErO   r8   Nr(   )r   rF   r8   s      r   logger_deletez_LoggingAPI.logger_delete   s#    " ;- t4r   )r   r   r   __doc__r   rE   rR   rX    r   r   r"   r"   7   sA    : C5R 4Jl5r   r"   c                   J    e Zd ZdZd ZdddddZdddZd	 Zddd
Zd Z	y)	_SinksAPIz}Helper mapping sink-related APIs.

    See
    https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks
    c                 H    || _         |j                  j                  | _        y r$   r%   r)   s     r   r   z_SinksAPI.__init__   r*   r   Nr-   r.   r/   c          	          i }|||d<   d| d}t        j                  | j                  | j                  j                  j                  |t
        d||      }t        ||      S )aU  List sinks for the parent resource.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/list

        Args:
            parent (str): The parent resource whose sinks are to be listed:

                ::

                    "projects/[PROJECT_ID]"
                    "organizations/[ORGANIZATION_ID]"
                    "billingAccounts/[BILLING_ACCOUNT_ID]"
                    "folders/[FOLDER_ID]".
            max_results (Optional[int]):
                Optional. The maximum number of entries to return.
                Non-positive values are treated as 0. If None, uses API defaults.
            page_size (int): number of entries to fetch in each API call. Although
                requests are paged internally, logs are returned by the generator
                one at a time. If not passed, defaults to a value set by the API.
            page_token (str): opaque marker for the starting "page" of entries. If not
                passed, the API will return the first page of entries.

        Returns:
            Generator[~logging_v2.Sink]
        r4   rT   /sinkssinksr7   )r   r@   r&   r'   r(   _item_to_sinkrB   )r   parentr-   r.   r/   r;   r8   rD   s           r   
list_sinksz_SinksAPI.list_sinks   so    6  '0L$6(&! --<<00<<'!%
 h44r   F)unique_writer_identityc                L    d| d}|||d}d|i}| j                  d|||      S )a  Create a sink resource.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create

        Args:
            parent(str): The resource in which to create the sink:

            ::

                "projects/[PROJECT_ID]"
                "organizations/[ORGANIZATION_ID]"
                "billingAccounts/[BILLING_ACCOUNT_ID]"
                "folders/[FOLDER_ID]".
            sink_name (str): The name of the sink.
            filter_ (str): The advanced logs filter expression defining the
                entries exported by the sink.
            destination (str): Destination URI for the entries exported by
                the sink.
            unique_writer_identity (Optional[bool]):  determines the kind of
                IAM identity returned as writer_identity in the new sink.

        Returns:
            dict: The sink resource returned from the API.
        rT   r`   namer2   destinationuniqueWriterIdentityr<   )rO   r8   rP   query_paramsrW   )	r   rc   	sink_namer+   ri   re   targetrP   rk   s	            r   sink_createz_SinksAPI.sink_create  sJ    8 VHF#!W[Q.0FGT   
 	
r   c                 2    d| }| j                  d|      S )a  Retrieve a sink resource.

        Args:
            sink_name (str): The resource name of the sink:

            ::

                "projects/[PROJECT_ID]/sinks/[SINK_ID]"
                "organizations/[ORGANIZATION_ID]/sinks/[SINK_ID]"
                "billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_ID]"
                "folders/[FOLDER_ID]/sinks/[SINK_ID]"

        Returns:
            dict: The JSON sink object returned from the API.
        rT   GETrV   rW   r   rl   rm   s      r   sink_getz_SinksAPI.sink_get1  s%      YKu6::r   c                r    d| }|j                  d      d   }|||d}d|i}| j                  d|||      S )a8  Update a sink resource.

        Args:
            sink_name (str): Required. The resource name of the sink:

            ::

                "projects/[PROJECT_ID]/sinks/[SINK_ID]"
                "organizations/[ORGANIZATION_ID]/sinks/[SINK_ID]"
                "billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_ID]"
                "folders/[FOLDER_ID]/sinks/[SINK_ID]"
            filter_ (str): The advanced logs filter expression defining the
                entries exported by the sink.
            destination (str): destination URI for the entries exported by
                the sink.
            unique_writer_identity (Optional[bool]): determines the kind of
                IAM identity returned as writer_identity in the new sink.


        Returns:
            dict: The returned (updated) resource.
        rT   rg   rj   PUT)rO   r8   rk   rP   )splitr(   )	r   rl   r+   ri   re   rm   rh   rP   rk   s	            r   sink_updatez_SinksAPI.sink_updateD  sZ    2 YKs#B'L.0FGvLt   
 	
r   c                 4    d| }| j                  d|       y)a0  Delete a sink resource.

        Args:
            sink_name (str): Required. The full resource name of the sink to delete,
                including the parent resource and the sink identifier:

                ::

                    "projects/[PROJECT_ID]/sinks/[SINK_ID]"
                    "organizations/[ORGANIZATION_ID]/sinks/[SINK_ID]"
                    "billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_ID]"
                    "folders/[FOLDER_ID]/sinks/[SINK_ID]"

                Example: ``"projects/my-project-id/sinks/my-sink-id"``.
        rT   rU   rV   NrW   rq   s      r   sink_deletez_SinksAPI.sink_deletee  s"      YKv6r   )
r   r   r   rY   r   rd   rn   rr   rw   ry   rZ   r   r   r\   r\      s@    : 15QU +5\ RW!
F;( JO
B7r   r\   c                   >    e Zd ZdZd ZdddddZd Zd Zd Zd	 Z	y)
_MetricsAPIz!Helper mapping sink-related APIs.c                 H    || _         |j                  j                  | _        y r$   r%   r)   s     r   r   z_MetricsAPI.__init__|  r*   r   Nr^   c          	          i }|||d<   d| d}t        j                  | j                  | j                  j                  j                  |t
        d||      }t        ||      S )aF  List metrics for the project associated with this client.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/list

        Args:
            max_results (Optional[int]):
                Optional. The maximum number of entries to return.
                Non-positive values are treated as 0. If None, uses API defaults.
            page_size (int): number of entries to fetch in each API call. Although
                requests are paged internally, logs are returned by the generator
                one at a time. If not passed, defaults to a value set by the API.
            page_token (str): opaque marker for the starting "page" of entries. If not
                passed, the API will return the first page of entries.

        Returns:
            Generator[logging_v2.Metric]

        r4   
/projects//metricsmetricsr7   )r   r@   r&   r'   r(   _item_to_metricrB   )r   projectr-   r.   r/   r;   r8   rD   s           r   list_metricsz_MetricsAPI.list_metrics  so    ,  '0L$G9H- --<<00<<)!%
 h44r   c                 D    d| d}|||d}| j                  d||       y)a  Create a metric resource.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/create

        Args:
            project (str): ID of the project in which to create the metric.
            metric_name (str): The name of the metric
            filter_ (str): The advanced logs filter expression defining the
                entries exported by the metric.
            description (str): description of the metric.
        r~   r   rh   r2   descriptionr<   rN   NrW   r   r   metric_namer+   r   rm   rP   s          r   metric_createz_MetricsAPI.metric_create  s3     gYh/#w{SV$?r   c                 8    d| d| }| j                  d|      S )zRetrieve a metric resource.

        Args:
            project (str): ID of the project containing the metric.
            metric_name (str): The name of the metric

        Returns:
            dict: The JSON metric object returned from the API.
        r~   	/metrics/rp   rV   rW   r   r   r   rm   s       r   
metric_getz_MetricsAPI.metric_get  s,     gYi}=u6::r   c                 F    d| d| }|||d}| j                  d||      S )a  Update a metric resource.

         See
         https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/update

        Args:
             project (str): ID of the project containing the metric.
             metric_name (str): the name of the metric
             filter_ (str): the advanced logs filter expression defining the
                 entries exported by the metric.
             description (str): description of the metric.

         Returns:
             dict: The returned (updated) resource.
        r~   r   r   ru   rN   rW   r   s          r   metric_updatez_MetricsAPI.metric_update  s:      gYi}=#w{Su6EEr   c                 :    d| d| }| j                  d|       y)zDelete a metric resource.

        Args:
            project (str): ID of the project containing the metric.
            metric_name (str): The name of the metric
        r~   r   rU   rV   NrW   r   s       r   metric_deletez_MetricsAPI.metric_delete  s)     gYi}=v6r   )
r   r   r   rY   r   r   r   r   r   r   rZ   r   r   r{   r{   y  s/    +:
 '+dt%5N@";F(7r   r{   c              #   f   K   ||dk  rt        d      d}| D ]  }|||k\  r y | |dz  } y w)Nr   zmax_results must be positive   )
ValueError)	page_iterr-   ipages       r   rB   rB     sP     ;?788	A "qK'7
	Q	s   /1c                 0    t        || j                  |      S )a  Convert a log entry resource to the native object.

    .. note::

        This method does not have the correct signature to be used as
        the ``item_to_value`` argument to
        :class:`~google.api_core.page_iterator.Iterator`. It is intended to be
        patched with a mutable ``loggers`` argument that can be updated
        on subsequent calls. For an example, see how the method is
        used above in :meth:`_LoggingAPI.list_entries`.

    Args:
        iterator (google.api_core.page_iterator.Iterator): The iterator that
            is currently in use.
        resource (dict): Log entry JSON resource returned from the API.
        loggers (Mapping[str, logging_v2.logger.Logger]):
            A mapping of logger fullnames -> loggers.  If the logger
            that owns the entry is not in ``loggers``, the entry
            will have a newly-created logger.

    Returns:
        ~logging_v2.entries._BaseEntry: The next log entry in the page.
    )r   r   )rD   rG   r5   s      r   r?   r?     s    0 x'BBr   c                 B    t        j                  || j                        S )a6  Convert a sink resource to the native object.

    Args:
        iterator (google.api_core.page_iterator.Iterator): The iterator that
            is currently in use.
        resource (dict): Sink JSON resource returned from the API.

    Returns:
        ~logging_v2.sink.Sink: The next sink in the page.
    )r   from_api_reprr   rD   rG   s     r   rb   rb     s     h88r   c                 B    t        j                  || j                        S )aJ  Convert a metric resource to the native object.

    Args:
        iterator (google.api_core.page_iterator.Iterator): The iterator that
            is currently in use.
        resource (dict): Sink JSON resource returned from the API.

    Returns:
        ~logging_v2.metric.Metric:
            The next metric in the page.
    )r   r   r   r   s     r   r   r     s     (//::r   r$   )rY   r=   google.api_corer   google.cloudr   google.cloud.logging_v2r    google.cloud.logging_v2._helpersr   google.cloud.logging_v2.metricr   google.cloud.logging_v2.sinkr   JSONConnectionr
   objectr"   r\   r{   rB   r?   rb   r   rZ   r   r   <module>r      ss    6  )  / @ 1 -;%% ;6\5& \5~`7 `7Fh7& h7V	C69;r   