
    A                     4   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rSrSrSrS rS rS rS rS rS rS rS rS r S r!S r"S r#S r$S r%S r&S r'S r(S  r)S!\\*   S"\\RV                     4S# jr,S$ r-g%)&z0Hooks for Backup for GKE command line arguments.    )absolute_import)division)unicode_literals)Optional)messages)util)
exceptions)log)
console_io%cluster_resource_selected_group_kinds%cluster_resource_excluded_group_kinds cluster_resource_all_group_kindscluster_resource_no_group_kindsc                     A ASUl         U$ )NT)force)refargsrequests      @lib/googlecloudsdk/command_lib/container/backup_restore/hooks.pyAddForceToDeleteRequestr   $   s    	
 '-	.    c                    U (       d  g[         R                  " 5       n/ n U  H  nUR                  S5      n[        U5      S:X  a  SnUS   nO+[        U5      S:X  a  Uu  pgO[        R
                  " US5      eU(       d  [        R
                  " US5      eUR                  5       nXhl        Xxl        UR                  U5        M     U$ ! [         a    [        R
                  " US5      ef = f)	zProcess list of group kinds.N/    r      z7Cluster resource scope selected group kinds is invalid.z5Cluster resource scope selected group kinds is empty.)api_utilGetMessagesModulesplitlenr	   InvalidArgumentException	GroupKindresourceGroupresourceKindappend
