
    3                     \   S r SSKJr  SSKJr  SSKJr  SSKJr  \R                  r	SSS\	4S jr
    SS	 jr   SS
 jrS r\	S4S jrSSSS\	4S jr\	4S jr\	4S jrSSS\	4S jr\	4S jr\	4S jrS\	4S jr\	4S jr\	4S jr\	4S jr\	4S jr\	4S jr\	4S jr\	4S jr\	4S jrg)zGUseful commands for interacting with the Cloud Resource Management API.    )
list_pager)projects_util)folders)iam_utilN  c           	          [         R                  " U5      n[         R                  " U5      n[        R                  " UR
                  UR                  [        U5      S9UU SSS9$ )an  Make API calls to List active projects.

Args:
  limit: The number of projects to limit the results to. This limit is passed
    to the server and the server does the limiting.
  filter: The client side filter expression.
  batch_size: the number of projects to get with each request.
  api_version: the version of the api

Returns:
  Generator that yields projects
)filterprojectspageSize
batch_sizelimitfieldbatch_size_attribute)r   	GetClientGetMessagesr   YieldFromListr
   'CloudresourcemanagerProjectsListRequest%_AddActiveProjectFilterIfNotSpecified)r   r	   r   api_versionclientmessagess         ?lib/googlecloudsdk/api_lib/cloudresourcemanager/projects_api.pyListr      sf      "";/&&&{3(		!	!oo666v> 7 @%
' '    c           
          [         R                  " U5      n[         R                  " U5      n[        R                  " UR
                  UR                  US9SUU SSS9$ )a  Make API calls to search projects for which the user has resourcemanager.projects.get permission.

Args:
  limit: The number of projects to limit the results to. This limit is passed
    to the server and the server does the limiting.
  query: The server side filter expression.
  batch_size: The number of projects to get with each request.
  api_version: The version of the api.

Returns:
  Generator that yields projects.
)querySearchr
   r   )methodr   r   r   r   )r   r   r   r   r   r
   )CloudresourcemanagerProjectsSearchRequest)r   r   r   r   r   r   s         r   r   r   7   sc      "";/&&&{3(		!	!oo88 9 %
' 'r   c           	          [         R                  " S5      n[         R                  " S5      n[        R                  " UR
                  UR                  US9UU SSS9$ )ah  Make API calls to List active projects.

Args:
  limit: The number of projects to limit the results to. This limit is passed
    to the server and the server does the limiting.
  batch_size: the number of projects to get with each request.
  parent: The parent folder or organization whose children are to be listed.

Returns:
  Generator that yields projects
v3)parentr
   r   r   )r   r   r   r   r   r
   r   )r   r   r#   r   r   s        r   ListV3r$   T   s`     ""4(&&&t,(		!	!oo66 7 %
