B
    uf3                 @   s`   d Z ddlZddlZddlZddlZdZdZdZdZdZ	G dd	 d	e
ZG d
d dejjZdS )a6  Non-API-specific IAM policy definitions

For allowed roles / permissions, see:
https://cloud.google.com/iam/docs/understanding-roles

Example usage:

.. code-block:: python

   # ``get_iam_policy`` returns a :class:'~google.api_core.iam.Policy`.
   policy = resource.get_iam_policy(requested_policy_version=3)

   phred = "user:phred@example.com"
   admin_group = "group:admins@groups.example.com"
   account = "serviceAccount:account-1234@accounts.example.com"

   policy.version = 3
   policy.bindings = [
       {
           "role": "roles/owner",
           "members": {phred, admin_group, account}
       },
       {
           "role": "roles/editor",
           "members": {"allAuthenticatedUsers"}
       },
       {
           "role": "roles/viewer",
           "members": {"allUsers"}
           "condition": {
               "title": "request_time",
               "description": "Requests made before 2021-01-01T00:00:00Z",
               "expression": "request.time < timestamp("2021-01-01T00:00:00Z")"
           }
       }
   ]

   resource.set_iam_policy(policy)
    Nzroles/ownerzroles/editorzroles/viewerz_Assigning to '{}' is deprecated. Use the `policy.bindings` property to modify bindings instead.zWDict access is not supported on policies with version > 1 or with conditional bindings.c               @   s   e Zd ZdZdS )InvalidOperationExceptionz1Raised when trying to use Policy class as a dict.N)__name__
__module____qualname____doc__ r   r   U/home/ankuromar296_gmail_com/myenv/lib/python3.7/site-packages/google/api_core/iam.pyr   M   s   r   c               @   s(  e Zd ZdZefZefZefZ	d/ddZ
dd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Z d-d. Z!dS )0Policya1  IAM Policy

    Args:
        etag (Optional[str]): ETag used to identify a unique of the policy
        version (Optional[int]): The syntax schema version of the policy.

    Note:
        Using conditions in bindings requires the policy's version to be set
        to `3` or greater, depending on the versions that are currently supported.

        Accessing the policy using dict operations will raise InvalidOperationException
        when the policy's version is set to 3.

        Use the policy.bindings getter/setter to retrieve and modify the policy's bindings.

    See:
        IAM Policy https://cloud.google.com/iam/reference/rest/v1/Policy
        Policy versions https://cloud.google.com/iam/docs/policies#versions
        Conditions overview https://cloud.google.com/iam/docs/conditions-overview.
    Nc             C   s   || _ || _g | _d S )N)etagversion	_bindings)selfr
   r   r   r   r   __init__r   s    zPolicy.__init__c             C   s   |    dd | jD S )Nc             s   s   | ]}|d  r|d V  qdS )membersroleNr   ).0bindingr   r   r   	<genexpr>z   s    z"Policy.__iter__.<locals>.<genexpr>)__check_version__r   )r   r   r   r   __iter__w   s    zPolicy.__iter__c             C   s   |    tt|  S )N)r   lenlistr   )r   r   r   r   __len__|   s    zPolicy.__len__c             C   sL   |    x"| jD ]}|d |kr|d S qW |t d}| j| |d S )Nr   r   )r   r   )r   r   setappend)r   keybnew_bindingr   r   r   __getitem__   s    zPolicy.__getitem__c             C   sN   |    t|}x&| jD ]}|d |kr||d< d S qW | j||d d S )Nr   r   )r   r   )r   r   r   r   )r   r   valuer   r   r   r   __setitem__   s    zPolicy.__setitem__c             C   s@   |    x*| jD ] }|d |kr| j| d S qW t|d S )Nr   )r   r   removeKeyError)r   r   r   r   r   r   __delitem__   s    zPolicy.__delitem__c             C   s,   | j dk	o| j dk}|s |  r(ttdS )z[Raise InvalidOperationException if version is greater than 1 or policy contains conditions.N   )r   _contains_conditionsr   _DICT_ACCESS_MSG)r   Zraise_versionr   r   r   r      s    zPolicy.__check_version__c             C   s&   x | j D ]}|dd k	rdS qW dS )N	conditionTF)r   get)r   r   r   r   r   r%      s    zPolicy._contains_conditionsc             C   s   | j S )aE  The policy's list of bindings.

        A binding is specified by a dictionary with keys:

        * role (str): Role that is assigned to `members`.

        * members (:obj:`set` of str): Specifies the identities associated to this binding.

        * condition (:obj:`dict` of str:str): Specifies a condition under which this binding will apply.

          * title (str): Title for the condition.

          * description (:obj:str, optional): Description of the condition.

          * expression: A CEL expression.

        Type:
           :obj:`list` of :obj:`dict`

        See:
           Policy versions https://cloud.google.com/iam/docs/policies#versions
           Conditions overview https://cloud.google.com/iam/docs/conditions-overview.

        Example:

        .. code-block:: python

           USER = "user:phred@example.com"
           ADMIN_GROUP = "group:admins@groups.example.com"
           SERVICE_ACCOUNT = "serviceAccount:account-1234@accounts.example.com"
           CONDITION = {
               "title": "request_time",
               "description": "Requests made before 2021-01-01T00:00:00Z", # Optional
               "expression": "request.time < timestamp("2021-01-01T00:00:00Z")"
           }

           # Set policy's version to 3 before setting bindings containing conditions.
           policy.version = 3

           policy.bindings = [
               {
                   "role": "roles/viewer",
                   "members": {USER, ADMIN_GROUP, SERVICE_ACCOUNT},
                   "condition": CONDITION
               },
               ...
           ]
        )r   )r   r   r   r   bindings   s    2zPolicy.bindingsc             C   s
   || _ d S )N)r   )r   r)   r   r   r   r)      s    c             C   s>   t  }x.| jD ]$}x| |dD ]}|| q W qW t|S )zLegacy access to owner role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r   )r   _OWNER_ROLESr(   add	frozenset)r   resultr   memberr   r   r   owners   s
    zPolicy.ownersc             C   s    t tdtt || t< dS )zUpdate owners.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r/   N)warningswarn_ASSIGNMENT_DEPRECATED_MSGformat