ValueError)	group_kindsflagmessagegksresource
group_kindgroupkindgks	            r   ParseGroupKindsr0   .   s    	&&('
#C>>#&j	ZA	!}z?a t11E
 	
 11CE 	E bo	jjn'  ( J	 C

-
-AC CCs   B(C
 
"C,c                     [         R                  " 5       nUR                  5       nUR                  R	                  [        U S5      5        U$ )Nz---cluster-resource-scope-selected-group-kinds)r   r   ClusterResourceRestoreScopeselectedGroupKindsextendr0   r'   r)   crrss      r   ProcessSelectedGroupKindsr7   P   G    &&('		,	,	.$  
F
 
+r   c                     [         R                  " 5       nUR                  5       nUR                  R	                  [        U S5      5        U$ )Nz---cluster-resource-scope-excluded-group-kinds)r   r   r2   excludedGroupKindsr4   r0   r5   s      r   ProcessExcludedGroupKindsr;   [   r8   r   c                 \    [         R                  " 5       nUR                  5       nXl        U$ N)r   r   r2   allGroupKinds)all_group_kindsr)   r6   s      r   ProcessAllGroupKindsr@   f   s)    &&('		,	,	.$&	+r   c                 \    [         R                  " 5       nUR                  5       nXl        U$ r=   )r   r   r2   noGroupKinds)no_group_kindsr)   r6   s      r   ProcessNoGroupKindsrD   m   s)    &&('		,	,	.$$	+r   c                 B    U (       d  [         R                  " SS5      eU $ )Nz--all-namespacesz All namespaces can only be true.r	   r!   )all_namespacess    r   ProcessAllNamespacesrH   t   s&    	

-
-*, , 
r   c                 B    U (       d  [         R                  " SS5      eU $ )Nz--no-namespaceszNo namespaces can only be true.rF   )no_namespacess    r   ProcessNoNamespacesrK   |   s&    	

-
-)+ + 
r   c                 B    U (       d  [         R                  " SS5      eU $ )Nz--selected-namespacesz&Selected namespaces must not be empty.rF   )selected_namespacess    r   ProcessSelectedNamespacesrN      &    	

-
-02 2 
r   c                 B    U (       d  [         R                  " SS5      eU $ )Nz--excluded-namespacesz&Excluded namespaces must not be empty.rF   )excluded_namespacess    r   ProcessExcludedNamespacesrR      rO   r   c                 n   U (       d  [         R                  " SS5      e[        R                  " 5       nUR	                  5       n U R                  S5       H  nUR                  S5      u  pEU(       d&  [         R                  " SSR                  U5      5      eU(       d&  [         R                  " SSR                  U5      5      eUR                  5       nXVl        XFl	        UR                  R                  U5        M     U$ ! [         a'    [         R                  " SSR                  U 5      5      ef = f)z%Processes selected-applications flag.z--selected-applicationsz(Selected applications must not be empty.,r   z/Namespace of selected application {0} is empty.z*Name of selected application {0} is empty.z%Selected applications {0} is invalid.)r	   r!   r   r   NamespacedNamesr   formatNamespacedNamename	namespacenamespacedNamesr%   r&   )selected_applicationsr)   sanamespaced_namerY   rX   nns          r   ProcessSelectedApplicationsr_      s'   	

-
-!24 4 &&(' "066s;'--c2oi11%=DD!" 	" 11%8??!" 	" !!#bgl# <  I	 

-
-!/667LM s   B=D 1D4c                    U (       d  [         R                  " SS5      e[        R                  " 5       nUR	                  5       nU R                  S5       H  nUR                  S5      nUS   (       d  [         R                  " SS5      eUR                  5       nUS   Ul        [        U5      S:X  a  SOUS   Ul	        UR                  R                  U5        M     U$ )	z)Processes selected-namespace-labels flag.z--selected-namespace-labelsz6Input for selected-namespace-labels must not be empty.rT   =r   z'Key of namespace label cannot be empty.r   r   )r	   r!   r   r   ResourceLabelsr   Labelkeyr    valueresourceLabelsr%   )selected_namespace_labelsr)   rlskey_value_pairpartsrls         r   ProcessSelectedNamespaceLabelsrl      s    	"

-
-%@  &&(' #177<n  %E8//
'
3  
B1XBFZ1_r%(BHb! = 
*r   c                 ^   A UR                  S5      (       a6  SUR                  R                  l        SUR                  R                  l        UR                  S5      (       a6  SUR                  R                  l        SUR                  R                  l        UR                  S5      (       a6  SUR                  R                  l        SUR                  R                  l        UR                  S5      (       a-  UR                  S5      (       a  [        R                  " SS5      eUR                  S5      (       a  SUR                  R                  l	        UR                  S5      (       a  SUR                  R                  l
        [        5       nUR                  R                  S	5       H  nUR                  S
5      (       a  UR                  S
5        M,  UR                  S5      (       a  UR                  S5        MU  UR                  S5      (       a$  UR                  S5        UR                  S5        M  UR                  S5      (       a$  UR                  S5        UR                  S5        M  UR                  U5        M     S	R!                  [#        U5      5      Ul        U$ )z?Preprocesses request and update mask for backup update command.rM   Nr[   rG   target_rpo_minutescron_schedulez--cron-schedulez=Cannot specify both --target_rpo_minutes and --cron_schedule.rT   zbackupConfig.selectedNamespacesz!backupConfig.selectedApplicationszbackupSchedule.cronSchedulezbackupSchedule.rpoConfigz)backupSchedule.rpoConfig.targetRpoMinutes)IsSpecified
backupPlanbackupConfigselectedApplicationsallNamespacesselectedNamespacesr	   r!   backupSchedulecronSchedule	rpoConfigset
updateMaskr   
startswithaddjoinsortedr   r   r   	new_masksmasks        r   PreprocessUpdateBackupPlanr      s   	 
+,,;?G##848G##1	-..9=G##648G##1	&'';?G##89=G##6 +,,
''

-
-GI I 
*++59G%%2	o&&26G%%/ e)  &&s+d899mm56	<	=	=mm78	6	7	7mm12mm./	D	E	Emm?@mm12mmD , xxy 12'	.r   c                 F   A [        U[        5      (       aH  UR                  [        5      (       a.  [        UR                  5      UR
                  R                  l        [        U[        5      (       aH  UR                  [        5      (       a.  [        UR                  5      UR
                  R                  l        [        U[        5      (       aH  UR                  [        5      (       a.  [        UR                  5      UR
                  R                  l        [        U[        5      (       aH  UR                  [        5      (       a.  [        UR                   5      UR
                  R                  l        UR                  S5      (       al  SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        UR                  S5      (       al  SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        UR                  S5      (       al  SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        UR                  S5      (       al  SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        UR                  S5      (       al  SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        SUR
                  R                  l        / nUR                  S5      (       a  [-        UR
                  R                  R.                  5      (       ai  [0        R2                  " SS	S
9  [4        R6                  " [4        R8                  " SS	S9/ 5      UR
                  R                  l        UR;                  S5        UR                  S5      (       a  [-        UR
                  R                  R<                  5      (       ai  [0        R2                  " SS	S
9  [4        R6                  " [4        R8                  " SS	S9/ 5      UR
                  R                  l        UR;                  S5        UR>                  RA                  S5       H  nURC                  S5      (       a  SnOcURC                  S5      (       a  SnOJURC                  S5      (       a  SnO1URC                  S5      (       a  SnOURC                  S5      (       a  SnUR;                  U5        M     SRE                  U5      Ul        U$ )z-Preprocess request for updating restore plan.rG   NrJ   rM   rQ   r[   substitution_rules_filez
      The given restore plan already has the transformation rules. Updating the
      restore plan with new substitution rules will delete the existing
      transformation rules.
      T)cancel_on_nor   numberrepeatedz!restoreConfig.transformationRulestransformation_rules_filez
      The given restore plan already has the substitution rules. Updating the
      restore plan with new transformation rules will delete the existing
      substitution rules.
      zrestoreConfig.substitutionRulesrT   z restoreConfig.selectedNamespacesz restoreConfig.excludedNamespacesz"restoreConfig.selectedApplicationszrestoreConfig.noNamespaceszrestoreConfig.allNamespaces)#hasattr%CLUSTER_RESOURCE_SELECTED_GROUP_KINDSrp   r7   r   restorePlanrestoreConfigclusterResourceRestoreScope%CLUSTER_RESOURCE_EXCLUDED_GROUP_KINDSr;   r    CLUSTER_RESOURCE_ALL_GROUP_KINDSr@   r   CLUSTER_RESOURCE_NO_GROUP_KINDSrD   r   noNamespacesru   excludedNamespacesrs   rt   booltransformationRulesr   PromptContinuer   	FieldListStringFieldr%   substitutionRulesrz   r   r{   r}   r   s        r   PreprocessUpdateRestorePlanr      s   	 
1 >??!$"L"LM %%A 
1 >??!$"L"LM %%A T3449I9I&: : 	TBBC %%A T2338H8H%9 9 	D@@A %%A
 
&''59G%%2;?G%%8;?G%%8=AG%%:	o&&6:G%%3;?G%%8;?G%%8=AG%%:	+,,6:G%%359G%%2;?G%%8=AG%%:	+,,6:G%%359G%%2;?G%%8=AG%%:	-..6:G%%359G%%2;?G%%8;?G%%8) 011
w""00DD
E
E	

  =E<N<NA5r=G%%9 89 233
w""00BB
C
C	

  ;C:L:LA5r;G%%7 67  &&s+d9::/d	;	<	</d	=	>	>1d	5	6	6)d	6	7	7*dT , xx	*'	.r   c           	      h   U (       d*  [         R                  " [         R                  " SSS9/ 5      $ [        R                  " S5        [
        R                  " U SS9n[        R                  " 5       n[        R                  " UR                  U[        R                  " SSS	5      S
9nUR                  $ )zBReads content of the substitution rule file specified in file_arg.r   Tr   zyThe substitutionRules field is deprecated and can only be managed via gcloud/API. Please migrate to transformation rules.Fbinary	gkebackupv1SubstitutionRulesmessage_typestreamschema_path)r   r   r   r
   warningr   ReadFromFileOrStdinr   r   export_utilImportRestoreConfigGetSchemaPathr   file_argdatamstemp_restore_configs       r   ReadSubstitutionRuleFiler   m  s    	h22!dKRPP++= 
	'	'	?$!!#"#**##++
t0 
	.	..r   c           	          U (       d  g[         R                  " U SS9n[        R                  " 5       n[        R
                  " UR                  U[        R                  " SSS5      S9nUR                  $ )zDReads content of the transformation rule file specified in file_arg.NFr   r   r   TransformationRulesr   )	r   r   r   r   r   r   r   r   r   r   s       r   ReadTransformationRuleFiler     si    			'	'	?$!!#"#**##++
t2 
	0	00r   c           	          U (       d  g[         R                  " U SS9n[        R                  " 5       n[        R
                  " UR                  U[        R                  " SSS5      S9nU$ )z>Reads content of the restore order file specified in file_arg.NFr   r   r   RestoreOrderr   )r   r   r   r   r   r   r   r   )r   r   r   temp_restore_orders       r   ReadRestoreOrderFiler     s`    			'	'	?$!!#""))??++
t^ 
r   c           	          U (       d  g[         R                  " U SS9n[        R                  " 5       n[        R
                  " UR                  U[        R                  " SSS5      S9nUR                  $ )zAReads content of the exclusion window file specified in file_arg.NFr   r   r   ExclusionWindowsr   )	r   r   r   r   r   r   	RpoConfigr   exclusionWindows)r   r   r   temp_rpo_configs       r   ReadExclusionWindowsFiler     sf    			'	'	?$!!#"&&<<++
t// 
	)	))r   r   returnc           	          U (       d  g[         R                  " U SS9n[        R                  " 5       n[        R
                  " UR                  U[        R                  " SSS5      S9R                  $ )z4Reads the volume data restore policy overrides file.NFr   r   r    VolumeDataRestorePolicyOverridesr   )	r   r   r   r   r   r   Restorer    volumeDataRestorePolicyOverrides)r   r   r   s      r   (ReadVolumeDataRestorePolicyOverridesFiler     se     
		'	'	?$!!#"			::++
t?
 %$%r   c           	      D   U (       d  g[         R                  " U SS9n [        R                  " [        R
                  " 5       R                  U[        R                  " SSS5      S9nU$ ! [         a+  n[        R                  " SS	R                  U5      5      eSnAff = f)
z?Reads content of the restore filter file specified in file_arg.NFr   r   r   Filterr   z--filter-filez{0})r   r   r   r   r   r   r   r   	Exceptionr	   r!   rV   )r   r   restore_filteres       r   ReadRestoreFilterFiler     s    			'	'	?$ ''//188--x
N 
 
 

-
-Q s   A	A* *
B4&BBN).__doc__
__future__r   r   r   typingr   apitools.base.protorpcliter   /googlecloudsdk.api_lib.container.backup_restorer   r   googlecloudsdk.callioper	   !googlecloudsdk.command_lib.exportr   googlecloudsdk.corer
   googlecloudsdk.core.consoler   r   r   r   r   r   r0   r7   r;   r@   rD   rH   rK   rN   rR   r_   rl   r   r   r   r   r   r   strr   r   r    r   r   <module>r      s    7 &  '  / L . A # 2(O %(O %#E  "C CDB0-`m`/(1  * %sm%h778%"r   