
    3                     Z   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mZ ddl	m
Z
 ddlmZ ej                  Zd	d	d
efdZ	 	 	 	 d dZ	 	 	 d!dZd ZedfdZd	d	d	d	efdZefdZefdZd	d	d	efdZefdZefdZd	efdZefdZefdZefdZefdZefdZefdZefdZ efdZ!y	)"zGUseful commands for interacting with the Cloud Resource Management API.    )absolute_import)division)unicode_literals)
list_pager)projects_util)folders)iam_utilN  c                     t        j                  |      }t        j                  |      }t        j                  |j
                  |j                  t        |            || dd      S )a  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           	          t        j                  |      }t        j                  |      }t        j                  |j
                  |j                  |      d|| dd      S )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!   :   sc      "";/&&&{3(		!	!oo88 9 %
' 'r   c                     t        j                  d      }t        j                  d      }t        j                  |j
                  |j                  |      || dd      S )ax  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'   W   s`     ""4(&&&t,(		!	!oo66 7 %
' 'r   c                 6    | syd| v r| S dj                  |       S )NzlifecycleState:ACTIVElifecycleStatezlifecycleState:ACTIVE AND ({}))format)filter_exprs    r   r   r   q   s'    	"$	)	0	0	==r   Fc                     t        j                  |      }|rd|_        |j                  j	                  |j
                  j                  | j                              S )zGet project information.N	projectId)r   r   check_response_funcr   GetMESSAGES_MODULE&CloudresourcemanagerProjectsGetRequestr.   )project_refr   disable_api_enablement_checkr   s       r   r0   r0   y   s[     "";/& "!%F			CC)) D +
, ,r   c           	          t        j                  |      }t        j                  |      }|j                  j	                  |j                  | j                         |r|n| j                         |||            S )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)r3   display_namer&   r7   r8   r   r   r   s           r   r9   r9      ss    ( "";/&&&{3(			$$&+|1A1A1C  
 r   c                    t        j                  |      }t        j                  |      }|j                  j	                  |j                  | j                                      t        j                  | j                               S )zDelete an existing project.r-   )r   r   r   r   Delete)CloudresourcemanagerProjectsDeleteRequestr;   DeletedResourcer3   r   r   r   s       r   r>   r>      so    "";/&&&{3(//88$$& 9 () 
	&	&{'7'7'9	::r   c                    t        j                  |      }t        j                  |      }|j                  j	                  |j                  | j                                      t        j                  | j                               S )z)Undelete a project that has been deleted.r-   )r   r   r   r   Undelete+CloudresourcemanagerProjectsUndeleteRequestr;   r@   rA   s       r   rC   rC      so    "";/&&&{3(//::$$& ; () 
	&	&{'7'7'9	::r   c                    t        j                  |      }t        j                  |      }|j                  j	                  |j
                  j                  | j                              }|r||_        |r||_	        |rM|j                  |j                  j                  |j                        }|j                  r|j                  |_        |j                  j                  |      S )zUpdate project information.r-   )r   r   r   r   r0   r1   r2   r.   r6   r&   Applyr:   LabelsValuer7   needs_updateUpdate)	r3   r6   r&   labels_diffr   r   r   projectlabels_updates	            r   rI   rI      s     "";/&&&{3(OOCC)) D +,' 
GLGN%%h&6&6&B&B&-nn6M!!$++gn				((r   c                 *   t        j                  |      }t        j                  |      }|j                  |j	                  |j                  t        j                              | j                               }|j                  j                  |      S )z#Get IAM policy for a given project.)requestedPolicyVersion)options)getIamPolicyRequestresource)r   r   r   /CloudresourcemanagerProjectsGetIamPolicyRequestGetIamPolicyRequestGetPolicyOptionsr	   !MAX_LIBRARY_IAM_SUPPORTED_VERSIONr;   r   GetIamPolicy)r3   r   r   r   policy_requests        r   rV   rV      s    "";/&&&{3(KK"66++88 , : 7 ; ! L . 
	%	%n	55r   c                     t        j                  |      }t        j                  |      }|j                  |j	                         |       }|j
                  j                  |      S )z!Get ancestry for a given project.)getAncestryRequestr.   )r   r   r   .CloudresourcemanagerProjectsGetAncestryRequestGetAncestryRequestr   GetAncestry)
project_idr   r   r   ancestry_requests        r   r\   r\      s`    "";/&&&{3(LL!446 M 
 
	$	$%5	66r   c                 .   t        j                  |      }t        j                  |      }t        j                  |_        |j                  |      }|||_        |j                  | j                         |      }|j                  j                  |      S )z$Set IAM policy, for a given project.)policy)rQ   setIamPolicyRequest)r   r   r   r	   rU   versionSetIamPolicyRequest
updateMask/CloudresourcemanagerProjectsSetIamPolicyRequestr;   r   SetIamPolicy)r3   r`   update_maskr   r   r   set_iam_policy_requestrW   s           r   rf   rf      s    
 "";/&&&{3(==&.#77v7F(3%KK!0 L 2. 
	%	%n	55r   c                     t        j                  |      }t        j                  ||j                        \  }}d|vr|dz  }d|vr|dz  }t        | |||      S )z1Read projects IAM policy from a file, and set it.bindingsz	,bindingsetagz,etag)r   r   r	   ParsePolicyFileWithUpdateMaskPolicyrf   )r3   policy_filer   r   r`   rg   s         r   SetIamPolicyFromFilero     si     &&{3( >>8??$&+
 {";K;7K	k6;	DDr   c                 "    t        | ||fg|      S )N)AddIamPolicyBindings)r3   memberroler   s       r   AddIamPolicyBindingrt     s     
kVTN+;[	IIr   c                     t        j                  |      }t        | |      }|D ]'  \  }}t        j                  |j
                  |||       ) t        | ||      S )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   rV   r	   AddBindingToIamPolicyBindingrf   )r3   member_rolesr   r   r`   rr   rs   s          r   rq   rq      sW     &&{3([1&"lfd""8#3#3VVTJ #	k6{	CCr   c                     t        j                  |      }t        | |      }t        j                  |j
                  |j                  ||||       t        | ||      S )z;Add iam binding with condition to project_ref's iam policy.rv   )r   r   rV   r	   "AddBindingToIamPolicyWithConditionrx   Exprrf   )r3   rr   rs   	conditionr   r   r`   s          r    AddIamPolicyBindingWithConditionr~   5  sU     &&{3(=&
--h.>.>.4fdIO	k6{	CCr   c                 f    t        | |      }t        j                  |||       t        | ||      S )Nrv   )rV   r	   RemoveBindingFromIamPolicyrf   )r3   rr   rs   r   r`   s        r   RemoveIamPolicyBindingr   C  s1     =&
%%ffd;	k6{	CCr   c                 j    t        | |      }t        j                  |||||       t        | ||      S )z@Remove iam binding with condition from project_ref's iam policy.rv   )rV   r	   'RemoveBindingFromIamPolicyWithConditionrf   )r3   rr   rs   r}   all_conditionsr   r`   s          r   #RemoveIamPolicyBindingWithConditionr   L  s9     =&
226643<nN	k6{	CCr   c                     t        j                  |      }t        j                  |      }|j                  | j	                         |j                  |            }|j                  j                  |      S )zJReturn a subset of the given permissions that a caller has on project_ref.)permissions)rQ   testIamPermissionsRequest)r   r   r   5CloudresourcemanagerProjectsTestIamPermissionsRequestr;   TestIamPermissionsRequestr   TestIamPermissions)r3   r   r   r   r   requests         r   r   r   Y  st     "";/&&&{3(JJ! ( B B! !C !# K $' 
	+	+G	44r   c                    t        j                  |      }| sy | j                  d      r&|j                  t	        j
                  |       d      S | j                  d      r|j                  | t        d      d  d      S y )Nzfolders/folder)idtypezorganizations/organization)r   r   
startswith
ResourceIdr   FolderNameToIdlen)parent_namer   r   s      r   ParentNameToResourceIdr   g  s    &&{3(	j)!!+.X  ? ?./s+,-.^  E E 0r   )NNr
   r%   )Nr
   N)"__doc__
__future__r   r   r   apitools.base.pyr   +googlecloudsdk.api_lib.cloudresourcemanagerr   'googlecloudsdk.api_lib.resource_managerr   googlecloudsdk.command_lib.iamr	   DEFAULT_API_VERSIONr   r!   r'   r   r0   r9   r>   rC   rI   rV   r\   rf   ro   rt   rq   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   