OWNER_ROLEDeprecationWarning)r   r   r   r   r   r/      s    c             C   s>   t  }x.| jD ]$}x| |dD ]}|| q W qW t|S )zLegacy access to editor role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r   )r   _EDITOR_ROLESr(   r+   r,   )r   r-   r   r.   r   r   r   editors   s
    zPolicy.editorsc             C   s    t tdtt || t< dS )zUpdate editors.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        r7   N)r0   r1   r2   r3   EDITOR_ROLEr5   )r   r   r   r   r   r7     s    
c             C   s>   t  }x.| jD ]$}x| |dD ]}|| q W qW t|S )zLegacy access to viewer role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        r   )r   _VIEWER_ROLESr(   r+   r,   )r   r-   r   r.   r   r   r   viewers  s
    zPolicy.viewersc             C   s    t tdtt || t< dS )zUpdate viewers.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        r:   N)r0   r1   r2   r3   VIEWER_ROLEr5   )r   r   r   r   r   r:   (  s    
c             C   s
   d| f S )zFactory method for a user member.

        Args:
            email (str): E-mail for this particular user.

        Returns:
            str: A member string corresponding to the given user.
        zuser:%sr   )emailr   r   r   user6  s    
zPolicy.userc             C   s
   d| f S )zFactory method for a service account member.

        Args:
            email (str): E-mail for this particular service account.

        Returns:
            str: A member string corresponding to the given service account.

        zserviceAccount:%sr   )r<   r   r   r   service_accountB  s    zPolicy.service_accountc             C   s
   d| f S )zFactory method for a group member.

        Args:
            email (str): An id or e-mail for this particular group.

        Returns:
            str: A member string corresponding to the given group.
        zgroup:%sr   )r<   r   r   r   groupO  s    
zPolicy.groupc             C   s
   d| f S )zFactory method for a domain member.

        Args:
            domain (str): The domain for this member.

        Returns:
            str: A member string corresponding to the given domain.
        z	domain:%sr   )domainr   r   r   r@   [  s    
zPolicy.domainc               C   s   dS )zFactory method for a member representing all users.

        Returns:
            str: A member string representing all users.
        ZallUsersr   r   r   r   r   	all_usersg  s    zPolicy.all_usersc               C   s   dS )zFactory method for a member representing all authenticated users.

        Returns:
            str: A member string representing all authenticated users.
        ZallAuthenticatedUsersr   r   r   r   r   authenticated_usersp  s    zPolicy.authenticated_usersc             C   sT   | d}| d}| ||}| dg |_x"|jD ]}t| dd|d< q4W |S )zFactory: create a policy from a JSON resource.

        Args:
            resource (dict): policy resource returned by ``getIamPolicy`` API.

        Returns:
            :class:`Policy`: the parsed policy
        r   r
   r)   r   r   )r(   r)   r   )clsresourcer   r
   policyr   r   r   r   from_api_repry  s    



zPolicy.from_api_reprc             C   s   i }| j dk	r| j |d< | jdk	r,| j|d< | jrt| jdkrg }xN| jD ]D}|d}|rL|d t|d}|d}|r||d< || qLW |rtd}t||d	|d
< |S )zRender a JSON policy resource.

        Returns:
            dict: a resource to be passed to the ``setIamPolicy`` API.
        Nr
   r   r   r   r   )r   r   r'   )r   r)   )	r
   r   r   r   r(   sortedr   operator
itemgetter)r   rD   r)   r   r   r   r'   r   r   r   r   to_api_repr  s&    






zPolicy.to_api_repr)NN)"r   r   r   r   r4   r*   r8   r6   r;   r9   r   r   r   r   r    r#   r   r%   propertyr)   setterr/   r7   r:   staticmethodr=   r>   r?   r@   rA   rB   classmethodrF   rJ   r   r   r   r   r	   S   s8   
	4		r	   )r   collectionscollections.abcrH   r0   r4   r8   r;   r2   r&   	Exceptionr   abcMutableMappingr	   r   r   r   r   <module>4   s   