|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START bigqueryreservation_quickstart] |
| 16 | +import argparse |
| 17 | + |
| 18 | +from google.cloud import bigquery_reservation_v1 |
| 19 | + |
| 20 | + |
| 21 | +def main(project_id: str = "your-project-id", location: str = "US") -> None: |
| 22 | + # Constructs the client for interacting with the service. |
| 23 | + client = bigquery_reservation_v1.ReservationServiceClient() |
| 24 | + |
| 25 | + report_capacity_commitments(client, project_id, location) |
| 26 | + report_reservations(client, project_id, location) |
| 27 | + |
| 28 | + |
| 29 | +def report_capacity_commitments( |
| 30 | + client: bigquery_reservation_v1.ReservationServiceClient, |
| 31 | + project_id: str, |
| 32 | + location: str, |
| 33 | +) -> None: |
| 34 | + """Prints details and summary information about capacity commitments for |
| 35 | + a given admin project and location. |
| 36 | + """ |
| 37 | + print(f"Capacity commitments in project {project_id} in location {location}") |
| 38 | + req = bigquery_reservation_v1.ListCapacityCommitmentsRequest( |
| 39 | + parent=client.common_location_path(project_id, location) |
| 40 | + ) |
| 41 | + total_commitments = 0 |
| 42 | + for commitment in client.list_capacity_commitments(request=req): |
| 43 | + print(f"\tCommitment {commitment.name} in state {commitment.state}") |
| 44 | + total_commitments = total_commitments + 1 |
| 45 | + print(f"\n{total_commitments} commitments processed.") |
| 46 | + |
| 47 | + |
| 48 | +def report_reservations( |
| 49 | + client: bigquery_reservation_v1.ReservationServiceClient, |
| 50 | + project_id: str, |
| 51 | + location: str, |
| 52 | +) -> None: |
| 53 | + """Prints details and summary information about reservations defined within |
| 54 | + a given admin project and location. |
| 55 | + """ |
| 56 | + print("Reservations in project {} in location {}".format(project_id, location)) |
| 57 | + req = bigquery_reservation_v1.ListReservationsRequest( |
| 58 | + parent=client.common_location_path(project_id, location) |
| 59 | + ) |
| 60 | + total_reservations = 0 |
| 61 | + for reservation in client.list_reservations(request=req): |
| 62 | + print( |
| 63 | + f"\tReservation {reservation.name} " |
| 64 | + f"has {reservation.slot_capacity} slot capacity." |
| 65 | + ) |
| 66 | + total_reservations = total_reservations + 1 |
| 67 | + print(f"\n{total_reservations} reservations processed.") |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + parser = argparse.ArgumentParser() |
| 72 | + parser.add_argument("--project_id", type=str) |
| 73 | + parser.add_argument("--location", default="US", type=str) |
| 74 | + args = parser.parse_args() |
| 75 | + main(project_id=args.project_id, location=args.location) |
| 76 | + |
| 77 | +# [END bigqueryreservation_quickstart] |
0 commit comments