
    h9                         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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"g)z9Shared utility functions for Cloud SCC findings commands.    )absolute_import)division)print_function)unicode_literalsN)encoding)securitycenter_client)errors)util)timesc                     SU R                   ;   aC  U R                  S5      (       d  U R                  S5      (       a  [        R                  " S5      egg)IValidates that only a full resource name or split arguments are provided./organizationsourcezOnly provide a full resource name (organizations/123/sources/456/findings/789) or an --organization flag and --sources flag, not both.N)findingIsKnownAndSpecifiedr	   InvalidSCCInputErrorargss    3lib/googlecloudsdk/command_lib/scc/findings/util.py.ValidateMutexOnFindingAndSourceAndOrganizationr      sR    DLL
~..		!	!(	+	+

%
%	)  
,     c                    [         R                  " S5      n[         R                  " S5      n[         R                  " S5      nUR                  U R                  5      (       a  U R                  $ UR                  U R                  5      (       a  US:X  a  [	        X5      $ U R                  $ UR                  U R                  5      (       a  [        X5       SU R                   3$ [        R                  " S5      e)a"  Returns relative resource name for a finding name.

Args:
  args: Argument namespace.
  version: Api version.

Returns:
  Relative resource name
  if no location is specified the result will be one of the following forms
    `organizations/{organization_id}/sources/{source_id}/finding/{finding_id}`
    `folders/{folders_id}/sources/{source_id}/finding/{finding_id}`
    `projects/{projects_id}/sources/{source_id}/finding/{finding_id}`
  if a location is specified the result will be one of the following forms
    `organizations/{organization_id}/sources/{source_id}/locations/{location_id}/finding/{finding_id}`
    `folders/{folders_id}/sources/{source_id}/locations/{location_id}/finding/{finding_id}`
    `projects/{projects_id}/sources/{source_id}/locations/{location_id}/finding/{finding_id}`
J(organizations|projects|folders)/.*/sources/[0-9-]+/findings/[a-zA-Z0-9]+$W(organizations|projects|folders)/.*/sources/[0-9-]+/locations/.*/findings/[a-zA-Z0-9]+$z^[a-zA-Z0-9]+$v2z
/findings/zHFinding must match either the full resource name or only the finding id.)recompilematchr   GetRegionalizedResourceNameGetFullSourceNamer	   r   )r   versionresource_patternregion_resource_pattern
id_patterns        r   GetFullFindingNamer&   ,   s    $ ZZR JJ_ zz*+*""4<<00<<DLL))$(77<<dll##./z$,,HH##P	 r   c                    [         R                  " S5      n[         R                  " S5      n[         R                  " S5      nU R                  (       d  [        R                  " S5      eUR                  U R                  5      (       a  U R                  $ [        R                  " X5      nUR                  U R                  5      (       a  U R                  nUS:X  a  U SU 3$ U$ UR                  U R                  5      (       aV  [        U S5      (       d  [        U S5      (       a4  [        R                  " U 5       S	U R                   3nUS:X  a  U SU 3$ U$ UR                  U R                  5      (       a4  [        R                  " U 5       S	U R                   3nUS:X  a  U SU 3$ U$ [        R                  " S
5      e)ah  Returns relative resource name for a source from --source argument.

Args:
  args: Argument namespace.
  version: Api version.

Returns:
  Relative resource name
  if args.source is not provided an exception will be raised
  if no location is specified in argument: sources/{source_id}
  if a location is specified: sources/{source_id}/locations/{location_id}
z3(organizations|projects|folders)/.*/sources/[0-9-]+zL(organizations|projects|folders)/.*/sources/[0-9-]+/locations/[a-zA-Z0-9-]+$z[0-9-]+zGFinding source must be provided in --source flag or full resource name.r   /locations/r   parentz	/sources/zJThe source must either be the full resource name or the numeric source ID.)r   r   r   r	   r   r   scc_utilValidateAndGetLocationhasattr(GetFindingsParentFromPositionalArguments GetParentFromPositionalArguments)r   r"   r#   r$   r%   locationr   s          r   r!   r!   T   s    ZZ; JJT zz)$*	

