Get the IAM policy for a reservation

Retrieves the Identity and Access Management (IAM) policy for a BigQuery reservation. The policy defines which principals have which roles, and is used to view the access control settings for the resource.

Code sample

Node.js

Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery Node.js API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  const 
  
 { 
  
 ReservationServiceClient 
 , 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/bigquery-reservation 
' 
 ). 
 v1 
 ; 
 const 
  
 { 
 status 
 } 
  
 = 
  
 require 
 ( 
 '@grpc/grpc-js' 
 ); 
 const 
  
 client 
  
 = 
  
 new 
  
  ReservationServiceClient 
 
 (); 
 /** 
 * Gets the IAM policy for a reservation. 
 * An IAM policy is a collection of bindings that associates one or more members, 
 * such as service accounts or users, with a single role. 
 * 
 * @param {string} projectId - Google Cloud project ID, for example "example-project-id". 
 * @param {string} location - Location of the reservation, for example "us-central1". 
 * @param {string} reservationId - ID of the reservation, for example "example-reservation". 
 */ 
 async 
  
 function 
  
 getReservationIamPolicy 
 ( 
  
 projectId 
 , 
  
 location 
  
 = 
  
 'us-central1' 
 , 
  
 reservationId 
  
 = 
  
 'example-reservation' 
 , 
 ) 
  
 { 
  
 const 
  
 resource 
  
 = 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 location 
 } 
 /reservations/ 
 ${ 
 reservationId 
 } 
 ` 
 ; 
  
 const 
  
 request 
  
 = 
  
 { 
  
 resource 
 , 
  
 }; 
  
 try 
  
 { 
  
 const 
  
 [ 
 policy 
 ] 
  
 = 
  
 await 
  
 client 
 . 
 getIamPolicy 
 ( 
 request 
 ); 
  
 console 
 . 
 log 
 ( 
 `Policy for reservation 
 ${ 
 reservationId 
 } 
 :` 
 ); 
  
 if 
  
 ( 
 policy 
 . 
 bindings 
 && 
 policy 
 . 
 bindings 
 . 
 length 
 > 
 0 
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
 JSON 
 . 
 stringify 
 ( 
 policy 
 . 
 bindings 
 , 
  
 null 
 , 
  
 2 
 )); 
  
 } 
  
 else 
  
 { 
  
 console 
 . 
 log 
 ( 
 'This reservation has no policy bindings.' 
 ); 
  
 } 
  
 } 
  
 catch 
  
 ( 
 err 
 ) 
  
 { 
  
 if 
  
 ( 
 err 
 . 
 code 
  
 === 
  
 status 
 . 
 NOT_FOUND 
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
  
 `Reservation ' 
 ${ 
 reservationId 
 } 
 ' not found in project ' 
 ${ 
 projectId 
 } 
 ' at location ' 
 ${ 
 location 
 } 
 '.` 
 , 
  
 ); 
  
 } 
  
 else 
  
 { 
  
 console 
 . 
 error 
 ( 
 'Error getting IAM policy:' 
 , 
  
 err 
 ); 
  
 } 
  
 } 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery Python API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  from 
  
 google.api_core.exceptions 
  
 import 
 NotFound 
 from 
  
 google.cloud 
  
 import 
  bigquery_reservation_v1 
 
 client 
 = 
  bigquery_reservation_v1 
 
 . 
  ReservationServiceClient 
 
 () 
 def 
  
 get_reservation_iam_policy 
 ( 
 project_id 
 : 
 str 
 , 
 location 
 : 
 str 
 , 
 reservation_id 
 : 
 str 
 ): 
  
 """Gets the IAM policy for a reservation. 
 An IAM policy is a collection of bindings that associates one or more 
 principals with a single role. This sample demonstrates how to retrieve 
 the policy for a reservation. 
 Args: 
 project_id: The Google Cloud project ID. 
 location: The geographic location of the reservation, such as "us-central1". 
 reservation_id: The ID of the reservation to get the policy for. 
 """ 
 resource 
 = 
 client 
 . 
  reservation_path 
 
 ( 
 project_id 
 , 
 location 
 , 
 reservation_id 
 ) 
 try 
 : 
 policy 
 = 
 client 
 . 
  get_iam_policy 
 
 ( 
 resource 
 = 
 resource 
 ) 
 print 
 ( 
 f 
 "Got IAM policy for reservation: 
 { 
 resource 
 } 
 " 
 ) 
 for 
 binding 
 in 
 policy 
 . 
 bindings 
 : 
 print 
 ( 
 f 
 "  Role: 
 { 
 binding 
 . 
 role 
 } 
 " 
 ) 
 print 
 ( 
 f 
 "  Members: 
 { 
 binding 
 . 
 members 
 } 
 " 
 ) 
 except 
 NotFound 
 : 
 print 
 ( 
 f 
 "Reservation not found: 
 { 
 resource 
 } 
 " 
 ) 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: