
    !                     T   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddl	mZ dd	l	mZ dd
lm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Zej2                  Z ej
                  ddd      ZdZ ej:                  d      Zd Zd Z efdZ!efdZ"d Z#d Z$d Z%d Z&y)z3Common utility functions for all projects commands.    )absolute_import)division)unicode_literalsN)HttpForbiddenError)organizations)projects_api)projects_util)policies)folders)iam_util)
exceptions)	resourceszcloudresourcemanager.projectsi  
      zJ
    table(
      projectId:sort=1,
      name,
      projectNumber
    )
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                     t        | t        j                        sy| j                         st        j                  |       ryy)zDChecks to see if the project string is valid project name or number.FT)
isinstancesixstring_typesisdigit_VALID_PROJECT_REGEXmatch)projects    /lib/googlecloudsdk/command_lib/projects/util.pyValidateProjectIdentifierr   ?   s3    	GS--	.__.44W=	    c                 R    t        j                  t        |             j                  S )N)r   GetParseProjectprojectNumber)
project_ids    r   GetProjectNumberr!   J   s    			,z2	3	A	AAr   c                     t         j                  j                         }|j                  d|       |j	                  d d| it
              S )Ncloudresourcemanager	projectId)params
collection)r   REGISTRYCloneRegisterApiByNameParsePROJECTS_COLLECTION)r    api_versionregistrys      r   r   r   N   sN     %%'(
3[A	
K,9L 
 
N Nr   c                     t        | t              r| j                  dd       n| j                  }t	        ||      }|j                         S )Nr$   )r   dictgetr$   r   SelfLink)resourcer,   r    refs       r   ProjectsUriFuncr4   W   sF     
Hd	# ll;%)1););  	Z-#	r   c                 X    d d fd}d } ||       D ]  } ||      s|c S  y)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                    | j                         } t        j                  dd| t        j                        } t        j                  dd| t        j                        } | j	                  d      j                  d      } | S )Nz[^a-z0-9\s/._-] )flagsz	[\s/._-]+-z-0123456789)lowerresubUlstriprstrip)names    r   SimplifyNamez IdFromName.<locals>.SimplifyNamei   s\    ::<D66$b$bdd;D66,T6D;;}%,,S1DKr   c                      t         j                   j                         } dj                  | t        z
  j                  | j
                        S )Nz{}{:02})datetimeutcnowformat_CLOUD_CONSOLE_LAUNCH_DATEdayshour)nows    r   CloudConsoleNowStringz)IdFromName.<locals>.CloudConsoleNowStringp   s9    



"
"
$CS#==CCSXXNNr   c              3   D   K    |       }|dz           z    | y w)Nr9    )r@   baserJ   rA   s     r   GenIdszIdFromName.<locals>.GenIdst   s+     D *,.
..
Js    c                 4    dt        |       cxk  xr dk  S c S )N      )len)is    r   	IsValidIdzIdFromName.<locals>.IsValidId   s    A"r   NrL   )project_namerN   rT   rS   rJ   rA   s       @@r   
IdFromNamerV   _   s:    O ,a|h   
r   c                  Z    t        j                  ddd      } | dxx   dz  cc<   d| d<   | S )	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   rZ   rZ      sL    CC'4- MN 3  
 
r   c                     ~ ~|j                   j                  }|r[|j                  d      }d|vr|j                  d       d|vr|j                  d       dj	                  |      |j                   _        |S )z?Hook to perserve SetIAMPolicy behavior for declarative surface.,bindingsetag)setIamPolicyRequest
updateMasksplitappendjoin)r3   argsrequestupdate_maskmask_fieldss        r   SetIamPolicyFromFileHookri      sv    	
++66+ ##C(K$$[  -0XXk-BG*	.r   c                 ~   g }t        j                  |       }	 |j                  D ]r  }|j                  j                  }|j                  j
                  }|dk(  rht        |       }|j                  d| t        j                  |      d       |r3t        j                  | d|      }	|	D ]  }
|j                  d| |
d        |dk(  r]|j                  ||t        j                  |      d       |r3t        j                  |d|      }	|	D ]  }
|j                  d||
d        |dk(  s|j                  ||t        j                         j                  |      d       |s@t        j                  |d|      }	|	D ]  }
|j                  d||
d        u |S # t        $ r t        j                   d      w xY w)af  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
resourceIdrk   rl   r   rc   GetIamPolicyr
   ListDenyPoliciesr   r   Clientr   r   #AncestorsIamPolicyAccessDeniedError)r    include_denyrelease_trackiam_policiesancestryr2   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F +8F %7F F<)'__doc__
__future__r   r   r   rC   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.projectsr   googlecloudsdk.corer   r   r+   DEFAULT_API_VERSIONPROJECTS_API_VERSIONrF   LIST_FORMATcompiler   r   r!   r   r4   rV   rZ   ri   r   rL   r   r   <module>r      s    : &  '  	 : E D E / ; 3 : ) 
5 $88 .X..tR<  "rzz	
 "B *> N +? )X $Hr   