a
    !fW                     @   sN  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 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 ddlmZ ddlmZ ddlmZ G dd deZG dd deZG dd deZd.ddZ dd Z!dd Z"dd Z#d d! Z$d"d# Z%d$d% Z&d&d' Z'd(d) Z(d*d+ Z)d,d- Z*dS )/z%GAX wrapper for Logging API requests.    N)CallOptions)INITIAL_PAGE)GaxError)exc_to_code)LogSeverity)LogSink)	LogMetric)LogEntry)Parse)
StatusCode)Conflict)NotFound)_datetime_to_pb_timestamp)_datetime_to_rfc3339)_pb_timestamp_to_datetimec                   @   s4   e Zd ZdZdd ZdddZdd	d
Zdd ZdS )_LoggingAPIzHelper mapping logging-related APIs.

    :type gax_api:
        :class:`google.logging.v2.logging_service_v2_api.LoggingServiceV2Api`
    :param gax_api: API object used to make GAX requests.
    c                 C   s
   || _ d S N_gax_apiselfZgax_api r   T/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/logging/_gax.py__init__.   s    z_LoggingAPI.__init__ r   Nc           
      C   s@   t |}| j|||||}dd | D }|jp6d}	||	fS )a  Return a page of log entry resources.

        :type projects: list of strings
        :param projects: project IDs to include. If not passed,
                         defaults to the project bound to the API's client.

        :type filter_: str
        :param filter_: a filter expression. See:
                        https://cloud.google.com/logging/docs/view/advanced_filters

        :type order_by: str
        :param order_by: One of :data:`gcloud.logging.ASCENDING` or
                         :data:`gcloud.logging.DESCENDING`.

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

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

        :rtype: tuple, (list, str)
        :returns: list of mappings, plus a "next page token" string:
                  if not None, indicates that more entries can be retrieved
                  with another call (pass that value as ``page_token``).
        c                 S   s   g | ]}t |qS r   )_log_entry_pb_to_mapping).0entry_pbr   r   r   
<listcomp>R   s   z,_LoggingAPI.list_entries.<locals>.<listcomp>N)_build_paging_optionsr   Zlist_log_entriesnext
page_token)
r   Zprojectsfilter_Zorder_by	page_sizer!   options	page_iterentriestokenr   r   r   list_entries1   s    

z_LoggingAPI.list_entriesc                 C   s0   d}d}dd |D }| j |||||| dS )a  API call:  log an entry resource via a POST request

        :type entries: sequence of mapping
        :param entries: the log entry resources to log.

        :type logger_name: string
        :param logger_name: name of default logger to which to log the entries;
                            individual entries may override.

        :type resource: mapping
        :param resource: default resource to associate with entries;
                         individual entries may override.

        :type labels: mapping
        :param labels: default labels to associate with entries;
                       individual entries may override.
        NFc                 S   s   g | ]}t |qS r   )_log_entry_mapping_to_pb)r   entryr   r   r   r   l       z-_LoggingAPI.write_entries.<locals>.<listcomp>)r   Zwrite_log_entries)r   r&   logger_nameresourcelabelsr$   Zpartial_successZ	entry_pbsr   r   r   write_entriesW   s    z_LoggingAPI.write_entriesc                 C   s"   d}d||f }| j || dS )a$  API call:  delete all entries in a logger via a DELETE request

        :type project: string
        :param project: ID of project containing the log entries to delete

        :type logger_name: string
        :param logger_name: name of logger containing the log entries to delete
        Nzprojects/%s/logs/%s)r   Z
delete_log)r   projectr,   r$   pathr   r   r   logger_deletep   s    	z_LoggingAPI.logger_delete)r   r   r   N)NNN)__name__
__module____qualname____doc__r   r(   r/   r2   r   r   r   r   r   '   s     
&  
r   c                   @   sB   e Zd ZdZdd ZdddZdd	 Zd
d Zdd Zdd Z	dS )	_SinksAPIzHelper mapping sink-related APIs.

    :type gax_api:
        :class:`google.logging.v2.config_service_v2_api.ConfigServiceV2Api`
    :param gax_api: API object used to make GAX requests.
    c                 C   s
   || _ d S r   r   r   r   r   r   r      s    z_SinksAPI.__init__r   Nc           	      C   sF   t |}d|f }| j|||}dd | D }|jp<d}||fS )a#  List sinks for the project associated with this client.

        :type project: string
        :param project: ID of the project whose sinks are to be listed.

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

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

        :rtype: tuple, (list, str)
        :returns: list of mappings, plus a "next page token" string:
                  if not None, indicates that more sinks can be retrieved
                  with another call (pass that value as ``page_token``).
        projects/%sc                 S   s   g | ]}t |qS r   )_log_sink_pb_to_mapping)r   Zlog_sink_pbr   r   r   r      s   z(_SinksAPI.list_sinks.<locals>.<listcomp>N)r   r   
