
    `#                         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	 rS
 rS rS rS r   SS\4S jjrS rg)z0Shared utility functions for Cloud SCC commands.    )absolute_import)division)print_function)unicode_literalsN)errors)
propertiesc                    [         R                  " S5      nSn[        U S5      (       aP  U R                  (       d3  [        R
                  R                  R                  R                  5       nOU R                  nUc2  [        R
                  R                  R                  R                  5       n[        U S5      (       a  [        U SS5      OSn[        U S5      (       a  [        U SS5      OSn[        U S5      (       a  [        U SS5      OSnU(       a  UR                  U5      (       a  SU 3OUnOIU(       a  UR                  U5      (       a  SU 3OUnO$U(       a  UR                  U5      (       a  S	U 3OUnUc  [        R                  " S
5      eUR                  U5      (       a  SU-   nUR                  S5      (       dD  UR                  S5      (       d.  UR                  S	5      (       d  Sn[        R                  " U5      eU$ z@Converts user input to one of: organization, project, or folder.z[0-9]+Nparentorganizationprojectfolderorganizations/	projects/folders/zCCould not find Parent argument. Please provide the parent argument.zQParent must match either [0-9]+, organizations/[0-9]+, projects/.* or folders/.*.)recompilehasattrr   r   VALUESsccGetr   getattrmatchr   InvalidSCCInputError
startswithargs
id_patternr   r   r   r   error_messages          *lib/googlecloudsdk/command_lib/scc/util.py(GetFindingsParentFromPositionalArgumentsr!      s   zz(#*&T8;;  $$++//1f{{f^""//335F 
~	&	& dND) 
 /6dI.F.FGD)T*D',3D(,C,C744(& L)) '  &0&6&6w&?&?y	"WF$.$4$4V$<$<xx &F^

%
%M  f&F ())			;	'	'			:	&	&	  
%
%m
44	-    c                    [         R                  " S5      nSn[        U S5      (       aP  U R                  (       d3  [        R
                  R                  R                  R                  5       nOU R                  nUc2  [        R
                  R                  R                  R                  5       n[        U S5      (       a  [        U SS5      OSn[        U S5      (       a  [        U SS5      OSn[        U S5      (       a  [        U SS5      OSnU(       a  UR                  U5      (       a  SU 3OUnOIU(       a  UR                  U5      (       a  SU 3OUnO$U(       a  UR                  U5      (       a  S	U 3OUnUc  [        U S5      (       a  U R                  nUc  [        U S5      (       a  U R                  nUc  [        R                  " S
5      eUR                  U5      (       a  SU-   nUR                  S5      (       dD  UR                  S5      (       d.  UR                  S	5      (       d  Sn[        R                  " U5      eU$ r
   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s          r     GetParentFromPositionalArgumentsr$   X   s   zz(#*&T8;;  $$++//1f{{f^""//335F 
~	&	& dND) 
 /6dI.F.FGD)T*D',3D(,C,C744(& L)) '  &0&6&6w&?&?y	"WF$.$4$4V$<$<xx &F^i00\\F^h//[[F^

%
%M  f&F ())			;	'	'			:	&	&	  
%
%m
44	-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                   -   $ [        U S5      (       a  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                  -   $ [        U S5      (       a  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                  -   $ gg)z/Gets and validates parent from named arguments.N/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}$'.r   r   z^folders/.*$zPWhen providing a full resource path, it must include the pattern '^folders/.*$'.r   r   z^projects/.*$zQWhen providing a full resource path, it must include the pattern '^projects/.*$'.r   )	r   r   r   r   r   r   r   r   r   )r   patterns     r    GetParentFromNamedArgumentsr(      s   	"
d

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

    

?+g]]4,,--))F
 	
  $"3"333T8!8
dkk

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

 {{$++%%T9$,,":
dll

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

 ||4<<'' #;r"   c                 &    U R                  SS5      $ )z2Removes spaces from a field mask provided by user.  )replace)masks    r    CleanUpUserMaskInputr.      s    	c2	r"   c                     SU;   nU R                  S5      (       a  U(       a  [        R                  " S5      eU R                  S5      =(       d    U$ )z&Returns true if location is specified.z/locations/locationzPOnly provide location in a full resource name or in a --location flag, not both.)IsKnownAndSpecifiedr   r   )r   resource_namelocation_in_resource_names      r    IsLocationSpecifiedr4      sR    +}<	j)).G

%
%	- 
 
	!	!*	-	J1JJr"   "version_specific_existing_resourcec                 4   [        X5      nU(       a	  U(       a  ggU(       a]  U HW  nU R                  U5      (       a  U(       a  [        R                  " S5      eU R                  U5      (       d  MN  U(       a  MW    g   U R                  S:X  a	  U(       a  ggg)a  Returns the correct version to call based on the user supplied arguments.

Args:
  args: arguments
  resource_name: (optional) resource name e.g. finding, mute_config
  deprecated_args: (optional) list of deprecated arguments for a command
  version_specific_existing_resource: (optional) command is invoked on a
    resource which is not interoperable between versions.

Returns:
  Version of securitycenter api to handle command, either "v1" or "v2"
v2v1z<Location is not available when deprecated arguments are used)r4   r1   r   r   api_version)r   r2   deprecated_argsr5   location_specifiedarguments         r    GetVersionFromArgumentsr=      s    $ +4?
 ( #		!	!(	+	+0B))J
 	
 
	!	!(	+	+4F4F $ 
	r"   c                    US:X  a  U R                   b  [        R                  " S5      n[        R                  " S5      nUR                  U R                   5      (       a  U R                   R	                  S5      S   $ UR                  U R                   5      (       a  U R                   $ [
        R                  " S5      eU R                   $ )z5Validates --location flag input and returns location.r7   z^locations/[A-Za-z0-9-]{0,61}$z^[A-Za-z0-9-]{0,61}$r&      z[location does not match the pattern '^locations/[A-Za-z0-9-]{0,61}$'. or [A-Za-z0-9-]{0,61})r0   r   r   r   splitr   r   )r   versionname_patternr   s       r    ValidateAndGetLocationrC     s    _}} ZZ @Al::45j			DMM	*	*}}""3'**			$--	(	(}}''E 
 
r"   )r+   NF)__doc__
__future__r   r   r   r   r   googlecloudsdk.command_lib.sccr   googlecloudsdk.corer   r!   r$   r(   r.   r4   boolr=   rC    r"   r    <module>rJ      s\    7 &  % ' 	 1 *9x?D-(`

K /4	, )-	,^r"   