
    %                        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ZddlZddl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ej4                  dedej8                  ej:                  j<                  ej>                  j<                  f   fdZ dej4                  dej8                  edf   fdZ!y)z<Shared utility functions for Cloud SCC muteconfigs commands.    )absolute_import)division)print_function)unicode_literalsN)errors)util)times)securitycenter_v1_messages)securitycenter_v2_messagesc                 j   | j                   t        j                  d      }t        j                  d      }|j                  | j                         r| j                   S |j                  | j                         rd| j                    S d| j                   v rt	        j
                  d      t	        j
                  d      | j                  nd| j                  v rQt        j                  d      }|j                  | j                        st	        j
                  d	      | j                  S d
| j                   S | j                  nd| j                  v rQt        j                  d      }|j                  | j                        st	        j
                  d      | j                  S d| j                   S y)zValidates parent.Nz^organizations/[0-9]{1,19}$z^[0-9]{1,19}$zorganizations//z_When providing a full resource path, it must include the pattern '^organizations/[0-9]{1,19}$'.z8Organization does not match the pattern '^[0-9]{1,19}$'.z^folders/.*$zPWhen providing a full resource path, it must include the pattern '^folders/.*$'.zfolders/z^projects/.*$zQWhen providing a full resource path, it must include the pattern '^projects/.*$'.z	projects/)organizationrecompilematchr   InvalidSCCInputErrorfolderproject)argsname_pattern
id_patternpatterns       6lib/googlecloudsdk/command_lib/scc/muteconfigs/util.pyValidateAndGetParentr   !   s   	"::;<LO,J$++,))*d//011
d''+ 
 
%
%B  
[[
dkk

>*g]]4;;'))
 	

 {{}%%	\\
dll

?+g]]4<<())
 	

 ||''     c                     | j                   }t        j                  d      }|j                  |      st	        j
                  d      |S )zValidate muteConfigId.z!^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$zNMute config id does not match the pattern '^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$'.)mute_configr   r   r   r   r   )r   mute_config_idr   s      r   ValidateAndGetMuteConfigIdr   Q   sH    ##.JJ:;'	~	&

%
%	0 
 r   c           
      \   | j                   }t        j                  d      }t        j                  d      }|j                  |      r|S |j                  |      r>|dk(  r7|j	                  d      }|d    d|d    d| j
                   d|d    d|d	    	S |S t        j                  d
      )z(Validates muteConfig full resource name.zP(organizations|projects|folders)/.*/muteConfigs/[a-z]([a-z0-9-]{0,61}[a-z0-9])?$z](organizations|projects|folders)/.*/locations/.*/muteConfigs/[a-z]([a-z0-9-]{0,61}[a-z0-9])?$v2r   r      /locations/      zrMute config must match the full resource name, or `--organization=`, `--folder=` or `--project=` must be provided.)r   r   r   r   splitlocationr   r   )r   versionr   resource_patternregionalized_resource_patternmute_config_componentss         r   (ValidateAndGetMuteConfigFullResourceNamer,   ^   s      +ZZX #%**e# #((5 K($*005&q)*!,B1,E+FkRVR_R_Q``abxyzb{a||}  U  VW  X  ~Y  Z  Z 	##7	 r   c                 F    | j                  d      }|t        |      dz
     S )z/Gets muteConfig id from the full resource name.r   r"   )r&   len)r   r+   s     r   #GetMuteConfigIdFromFullResourceNamer/   {   s)    &,,S1	$: ;a ?	@@r   c                     | j                  d      }|dk(  r|d    d|d    S |dk(  r|d    d|d    d|d    d|d    S y)	z(Gets parent from the full resource name.r   v1r   r"   r!   r$   r%   N)r&   )r   r(   r+   s      r   GetParentFromFullResourceNamer2      s    &,,S1_ %Q'(*@*C)DEE_ %Q'(*@*C)DAF\]^F_E``abxyzb{a|}}	 r   c                     t        |       }|*|dk(  rt        | |      }t        |       }| d| |_        |S t	        j
                  | |      | _        t        | |      }||_        |S )z&Generates the name of the mute config.r!   z/muteConfigs/)r    ValidateAndGetRegionalizedParentr   namescc_utilValidateAndGetLocationr'   r,   )r   reqr(   parentr   r   s         r   GenerateMuteConfigNamer:      sw    %&$/f=f/5N~&67CH
 
* 33D'BDM:4IKCH	*r   c                    | j                   ud| j                   v rVt        j                  d      }|j                  | j                         st	        j
                  d      | d| j                    S | d| j                    S y)zAppends location to parent.Nr   z^locations/.*$zRWhen providing a full resource path, it must include the pattern '^locations/.*$'.r#   )r'   r   r   r   r   r   )r   r9   r   s      r   r4   r4      s    	]]
dmm

+,g]]4==))) 
 	

 4==/**x{4==/22 r   r   r(   returnc                 
   |dk(  rt         j                  nt        j                  }| j                  dk(  r|j
                  j                  S | j                  dk(  r|j
                  j                  S t        j                  d      )zParses and validates type.r!   staticdynamicz*Type must be either 'static' or 'dynamic'.)
r   %GoogleCloudSecuritycenterV2MuteConfigr
   %GoogleCloudSecuritycenterV1MuteConfigtypeTypeValueValuesEnumSTATICDYNAMICr   r   )r   r(   mute_config_classs      r   ValidateAndGetTyperG      sx     
D !FF%KK 
 
YY(00777yyI00888##2	 r   c                 ^   | j                   yt        | d      r$| j                  dk7  rt        j                  d      	 t        j                  | j                         }t        j                  |      S # t
        j                  t
        j                  f$ r t        j                  d      w xY w)z!Parses and validates expiry time.NrB   r?   z9Expiry time is only supported for 'dynamic' mute configs.z^Invalid expiry time. See `$ gcloud topic datetimes` for information on supported time formats.)
expiry_timehasattrrB   r   r   r	   ParseDateTimeFormatDateTimeDateTimeSyntaxErrorDateTimeValueError)r   expiry_time_dts     r   ValidateAndGetExpiryTimerP      s     
T6tyyI5

%
%C (()9)9:N//

#
#U%=%=	> 

%
%	# s   3A3 39B,)"__doc__
__future__r   r   r   r   argparser   typinggooglecloudsdk.command_lib.sccr   r   r6   googlecloudsdk.core.utilr	   7googlecloudsdk.generated_clients.apis.securitycenter.v1r
   7googlecloudsdk.generated_clients.apis.securitycenter.v2r   r   r   r,   r/   r2   r:   r4   	NamespacestrUnionrA   rC   r@   rG   rP    r   r   <module>r]      s    C &  % '  	  1 ; * ^ ^-(`
:A~3 X// # &,,DDXXDDXXZC &


\\#t)r   