Retrieves a list of all assignments for a specified BigQuery reservation. An assignment connects a project, folder, or organization to a reservation.

## 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.Assignment.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.ListAssignmentsRequest.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.ReservationName.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 ListAssignments {

      /**
       * Lists all assignments for a specified BigQuery reservation. To list all assignments in a
       * given location across all reservations, pass "-" as the reservationId.
       *
       * @param projectId The Google Cloud project ID (for example, "example-project-id").
       * @param location The geographic location of the reservation (for example, "us-central1").
       * @param reservationId The ID of the reservation to list assignments for, or "-" to list all
       *     assignments for the project and location (for example, "example-reservation").
       */
      public static void listAssignments(String projectId, String location, String reservationId) {
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationName.html parent = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationName.html.of(projectId, location, reservationId);

        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ListAssignmentsRequest.html request =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ListAssignmentsRequest.html.newBuilder().setParent(parent.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationName.html#com_google_cloud_bigquery_reservation_v1_ReservationName_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 assignments for parent: '%s'%n", parent);
          boolean foundAssignments = false;
          for (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.Assignment.html assignment : client.listAssignments(request).iterateAll()) {
            foundAssignments = true;
            System.out.printf("  Got assignment: %s%n", assignment.getName());
          }

          if (!foundAssignments) {
            System.out.println("No assignments found.");
          }
        } 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 assignments for '%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 reservationServiceClient = new https://docs.cloud.google.com/nodejs/docs/reference/bigquery-reservation/latest/overview.html();

    /**
     * Lists assignments.
     * An assignment lets a project, folder, or organization use slots from a
     * specified reservation. By using a wildcard `-` for the reservation ID,
     * this sample lists all assignments for a given project and location.
     *
     * @param {string} projectId The ID of the project. Example: 'example-project-id'
     * @param {string} location The location of the reservation. Example: 'us-central1'
     */
    async function listAssignments(projectId, location = 'us-central1') {
      const request = {
        parent: `projects/${projectId}/locations/${location}/reservations/-`,
      };

      try {
        const iterable = reservationServiceClient.https://docs.cloud.google.com/nodejs/docs/reference/bigquery-reservation/latest/bigquery-reservation/v1.reservationserviceclient.html(request);
        console.log(`Assignments in parent "${request.parent}":`);

        let found = false;
        for await (const assignment of iterable) {
          found = true;
          console.log(`- ${assignment.name}`);
          console.log(`  - Assignee: ${assignment.assignee}`);
          console.log(`  - Job Type: ${assignment.jobType}`);
        }

        if (!found) {
          console.log('  No assignments found.');
        }
      } catch (err) {
        if (err.code === status.NOT_FOUND) {
          console.log(`Parent resource not found: ${request.parent}`);
        } else {
          console.error('Error listing assignments:', 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


    def list_assignments(
        project_id: str,
        location: str,
        reservation_id: str,
    ):
        """Lists all assignments for a reservation.

        This sample shows how to list all assignments in a given reservation.
        To list all assignments in a given location, across all reservations,
        use a wildcard `-` for the reservation ID.

        Args:
            project_id: The Google Cloud project ID.
            location: The geographic location of the assignments , for example, 'us-central1'.
            reservation_id: The ID of the reservation to list assignments for, or "-"
                to list all assignments for the project and location.
        """
        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()
        parent = 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_reservation_path(project_id, location, reservation_id)

        try:
            print(f"Listing assignments for parent: '{parent}'")
            assignment_list = 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_assignments(parent=parent)

            found_assignments = False
            for assignment in assignment_list:
                found_assignments = True
                print(f"  Got assignment: {assignment.name}")

            if not found_assignments:
                print("No assignments found.")

        except exceptions.NotFound:
            print(f"Parent resource '{parent}' not found.")

## 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).