
    3                        S 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  \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.    )absolute_import)division)unicode_literals)
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!   :   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'   W   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   q   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   r0   r0   y   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)r3   display_namer&   r7   r8   r   r   r   s           r   r9   r9      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)CloudresourcemanagerProjectsDeleteRequestr;   DeletedResourcer3   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+CloudresourcemanagerProjectsUndeleteRequestr;   r@   rA   s       r   rC   rC      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   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 +,' 
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_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                     [         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   r\   r\      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)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                     [         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Policyrf   )r3   policy_filer   r   r`   rg   s         r   SetIamPolicyFromFilero     sa     &&{3( >>??$&
 {";K;7K	k;	DDr   c                      [        XU4/U5      $ )N)AddIamPolicyBindings)r3   memberroler   s       r   AddIamPolicyBindingrt     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   rV   r	   AddBindingToIamPolicyBindingrf   )r3   member_rolesr   r   r`   rr   rs   s          r   rq   rq      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.rv   )r   r   rV   r	   "AddBindingToIamPolicyWithConditionrx   Exprrf   )r3   rr   rs   	conditionr   r   r`   s          r    AddIamPolicyBindingWithConditionr~   5  sM     &&{3(=&
--h.>.>.4dO	k{	CCr   c                 V    [        XS9n[        R                  " XAU5        [        XUS9$ )Nrv   )rV   r	   RemoveBindingFromIamPolicyrf   )r3   rr   rs   r   r`   s        r   RemoveIamPolicyBindingr   C  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.rv   )rV   r	   'RemoveBindingFromIamPolicyWithConditionrf   )r3   rr   rs   r}   all_conditionsr   r`   s          r   #RemoveIamPolicyBindingWithConditionr   L  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)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                    [         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   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   