Create a reservation assignment

Create an assignment to map a project, folder, or organization to a BigQuery reservation.

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();

/**
 * Creates an assignment to assign a project to a reservation.
 * A reservation assignment lets a project submit jobs of a certain type
 * using slots from the specified reservation.
 *
 * @param {string} projectId Google Cloud Project ID that owns the reservation, for example 'example-project-id'.
 * @param {string} location Google Cloud Location, for example 'us-central1'.
 * @param {string} reservationId The ID of the reservation to which the project will be assigned, for example 'example-reservation'.
 */
async function createAssignment(
  projectId,
  location = 'us-central1',
  reservationId = 'example-reservation',
) {
  const parent = client.reservationPath(projectId, location, reservationId);
  const request = {
    parent,
    assignment: {
      assignee: `projects/${projectId}`,
      jobType: 'QUERY',
    },
  };

  try {
    const [assignment] = await client.createAssignment(request);
    console.log(`Created assignment: ${assignment.name}`);
    console.log(`  Assignee: ${assignment.assignee}`);
    console.log(`  Job Type: ${assignment.jobType}`);
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.log(
        `Assignment for project ${projectId} to reservation ${reservationId} already exists.`,
      );
    } else {
      console.error('Error creating assignment:', 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 import exceptions
from google.cloud import bigquery_reservation_v1

client = bigquery_reservation_v1.ReservationServiceClient()


def create_assignment(project_id: str, location: str, reservation_id: str):
    """Creates a new assignment for a reservation.

    This shows how to create an assignment that gives a project access to
    a reservation.

    Args:
        project_id: The Google Cloud project ID of the reservation owner.
        location: The geographic location of the reservation, for example, "US".
        reservation_id: The ID of the reservation to create the assignment in.
    """

    parent = client.reservation_path(project_id, location, reservation_id)
    assignee = f"projects/{project_id}"
    assignment = bigquery_reservation_v1.Assignment(
        assignee=assignee,
        job_type=bigquery_reservation_v1.Assignment.JobType.QUERY,
    )

    try:
        created_assignment = client.create_assignment(
            parent=parent, assignment=assignment
        )
        print(f"Created assignment: {created_assignment.name}")
    except exceptions.AlreadyExists:
        print(f"Assignment for project {project_id} and job type QUERY already exists.")

What's next

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