
    $                         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
ZddZd Zd Zd Zd Zd Zy)z1Common utility functions for sql import commands.    )import_util)
operations)validate)base)flags)log)
properties)
console_ioc                     t         j                  j                  |        t        j                  |        d}|r|dz   }t        j
                  | |j                  |             |rt        j                  | d       yy)a  Adds the base flags for importing data.

  Args:
    parser: An argparse parser that you can use to add arguments that go on the
      command line after this command. Positional arguments are allowed.
    filetype: String, description of the file type being imported.
    gz_supported: Boolean, if True then .gz compressed files are supported.
    user_supported: Boolean, if True then a Postgres user can be specified.
  Path to the {filetype} file in Google Cloud Storage from which the import is made. The URI is in the form `gs://bucketName/fileName`.0 Compressed gzip files (.gz) are also supported.filetype*PostgreSQL user for this import operation.N)r   
ASYNC_FLAGAddToParserr   AddInstanceArgumentAddUriArgumentformatAddUserparserr   gz_supporteduser_supporteduri_help_texts        1lib/googlecloudsdk/command_lib/sql/import_util.pyAddBaseImportFlagsr      sw     //f%F#$ 
 !:M v}33X3FG	MM&FG     c                     t         j                  j                  |        t        j                  |        d}|r|dz   }t        j
                  | |j                  |             |rt        j                  | d       yy)a  Adds the base flags for importing data for bak import.

  Args:
    parser: An argparse parser that you can use to add arguments that go on the
      command line after this command. Positional arguments are allowed.
    filetype: String, description of the file type being imported.
    gz_supported: Boolean, if True then .gz compressed files are supported.
    user_supported: Boolean, if True then a Postgres user can be specified.
  r   r   r   r   N)r   r   r   r   r   AddBakImportUriArgumentr   r   r   s        r   AddBakImportFlagsr!   9   sw     //f%F#$ 
 !:M (<(<h(<(OP	MM&FG r   c                 *   |j                   }|j                  }|j                  |j                  j                  j
                  k(  }|j                  |j                  j                  j                  k(  }|r| j                  n| j                  }t        j                  | j                         |r t        j                  || j                         |j                  j                  | j                  dt         j"                  j$                  j&                  j(                  id      }|r?| j                  r3t+        j,                  dj/                  | j0                        dd       n3t+        j,                  dj/                  || j                  	      dd       |j3                  |j                  |j&                  |j5                  |
            }	|j6                  j9                  |	      }
|j                  j;                  d|
j<                  |j&                        }| j>                  r@|j@                  jC                  |jE                  |j&                  |jF                              S d}|r| j                  rd}t@        jH                  jK                  |||       |rF| j                  r:tL        jN                  jQ                  dj/                  | j0                               ytL        jN                  jQ                  dj/                  ||             y)a|  Imports data into 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.
    import_context: ImportContext; format-specific import metadata.

  Returns:
    A dict representing the import 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
collectionz6Bring database [{database}] online with recovery-only.)databaseT)messagedefaultcancel_on_noz3Data from [{uri}] will be imported to [{instance}].)uriinstance)importContext)r+   r#   instancesImportRequestzsql.operations)	operationr#   )r#   r.   z&Importing data into Cloud SQL instancezBring database onlinez7Bring database [{database}] online with recovery-only.
z1Imported data from [{bucket}] into [{instance}].
)r+   bucketN))
sql_clientsql_messagesfileTypeImportContextFileTypeValueValuesEnumBAKTDE	cert_pathr*   r   ValidateInstanceNamer+   ValidateURIrecovery_onlyresource_parserParser	   VALUEScorer#   	GetOrFailr
   PromptContinuer   r&   SqlInstancesImportRequestInstancesImportRequest	instancesImportCreatenameasync_r   GetSqlOperationsGetRequestr.   OperationsV1Beta4WaitForOperationr   statuswrite)argsclientimport_contextr0   r1   is_bak_importis_tde_importimport_sourceinstance_refimport_requestresult_operationoperation_refr'   s                r   RunImportCommandrX   T   s   &   *$$,		#	#	;	;	?	?@ 
 		#	#	;	;	?	?@ 
 %2$..txx-
.(:(:;''--
mm**//77AAB  . , t))DKK L   ELL M 
   99$$"")@@& A  : .  ))00@((// %%"" 0 - 
[[  $$,,!))]5L5L 	- 	
  5't))%G//- t))JJBII]] 	J 	
 
 JJ<CC!- 	D 	
 
r   c           
          t        j                  |j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                        }t        | ||      S )a  Imports data from a SQL dump file into 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 representing the import operation resource, if '--async' is used,
    or else None.
  )parallelthreadsclean	if_exists)r   SqlImportContextr1   r*   r&   userrZ   r[   r\   r]   rX   rN   rO   sql_import_contexts      r   RunSqlImportCommandrb      sa     #33
hh
mm
ii}}llJJ	 
$(:	;;r   c                     t        j                  |j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  
      }t        | ||      S )a  Imports data from a CSV file into 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 representing the import operation resource, if '--async' is used,
    or else None.
  )r   CsvImportContextr1   r*   r&   tablecolumnsr_   quoteescapefields_terminated_bylines_terminated_byrX   )rN   rO   csv_import_contexts      r   RunCsvImportCommandrl      ss     #33
hh
mm
jj
ll
ii
jj
kk

 
$(:	;;r   c                 b   t        j                  |j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        }t        | ||      S )a  Imports data from a BAK file into 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 representing the import operation resource, if '--async' is used,
    or else None.
  )r   BakImportContextr1   r*   r&   r7   pvk_pathpvk_passwordkeep_encryptedstripedno_recoveryr:   bak_typestop_atstop_at_markrX   r`   s      r   RunBakImportCommandrw      s     #33
hh
mm
nn
mm


ll


mm
ll
 
$(:	;;r   c                     t        j                  |j                  | j                  | j                  | j
                  | j                        }t        | ||      S )a  Imports data from a TDE certificate file into 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 representing the import operation resource, if '--async' is used,
    or else None.
  )r   TdeImportContextr1   certificater7   ro   rp   rX   )rN   rO   tde_import_contexts      r   RunTdeImportCommandr|     sP     #33

nn
mm
 
$(:	;;r   N)TT)__doc__googlecloudsdk.api_lib.sqlr   r   r   googlecloudsdk.callioper   googlecloudsdk.command_lib.sqlr   googlecloudsdk.corer   r	   googlecloudsdk.core.consoler
   r   r!   rX   rb   rl   rw   r|    r   r   <module>r      sP    8 3 1 / ( 0 # * 2 9=H:H6hV<2<6<<<r   