' 'r   c                 D    U (       d  gSU ;   a  U $ SR                  U 5      $ )NzlifecycleState:ACTIVElifecycleStatezlifecycleState:ACTIVE AND ({}))format)filter_exprs    r   r   r   n   s'    	"$	)	0	0	==r   Fc                     [         R                  " U5      nU(       a  SUl        UR                  R	                  UR
                  R                  U R                  S95      $ )zGet project information.N	projectId)r   r   check_response_funcr
   GetMESSAGES_MODULE&CloudresourcemanagerProjectsGetRequestr+   )project_refr   disable_api_enablement_checkr   s       r   r-   r-   v   s[     "";/& "!%F			CC)) D +
, ,r   c           
          [         R                  " U5      n[         R                  " U5      nUR                  R	                  UR                  U R                  5       U(       a  UOU R                  5       UUUS95      $ )a  Create a new project.

Args:
  project_ref: The identifier for the project
  display_name: Optional display name for the project
  parent: Optional for the project (ex. folders/123 or organizations/5231)
  labels: Optional labels to apply to the project
  tags: Optional tags to bind to the project
  api_version: the version of the api

Returns:
  An Operation object which can be used to check on the progress of the
  project creation.
)r+   namer#   labelstags)r   r   r   r
   CreateProjectName)r0   display_namer#   r4   r5   r   r   r   s           r   r6   r6      ss    ( "";/&&&{3(			$$&+|1A1A1C  
 r   c                    [         R                  " U5      n[         R                  " U5      nUR                  R	                  UR                  U R                  5       S95        [         R                  " U R                  5       5      $ )zDelete an existing project.r*   )r   r   r   r
   Delete)CloudresourcemanagerProjectsDeleteRequestr8   DeletedResourcer0   r   r   r   s       r   r;   r;      so    "";/&&&{3(//88$$& 9 () 
	&	&{'7'7'9	::r   c                    [         R                  " U5      n[         R                  " U5      nUR                  R	                  UR                  U R                  5       S95        [         R                  " U R                  5       5      $ )z)Undelete a project that has been deleted.r*   )r   r   r   r
   Undelete+CloudresourcemanagerProjectsUndeleteRequestr8   r=   r>   s       r   r@   r@      so    "";/&&&{3(//::$$& ; () 
	&	&{'7'7'9	::r   c                    [         R                  " U5      n[         R                  " U5      nUR                  R	                  UR
                  R                  U R                  S95      nU(       a  Xl        U(       a  X'l	        U(       aR  UR                  UR                  R                  UR                  5      nUR                  (       a  UR                  Ul        UR                  R                  U5      $ )zUpdate project information.r*   )r   r   r   r
   r-   r.   r/   r+   r3   r#   Applyr7   LabelsValuer4   needs_updateUpdate)	r0   r3   r#   labels_diffr   r   r   projectlabels_updates	            r   rF   rF      s     "";/&&&{3(OOCC)) D +,' 
LN%%h&6&6&B&B&-nn6M!!$++gn				((r   c                    [         R                  " U5      n[         R                  " U5      nUR                  UR	                  UR                  [        R                  S9S9U R                  5       S9nUR                  R                  U5      $ )z#Get IAM policy for a given project.)requestedPolicyVersion)options)getIamPolicyRequestresource)r   r   r   /CloudresourcemanagerProjectsGetIamPolicyRequestGetIamPolicyRequestGetPolicyOptionsr   !MAX_LIBRARY_IAM_SUPPORTED_VERSIONr8   r
   GetIamPolicy)r0   r   r   r   policy_requests        r   rS   rS      s    "";/&&&{3(KK"66++88 , : 7 ; ! L . 
	%	%n	55r   c                     [         R                  " U5      n[         R                  " U5      nUR                  UR	                  5       U S9nUR
                  R                  U5      $ )z!Get ancestry for a given project.)getAncestryRequestr+   )r   r   r   .CloudresourcemanagerProjectsGetAncestryRequestGetAncestryRequestr
   GetAncestry)
project_idr   r   r   ancestry_requests        r   rY   rY      s`    "";/&&&{3(LL!446 M 
 
	$	$%5	66r   c                 &   [         R                  " U5      n[         R                  " U5      n[        R                  Ul        UR                  US9nUb  X&l        UR                  U R                  5       US9nUR                  R                  U5      $ )z$Set IAM policy, for a given project.)policy)rN   setIamPolicyRequest)r   r   r   r   rR   versionSetIamPolicyRequest
updateMask/CloudresourcemanagerProjectsSetIamPolicyRequestr8   r
   SetIamPolicy)r0   r]   update_maskr   r   r   set_iam_policy_requestrT   s           r   rc   rc      s    
 "";/&&&{3(==&.#77v7F(3%KK!0 L 2. 
	%	%n	55r   c                     [         R                  " U5      n[        R                  " XR                  5      u  pESU;  a  US-  nSU;  a  US-  n[        XXR5      $ )z1Read projects IAM policy from a file, and set it.bindingsz	,bindingsetagz,etag)r   r   r   ParsePolicyFileWithUpdateMaskPolicyrc   )r0   policy_filer   r   r]   rd   s         r   SetIamPolicyFromFilerl     sa     &&{3( >>??$&
 {";K;7K	k;	DDr   c                      [        XU4/U5      $ )N)AddIamPolicyBindings)r0   memberroler   s       r   AddIamPolicyBindingrq     s     
kTN+;[	IIr   c                     [         R                  " U5      n[        X5      nU H'  u  pV[        R                  " UR
                  XEU5        M)     [        XUS9$ )zAdds iam bindings to project_ref's iam policy.

Args:
  project_ref: The project for the binding
  member_roles: List of 2-tuples of the form [(member, role), ...].
  api_version: The version of the api

Returns:
  The updated IAM Policy
r   )r   r   rS   r   AddBindingToIamPolicyBindingrc   )r0   member_rolesr   r   r]   ro   rp   s          r   rn   rn     sO     &&{3(1&"lf""8#3#3VTJ #	k{	CCr   c                     [         R                  " U5      n[        XS9n[        R                  " UR
                  UR                  XaX#5        [        XUS9$ )z;Add iam binding with condition to project_ref's iam policy.rs   )r   r   rS   r   "AddBindingToIamPolicyWithConditionru   Exprrc   )r0   ro   rp   	conditionr   r   r]   s          r    AddIamPolicyBindingWithConditionr{   2  sM     &&{3(=&
--h.>.>.4dO	k{	CCr   c                 V    [        XS9n[        R                  " XAU5        [        XUS9$ )Nrs   )rS   r   RemoveBindingFromIamPolicyrc   )r0   ro   rp   r   r]   s        r   RemoveIamPolicyBindingr~   @  s+     =&
%%fd;	k{	CCr   c                 X    [        XS9n[        R                  " XaUX45        [        XUS9$ )z@Remove iam binding with condition from project_ref's iam policy.rs   )rS   r   'RemoveBindingFromIamPolicyWithConditionrc   )r0   ro   rp   rz   all_conditionsr   r]   s          r   #RemoveIamPolicyBindingWithConditionr   I  s1     =&
22643<N	k{	CCr   c                     [         R                  " U5      n[         R                  " U5      nUR                  U R	                  5       UR                  US9S9nUR                  R                  U5      $ )zJReturn a subset of the given permissions that a caller has on project_ref.)permissions)rN   testIamPermissionsRequest)r   r   r   5CloudresourcemanagerProjectsTestIamPermissionsRequestr8   TestIamPermissionsRequestr
   TestIamPermissions)r0   r   r   r   r   requests         r   r   r   V  st     "";/&&&{3(JJ! ( B B! !C !# K $' 
	+	+G	44r   c                    [         R                  " U5      nU (       d  g U R                  S5      (       a$  UR                  [        R
                  " U 5      SS9$ U R                  S5      (       a  UR                  U [        S5      S  SS9$ g )Nzfolders/folder)idtypezorganizations/organization)r   r   
startswith
ResourceIdr   FolderNameToIdlen)parent_namer   r   s      r   ParentNameToResourceIdr   d  s    &&{3(	j))!!+.X  ? ?.//s+,-.^  E E 0r   )NNr   r"   )Nr   N)__doc__apitools.base.pyr   +googlecloudsdk.api_lib.cloudresourcemanagerr   'googlecloudsdk.api_lib.resource_managerr   googlecloudsdk.command_lib.iamr   DEFAULT_API_VERSIONr   r   r$   r   r-   r6   r;   r@   rF   rS   rY   rc   rl   rq   rn   r{   r~   r   r   r    r   r   <module>r      s%   N ( E ; 3#77  ('8 ': '4> "5%*
, *> %8 ; ': ; *	)8 +> 6 )< 
7 "06, &9E* %8J &9D2 2E	D" (;D 5H
D $75 5H 	Er   