%
%	 
 ""4;;//;;,,T;(DKK(([[F$x{8*--Mt{{##tY74#:#:AA$GH	RVR]R]Q^_F$x{8*--Mdkk""99$?@	$++WF$x{8*--M##'	 r   c           
      >   [         R                  " S5      nUR                  U 5      (       d  [        R                  " S5      eU R                  S5      nUS:X  a  [        U 5       SUS    SUS    3$ US:X  a%  [        U 5       SUS    SUS    SUS    SUS	    3	$ g
)a  Get parent (with source) from Finding name i.e remove 'findings/{finding_name}'.

Args:
  resource_name: finding name {parent with source}/findings/{findingID}
  version: API version.

Returns:
  The parent with source or parent with source and location
  examples:
  if no location is specified the result will be one of the following forms
    `organizations/{organization_id}/sources/{source_id}`
    `folders/{folders_id}/sources/{source_id}`
    `projects/{projects_id}/sources/{source_id}`
  if a location is specified the result will be one of the following forms
    `organizations/{organization_id}/sources/{source_id}/locations/{location_id}`
    `folders/{folders_id}/sources/{source_id}/locations/{location_id}`
    `projects/{projects_id}/sources/{source_id}/locations/{location_id}`
z2(organizations|projects|folders)/.*/sources/[0-9]+zfWhen providing a full resource path, it must also include the organization, project, or folder prefix.r   v1      r         N)r   r   r   r	   r   splitGetParentFromResourceName)resource_namer"   r#   list_source_componentss       r   GetSourceParentFromFindingNamer:      s   & ZZ: 
			.	.

%
%	7  )..s3_'67q9OPQ9R8SSTUklmUnTopp_'67q9OPQ9R8SSTUklmUnToop  rH  IJ  rK  qL  LM  Nd  ef  Ng  Mh  i  i r   c                 "   [         R                  " S5      n[         R                  " S5      nUR                  U 5      (       d,  UR                  U 5      (       d  [        R                  " S5      eU R                  S5      nU[        U5      S-
     $ )z.Gets a finding id from the full resource name.r   r   z|When providing a full resource path, it must include the pattern organizations/[0-9]+/sources/[0-9-]+/findings/[a-zA-Z0-9]+.r      )r   r   r   r	   r   r6   len)finding_namer#   r$   list_finding_componentss       r   GetFindingIdFromNamer@      s    ZZR JJ_ 
		
 
#)),77

