
    W#                     8   d Z ddlmZ ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ d	Zd
ej                   dddee   defdZd
ej                   dddee   defdZd
ej                   ddfdZd
ej                   dddeeef   fdZ	 d"d
ej                   dddefdZd
ej                   dddededeeee   f   f
dZ	 d#d
ej                   dddedededeeee   f   fdZd
ej                   fdZd Zd
ej                   ddde
j:                  fd Zy!)$z2The BigQuery CLI row access policy client library.    )absolute_import)division)print_function)AnyDictList)iam)bigquery_client)bq_id_utilsz!roles/bigquery.filteredDataViewerbqclientpolicy_referencez4bq_id_utils.ApiClientHelper.RowAccessPolicyReferencegranteesfilter_predicatec                 "   |j                   |j                  |j                  |j                  d||d}| j	                         j                         j                  |j                   |j                  |j                  |      j                         S )a(  Create a row access policy on the given table reference.

  Arguments:
   bqclient: BigQuery client to use for the request.
   policy_reference: Reference to the row access policy to create.
   grantees: Users or groups that can access rows protected by the row access
     policy.
   filter_predicate: A SQL boolean expression that needs to be true for a row to
     be included in the result.

  Returns:
    rowAccessPolicy: The created row access policy defined in
    google3/google/cloud/bigquery/v2/row_access_policy.proto;l=235;rcl=642795091
  	projectId	datasetIdtableIdpolicyIdrowAccessPolicyReferencefilterPredicater   )r   r   r   body)r   r   r   r   GetRowAccessPoliciesApiClientrowAccessPoliciesinsertexecuter   r   r   r   row_access_policys        /platform/bq/clients/client_row_access_policy.pycreate_row_access_policyr!      s    , (11'11%--&//	# *	 ,,.v$..$.."** 	   wy
    c                 8   |j                   |j                  |j                  |j                  d||d}| j	                         j                         j                  |j                   |j                  |j                  |j                  |      j                         S )a(  Update a row access policy on the given table reference.

  Arguments:
   bqclient: BigQuery client to use for the request.
   policy_reference: Reference to the row access policy to update.
   grantees: Users or groups that can access rows protected by the row access
     policy.
   filter_predicate: A SQL boolean expression that needs to be true for a row to
     be included in the result.

  Returns:
    rowAccessPolicy: The updated row access policy defined in
    google3/google/cloud/bigquery/v2/row_access_policy.proto;l=235;rcl=642795091
  r   r   )r   r   r   r   r   )r   r   r   r   r   r   updater   r   s        r    update_row_access_policyr%   ?   s    , (11'11%--&//	# *	 ,,.v$..$.."**#,,    wyr"   c                 >    t        | |      }d|v rt        | |       |S )z5Get a row access policy on the given table reference.r   ) _get_row_access_policy_reference_set_row_access_policy_grantees)r   r   responses      r    get_row_access_policyr*   k   s(    
 .h8HI(8+#Hh7	/r"   returnc                     | j                         j                         j                  |j                  |j                  |j
                  |j                        j                         S )zEReturns the RowAccessPolicyReference for the given row access policy.r   )r   r   getr   r   r   r   r   )r   r   s     r    r'   r'   w   sZ     ,,.
s$..$.."**#,,	   wy
r"   forcec                     | j                         j                         j                  |j                  |j                  |j
                  |j                  |      j                         S )z8Delete a row access policy on the given table reference.)r   r   r   r   r.   )r   r   deleter   r   r   r   r   )r   r   r.   s      r    delete_row_access_policyr1      s]     ,,.v$..$.."**#,,   wyr"   table_referencez*bq_id_utils.ApiClientHelper.TableReference	page_size
page_tokenc                     | j                         j                         j                  |j                  |j                  |j
                  ||      j                         S )z8Lists row access policies for the given table reference.)r   r   r   pageSize	pageToken)r   r   listr   r   r   r   r   r2   r3   r4   s       r    _list_row_access_policiesr:      sW     ,,.t#--#--!))   wyr"   max_concurrent_iam_callsc                 ^    t        | |||      }d|v r|d   }|D ]  }t        | |        |S )ad  Lists row access policies for the given table reference.

  Arguments:
    bqclient: BigQuery client to use for the request.
    table_reference: Reference to the table.
    page_size: Number of results to return.
    page_token: Token to retrieve the next page of results.
    max_concurrent_iam_calls: Number of concurrent calls to getIAMPolicy.

  Returns:
    A dict that contains entries:
      'rowAccessPolicies': a list of row access policies, with an additional
        'grantees' field that contains the row access policy grantees.
      'nextPageToken': nextPageToken for the next page, if present.
  r9   r   )r   r   )r:   r(   )r   r2   r3   r4   r;   r)   row_access_policiesr   s           r    &list_row_access_policies_with_granteesr>      sP    , '%	( H$"#670%- 1
 
/r"   c                     t        j                  j                  j                  di |d   }t	        | |      }t        |      }||d<   y)z1Sets the grantees on the given Row Access Policy.r   )r   	referencer   N )r   ApiClientHelperRowAccessPolicyReferenceCreate get_row_access_policy_iam_policy/_get_grantees_from_row_access_policy_iam_policy)r   r   row_access_policy_ref
iam_policyr   s        r    r(   r(      sZ    
 !!::AA 89 
 0#8* =ZH("*Jr"   c                 ~    | j                  d      }|sg S t        d |D        d      }|sg S |j                  dg       S )zAReturns the filtered data viewer members of the given IAM policy.bindingsc              3   P   K   | ]  }|j                  d       t        k(  r|   yw)roleN)r-   _FILTERED_DATA_VIEWER_ROLE).0bindings     r    	<genexpr>zB_get_grantees_from_row_access_policy_iam_policy.<locals>.<genexpr>   s*      !g[[ $>> !s   $&Nmembers)r-   next)rH   rJ   filtered_data_viewer_bindings      r    rF   rF      sR    ^^J'(	I!%!
 " 
&I	%	)	))R	88r"   r@   c                 F   t        j                  |t         j                  j                  d       d|j                  d|j
                  d|j                  d|j                  }| j                         j                         j                  |      j                         S )as  Gets IAM policy for the given row access policy resource.

  Arguments:
    bqclient: BigQuery client to use for the request.
    reference: the RowAccessPolicyReference for the row access policy resource.

  Returns:
    The IAM policy attached to the given row access policy resource.

  Raises:
    BigqueryTypeError: if reference is not a RowAccessPolicyReference.
  rE   )methodz	projects/z
/datasets/z/tables/z/rowAccessPolicies/)resource)r   	typecheckrB   rC   r   r   r   r   GetIAMPolicyApiClientr   getIamPolicyr   )r   r@   formatted_resources      r    rE   rE      s      !!::/ 










  $$&|/|0wy	r"   N)F)   )__doc__
__future__r   r   r   typingr   r   r   google.api_corer	   clientsr
   utilsr   rM   BigqueryClientstrr!   r%   r*   r'   boolr1   intr:   r>   r(   rF   PolicyrE   rA   r"   r    <module>rg      s   8 &  % " "   #  A (,,(L( 3i( 	(V),,)L) 3i) 	)X	,,	L	,,L 
#s(^* ,,L (,,A  	
 
#tCy.4 %&#,,#A# # 	#
 "# 
#tCy.#L+,,+ 9(#,,#E# 	ZZ#r"   