
    A                     2   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ZdZdZdZd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d  Z)d!ee*   d"eejV                     fd#Z,d$ Z-y%)&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                     ~ ~d|_         |S )NT)force)refargsrequests      @lib/googlecloudsdk/command_lib/container/backup_restore/hooks.pyAddForceToDeleteRequestr   $   s    	
 '-	.    c                    | syt        j                         }g }	 | D ]  }|j                  d      }t        |      dk(  rd}|d   }n*t        |      dk(  r|\  }}nt	        j
                  |d      |st	        j
                  |d      |j                         }||_        ||_        |j                  |        |S # t        $ r t	        j
                  |d      w xY w)	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bbo	jjn'  ( J	 C

-
-AC CCs   B B< < Cc                     t        j                         }|j                         }|j                  j	                  t        | d             |S )Nz---cluster-resource-scope-selected-group-kinds)r   r   ClusterResourceRestoreScopeselectedGroupKindsextendr0   r'   r)   crrss      r   ProcessSelectedGroupKindsr7   P   G    &&('		,	,	.$  
F
 
+r   c                     t        j                         }|j                         }|j                  j	                  t        | d             |S )Nz---cluster-resource-scope-excluded-group-kinds)r   r   r2   excludedGroupKindsr4   r0   r5   s      r   ProcessExcludedGroupKindsr;   [   r8   r   c                 \    t        j                         }|j                         }| |_        |S N)r   r   r2   allGroupKinds)all_group_kindsr)   r6   s      r   ProcessAllGroupKindsr@   f   s+    &&('		,	,	.$&$	+r   c                 \    t        j                         }|j                         }| |_        |S r=   )r   r   r2   noGroupKinds)no_group_kindsr)   r6   s      r   ProcessNoGroupKindsrD   m   s+    &&('		,	,	.$$$	+r   c                 6    | st        j                  dd      | S )Nz--all-namespacesz All namespaces can only be true.r	   r!   )all_namespacess    r   ProcessAllNamespacesrH   t   s&    	

-
-*, , 
r   c                 6    | st        j                  dd      | S )Nz--no-namespaceszNo namespaces can only be true.rF   )no_namespacess    r   ProcessNoNamespacesrK   |   s&    	

-
-)+ + 
r   c                 6    | st        j                  dd      | S )Nz--selected-namespacesz&Selected namespaces must not be empty.rF   )selected_namespacess    r   ProcessSelectedNamespacesrN      &    	

-
-02 2 
r   c                 6    | st        j                  dd      | S )Nz--excluded-namespacesz&Excluded namespaces must not be empty.rF   )excluded_namespacess    r   ProcessExcludedNamespacesrR      rO   r   c                 F   | st        j                  dd      t        j                         }|j	                         }	 | j                  d      D ]  }|j                  d      \  }}|s%t        j                  ddj                  |            |s%t        j                  ddj                  |            |j                         }||_        ||_	        |j                  j                  |        |S # t        $ r& t        j                  ddj                  |             w xY w)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bgbl# <  I	 

-
-!/667LM s   B2C1 1/D c                    | st        j                  dd      t        j                         }|j	                         }| j                  d      D ]}  }|j                  d      }|d   st        j                  dd      |j                         }|d   |_        t        |      dk(  rdn|d   |_	        |j                  j                  |        |S )	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                    ~ |j                  d      r6d|j                  j                  _        d|j                  j                  _        |j                  d      r6d|j                  j                  _        d|j                  j                  _        |j                  d      r6d|j                  j                  _        d|j                  j                  _        |j                  d      r'|j                  d      rt        j                  dd      |j                  d      rd|j                  j                  _	        |j                  d      rd|j                  j                  _
        t               }|j                  j                  d	      D ]  }|j                  d
      r|j                  d
       &|j                  d      r|j                  d       I|j                  d      r#|j                  d       |j                  d       }|j                  d      r#|j                  d       |j                  d       |j                  |        d	j!                  t#        |            |_        |S )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mm56	<	=mm78	6	7mm12mm./	D	Emm?@mm12mmD , xxy 12'	.r   c                 r   ~ t        |t              rC|j                  t              r.t        |j                        |j
                  j                  _        t        |t              rC|j                  t              r.t        |j                        |j
                  j                  _        t        |t              rC|j                  t              r.t        |j                        |j
                  j                  _        t        |t              rC|j                  t              r.t        |j                         |j
                  j                  _        |j                  d      rld|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        |j                  d      rld|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        |j                  d      rld|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        |j                  d      rld|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        |j                  d      rld|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        d|j
                  j                  _        g }|j                  d      rt-        |j
                  j                  j.                        rlt1        j2                  dd	
       t5        j6                  t5        j8                  dd	      g       |j
                  j                  _        |j;                  d       |j                  d      rt-        |j
                  j                  j<                        rlt1        j2                  dd	
       t5        j6                  t5        j8                  dd	      g       |j
                  j                  _        |j;                  d       |j>                  jA                  d      D ]v  }|jC                  d      rd}nO|jC                  d      rd}n;|jC                  d      rd}n'|jC                  d      rd}n|jC                  d      rd}|j;                  |       x djE                  |      |_        |S )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9I9I&: 	TBBC %%A T238H8H%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
w""00DD
E	

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

  ;C:L:LA5r;G%%7 67  &&s+d9:/d	;	</d	=	>1d	5	6)d	6	7*dT , xx	*'	.r   c           	      b   | s+t        j                  t        j                  dd      g       S t        j                  d       t        j                  | d      }t        j                         }t        j                  |j                  |t        j                  ddd	      
      }|j                  S )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           	          | syt        j                  | d      }t        j                         }t	        j
                  |j                  |t	        j                  ddd            }|j                  S )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           	          | syt        j                  | d      }t        j                         }t	        j
                  |j                  |t	        j                  ddd            }|S )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           	          | syt        j                  | d      }t        j                         }t	        j
                  |j                  |t	        j                  ddd            }|j                  S )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           	          | syt        j                  | d      }t        j                         }t	        j
                  |j                  |t	        j                  ddd            j                  S )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           	      :   | syt        j                  | d      }	 t        j                  t	        j
                         j                  |t        j                  ddd            }|S # t        $ r*}t        j                  dd	j                  |            d}~ww xY w)
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0%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   