%
%	F  )..s3	 %<!=!A	BBr   c                 >    U R                  S5      nUS    SUS    3$ )Nr   r   r<   )r6   )r8   list_organization_componentss     r   r7   r7      s1    !.!4!4S!9(+,A.J1.M-N	OOr   c                 4   [         R                  " U5      nU (       a  U R                  5       n 0 nUS:X  af  UR                  R                  R
                  nUR                  R                  R                  UR                  R                  R                  US.US'   OeUR                  R                  R
                  nUR                  R                  R                  UR                  R                  R                  US.US'   X1   R                  XU   S   5      $ )zDConvert state input to messages.Finding.StateValueValuesEnum object.r1   )ACTIVEINACTIVESTATE_UNSPECIFIEDr   rF   )
r   GetMessagesupperFindingStateValueValuesEnumrF   rD   rE   "GoogleCloudSecuritycenterV2Findingget)stater"   messages
state_dictunspecified_statev2_unspecified_states         r   ConvertStateInputrR      s    "..w7(
KKME*_ ((==OO""77>>$$99BB.Jt 	33HHZZ 
 77LLSS 77LLUU1Jt 
		 	  !45
 r   c                     U S:X  a   UR                   R                  R                  $ U S:X  a   UR                   R                  R                  $ [        R
                  " S5      e)z]Convert mute state input to messages.BulkMuteFindingsRequest.MuteStateValueValuesEnum object.muted	undefinedz-Mute state must be one of [muted, undefined].)BulkMuteFindingsRequestMuteStateValueValuesEnumMUTED	UNDEFINEDr	   r   )
mute_staterN   s     r   ConvertMuteStateInputr[      sX    7++DDJJJ[ ++DDNNN##5	 r   c                    U R                   b  SU R                   ;   aX  [        R                  " S5      nUR                  U R                   5      (       d  [        R
                  " S5      eU R                   $ [        R                  " S5      nUR                  U R                   5      (       d  [        R
                  " S5      eSU R                    3$ U R                  bw  SU R                  ;   aX  [        R                  " S5      nUR                  U R                  5      (       d  [        R
                  " S	5      eU R                  $ S
U R                   3$ U R                  bw  SU R                  ;   aX  [        R                  " S5      nUR                  U R                  5      (       d  [        R
                  " S5      eU R                  $ SU R                   3$ g)zValidates parent.Nr   z^organizations/[0-9]{1,19}$z_When providing a full resource path, it must include the pattern '^organizations/[0-9]{1,19}$'.z^[0-9]{1,19}$z8Organization does not match the pattern '^[0-9]{1,19}$'.zorganizations/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/)r   r   r   r   r	   r   folderproject)r   patterns     r   ValidateAndGetParentr`      s   	"
d

89g]]4,,--))-
 	

    

?+g]]4,,--))F
 	
   1 1233	[[
dkk

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

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

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

 ||'' r   c                 l    SU R                   ;   a$  U R                  b  [        R                  " S5      egg)r   r   Nz_Only provide a full resource name (organizations/123/sources/456) or a --parent flag, not both.)r   r)   r	   r   r   s    r   ValidateMutexOnSourceAndParentrb   $  s8    DKKDKK3

%
%	H  4r   c                     A[        U [        5      (       a  U nO[        U 5      n[        U5      S:  a  [        R                  " S5      eU H  nUR
                  R                  s  $    g)z-Returns security marks from finding response.r<   zVListFindingResponse must only return one finding since it is filtered by Finding Name.N)
isinstancelistr=   r	   r   r   securityMarks)responser   list_finding_responsefinding_results       r    ExtractSecurityMarksFromResponserj   -  sf    
$$ N	!#

%
%	$  .n!!/// .r   c                     U R                   c  [        R                  " S5      eSU R                  ;   a  [        R                  " S5      eg)zHValidates that source and finding id are provided if parent is provided.Nz--source flag must be provided.r   z?Finding id must be provided, instead of the full resource name.)r   r	   r   r   r   s    r   *ValidateSourceAndFindingIdIfParentProvidedrl   =  sE    	[[

%
%&G
HHDLL

%
%I  r   c                 "   U R                   (       a~  SU R                   ;   a]  [        R                  " S5      nUR                  U R                   5      (       d  [        R
                  " S5      eU SU R                    3$ U SU R                    3$ g)zAppends location to parent.r   z^locations/[A-Za-z0-9-]{0,61}$zRWhen providing a full resource path, it must include the pattern '^locations/.*$'.r(   N)r/   r   r   r   r	   r   )r   r)   r_   s      r   (ValidateLocationAndGetRegionalizedParentrn   G  s    	]]
dmm

;<g]]4==)))) 
 	

 4==/**x{4==/22 r   c                     [         R                  " X5      nU R                  R                  S5      nUS    SUS    SUS    SUS    SU SUS    SUS    3$ )	z#Returns regionalized resource name.r   r   r<   r2   r3   r(   r4   r5   )r*   r+   r   r6   )r   r"   r/   name_componentss       r   r    r    W  s    ,,T;(LL&&s+/Aq!3 4Aoa6H5I?[\K]J^^ijrisst  vE  FG  vH  uI  IJ  KZ  [\  K]  J^  
_  _r   c                    [         R                  " U5      nUS:X  a*  [        R                  " XR                  R
                  5      $ US:X  a+  [        R                  " U UR                  R
                  5      $ [        R                  " S5      e)zAHook to capture "key1=val1,key2=val2" as SourceProperties object.r1   r   zInvalid API version)	r   rG   r   DictToMessagerI   SourcePropertiesValuerK   r	   InvalidAPIVersion)source_properties_dictr"   rN   s      r   ConvertSourcePropertiesrv   ^  s}    "..w7(_!! 0 0 F F  $!!33II  	  !677r   c                     U R                   (       d  [        R                  " U 5      nOU R                   n[        R                  " XU5      $ )zADetermine what version to call from --location and --api-version.)r)   r*   r.   GetVersionFromArguments)r   deprecated_argsr)   s      r    GetApiVersionUsingDeprecatedArgsrz   m  s7    	 66t<F[[F		)	)$	HHr   c                      [         R                  " U 5      n[         R                  " U5      $ ! [         R                  [         R                  4 a  n[
        R                  " S5      UeSnAff = f)zValidates the export time.z[Invalid export time format. Please provide a valid date/time. Example: 2024-08-20T12:00:00ZN)r   ParseDateTimeFormatDateTimeDateTimeSyntaxErrorDateTimeValueErrorr	   r   )export_timeread_time_dtes      r   ValidateAndFormatExportTimer   x  sf    &&{3L--

#
#U%=%=	> 

%
%	  s   +. $A.A))A.c                     [         R                  " S5      nUR                  U 5      (       d  [        R                  " S5      eU $ )zValidates the dataset.z/^projects/[a-zA-Z0-9-]+/datasets/[a-zA-Z0-9_]+$zMDataset must match the pattern projects/[a-zA-Z0-9-]+/datasets/[a-zA-Z0-9_]+.)r   r   r   r	   r   )datasetdataset_patterns     r   ValidateDatasetr     sE    JJ7/ 
		w	'	'

%
%	:  
.r   )#__doc__
__future__r   r   r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.sccr   googlecloudsdk.command_lib.sccr	   r
   r*   googlecloudsdk.core.utilr   r   r&   r!   r:   r@   r7   rR   r[   r`   rb   rj   rl   rn   r    rv   rz   r   r    r   r   <module>r      s    @ &  % ' 	 % < 1 ; *
%P7t iFC&P
@-(`0 3 _8I	
r   