list_sinksr    r!   )	r   r0   r#   r!   r$   r1   r%   Zsinksr'   r   r   r   r:      s    

z_SinksAPI.list_sinksc           
   
   C   s   d}d|f }t |||d}z| j||| W nJ tyz } z2t|jtjkrdd||f }	t|	 W Y d}~n
d}~0 0 dS )a  API call:  create a sink resource.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks/create

        :type project: string
        :param project: ID of the project in which to create the sink.

        :type sink_name: string
        :param sink_name: the name of the sink

        :type filter_: string
        :param filter_: the advanced logs filter expression defining the
                        entries exported by the sink.

        :type destination: string
        :param destination: destination URI for the entries exported by
                            the sink.
        Nr8   namefilterdestinationprojects/%s/sinks/%s)	r   r   Zcreate_sinkr   r   causer   FAILED_PRECONDITIONr   )
r   r0   	sink_namer"   r>   r$   parentsink_pbexcr1   r   r   r   sink_create   s    
z_SinksAPI.sink_createc              
   C   sj   d}d||f }z| j ||}W n> ty` } z&t|jtjkrJt| W Y d}~n
d}~0 0 t|S )ah  API call:  retrieve a sink resource.

        :type project: string
        :param project: ID of the project containing the sink.

        :type sink_name: string
        :param sink_name: the name of the sink

        :rtype: dict
        :returns: The sink object returned from the API (converted from a
                  protobuf to a dictionary).
        Nr?   )	r   Zget_sinkr   r   r@   r   	NOT_FOUNDr   r9   )r   r0   rB   r$   r1   rD   rE   r   r   r   sink_get   s    z_SinksAPI.sink_getc           	   
   C   sz   d}d||f }t |||d}z| j||| W n> typ } z&t|jtjkrZt| W Y d}~n
d}~0 0 t	|S )a  API call:  update a sink resource.

        :type project: string
        :param project: ID of the project containing the sink.

        :type sink_name: string
        :param sink_name: the name of the sink

        :type filter_: string
        :param filter_: the advanced logs filter expression defining the
                        entries exported by the sink.

        :type destination: string
        :param destination: destination URI for the entries exported by
                            the sink.

        :rtype: dict
        :returns: The sink object returned from the API (converted from a
                  protobuf to a dictionary).
        Nr?   r;   )
r   r   Zupdate_sinkr   r   r@   r   rG   r   r9   )	r   r0   rB   r"   r>   r$   r1   rD   rE   r   r   r   sink_update   s    z_SinksAPI.sink_updatec              
   C   sf   d}d||f }z| j || W n> ty` } z&t|jtjkrJt| W Y d}~n
d}~0 0 dS )zAPI call:  delete a sink resource.

        :type project: string
        :param project: ID of the project containing the sink.

        :type sink_name: string
        :param sink_name: the name of the sink
        Nr?   )r   Zdelete_sinkr   r   r@   r   rG   r   )r   r0   rB   r$   r1   rE   r   r   r   sink_delete   s    	z_SinksAPI.sink_delete)r   N)
r3   r4   r5   r6   r   r:   rF   rH   rI   rJ   r   r   r   r   r7   ~   s   
  r7   c                   @   sB   e Zd ZdZdd ZdddZdd	 Zd
