-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add scheduled query samples (#83)
* docs: add scheduled query samples * test: opt-out of type annotations for now * test: use environment variable for project ID * set quota project * consolidate config creation to conserve quota
- Loading branch information
Showing
11 changed files
with
448 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
packages/google-cloud-bigquery-datatransfer/samples/snippets/manage_transfer_configs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def list_configs(override_values={}): | ||
# [START bigquerydatatransfer_list_configs] | ||
from google.cloud import bigquery_datatransfer | ||
|
||
transfer_client = bigquery_datatransfer.DataTransferServiceClient() | ||
|
||
project_id = "my-project" | ||
# [END bigquerydatatransfer_list_configs] | ||
# To facilitate testing, we replace values with alternatives | ||
# provided by the testing harness. | ||
project_id = override_values.get("project_id", project_id) | ||
# [START bigquerydatatransfer_list_configs] | ||
parent = transfer_client.common_project_path(project_id) | ||
|
||
configs = transfer_client.list_transfer_configs(parent=parent) | ||
print("Got the following configs:") | ||
for config in configs: | ||
print(f"\tID: {config.name}, Schedule: {config.schedule}") | ||
# [END bigquerydatatransfer_list_configs] | ||
|
||
|
||
def update_config(override_values={}): | ||
# [START bigquerydatatransfer_update_config] | ||
from google.cloud import bigquery_datatransfer | ||
from google.protobuf import field_mask_pb2 | ||
|
||
transfer_client = bigquery_datatransfer.DataTransferServiceClient() | ||
|
||
transfer_config_name = "projects/1234/locations/us/transferConfigs/abcd" | ||
new_display_name = "My Transfer Config" | ||
# [END bigquerydatatransfer_update_config] | ||
# To facilitate testing, we replace values with alternatives | ||
# provided by the testing harness. | ||
new_display_name = override_values.get("new_display_name", new_display_name) | ||
transfer_config_name = override_values.get( | ||
"transfer_config_name", transfer_config_name | ||
) | ||
# [START bigquerydatatransfer_update_config] | ||
|
||
transfer_config = bigquery_datatransfer.TransferConfig(name=transfer_config_name) | ||
transfer_config.display_name = new_display_name | ||
|
||
transfer_config = transfer_client.update_transfer_config( | ||
{ | ||
"transfer_config": transfer_config, | ||
"update_mask": field_mask_pb2.FieldMask(paths=["display_name"]), | ||
} | ||
) | ||
|
||
print(f"Updated config: '{transfer_config.name}'") | ||
print(f"New display name: '{transfer_config.display_name}'") | ||
# [END bigquerydatatransfer_update_config] | ||
# Return the config name for testing purposes, so that it can be deleted. | ||
return transfer_config | ||
|
||
|
||
def update_credentials_with_service_account(override_values={}): | ||
# [START bigquerydatatransfer_update_credentials] | ||
from google.cloud import bigquery_datatransfer | ||
from google.protobuf import field_mask_pb2 | ||
|
||
transfer_client = bigquery_datatransfer.DataTransferServiceClient() | ||
|
||
service_account_name = "[email protected]" | ||
transfer_config_name = "projects/1234/locations/us/transferConfigs/abcd" | ||
# [END bigquerydatatransfer_update_credentials] | ||
# To facilitate testing, we replace values with alternatives | ||
# provided by the testing harness. | ||
service_account_name = override_values.get( | ||
"service_account_name", service_account_name | ||
) | ||
transfer_config_name = override_values.get( | ||
"transfer_config_name", transfer_config_name | ||
) | ||
# [START bigquerydatatransfer_update_credentials] | ||
|
||
transfer_config = bigquery_datatransfer.TransferConfig(name=transfer_config_name) | ||
|
||
transfer_config = transfer_client.update_transfer_config( | ||
{ | ||
"transfer_config": transfer_config, | ||
"update_mask": field_mask_pb2.FieldMask(paths=["service_account_name"]), | ||
"service_account_name": service_account_name, | ||
} | ||
) | ||
|
||
print("Updated config: '{}'".format(transfer_config.name)) | ||
# [END bigquerydatatransfer_update_credentials] | ||
# Return the config name for testing purposes, so that it can be deleted. | ||
return transfer_config | ||
|
||
|
||
def schedule_backfill(override_values={}): | ||
# [START bigquerydatatransfer_schedule_backfill] | ||
import datetime | ||
|
||
from google.cloud import bigquery_datatransfer | ||
|
||
transfer_client = bigquery_datatransfer.DataTransferServiceClient() | ||
|
||
transfer_config_name = "projects/1234/locations/us/transferConfigs/abcd" | ||
# [END bigquerydatatransfer_schedule_backfill] | ||
# To facilitate testing, we replace values with alternatives | ||
# provided by the testing harness. | ||
transfer_config_name = override_values.get( | ||
"transfer_config_name", transfer_config_name | ||
) | ||
# [START bigquerydatatransfer_schedule_backfill] | ||
now = datetime.datetime.now(datetime.timezone.utc) | ||
start_time = now - datetime.timedelta(days=5) | ||
end_time = now - datetime.timedelta(days=2) | ||
|
||
# Some data sources, such as scheduled_query only support daily run. | ||
# Truncate start_time and end_time to midnight time (00:00AM UTC). | ||
start_time = datetime.datetime( | ||
start_time.year, start_time.month, start_time.day, tzinfo=datetime.timezone.utc | ||
) | ||
end_time = datetime.datetime( | ||
end_time.year, end_time.month, end_time.day, tzinfo=datetime.timezone.utc | ||
) | ||
|
||
response = transfer_client.schedule_transfer_runs( | ||
parent=transfer_config_name, | ||
start_time=start_time, | ||
end_time=end_time, | ||
) | ||
|
||
print("Started transfer runs:") | ||
for run in response.runs: | ||
print(f"backfill: {run.run_time} run: {run.name}") | ||
# [END bigquerydatatransfer_schedule_backfill] | ||
return response.runs | ||
|
||
|
||
def delete_config(override_values={}): | ||
# [START bigquerydatatransfer_delete_transfer] | ||
import google.api_core.exceptions | ||
from google.cloud import bigquery_datatransfer | ||
|
||
transfer_client = bigquery_datatransfer.DataTransferServiceClient() | ||
|
||
transfer_config_name = "projects/1234/locations/us/transferConfigs/abcd" | ||
# [END bigquerydatatransfer_delete_transfer] | ||
# To facilitate testing, we replace values with alternatives | ||
# provided by the testing harness. | ||
transfer_config_name = override_values.get( | ||
"transfer_config_name", transfer_config_name | ||
) | ||
# [START bigquerydatatransfer_delete_transfer] | ||
try: | ||
transfer_client.delete_transfer_config(name=transfer_config_name) | ||
except google.api_core.exceptions.NotFound: | ||
print("Transfer config not found.") | ||
else: | ||
print(f"Deleted transfer config: {transfer_config_name}") | ||
# [END bigquerydatatransfer_delete_transfer] |
70 changes: 70 additions & 0 deletions
70
packages/google-cloud-bigquery-datatransfer/samples/snippets/manage_transfer_configs_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from . import manage_transfer_configs | ||
|
||
|
||
def test_list_configs(capsys, project_id, transfer_config_name): | ||
manage_transfer_configs.list_configs({"project_id": project_id}) | ||
out, _ = capsys.readouterr() | ||
assert "Got the following configs:" in out | ||
assert transfer_config_name in out | ||
|
||
|
||
def test_update_config(capsys, transfer_config_name): | ||
manage_transfer_configs.update_config( | ||
{ | ||
"new_display_name": "name from test_update_config", | ||
"transfer_config_name": transfer_config_name, | ||
} | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Updated config:" in out | ||
assert transfer_config_name in out | ||
assert "name from test_update_config" in out | ||
|
||
|
||
def test_update_credentials_with_service_account( | ||
capsys, project_id, service_account_name, transfer_config_name | ||
): | ||
manage_transfer_configs.update_credentials_with_service_account( | ||
{ | ||
"project_id": project_id, | ||
"service_account_name": service_account_name, | ||
"transfer_config_name": transfer_config_name, | ||
} | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Updated config:" in out | ||
assert transfer_config_name in out | ||
|
||
|
||
def test_schedule_backfill(capsys, transfer_config_name): | ||
runs = manage_transfer_configs.schedule_backfill( | ||
{ | ||
"transfer_config_name": transfer_config_name, | ||
} | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Started transfer runs:" in out | ||
# Run IDs should include the transfer name in their path. | ||
assert transfer_config_name in out | ||
# Check that there are runs for 5, 4, 3, and 2 days ago. | ||
assert len(runs) == 4 | ||
|
||
|
||
def test_delete_config(capsys, transfer_config_name): | ||
# transfer_config_name fixture in conftest.py calls the delete config | ||
# sample. To conserve limited BQ-DTS quota we only make basic checks. | ||
assert len(transfer_config_name) != 0 |
Oops, something went wrong.