Skip to content

Commit f2a86ef

Browse files
shollymantswast
authored andcommitted
chore: add reservation quickstart and test (#25)
* chore: add quickstart and test This PR introduces code for a reservation quickstart, but does not include the wiring for augmenting kokoro/nox/etc to include it in testing. This will be addressed in subsequent changes. * blacken * use new samples template * use samples template * move imports to sample * add type hints * add type hints * format strings * revert noxfile * use env var from noxfile Co-authored-by: Tim Swast <[email protected]>
1 parent 7ef57df commit f2a86ef

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

bigquery-reservation/snippets/__init__.py

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# A dictionary you want to inject into your test. Don't put any
36+
# secrets here. These values will override predefined values.
37+
"envs": {},
38+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import os
16+
17+
import pytest
18+
19+
from . import quickstart
20+
21+
22+
@pytest.fixture()
23+
def project_id() -> str:
24+
return os.environ["GOOGLE_CLOUD_PROJECT"]
25+
26+
27+
def test_quickstart(capsys: pytest.CaptureFixture, project_id: str) -> None:
28+
quickstart.main(project_id)
29+
out, _ = capsys.readouterr()
30+
assert " reservations processed." in out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==6.2.1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-bigquery-reservation==1.0.0

0 commit comments

Comments
 (0)