d Zdd Zdd Z	dS )_MetricsAPIzHelper mapping sink-related APIs.

    :type gax_api:
        :class:`google.logging.v2.metrics_service_v2_api.MetricsServiceV2Api`
    :param gax_api: API object used to make GAX requests.
    c                 C   s
   || _ d S r   r   r   r   r   r   r     s    z_MetricsAPI.__init__r   Nc           	      C   sF   t |}d|f }| j|||}dd | D }|jp<d}||fS )a/  List metrics for the project associated with this client.

        :type project: string
        :param project: ID of the project whose metrics are to be listed.

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

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

        :rtype: tuple, (list, str)
        :returns: list of mappings, plus a "next page token" string:
                  if not None, indicates that more metrics can be retrieved
                  with another call (pass that value as ``page_token``).
        r8   c                 S   s   g | ]}t |qS r   )_log_metric_pb_to_mapping)r   Zlog_metric_pbr   r   r   r   /  s   z,_MetricsAPI.list_metrics.<locals>.<listcomp>N)r   r   Zlist_log_metricsr    r!   )	r   r0   r#   r!   r$   r1   r%   Zmetricsr'   r   r   r   list_metrics  s    

z_MetricsAPI.list_metricsc           
   
   C   s   d}d|f }t |||d}z| j||| W nJ tyz } z2t|jtjkrdd||f }	t|	 W Y d}~n
d}~0 0 dS )aV  API call:  create a metric resource.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics/create

        :type project: string
        :param project: ID of the project in which to create the metric.

        :type metric_name: string
        :param metric_name: the name of the metric

        :type filter_: string
        :param filter_: the advanced logs filter expression defining the
                        entries exported by the metric.

        :type description: string
        :param description: description of the metric.
        Nr8   r<   r=   descriptionprojects/%s/metrics/%s)	r   r   Zcreate_log_metricr   r   r@   r   rA   r   )
r   r0   metric_namer"   rO   r$   rC   	metric_pbrE   r1   r   r   r   metric_create4  s    
z_MetricsAPI.metric_createc              
   C   sj   d}d||f }z| j ||}W n> ty` } z&t|jtjkrJt| W Y d}~n
d}~0 0 t|S )at  API call:  retrieve a metric resource.

        :type project: string
        :param project: ID of the project containing the metric.

        :type metric_name: string
        :param metric_name: the name of the metric

        :rtype: dict
        :returns: The metric object returned from the API (converted from a
                  protobuf to a dictionary).
        NrP   )	r   Zget_log_metricr   r   r@   r   rG   r   rL   )r   r0   rQ   r$   r1   rR   rE   r   r   r   
metric_getS  s    z_MetricsAPI.metric_getc           	   
   C   sz   d}d||f }t |||d}z| j||| W n> typ } z&t|jtjkrZt| W Y d}~n
d}~0 0 t	|S )al  API call:  update a metric resource.

        :type project: string
        :param project: ID of the project containing the metric.

        :type metric_name: string
        :param metric_name: the name of the metric

        :type filter_: string
        :param filter_: the advanced logs filter expression defining the
                        entries exported by the metric.

        :type description: string
        :param description: description of the metric.

        :rtype: dict
        :returns: The metric object returned from the API (converted from a
                  protobuf to a dictionary).
        NrP   rN   )
r   r   Zupdate_log_metricr   r   r@   r   rG   r   rL   )	r   r0   rQ   r"   rO   r$   r1   rR   rE   r   r   r   metric_updatej  s    z_MetricsAPI.metric_updatec              
   C   sf   d}d||f }z| j || W n> ty` } z&t|jtjkrJt| W Y d}~n
d}~0 0 dS )zAPI call:  delete a metric resource.

        :type project: string
        :param project: ID of the project containing the metric.

        :type metric_name: string
        :param metric_name: the name of the metric
        NrP   )r   Zdelete_log_metricr   r   r@   r   rG   r   )r   r0   rQ   r$   r1   rE   r   r   r   metric_delete  s    	z_MetricsAPI.metric_delete)r   N)
r3   r4   r5   r6   r   rM   rS   rT   rU   rV   r   r   r   r   rK     s   
 rK   c                 C   s"   | du rt } d| i}tf i |S )z6Helper for :meth:'_PublisherAPI.list_topics' et aliae.Nr!   )r   r   )r!   r$   r   r   r   r     s    r   c                 C   s   d| j i}| jr| j|d< |S )*Helper for  :func:_log_entry_pb_to_mappingtyper.   )rX   r.   )Zresource_pbmappingr   r   r   _mon_resource_pb_to_mapping  s
    
