
    '                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SS	K
Jr  SS
KJr  SSKJr  SSKJr  SSKJ	r  SSKJr  SSKJr  SSKJr  S rS rS r\R4                  \R6                  " \R8                  R:                  \R8                  R<                  \R8                  R>                  5       " S S\R@                  5      5       5       r!g)z*Restores a backup of a Cloud SQL instance.    )absolute_import)division)unicode_literals)api_util)
exceptions)
operations)validate)base)	constants)flags)	instances)log)
properties)
console_ioc                 z   [         R                  " UR                  5        [        R                  " UR
                  5      (       d  U (       aO  UR                  (       a  [        R                  " S5      eUR                  (       a  [        R                  " S5      egU(       a  [        R                  " SU 35      eg)a  Validates the backup request.

Args:
  is_project_backup: bool, Whether the backup request is for a project level
    backup.
  args: argparse.Namespace, The arguments that this command was invoked with.
  overrides: list[str], The list of flags that were overridden.
zP --backup-project is not supported when using backup name based restore command.zQ --backup-instance is not supported when using backup name based restore command.z_Overrides are only supported for backup name based restore to new instance. Unsupported flags: N)
r	   ValidateInstanceNamerestore_instancecommand_validateIsBackupDrBackupRequestidbackup_projectr   ArgumentErrorbackup_instance)is_project_backupargs	overridess      "lib/surface/sql/backups/restore.py_ValidateBackupRequestr   #   s      5 56--dgg66:K$$ 
 $$   $$++4+7      c                 <   [         R                  " U R                  5      (       a<  UR                  UR                  UR
                  UR                  U R                  S9S9$ UR                  UR                  UR
                  UR                  U R                  S9S9$ )a7  Get the restore backup request.

Args:
  args: argparse.Namespace, The arguments that this command was invoked with.
  sql_messages: sql_v1beta4_messages.SqlMessagesV1Beta4, The SQL API messages.
  instance_ref: base.ResourceParse, The parsed instance reference.

Returns:
  A SqlInstancesRestoreBackupRequest.
)backupdrBackupprojectinstanceinstancesRestoreBackupRequest)backup)r   r   r    SqlInstancesRestoreBackupRequestr#   r$   InstancesRestoreBackupRequest)r   sql_messagesinstance_refs      r   _GetRestoreBackupRequestr+   A   s     --dgg6688$$&&&2&P&P77 'Q '
 9   88$$&&&2&P&P77 'Q '
 9  r   c                 .   / nU R                  S5      (       a  UR                  S5        U R                  (       a  UR                  S5        U R                  S5      (       a  UR                  S5        U R                  (       a  UR                  S5        U$ )zGet the database instance clear overrides.

Args:
  args: argparse.Namespace, The arguments that this command was invoked with.

Returns:
  An array with the database instance fields that should be cleared.
clear_networkz)settings.ip_configuration.private_networkz,settings.active_directory_config.dns_serversclear_disk_encryptiondisk_encryption_configz settings.active_directory_config)IsKnownAndSpecifiedappend"clear_active_directory_dns_serversclear_active_directory)r   cleared_fieldss     r   !_GetRestoreInstanceClearOverridesr5   ^   s     .	o..EF	,,HI	56623	  <=	r   c                   .    \ rS rSrSr\S 5       rS rSrg)RestoreBackupx   aa  Restores a backup of a Cloud SQL instance.

The command lets you restore to an existing instance using ID. To restore
using a backupDR backup, use the backupDR ID. When backup Name is used to
restore it lets you restore to an existing instance or a new instance. When
restoring to new instance, optional flags can be used to customize the new
instance.
c                 6   [         R                  " U SS9  U R                  SS[         R                  SS9  U R                  S[         R                  SS	9  U R                  S
SS9  [        R
                  R                  U 5        [         R                  " U 5        g)zArgs is called by calliope to gather arguments for this command.

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.
zThe ID of the backup run to restore from or the backup NAME for restore to existing/new instance. To find the NAME, run the following command: $ gcloud sql backups list --filter=instance:{instance})	help_textz--restore-instanceTzGThe ID of the target Cloud SQL instance that the backup is restored to.)required	completerhelpz--backup-instancea}  The ID of the instance that the backup was taken from. This argument must be specified when the backup instance is different from the restore instance. If it is not specified, the backup instance is considered the same as the restore instance. This flag is not supported when restore happens from backup name, only supported when restore happens from backup ID in timestamp format.)r<   r=   z--backup-projecta  The project of the instance to which the backup belongs. If it isn't specified, backup and restore instances are in the same project. This flag is not supported when restore happens from backup name, only supported when restore happens from backup ID in timestamp format.)r=   N)r   AddBackupIdadd_argumentInstanceCompleterr
   
ASYNC_FLAGAddToParserAddSourceInstanceOverrideArgs)parsers    r   ArgsRestoreBackup.Args   s     
F ))   ))	   $  	 	OO'	''/r   c                     [         R                  " [         R                  5      nUR                  nUR                  n[        USS5      nU Vs/ s H  nU[        R                  ;   d  M  UPM     nn[        R                  " UR                  5      n[        XU5        UR                  R                  UR                  S[        R                   R"                  R$                  R&                  0SS9n	[(        R*                  " S5      (       d  gU(       ax  [-        XU	5      n
U(       aI  [.        R0                  R3                  XAU	S9nUU
R4                  l        [9        U5      U
R4                  l        UR<                  R?                  U
5      nOUR@                  (       d  UR                  Ul         [C        UR                  5      nUR<                  R?                  URE                  U	R$                  U	RF                  URI                  URK                  UUR@                  URL                  S9S	9S
95      nUR                  RO                  SURP                  U	R$                  S9nURR                  (       a=  URT                  RW                  URY                  UR$                  URZ                  S95      $ [T        R\                  R_                  X>S5        [`        Rb                  Re                  SRg                  U	S95        gs  snf )a  Restores a backup of a Cloud SQL instance.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
      with.

Returns:
  A dict object representing the operations resource describing the
  restoreBackup operation if the restoreBackup was successful.
_specified_argsNr#   zsql.instances)params
collectionz1. All current data on the instance will be lost when the backup is restored to an existing instance.
2. If restoring to a new instance, settings will be applied from the backups unless they are overriden.)r*   )backupRunId
instanceIdr#   )restoreBackupContextr"   zsql.operations)	operationr#   )r#   rN   zRestoring Cloud SQL instancezRestored [{instance}].
)r$   )4r   	SqlClientAPI_VERSION_DEFAULT
sql_clientr)   getattrr   TARGET_INSTANCE_OVERRIDE_FLAGSr   IsProjectLevelBackupRequestr   r   resource_parserParser   r   VALUEScorer#   	GetOrFailr   PromptContinuer+   command_utilInstancesV1Beta4ConstructCreateInstanceFromArgsr%   restoreInstanceSettingsr5   'restoreInstanceClearOverridesFieldNamesr   r7   r   intr'   r$   r(   RestoreBackupContextr   Createnameasync_r   GetSqlOperationsGetRequestrN   OperationsV1Beta4WaitForOperationr   statuswriteformat)selfr   clientrQ   r)   specified_args_dictkeyr   r   r*   restore_backup_requestinstance_resourceresult_operationbackup_run_idoperation_refs                  r   RunRestoreBackup.Run   s     < <=F""J&&L!$(94@ '&C)::: 	&   )DDTWWM,I>))//:,,1199CCD" 0 L
 $$	P 
 7
l  
))II J  	  	<<T .d3 	<<d $--;;
  !!#44$''lm#--;;

7
7"**#,,<<+7+L+L&3%)%9%9"&"5"5 ,M , = 	 8   **11"''$$ 2 &M
 {{""&&

.
.#++%// / 12 2
   11#AC JJ/666MNYs   K;*K; N)	__name__
__module____qualname____firstlineno____doc__staticmethodrE   ru   __static_attributes__rw   r   r   r7   r7   x   s"    
 10 10f]r   r7   N)"r|   
__future__r   r   r   googlecloudsdk.api_lib.sqlr   r   r   r	   googlecloudsdk.callioper
   googlecloudsdk.command_lib.sqlr   r   r   r[   r   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   r   r+   r5   DefaultUniverseOnlyReleaseTracksReleaseTrackGABETAALPHARestoreCommandr7   rw   r   r   <module>r      s    1 &  ' / 1 1 / ( 4 0 D G # * 2<:4 $++00$2C2C2I2I[D'' [ [r   