
    2                        S r SSKrSSK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  \R"                  " S5      rS\4S jrS rS\4S jrS\4S jrS\R0                  S\4S jrS\S\4S jrS\S\4S jrS rS rS rS r S\S\R0                  4S jr!S r"S\4S jr#S\4S  jr$S! r%S\14S" jr&S\R0                  S\4S# jr'S\S\4S$ jr(S% r)g)&z*Common flag parsing for management gcloud.    N)encoding)folders)	constants)errors)
properties)	resources)yaml)$securitycentermanagement_v1_messagesz[0-9]{1,20}returnc                     U R                   (       a&  [        U R                   5      R                  5        S3$ [        U 5      R                  5        S3$ )zReturns the relative path to the parent from args.

Args:
  args: command line args.

Returns:
  The relative path. e.g. 'projects/foo/locations/global',
  'folders/1234/locations/global'.
z/locations/global)parent_ParseParentRelativeName_GetParentResourceFromArgsargss    4lib/googlecloudsdk/command_lib/scc/manage/parsing.pyGetParentResourceNameFromArgsr      sI     
[[4;;'44677HII&t,99;<<M	NN    c                    U R                   (       a(  [        R                  R                  U R                   SS9$ U R                  (       a-  [
        R                  " 5       R                  U R                  SS9$ [        R                  R                  U R                  =(       d1    [        R                  R                  R                  R                  SS9SS9$ )N"cloudresourcemanager.organizations
collectioncloudresourcemanager.foldersTrequiredcloudresourcemanager.projects)organizationr   REGISTRYParsefolderr   FoldersRegistryprojectr   VALUEScoreGetr   s    r   r   r   /   s    	##&J $   {{""$** > +   ##I
))..66::D:I2 $  r   c                    [        U 5      nU R                  R                  5       n[        R                  R                  U5      nU(       a"  U S[        R                   SUR                   3$ [        R                  " U R                  5      e)a  Returns the specified service name from args if it exists.

Otherwise, an exception is raised detailing the parsing error along with the
expectation.

Args:
  args: The argument input as the gcloud command.

Raises:
  InvalidServiceNameError: the specified service name was invalid.
/)
r   service_namelowerr   SERVICE_INVENTORYgetSERVICE_RESOURCE_PLURAL_NAMEnamer   InvalidServiceNameError)r   r   maybe_service_name_or_abbrservices       r   GetServiceNameFromArgsr2   ?   sv     ).&#00668''++,FG'XQy==>a~NN

(
():):
;;r   c                     U R                   (       d  [        R                  " S5      e[        R	                  U R                   5      nU(       a  US   $ [        R                  " U R                   5      e)z Returns the module id from args.Nr   )module_id_or_namer   InvalidCustomModuleIdError_CUSTOM_MODULE_ID_REGEX	fullmatch)r   matchs     r   GetModuleIdFromArgsr9   W   sU    			

+
+D
11
!
+
+D,B,B
C%
8O

