
    %                     V   d 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 e	j                   e	j                  e	j                   j"                  e	j                   j$                  e	j                   j&                         G d
 de	j(                                      Zy)z'logging sinks create' command.    )absolute_import)division)unicode_literals)util)arg_parsers)base)log)
console_ioc                   8    e Zd ZdZed        Zd Zd Zd Zd Z	y)Createa  Create a log sink.

  Create a log sink used to route log entries to a destination. The sink routes
  all log entries that match its *--log-filter* flag.

  An empty filter matches all logs.

  Detailed information about filters can be found at:
  [](https://cloud.google.com/logging/docs/view/logging-query-language)

  The sink's destination can be a Cloud Logging log bucket, a Cloud Storage
  bucket, a BigQuery dataset, a Cloud Pub/Sub topic, or a Google Cloud project.

  The destination must already exist.

  If creating a log sink to route logs to a destination outside of Cloud Logging
  or to a Cloud Logging log bucket in another project, the log sink's service
  account must be granted permission to write to the destination.

  For more information about destination permissions, see:
  https://cloud.google.com/logging/docs/export/configure_export_v2#dest-auth

  Matching log entries are routed to the destination after the sink is created.

  ## EXAMPLES

  To route all Google Compute Engine logs to BigQuery, run:

    $ {command} my-bq-sink
    bigquery.googleapis.com/projects/my-project/datasets/my_dataset --log-filter='resource.type="gce_instance"'

  To route "syslog" from App Engine Flexible to a Cloud Storage bucket, run:

    $ {command} my-gcs-sink storage.googleapis.com/my-bucket --log-filter='logName="projects/my-project/appengine.googleapis.com%2Fsyslog"'

  To route Google App Engine logs with ERROR severity, run:

    $ {command} my-error-logs
    bigquery.googleapis.com/projects/my-project/datasets/my_dataset --log-filter='resource.type="gae_app" AND severity=ERROR'

  To route all logs to a log bucket in a different project, run:

    $ {command} my-sink
    logging.googleapis.com/projects/my-central-project/locations/global/buckets/my-central-bucket

  To route all logs to another project, run:

    $ {command} my-sink
    logging.googleapis.com/projects/my-destination-project
  c                    | j                  dd       | j                  dt        j                  dd             | j                  dd	d
       | j                  dd	dd       | j                  dd	dd       | j                  ddd       | j                  d      }|j                  dd	dd       | j                  ddt        j                  t
        t
        t
        t        dddg      d        | j                  d!d"       | j                  d#dd$%       t        j                  | d&       | j                  j                  d'       y')(z Register flags for this command.	sink_namezThe name for the sink.)helpdestinationzThe destination for the sink.z*Some destination types are not supported
.)defaultuniverse_helpz--log-filterFz_A filter expression for the sink. If present, the filter specifies which log entries to export.)requiredr   z--include-children
store_truezpWhether to export logs from all child projects and folders. Only applies to sinks for organizations and folders.)r   actionr   z--intercept-childrenzsWhether to intercept logs from all child projects and folders. Only applies to sinks for organizations and folders.z--custom-writer-identitySERVICE_ACCOUNT_EMAILzWriter identity for the sink. This flag can only be used if the destination is a log bucket in a different project. The writer identity is automatically generated when it is not provided for a sink.)metavarr   z-Settings for sink exporting data to BigQuery.z--use-partitioned-tablesa(  If specified, use BigQuery's partitioned tables. By default, Logging creates dated tables based on the log entries' timestamps, e.g. 'syslog_20170523'. Partitioned tables remove the suffix and special query syntax (https://cloud.google.com/bigquery/docs/querying-partitioned-tables) must be used.z--exclusionappend)namedescriptionfilterdisabledr   r   )specrequired_keysaD  Specify an exclusion filter for a log entry that is not to be exported. This flag can be repeated.

The ``name'' and ``filter'' attributes are required. The following keys are accepted:

*name*::: An identifier, such as ``load-balancer-exclusion''. Identifiers are limited to 100 characters and can include only letters, digits, underscores, hyphens, and periods.

*description*::: A description of this exclusion.

*filter*::: An advanced log filter that matches the log entries to be excluded.

*disabled*::: If this exclusion should be disabled and not exclude the log entries.)r   typer   z--descriptionzDescription of the sink.z
--disabledz9Sink will be disabled. Disabled sinks do not export logs.)r   r   zsink to createN)add_argumentr   UniverseHelpTextadd_argument_groupArgDictstrboolr   AddParentArgsdisplay_infoAddCacheUpdater)parserbigquery_groups     #lib/surface/logging/sinks/create.pyArgszCreate.ArgsV   s    *BC
))3G
   7  9
 E	  G C   "'	  	 ..< / >N";	   	=   " 	 "8,.)  +0 .HI
