
    `#                         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mZ ddl	m
Z
 d	 Zd
 Zd Zd Zd Z	 	 	 ddefdZd Zy)z0Shared utility functions for Cloud SCC commands.    )absolute_import)division)print_function)unicode_literalsN)errors)
propertiesc                 z   t        j                  d      }d}t        | d      rK| j                  s3t        j
                  j                  j                  j                         }n| j                  }|2t        j
                  j                  j                  j                         }t        | d      rt        | dd      nd}t        | d      rt        | dd      nd}t        | d      rt        | dd      nd}|r|j                  |      rd| n|}n5|r|j                  |      rd| n|}n|r|j                  |      rd	| n|}|t        j                  d
      |j                  |      rd|z   }|j                  d      s9|j                  d      s(|j                  d	      sd}t        j                  |      |S 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.FGD)T*D',3D(,C744(& L) '  &0&6&6w&?y	"WF$.$4$4V$<xx &F^

%
%M  f&F ()			;	'			:	&	  
%
%m
44	-    c                    t        j                  d      }d}t        | d      rK| j                  s3t        j
                  j                  j                  j                         }n| j                  }|2t        j
                  j                  j                  j                         }t        | d      rt        | dd      nd}t        | d      rt        | dd      nd}t        | d      rt        | dd      nd}|r|j                  |      rd| n|}n5|r|j                  |      rd| n|}n|r|j                  |      rd	| n|}|t        | d      r| j                  }|t        | d      r| j                  }|t        j                  d
      |j                  |      rd|z   }|j                  d      s9|j                  d      s(|j                  d	      sd}t        j                  |      |S 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.FGD)T*D',3D(,C744(& L) '  &0&6&6w&?y	"WF$.$4$4V$<xx &F^i0\\F^h/[[F^

%
%M  f&F ()			;	'			:	&	  
%
%m
44	-r"   c                    | j                   d| j                   v rQt        j                  d      }|j                  | j                         st	        j
                  d      | j                   S t        j                  d      }|j                  | j                         st	        j
                  d      d| j                   z   S t        | d      rz| j                  nd| j                  v rQt        j                  d	      }|j                  | j                        st	        j
                  d
      | j                  S d| j                  z   S t        | d      r{| j                  nd| j                  v rQt        j                  d      }|j                  | j                        st	        j
                  d      | j                  S d| j                  z   S yy)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                 &    | j                  dd      S )z2Removes spaces from a field mask provided by user.  )replace)masks    r    CleanUpUserMaskInputr.      s    	c2	r"   c                     d|v }| j                  d      r|rt        j                  d      | j                  d      xs |S )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      sO    +}<	j).G

%
%	- 
 
	!	!*	-	J1JJr"   "version_specific_existing_resourcec                     t        | |      }|r|ryy|rE|D ]@  }| j                  |      r|rt        j                  d      | j                  |      s=|r@ y | j                  dk(  r|ryyy)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
 	
 
	!	!(	+4F $ 
	r"   c                 z   |dk(  r| j                   t        j                  d      }t        j                  d      }|j                  | j                         r| j                   j	                  d      d   S |j                  | j                         r| j                   S t        j                  d      | j                   S )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"   