+
+D,B,B
CCr   module_typec                     U R                   (       d  [        R                  " 5       eSU 3SU 3SU 3/n[        U R                   5      =(       d    [	        U R                  5       5      S:H  nU H:  n [        R                  R                  U R                   US9R                  5       s  $    U(       a!  [        R                  " U R                   U5      e[        U 5      n[        U 5      nU SU SU 3$ ! [        R                   a     M  f = f)a  Returns the specified module name from args if it exists.

Otherwise, an exception is raised detailing the parsing error along with the
expectation.

Args:
  args: the args
  module_type: the module type (see
    googlecloudsdk.command_lib.scc.manage.constants)

Raises:
  MissingCustomModuleNameOrIdError: no module name or id was specified.
  InvalidCustomModuleNameError: the specified module name was invalid.
  InvalidCustomModuleIdError: the specified module id was invalid.
z1securitycentermanagement.organizations.locations.z,securitycentermanagement.projects.locations.z+securitycentermanagement.folders.locations.   r   r(   )r4   r    MissingCustomModuleNameOrIdError_IsPossibleResourceNamelenGetSpecifiedArgNamesr   r   r    r   RequiredFieldOmittedExceptionInvalidCustomModuleNameErrorr   r9   )r   r:   collectionsis_possible_resource_namer   r   	module_ids          r   GetModuleNameFromArgsrF   d   s    " 
		

1
1
33 	:+G4[MB3K=A+ d445 /	T&&(	)Q	. 
  j%%

 
 Z &     
-
-  ).&!$')81[M9+	.. 22 
s   35C55DDr   c                 p   U R                  S5      (       a  [        R                  R                  U SS9$ U R                  S5      (       a#  [        R
                  " 5       R                  U SS9$ U R                  S5      (       a  [        R                  R                  U SS9$ [        R                  " U 5      e)zUExtracts parent name from a string of the form {organizations|projects|folders}/<id>.organizations/r   r   zfolders/r   	projects/r   )
startswithr   r   r    r   r"   r   InvalidParentErrorr   s    r   r   r      s     '((##? $   $$""$**9 +   %%##2 $  
 
#
#F
++r   r.   c                     U R                  S5      =(       d)    U R                  S5      =(       d    U R                  S5      $ )Norganizationsprojectsr   )rJ   )r.   s    r   r>   r>      s5    
ooo& $		$$		#r   c                    U b<   [         R                  " U 5      n[        R                  " U[        R
                  5      $ g! [         R                   a*  n[        R                  " SR                  U5      5      eSnAff = f)z5Process the custom config file for the custom module.Nz%Error parsing custom config file [{}])
r	   loadr   DictToMessagemessagesCustomConfigYAMLParseErrorr   InvalidCustomConfigFileErrorformat)fileconfig_dictypes      r   GetCustomConfigFromArgsr[      sq    	IIdOk##K1F1FGG   //
1
8
8
= s   :A A?%A::A?c                      [         R                  " U 5      n[        R                  " U[        R
                  5      $ ! [         R                   a*  n[        R                  " SR                  U5      5      eSnAff = f)zJProcess the test resource data file for the custom module to test against.z Error parsing resource file [{}]N)
r	   rQ   r   rR   rS   SimulatedResourcerU   r   InvalidResourceFileErrorrW   )rX   resource_dictrZ   s      r   GetTestResourceFromArgsr`      sd    IIdOM!!-1K1KLL			 

)
)*11#6 s   := A;%A66A;c                    U bF   [         R                  " U 5      n[        R                  " U[        R
                  R                  5      $ g! [         R                   a*  n[        R                  " SR                  U5      5      eSnAff = f)z5Process the config custom file for the custom module.N$Error parsing config value file [{}])jsonloadsr   rR   rS    EventThreatDetectionCustomModuleConfigValueJSONDecodeErrorr   InvalidConfigValueFileErrorrW   rX   configes      r   GetConfigValueFromArgsrl      s}    	zz$f##
(;;GG    ..
0
7
7
: s   AA B	%BB	c                     U b-   [         R                  " U 5      n[         R                  " U5      $ g! [         R                   a*  n[        R
                  " SR                  U5      5      eSnAff = f)z3Converts the contents of a JSON file into a string.Nrb   )rc   rd   dumpsrg   r   rh   rW   ri   s      r   ParseJSONFilero      sh    	zz$fZZ   ..
0
7
7
: s   +2 A0%A++A0enablement_statec                    U[         R                  R                  :X  a  [        R                  R
                  nOSU[         R                  R                  :X  a  [        R                  R
                  nO[        R                  " SU S35      eU c  [        R                  " S5      eU R                  5       nUS:X  a  UR                  $ US:X  a  UR                  $ US:X  a  UR                  $ [        R                  " SU S35      e)	zParse the enablement state.zModule type "z" is not a valid module type.zAError parsing enablement state. Enablement state cannot be empty.ENABLEDDISABLED	INHERITED!Error parsing enablement state. "Y" is not a valid enablement state. Please provide one of ENABLED, DISABLED, or INHERITED.)r   CustomModuleTypeSHArS   #SecurityHealthAnalyticsCustomModuleEnablementStateValueValuesEnumETDre   r   InvalidModuleTypeErrorInvalidEnablementStateErrorupperrr   rs   rt   )rp   r:   
state_enumstates       r   GetEnablementStateFromArgsr      s   
 I..22244SS  i0044411PP  
'
'
}$AB  

,
,K  
 
 
"%
i


,
,
+E7 3I 	I r   c                     U R                   b  U R                  b  gU R                   b  gU R                  b  g[        R                  " S5      e)z*Create an update mask with the args given.zenablement_state,custom_configrp   custom_configzError parsing Update Mask. Either a custom configuration or an enablement state (or both) must be provided to update the custom module.)rp   custom_config_filer   InvalidUpdateMaskInputErrorr   s    r   CreateUpdateMaskFromArgsr     sR    	&4+B+B+N+(*

,
,	 r   rX   c                    U bF   [         R                  " U 5      n[        R                  " U[        R
                  R                  5      $ g! [         R                  [        4 a  n[        R                  " SU S35      eSnAff = f)z/Process the module config file for the service.Nz!Error parsing config value file [])r	   rQ   r   rR   rS   SecurityCenterServiceModulesValuerU   AttributeErrorr   rh   )rX   rj   rZ   s      r   GetModuleConfigValueFromArgsr   !  s    	yyf##
(00==   0 ..-cU!
4 s   AA B%A??Bc                     [         R                  R                  nU c  gU R                  5       nUS:X  a  UR                  $ US:X  a  UR
                  $ US:X  a  UR                  $ [        R                  " SU S35      e)z#Parse the service enablement state.Nrr   rs   rt   ru   rv   )	rS   r   &IntendedEnablementStateValueValuesEnumr~   rr   rs   rt   r   r}   )rp   r   r   s      r   !GetServiceEnablementStateFromArgsr   1  s     $$KK  

 
 
"%
i


,
,
+E7 3I 	I r   c                     U R                   b  U R                  b  gU R                   b  gU R                  b  g[        R                  " S5      e)z@Create an update mask with the args given for the given service.z!intended_enablement_state,modulesintended_enablement_statemoduleszError parsing Update Mask. Either a module configuration or an enablement state (or both) must be provided to update the service.)rp   module_config_filer   r   r   s    r   "CreateUpdateMaskFromArgsForServicer   H  sS    	&4+B+B+N.(&*

,
,	N r   c                     U R                   (       d  / $ U R                   R                  S5      nUR                  S5      nU Vs1 s H  o3R                  5       iM     nnU$ s  snf )z)Returns a list of module names from args.z[],)filter_modulesstripsplit)r   r   modules_listmodulemodules_sets        r   GetModuleListFromArgsr   W  sY     
		I%%d+'s#,.:;lFl+;	 <s   A!c                     U R                   (       a(  [        U R                   5      R                  5        SU 3$ [        U 5      R                  5        SU 3$ )aF  Returns the specified module name path from args if it exists.

Args:
  args: command line args.
  module_type: the module type (see
    googlecloudsdk.command_lib.scc.manage.constants)

Returns:
  The relative path. e.g.
  'organizations/1234/locations/global/{module_type}',
  'projects/foo/locations/global/{module_type}'.
z/locations/global/)r   _ParseParentFlagr   _GetParentResourceFromArg)r   r:   s     r   GetModuleNamePathFromArgsr   d  s]     
[[DKK(557
88J-	 
#4	(	5	5	788J	r   c                     U R                  S5      (       a  [        R                  R                  U SS9$ U R                  S5      (       a  [        R                  R                  U SS9$ [        R
                  " U 5      e)zExtracts parent name from {organizations|projects}/<id>.

Args:
  parent: The parent string to parse.

Returns:
  The relative path of the parent.

Raises:
  InvalidParentFlagError: The provided parent string is invalid.
rH   r   r   rI   r   )rJ   r   r   r    r   InvalidParentFlagErrorrL   s    r   r   r     s     '((##? $   {####2 $  
 	%%f--r   c                 4   U R                   (       a(  [        R                  R                  U R                   SS9$ [        R                  R                  U R                  =(       d1    [
        R                  R                  R                  R                  SS9SS9$ )ztReturns the parent resource from the given args.

Args:
  args: command line args.

Returns:
  The parent resource.
r   r   Tr   r   )	r   r   r   r    r#   r   r$   r%   r&   r   s    r   r   r     s     
##&J $   
			!	!
llGj'',,4488$8G0 
" 
 r   )*__doc__rc   reapitools.base.pyr   'googlecloudsdk.api_lib.resource_managerr   %googlecloudsdk.command_lib.scc.manager   r   googlecloudsdk.corer   r   r	   Agooglecloudsdk.generated_clients.apis.securitycentermanagement.v1r
   rS   compiler6   strr   r   r2   r9   rw   rF   r   boolr>   r[   r`   rl   ro   r   r   r   r   r   r   r   r   r    r   r   <module>r      s7   1  	 % ; ; 8 * ) $ ~**]3 O3 O  <C <0
D 
D2/Y-G-G 2/C 2/j, , ,(# $ 		 ##++#L s   .
C5 
 116.S .S .4r   