rZ   c                 C   s   t | }t|S )rW   )r   r   )Ztimestamp_pb	timestampr   r   r   _pb_timestamp_to_rfc3339  s    r\   c                 C   s   |  d}|du rd}nn|dkr(| j}n^|dkr8| j}nN|dkrH| j}n>|dkrddd | jjD }n"|d	krxt| j}ntd
|f |S ),Helper for :func:`_log_entry_pb_to_mapping`.kindNstring_value
bool_valuenumber_value
list_valuec                 S   s   g | ]}t |qS r   )_value_pb_to_value)r   elementr   r   r   r     s   z&_value_pb_to_value.<locals>.<listcomp>struct_valuez#Value protobuf had unknown kind: %s)	Z
WhichOneofr_   r`   ra   rb   values_struct_pb_to_mappingre   
ValueError)Zvalue_pbr^   resultr   r   r   rc     s"    
rc   c                    s   t  fdd jD S )r]   c                    s   g | ]}|t  j| fqS r   )rc   fields)r   keyZ	struct_pbr   r   r     s   z)_struct_pb_to_mapping.<locals>.<listcomp>)dictrj   rl   r   rl   r   rg     s    rg   c              
   C   s   | j t| jt| j| jt| j| j	d}| 
dr@| j|d< | 
drXt| j|d< | 
drl| j|d< | jr| j}|j|j|j|j|j|j|j|j|jd	|d	< | jr| j}|j|j|j|jd
|d< |S )zHelper for :meth:`list_entries`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    )logNamer-   severityinsertIdr[   r.   text_payloadtextPayloadjson_payloadjsonPayloadproto_payloadprotoPayload	ZrequestMethodZ
requestUrlstatusrefererZ	userAgentZcacheHitZrequestSizeZresponseSizeZremoteIphttpRequestproduceridfirstlast	operation)log_namerZ   r-   r   Namero   	insert_idr\   r[   r.   ZHasFieldrq   rg   rs   ru   http_requestrequest_methodrequest_urlrx   ry   
user_agent	cache_hitrequest_sizeresponse_size	remote_ipr   r|   r}   r~   r   )r   rY   requestr   r   r   r   r     sB    







r   c              
   C   sF   ddddddddd	d
	}|  D ] \}}|| v r t||| |  q dS )#Helper for _log_entry_mapping_to_pbr   r   rx   ry   r   r   r   r   r   rw   N)itemssetattr)infor   Zoptional_request_keysrk   pb_namer   r   r   _http_request_mapping_to_pb
  s    r   c                 C   s<   | d |_ | d |_d| v r&| d |_d| v r8| d |_dS )r   r|   r}   r~   r   Nr{   )r   r   r   r   r   _log_operation_mapping_to_pb  s    


r   c                 C   sF  t  }dddd}| D ] \}}|| v rt||| |  qd| v rT| d d |j_d| v r~| d }t|trxt|}||_	d| v rt
| d }|j| d	| v r| d	  D ]\}}||j|< qd
| v r| d
  D ]\}}||j|< qd| v rtt| d |j d| v r(t| d |j d| v rBt| d |j |S )zHelper for :meth:`write_entries`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    r   r   rq   )rn   rp   rr   r-   rX   ro   r[   r.   rt   rv   rz   r   )r	   r   r   r-   rX   
isinstancestrr   Valuero   r   r[   ZCopyFromr.   rs   r
   jsondumpsru   r   r   r   r   )rY   r   Zoptional_scalar_keysrk   r   ro   r[   valuer   r   r   r)   (  sF    






r)   c                 C   s   | j | j| jdS )zHelper for :meth:`list_sinks`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    r<   r>   r=   r   )rD   r   r   r   r9   `  s    r9   c                 C   s   | j | j| jdS )zHelper for :meth:`list_metrics`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    r<   rO   r=   r   )rR   r   r   r   rL   n  s    rL   )N)+r6   r   Z
google.gaxr   r   Zgoogle.gax.errorsr   Zgoogle.gax.grpcr   Z$google.logging.type.log_severity_pb2r   Z$google.logging.v2.logging_config_pb2r   Z%google.logging.v2.logging_metrics_pb2r   Zgoogle.logging.v2.log_entry_pb2r	   Zgoogle.protobuf.json_formatr
   Zgrpc.beta.interfacesr   Zgcloud.exceptionsr   r   Zgcloud._helpersr   r   r   objectr   r7   rK   r   rZ   r\   rc   rg   r   r   r   r)   r9   rL   r   r   r   r   <module>   s@   W  

28