
    |3              	       z   S r SSK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\R2                  r\R                  " SSS5      rSr\R:                  " S5      r\R>                   " S S\R@                  5      5       r!\!RD                  1 Sk\!RF                  1 Sk\!RH                  1 Sk\!RJ                  1 Sk0r&0 r'\&RQ                  5        H  u  r)r*\* H  r+\)\'\+'   M
     M     S r,S r-\4S jr.\4S jr/S r0S  r1S! r2S" r3S# r4S$ r5S% r6g)&z3Common utility functions for all projects commands.    N)
list_pager)HttpForbiddenError)organizations)projects_api)projects_util)policies)folders)tags)iam_util)
exceptions)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])?))$c                   $    \ rS rSrSrSrSrSrSrg)Environment@   
ProductionDevelopmentTestStaging N)	__name__
__module____qualname____firstlineno__
PRODUCTIONDEVELOPMENTTESTSTAGING__static_attributes__r       /lib/googlecloudsdk/command_lib/projects/util.pyr   r   @   s    *+	$'r"   r   >   Prodprodr   
production>   Devdevr   development>   qar   testTesting
testing QAQuality assurancequality assurance>   Stagestager   stagingc                     [        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    r#   ValidateProjectIdentifierr:   d   s6    	GS	!	!__.44W==	r"   c                 T    [         R                  " [        U 5      5      R                  $ N)r   GetParseProjectprojectNumber)
project_ids    r#   GetProjectNumberrA   o   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)r@   api_versionregistrys      r#   r>   r>   s   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       $ )NrD   )r4   dictgetrD   r>   SelfLink)resourcerL   r@   refs       r#   ProjectsUriFuncrT   |   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)NrY   r   )r`   base_rk   ra   s     r#   GenIdsIdFromName.<locals>.GenIds   s*     E #+-/
//
Ks    c                 >    S[        U 5      s=:*  =(       a    S:*  $ s  $ )N      )len)is    r#   	IsValidIdIdFromName.<locals>.IsValidId   s    A"r"   Nr   )project_namero   rv   ru   rk   ra   s       @@r#   
IdFromNamery      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.r9   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#   r}   r}      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)rS   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
r9   )typeidpolicyfolderorganizationzKUser is not permitted to access IAM policy for one or more of the ancestors)r   GetAncestryancestor
resourceIdr   r   r>   r   GetIamPolicyr   ListDenyPoliciesr	   r   Clientr   r   #AncestorsIamPolicyAccessDeniedError)r@   include_denyrelease_trackiam_policiesancestryrR   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                  " 5       n[        U 5      nSR                  UR	                  5       5      n UR                  US9n[        R                  " [         R                  " 5       USSS9nU Ha  nUR                  R                  S5      (       d  M%  UR                  R                  S5      S   UR                  R                  S5      S   4s  $    g! [         a/  n[        R                  " S	R                  X5      5         S
nAgS
nAff = f)z/Returns the environment tag for the project id.z(//cloudresourcemanager.googleapis.com/{})parentpageSizeeffectiveTags)batch_size_attributefieldz/environment/z4Unable to get environment tag for project [{0}]: {1}N)NN)r
   TagMessagesr>   rf   RelativeName,CloudresourcemanagerEffectiveTagsListRequestr   YieldFromListEffectiveTagsServicenamespacedTagKeyendswithr   namespacedTagValue	Exceptionr   info)r@   messagesr   resource_nameeffective_tags_requesteffective_tags_responseeffective_tages           r#   GetEnvironmentTagr     s   (Z(+<CC -==  	> 	
 
 )66!!#'	 1		'	'	0	0	@	@**005b9,,2237;
 	
 1 
 
 HH>EE	
 
 
s%   AC" !=C" C" "
D,%DDc                 .    [         R                  U S 5      $ r<   )&_ENV_VARIANT_TO_STANDARD_VALUE_MAPPINGrP   )values    r#   GetStandardEnvironmentValuer   A  s    	/	3	3E4	@@r"   c                    [        U 5      u  pU(       d/  [        R                  R                  SR	                  U 5      5        g[        U5      nU(       d/  [        R                  R                  SR	                  U 5      5        gU[        R                  :X  a:  [        R                  R                  SR	                  XUR                  5      5        g[        R                  R                  SR	                  XUR                  5      5        g)z0Prints environment tag message given project ID.ar  Project '{0}' lacks an 'environment' tag. Please create or add a tag with key 'environment' and a value like 'Production', 'Development', 'Test', or 'Staging'. Add an 'environment' tag using `gcloud resource-manager tags bindings create`. See https://cloud.google.com/resource-manager/docs/creating-managing-projects#designate_project_environments_with_tags for details.Na{  Project '{0}' has an 'environment' tag with an unrecognized value. Please use a standard value such as 'Production', 'Development', 'Test', or 'Staging'. You can update the tag value using `gcloud resource-manager tags bindings create`. Refer to https://cloud.google.com/resource-manager/docs/creating-managing-projects#designate_project_environments_with_tags for more guidance.zCaution: Project '{0}' is designated as '{2}'(tagged 'environment: {1}'). Making changes could affect your '{2}' apps. Learn more at https://cloud.google.com/resource-manager/docs/creating-managing-projects#designate_project_environments_with_tagsa  Caution: Project '{0}' is designated as '{2}'(tagged '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`. Learn more at https://cloud.google.com/resource-manager/docs/creating-managing-projects#designate_project_environments_with_tags)	r   r   statusPrintrf   r   r   r   r   )r@   env_tag_keyenv_tag_valueenv_standard_values       r#   PrintEnvironmentTagMessager   E  s    0<+	JJ	
 z* 2 
JJ	 %fZ0 ;111JJ	N 
'9'?'?

 JJ	~ 
'9'?'?


r"   )7__doc__rd   enumr[   apitools.base.pyr   apitools.base.py.exceptionsr   +googlecloudsdk.api_lib.cloudresourcemanagerr   r   r   googlecloudsdk.api_lib.iamr   'googlecloudsdk.api_lib.resource_managerr	   r
   googlecloudsdk.command_lib.iamr   #googlecloudsdk.command_lib.projectsr   googlecloudsdk.corer   r   rK   DEFAULT_API_VERSIONPROJECTS_API_VERSIONrg   LIST_FORMATcompiler7   uniqueEnumr   r   r   r   r    &_ENV_STANDARD_TO_VARIANT_VALUE_MAPPINGr   itemsstandard_valuevariant_valuesvariant_valuer:   rA   r>   rT   ry   r}   r   r   r   r   r   r   r"   r#   <module>r      sa   :   	 ' : E D E / ; 8 3 : # )5 $88 %..tR<  zz	
 " $))   HI  A!* && *, & ,113%m<J*=9 & 4
B *> N +? )X $HV!HA2r"   