
    G-                        S r SSKJr  SSKJr  SSKJ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  SSKJr  SSKJr  SSKJr  SSKJr  Sr\R8                  r\R
                  " SSS5      rSr\R@                  " S5      r!1 Sk1 Sk1 Sk1 SkS.r"0 r#\"RI                  5        H  u  r%r&\& H  r'\%\#\''   M
     M     S r(S r)\4S jr*\4S jr+S r,S  r-S! r.S" r/S# r0S$ r1S% r2S& r3g)'z3Common utility functions for all projects commands.    )absolute_import)division)unicode_literalsN)
exceptions)HttpForbiddenError)organizations)projects_api)projects_util)policies)folders)iam_util)log)	resourceszcloudresourcemanager.projectsi  
      zv
    table(
      projectId:sort=1,
      name,
      projectNumber,
      tags.environment:label='ENVIRONMENT'
    )
zo^(?:(?:[-a-z0-9]{1,63}\.)*(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?):)?(?:(?:[a-z0-9](?:[-a-z0-9]{0,61}[a-z0-9])?))$>   Prodprod
Production
production>   DevdevDevelopmentdevelopment>   qaTesttestTesting
testing QAQuality assurancequality assurance>   StagestageStagingstaging)r   r   r   r#   c                     [        U [        5      (       d  gU R                  5       (       d  [        R	                  U 5      (       a  gg)zDChecks to see if the project string is valid project name or number.FT)
isinstancestrisdigit_VALID_PROJECT_REGEXmatch)projects    /lib/googlecloudsdk/command_lib/projects/util.pyValidateProjectIdentifierr-   W   s6    	GS	!	!__.44W==	    c                 T    [         R                  " [        U 5      5      R                  $ N)r	   GetParseProjectprojectNumber)
project_ids    r,   GetProjectNumberr5   b   s    			,z2	3	A	AAr.   c                     [         R                  R                  5       nUR                  SU5        UR	                  S SU 0[
        S9$ )Ncloudresourcemanager	projectId)params
collection)r   REGISTRYCloneRegisterApiByNameParsePROJECTS_COLLECTION)r4   api_versionregistrys      r,   r2   r2   f   sN     %%'(
3[A	
K,9L 
 
N Nr.   c                     [        U [        5      (       a  U R                  SS 5      OU R                  n[	        X!5      nUR                  5       $ )Nr8   )r&   dictgetr8   r2   SelfLink)resourcer@   r4   refs       r,   ProjectsUriFuncrH   o   sG     
Hd	#	# ll;%)1););  	Z-#	r.   c                 h   ^^ S mS mUU4S jnS nU" U 5       H  nU" U5      (       d  M  Us  $    g)zReturns a candidate id for a new project with the given name.

Args:
  project_name: Human-readable name of the project.

Returns:
  A candidate project id, or 'None' if no reasonable candidate is found.
c                     U R                  5       n [        R                  " SSU [        R                  S9n [        R                  " SSU [        R                  S9n U R	                  S5      R                  S5      n U $ )Nz[^a-z0-9\s/._-] )flagsz	[\s/._-]+-z-0123456789)lowerresubUlstriprstrip)names    r,   SimplifyName IdFromName.<locals>.SimplifyName   s\    ::<D66$b$bdd;D66,T6D;;}%,,S1DKr.   c                      [         R                   R                  5       n SR                  U [        -
  R                  U R
                  5      $ )Nz{}{:02})datetimeutcnowformat_CLOUD_CONSOLE_LAUNCH_DATEdayshour)nows    r,   CloudConsoleNowString)IdFromName.<locals>.CloudConsoleNowString   s9    



