
                             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 dd
lmZ ddlmZ ddej                   fdZd Zd Zd Zd Zd Zy)z1Common utility functions for sql export commands.    )absolute_import)division)unicode_literals)export_util)
operations)validate)base)flags)log)
propertiesTFc                     t         j                  j                  |        t        j                  |        d}|r|dz   }t        j
                  | |       t        j                  | ||       y)a^  Adds the base export flags to the parser.

  Args:
    parser: The current argparse parser to add these flags to.
    gz_supported: Boolean, specifies whether gz compression is supported.
    database_required: Boolean, specifies whether the database flag is required.
    database_help_text: String, specifies the help text for the database flag.
  zThe path to the file in Google Cloud Storage where the export will be stored. The URI is in the form gs://bucketName/fileName. If the file already exists, the operation fails.z< If the filename ends with .gz, the contents are compressed.N)r	   
ASYNC_FLAGAddToParserr
   AddInstanceArgumentAddUriArgumentAddDatabaseList)parsergz_supporteddatabase_requireddatabase_help_texturi_help_texts        1lib/googlecloudsdk/command_lib/sql/export_util.pyAddBaseExportFlagsr      se     //f%F#&- ! &@ AMv}- 24EF    c                    |j                   }|j                  }t        j                  | j                         |j
                  j                  | j                  dt        j                  j                  j                  j                  id      }|j                  |j                  |j                  |j                  |            }|j                  j                  |      }|j
                  j!                  d|j"                  |j                        }| j$                  r@|j&                  j)                  |j+                  |j                  |j,                              S |j.                  |j0                  j2                  j4                  k(  }	|	r| j6                  n| j8                  }
t&        j:                  j=                  ||d	       t>        j@                  jC                  d
jE                  ||
             y)a|  Exports data from a Cloud SQL instance.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.
    export_context: ExportContext; format-specific export metadata.

  Returns:
    A dict representing the export operation resource, if '--async' is used,
    or else None.

  Raises:
    HttpException: An HTTP error response was received while executing API
        request.
    ToolException: An error other than HTTP error occurred while executing the
        command.
  projectzsql.instances)params
collection)exportContext)instancer   instancesExportRequestzsql.operations)	operationr   )r   r"   zExporting Cloud SQL instancez%Exported [{instance}] to [{bucket}].
)r    bucketN)#
sql_clientsql_messagesr   ValidateInstanceNamer    resource_parserParser   VALUEScorer   	GetOrFailSqlInstancesExportRequestInstancesExportRequest	instancesExportCreatenameasync_r   GetSqlOperationsGetRequestr"   fileTypeExportContextFileTypeValueValuesEnumTDE	cert_pathuriOperationsV1Beta4WaitForOperationr   statuswriteformat)argsclientexport_contextr$   r%   instance_refexport_requestresult_operationoperation_refis_tde_exportexport_dests              r   RunExportCommandrI   9   s   &   *$$,
.''--
mm**//77AAB  . ",
  99$$"")@@& A ( : ).  ))00@((// %%"" 0 $-
 
[[  $$,,!))]5L5L 	- 	NO O
 		#	#	;	;	?	?@ 
 #0TXX+//
M0NP **;BBK C 1 2 
r   c                 b   t        j                  |j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  	      }| j                  rt        j                  j                  d       t        | ||      S )a  Exports data from a Cloud SQL instance to a MySQL dump file.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  )offloadparallelthreadsclean	if_existspServerless exports cost extra. See the pricing page for more information: https://cloud.google.com/sql/pricing.
)r   SqlExportContextr%   r:   databasetablerK   rL   rM   rN   rO   r   r=   r>   rI   r@   rA   sql_export_contexts      r   RunSqlExportCommandrV   w   s     #33
hh
mm
jjll}}llJJ
 
\\JJ{ 
$(:	;;r   c                 b   t        j                  |j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  	      }| j                  rt        j                  j                  d       t        | ||      S )a  Exports data from a Cloud SQL instance to a CSV file.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  )rK   quoteescapefields_terminated_bylines_terminated_byrP   )r   CsvExportContextr%   r:   rR   queryrK   rX   rY   rZ   r[   r   r=   r>   rI   )r@   rA   csv_export_contexts      r   RunCsvExportCommandr_      s     #33
hh
mm
jjllJJ[[4422	4 
\\JJ{ 
$(:	;;r   c                 
   t        j                  |j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  	      }t        | ||      S )a  Export data from a Cloud SQL instance to a SQL Server BAK file.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  )r   BakExportContextr%   r:   rR   stripe_countstripedbak_typedifferential_baseexport_log_start_timeexport_log_end_timerI   rT   s      r   RunBakExportCommandrh      sp     #33
hh
mm

ll
mm

  

 
$(:	;;r   c                     t        j                  |j                  | j                  | j                  | j
                  | j                        }t        | ||      S )a  Export TDE certificate from a Cloud SQL instance.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  )r   TdeExportContextr%   certificater9   pvk_pathpvk_passwordrI   )r@   rA   tde_export_contexts      r   RunTdeExportCommandro      sP     #33

nn
mm
 
$(:	;;r   N)__doc__
__future__r   r   r   googlecloudsdk.api_lib.sqlr   r   r   googlecloudsdk.callioper	   googlecloudsdk.command_lib.sqlr
   googlecloudsdk.corer   r   &DEFAULT_DATABASE_LIST_EXPORT_HELP_TEXTr   rI   rV   r_   rh   ro    r   r   <module>rx      sY    8 &  ' 2 1 / ( 0 # *
 CC	G6;|<<<:<4<r   