I  L
 	v/0
''-    c           
          t        j                         }t        j                         j                  j	                  |j                  | |j                  di |d|            S )z-Creates a v2 sink specified by the arguments.T)parentlogSinkuniqueWriterIdentitycustomWriterIdentity )r   GetMessages	GetClientprojects_sinksr   !LoggingProjectsSinksCreateRequestLogSink)selfr/   	sink_datacustom_writer_identitymessagess        r+   
CreateSinkzCreate.CreateSink   s_    !H>>**1122$H$$1y1!%!7	 	3 	9: :r-   c                 8   |j                   st        j                  dd       |j                  sN|j                  sB|j
                  rt        j                  d       |j                  rt        j                  d       t        j                  |j                  |      }|j                  |j                  d}|j                  d      r|j
                  |d<   |j                  d	      r|j                  |d
<   |j                  d      r|j                   |d<   |j                  d      ri }|j                  |d<   ||d<   |j                  d      r|j                   |d<   |j                  d      r|j"                  |d<   |j                  d      r|j$                  |d<   d }|j                  d      r|j&                  }| j)                  t        j*                  |      ||      }t        j,                  |       |j                  | _        |j0                  | _        |S )Nz+Sink with empty filter matches all entries.T)cancel_on_nozQinclude-children only has an effect for sinks at the folder or organization levelzSintercept-children only has an effect for sinks at the folder or organization level)r   r   include_childrenincludeChildrenintercept_childreninterceptChildren
log_filterr   use_partitioned_tablesusePartitionedTablesbigqueryOptions	exclusion
exclusionsr   r   r;   )rD   r
   PromptContinueorganizationfolderr@   r	   warningrB   r   GetSinkReferencer   sinksIdr   IsSpecifiedrE   rH   r   r   r;   r=   GetParentFromArgsCreatedResource_epilog_result_destinationwriterIdentity_epilog_writer_identity)r9   argssink_refr:   bigquery_optionsr;   results          r+   _RunzCreate._Run   s   ??
7dL 			$	
 
	 	 $	

 $$T^^T:H   ''I
 *+%)%:%:i!",-'+'>'>i#$% OOi01151L1L-.%5i!"$ $i&!%!1!1i
#"mmi
!01#::__t$i1GIF !&,&8&8D##)#8#8D Mr-   c                 $    | j                  |      S )zThis is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The created sink with its destination.
    )rZ   )r9   rV   s     r+   Runz
Create.Run   s     99T?r-   c                 X    t        j                  | j                  | j                         y )N)r   PrintPermissionInstructionsrS   rU   )r9   unused_resources_were_displayeds     r+   EpilogzCreate.Epilog   s"    $$T%D%D%)%A%ACr-   N)
__name__
__module____qualname____doc__staticmethodr,   r=   rZ   r\   r`   r3   r-   r+   r   r      s5    1h T. T.l:9v
Cr-   r   N)rd   
__future__r   r   r   googlecloudsdk.api_lib.loggingr   googlecloudsdk.callioper   r   googlecloudsdk.corer	   googlecloudsdk.core.consoler
   UniverseCompatibleReleaseTracksReleaseTrackGABETAALPHACreateCommandr   r3   r-   r+   <module>rr      s    & &  ' / / ( # 2 $++00$2C2C2I2I`CT `C `Cr-   