"
"
$CS#==CCSXXNNr.   c              3   D   >#    T" U 5      nUS-   T" 5       -   v   Uv   g 7f)NrM    )rT   baser_   rU   s     r,   GenIdsIdFromName.<locals>.GenIds   s*     D *,.
..
Js    c                 >    S[        U 5      s=:*  =(       a    S:*  $ s  $ )N      )len)is    r,   	IsValidIdIdFromName.<locals>.IsValidId   s    A"r.   Nrb   )project_namerd   rk   rj   r_   rU   s       @@r,   
IdFromNamern   w   s<    O ,a||h   
r.   c                  V    [         R                  " SSSS9n U S==   S-  ss'   SU S'   U $ )	z>Returns detailed_help for a remove-iam-policy-binding command.r+   zexample-project-id-1T)	conditionDESCRIPTIONzD One binding consists of a member, a role and an optional condition.zThis command uses the cloudresourcemanager/v1 API. The full documentation for this API can be found at: https://cloud.google.com/resource-managerzAPI REFERENCE)r   (GetDetailedHelpForRemoveIamPolicyBinding)detailed_helps    r,   rr   rr      sL    CC'4- MN 3  
 
r.   c                    A AUR                   R                  nU(       a_  UR                  S5      nSU;  a  UR                  S5        SU;  a  UR                  S5        SR	                  U5      UR                   l        U$ )z?Hook to perserve SetIAMPolicy behavior for declarative surface.,bindingsetag)setIamPolicyRequest
updateMasksplitappendjoin)rG   argsrequestupdate_maskmask_fieldss        r,   SetIamPolicyFromFileHookr      sv    	
++66+ ##C(K$$[  -0XXk-BG*	.r.   c                    / n[         R                  " U 5      n UR                   GH  nUR                  R                  nUR                  R
                  nUS:X  aq  [        U 5      nUR                  SU [         R                  " U5      S.5        U(       a6  [        R                  " U SU5      n	U	 H  n
UR                  SU U
S.5        M     US:X  af  UR                  UU[        R                  " U5      S.5        U(       a6  [        R                  " USU5      n	U	 H  n
UR                  SUU
S.5        M     US:X  d  GM  UR                  UU[        R                  " 5       R                  U5      S.5        U(       d  GM]  [        R                  " USU5      n	U	 H  n
UR                  SUU
S.5        M     GM     U$ ! [         a    [        R                   " S5      ef = f)aT  Get IAM policy for given project and its ancestors.

Args:
  project_id: project id
  include_deny: boolean that represents if we should show the deny policies in
    addition to the grants
  release_track: which release track, include deny is only supported for ALPHA
    or BETA

Returns:
  IAM policy for given project and its ancestors
r+   )typeidpolicyfolderorganizationzKUser is not permitted to access IAM policy for one or more of the ancestors)r	   GetAncestryancestor
resourceIdr   r   r2   r{   GetIamPolicyr   ListDenyPoliciesr   r   Clientr   r   #AncestorsIamPolicyAccessDeniedError)r4   include_denyrelease_trackiam_policiesancestryrF   resource_typeresource_idproject_refdeny_policiesdeny_policys              r,   GetIamPolicyWithAncestorsr      s    ,%%j1(8%%))..m''**k	)	#":."//<
 	
 "33J	4AC-*k! %!  + 
(	"!**;7
 	
 "33K4AC-*k !%!  + 
.	(!#**,99+F
 	
 <"33K4AC-*k&!%!  +Y &f 	 

8
8	 s   D#G  <G  <G   !G!c                 R    U R                    H  nUR                  S:X  d  M  Us  $    g)z,Returns the environment tag for the project.environmentN)additionalPropertieskey)tagstags     r,   GetEnvironmentTagr     s(    &&c
ww-j ' 
r.   c                 .    [         R                  U S 5      $ r0   )&_ENV_VARIANT_TO_STANDARD_VALUE_MAPPINGrD   )values    r,   GetStandardEnvironmentValuer     s    	/	3	3E4	@@r.   c                 
    [        U 5      n[        R                  " U5      n[        U5        g! [        R
                  [        R                  4 a/  n[        R                  " SR                  X5      5         SnAgSnAff = f)zAChecks for environment tag and prints a message given project ID.z4Unable to get environment tag for project [{0}]: {1}N)r2   r	   r1   -CheckAndPrintEnvironmentTagMessageWithProjectapitools_exceptions	HttpErrorcore_exceptionsErrorr   inforZ   )r4   r   r+   es       r,   /CheckAndPrintEnvironmentTagMessageWithProjectIDr     sh    	z*K{+G1':

'
')>)>	? HH>	
	 s   ,/ $B%A==Bc                    U R                   (       a  [        U R                   5      OSnU(       a  [        UR                  5      nU(       a<  [        R
                  " SR                  U R                  UR                  U5      5        g[        R
                  " SR                  U R                  5      5        g[        R
                  " SR                  U R                  5      5        g)z0Checks for environment tag and prints a message.NzProject '{0}' is tagged as 'environment: {1}'. Making changes could affect your '{2}' apps. If incorrect, you can update it by managing the tag binding for the 'environment' key using `gcloud resource-manager tags bindings create`.zProject '{0}' has an 'environment' tag key with invalid value. Use either 'Production', 'Development', 'Test', or 'Staging'. Add an 'environment' tag using `gcloud resource-manager tags bindings create`.zProject '{0}' has no 'environment' tag set. Use either 'Production', 'Development', 'Test', or 'Staging'. Add an 'environment' tag using `gcloud resource-manager tags bindings create`.)r   r   r   r   r   r   rZ   r8   )r+   env_tagenv_standard_values      r,   r   r   *  s    /6||gll+'4W]]C	hh= >DV0B>	 
hh$ %+F7+<+<$=	 HH	 F7,,-	r.   )4__doc__
__future__r   r   r   rX   rO   apitools.base.pyr   r   apitools.base.py.exceptionsr   +googlecloudsdk.api_lib.cloudresourcemanagerr   r	   r
   googlecloudsdk.api_lib.iamr   'googlecloudsdk.api_lib.resource_managerr   googlecloudsdk.command_lib.iamr   #googlecloudsdk.command_lib.projectsgooglecloudsdk.corer   r   r   r?   DEFAULT_API_VERSIONPROJECTS_API_VERSIONr[   LIST_FORMATcompiler)   &_ENV_STANDARD_TO_VARIANT_VALUE_MAPPINGr   itemsstandard_valuevariant_valuesvariant_valuer-   r5   r2   rH   rn   rr   r   r   r   r   r   r   rb   r.   r,   <module>r      s"   : &  '  	 > : E D E / ; 3 : = # )5 $88 %..tR<  zz	
 & ??7 8
* & *, & ,113%m<J*=9 & 4
B *> N +? )X $HVAr.   