
                         r    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  G d	 d
e      Zy)z3API library for cloudresourcemanager organizations.    )absolute_import)division)unicode_literals)
exceptions)
list_pager)projects_util)iam_utilc                   :    e Zd ZdZd	dZd
dZddZd Zd Zd Z	y)Clientz8Client class for cloudresourcemanager organizations API.Nc                 |    |xs t        j                         | _        |xs | j                  j                  | _        y N)r   	GetClientclientMESSAGES_MODULEmessages)selfr   r   s      @lib/googlecloudsdk/api_lib/cloudresourcemanager/organizations.py__init__zClient.__init__   s-    5M335DK; ; ;DM    c           	          | j                   j                  |      }t        j                  | j                  j
                  |d|d|d      S )N)filterSearchpageSizeorganizations)methodlimitbatch_size_attribute
batch_sizefield)r   SearchOrganizationsRequestr   YieldFromListr   r   )r   filter_r   	page_sizereqs        r   ListzClient.List#   sJ    
--
2
2'
2
BC##!!3' r   c                     | j                   j                  j                  | j                   j                  j	                  |            S )zReturns an Organization resource identified by the specified organization id.

    Args:
      organization_id: organization id

    Returns:
      An instance of Organization
    )organizationsId)r   r   Getr   +CloudresourcemanagerOrganizationsGetRequest)r   organization_ids     r   r(   z
Client.Get-   sB     ;;$$((##OO+ 	P 	-. .r   c                     dj                  |      }	 t        | j                  |            }t        |      dk(  r|d   S y# t        j                  $ r Y yw xY w)a  Returns an Organization resource identified by the domain name.

    If no organization is returned, or if more than one organization is
    returned, this method will return None.

    Args:
      domain: A string representing an organizations associated domain.
              e.g. 'example.com'

    Returns:
      An instance of Organization or None if a unique organization cannot be
      determined.
    z
domain:{0})r"   N   r   )formatlistr%   r   HttpBadRequestErrorlen)r   domaindomain_filter	orgs_lists       r   GetByDomainzClient.GetByDomain:   sc     !''/Mtyyy78i 9~q\ )) s   A AAc                 
   | j                   j                  | j                   j                  | j                   j                  t        j
                              |      }| j                  j                  j                  |      S )z|Returns IAM policy for a organization.

    Args:
      organization_id: organization id

    Returns:
      IAM policy
    )requestedPolicyVersion)options)getIamPolicyRequestr'   )	r   4CloudresourcemanagerOrganizationsGetIamPolicyRequestGetIamPolicyRequestGetPolicyOptionsr	   !MAX_LIBRARY_IAM_SUPPORTED_VERSIONr   r   GetIamPolicy)r   r*   requests      r   r=   zClient.GetIamPolicyR   sw     mmPP MM==MM22'/22 3 4 > 5 ( Q )G ;;$$11'::r   c                    t        j                  || j                  j                        \  }}t         j                  |_        d|vr|dz  }d|vr|dz  }| j                  j                  ||      }| j                  j                  ||      }| j                  j                  j                  |      }t        j                  |d       |S )zSets the IAM policy for an organization.

    Args:
      organization_id: organization id.
      policy_file: A JSON or YAML file containing the IAM policy.

    Returns:
      The output from the SetIamPolicy API call.
    bindingsz	,bindingsetagz,etag)policy
updateMask)r'   setIamPolicyRequestorganization)r	   ParsePolicyFileWithUpdateMaskr   Policyr<   versionSetIamPolicyRequest4CloudresourcemanagerOrganizationsSetIamPolicyRequestr   r   SetIamPolicyLogSetIamPolicy)r   r*   policy_filerB   update_maskset_iam_policy_requestpolicy_requestresults           r   rK   zClient.SetIamPolicyd   s     #@@T]]))+FK??FN $[ k[ Wk!]]>> ?  
 	JJ+ 6 	K 	8  [[&&33NCF_n=Mr   )NN)NNNr   )
__name__
__module____qualname____doc__r   r%   r(   r4   r=   rK    r   r   r   r      s#    @<.0;$ r   r   N)rU   
__future__r   r   r   apitools.base.pyr   r   +googlecloudsdk.api_lib.cloudresourcemanagerr   googlecloudsdk.command_lib.iamr	   objectr   rV   r   r   <module>r\      s-    : &  ' ' ' E 3hV hr   