Lists all BigQuery reservations within a specified project and location.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html;
    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ListReservationsRequest.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.LocationName.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.Reservation.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationServiceClient.html;
    import java.io.IOException;

    public class ListReservations {

      /**
       * Lists all BigQuery reservations in a given project and location. A reservation provides
       * computational resource guarantees, in the form of slots, to users.
       *
       * @param projectId The Google Cloud project ID (for example, "example-project-id").
       * @param location The geographic location of the reservations (for example, "us-central1").
       */
      public static void listReservations(String projectId, String location) {
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.LocationName.html parent = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.LocationName.html.of(projectId, location);

        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ListReservationsRequest.html request =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ListReservationsRequest.html.newBuilder().setParent(parent.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.LocationName.html#com_google_cloud_bigquery_reservation_v1_LocationName_toString__()).build();

        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationServiceClient.html client = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationServiceClient.html.create()) {
          System.out.printf("Listing reservations for parent: '%s':%n", parent);
          for (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.Reservation.html reservation : client.listReservations(request).iterateAll()) {
            System.out.printf("  Reservation: %s%n", reservation.getName());
            System.out.printf("  Slot capacity: %d%n", reservation.getSlotCapacity());
          }
          System.out.println("Finished listing reservations.");
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html err) {
          System.err.printf(
              "error: parent resource '%s' not found: %s%n", parent, err.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ErrorDetails.html#com_google_api_gax_rpc_ErrorDetails__T_getMessage_java_lang_Class_T__());
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html | IOException err) {
          System.err.printf(
              "error: failed to list reservations in '%s': %s%n", parent, err.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ErrorDetails.html#com_google_api_gax_rpc_ErrorDetails__T_getMessage_java_lang_Class_T__());
        }
      }

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Node.js API
reference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    const {ReservationServiceClient} =
      require('https://docs.cloud.google.com/nodejs/docs/reference/bigquery-reservation/latest/overview.html').v1;
    const {status} = require('@grpc/grpc-js');

    const client = new https://docs.cloud.google.com/nodejs/docs/reference/bigquery-reservation/latest/overview.html();

    /**
     * Lists all reservations for the project in the specified location.
     *
     * A reservation provides computational resource guarantees, in the form of
     * slots, to users.
     *
     * @param {string} projectId Google Cloud project ID. for example 'example-project-id'
     * @param {string} location Google Cloud location. for example 'us-central1'
     */
    async function listReservations(projectId, location = 'us-central1') {
      const request = {
        parent: `projects/${projectId}/locations/${location}`,
      };

      try {
        const [reservations] = await client.listReservations(request);

        if (reservations.length === 0) {
          console.log(
            `No reservations found in project ${projectId} in location ${location}.`,
          );
          return;
        }

        console.log(
          `Reservations in project ${projectId} in location ${location}:`,
        );
        for (const reservation of reservations) {
          console.log(`- Reservation: ${reservation.name}`);
          console.log(`  Slot capacity: ${reservation.slotCapacity}`);
          console.log(`  Ignore idle slots: ${reservation.ignoreIdleSlots}`);
        }
      } catch (err) {
        if (err.code === status.NOT_FOUND) {
          console.log(`Project or location not found: ${request.parent}`);
        } else {
          console.error('An error occurred while listing reservations:', err);
        }
      }
    }

### Python


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    from google.api_core import exceptions
    from google.cloud import https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest

    client = https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest.https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest/google.cloud.bigquery_reservation_v1.services.reservation_service.ReservationServiceClient.html()


    def list_reservations(project_id: str, location: str):
        """Lists all reservations for a project and location.

        A reservation provides computational resource guarantees, in the form of
        slots, to users. This sample shows how to list existing reservations
        within a specific project and location.

        Args:
            project_id: The Google Cloud project ID.
            location: The geographic location of the reservations, for example, us-central1.
        """

        parent = f"projects/{project_id}/locations/{location}"

        try:
            print(f"Listing reservations for parent: '{parent}':")
            for reservation in client.https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest/google.cloud.bigquery_reservation_v1.services.reservation_service.ReservationServiceClient.html#google_cloud_bigquery_reservation_v1_services_reservation_service_ReservationServiceClient_list_reservations(parent=parent):
                print(f"\tReservation: {reservation.name}")
                print(f"\tSlot capacity: {reservation.slot_capacity}")

            print("Finished listing reservations.")
        except exceptions.NotFound:
            print(
                f"Parent resource '{parent}' was not found. Please check your project ID and location."
            )

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquery).