
    2                        d 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dl
mZ dd	l
mZ dd
lmZ  ej"                  d      ZdefdZd ZdefdZdefdZdej0                  defdZdedefdZdedefdZd Zd Zd Zd Z dedej0                  fdZ!d Z"defdZ#defd Z$d! Z%dehfd"Z&dej0                  defd#Z'dedefd$Z(d% Z)y)&z*Common flag parsing for management gcloud.    N)encoding)folders)	constants)errors)
properties)	resources)yaml)$securitycentermanagement_v1_messagesz[0-9]{1,20}returnc                     | j                   r&t        | j                         j                          dS t        |       j                          dS )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                    | j                   r+t        j                  j                  | j                   d      S | j                  r/t        j                         j                  | j                  d      S t        j                  j                  | j                  xs4 t        j                  j                  j                  j                  d      d      S )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                    t        |       }| j                  j                         }t        j                  j                  |      }|r"| dt        j                   d|j                   S t        j                  | j                        )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                     | j                   st        j                  d      t        j	                  | j                         }|r|d   S t        j                  | j                         )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                    | j                   st        j                         d| d| d| g}t        | j                         xs t	        | j                               dk(  }|D ]=  }	 t        j                  j                  | j                   |      j                         c S  |r t        j                  | j                   |      t        |       }t        |       }| d| d| S # t        j                  $ r Y w xY w)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   (8C&&C<;C<r   c                 `   | j                  d      r!t        j                  j                  | d      S | j                  d      r%t	        j
                         j                  | d      S | j                  d      r!t        j                  j                  | d      S t        j                  |       )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                 p    | j                  d      xs$ | j                  d      xs | j                  d      S )Norganizationsprojectsr   )rJ   )r.   s    r   r>   r>      s5    
ooo& $		$$		#r   c                     | :	 t        j                  |       }t        j                  |t        j
                        S y# t         j                  $ r)}t        j                  dj                  |            d}~ww xY w)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   8> A:$A55A:c                     	 t        j                  |       }t        j                  |t        j
                        S # t         j                  $ r)}t        j                  dj                  |            d}~ww xY w)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   8; A7$A22A7c                    | D	 t        j                  |       }t        j                  |t        j
                  j                        S y# t         j                  $ r)}t        j                  dj                  |            d}~ww xY w)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$A??Bc                     | +	 t        j                  |       }t        j                  |      S y# t         j                  $ r)}t	        j
                  dj                  |            d}~ww xY w)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   )/ A+$A&&A+enablement_statec                    |t         j                  j                  k(  rt        j                  j
                  }nQ|t         j                  j                  k(  rt        j                  j
                  }nt        j                  d| d      | t        j                  d      | j                         }|dk(  r|j                  S |dk(  r|j                  S |dk(  r|j                  S t        j                  d| d      )	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                     | j                   | j                  y| j                   y| j                  yt        j                  d      )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                    | D	 t        j                  |       }t        j                  |t        j
                  j                        S y# t         j                  t        f$ r}t        j                  d| d      d}~ww xY w)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 A?!A::A?c                     t         j                  j                  }| y| j                         }|dk(  r|j                  S |dk(  r|j
                  S |dk(  r|j                  S t        j                  d| d      )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                     | j                   | j                  y| j                   y| j                  yt        j                  d      )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                     | j                   sg S | j                   j                  d      }|j                  d      }|D ch c]  }|j                          }}|S c c}w )z)Returns a list of module names from args.z[],)filter_modulesstripsplit)r   r   modules_listmodulemodules_sets        r   GetModuleListFromArgsr   W  s[     
		I%%d+'s#,.:;lFl+;	 <s   Ac                     | j                   r(t        | j                         j                          d| S t        |       j                          d| S )aX  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                     | j                  d      r!t        j                  j                  | d      S | j                  d      r!t        j                  j                  | d      S t	        j
                  |       )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                 2   | j                   r+t        j                  j                  | j                   d      S t        j                  j                  | j                  xs4 t
        j                  j                  j                  j                  d      d      S )z~Returns 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      s9   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   