Skip to content

Commit 2604a48

Browse files
steffnaypartheagcf-owl-bot[bot]
authored andcommitted
docs(samples): add create_migration_workflow snippet (#71)
* docs(samples): add handwritten snippet structure * docs(samples): add create_migration_workflow snippet * fix copyright * remove types * lint * cleanup noxfile * enforce type hints * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update samples/snippets/create_migration_workflow.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update samples/snippets/create_migration_workflow.py Co-authored-by: Anthonios Partheniou <[email protected]> * pin dependencies * add type annotations Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 372cf0d commit 2604a48

File tree

6 files changed

+200
-0
lines changed

6 files changed

+200
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2022 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 bigquery_migration_create_workflow]
16+
def create_migration_workflow(
17+
gcs_input_path: str, gcs_output_path: str, project_id: str
18+
) -> None:
19+
"""Creates a migration workflow of a Batch SQL Translation and prints the response."""
20+
21+
from google.cloud import bigquery_migration_v2
22+
23+
parent = f"projects/{project_id}/locations/us"
24+
25+
# Construct a BigQuery Migration client object.
26+
client = bigquery_migration_v2.MigrationServiceClient()
27+
28+
# Set the source dialect to Teradata SQL.
29+
source_dialect = bigquery_migration_v2.Dialect()
30+
source_dialect.teradata_dialect = bigquery_migration_v2.TeradataDialect(
31+
mode=bigquery_migration_v2.TeradataDialect.Mode.SQL
32+
)
33+
34+
# Set the target dialect to BigQuery dialect.
35+
target_dialect = bigquery_migration_v2.Dialect()
36+
target_dialect.bigquery_dialect = bigquery_migration_v2.BigQueryDialect()
37+
38+
# Prepare the config proto.
39+
translation_config = bigquery_migration_v2.TranslationConfigDetails(
40+
gcs_source_path=gcs_input_path,
41+
gcs_target_path=gcs_output_path,
42+
source_dialect=source_dialect,
43+
target_dialect=target_dialect,
44+
)
45+
46+
# Prepare the task.
47+
migration_task = bigquery_migration_v2.MigrationTask(
48+
type_="Translation_Teradata2BQ", translation_config_details=translation_config
49+
)
50+
51+
# Prepare the workflow.
52+
workflow = bigquery_migration_v2.MigrationWorkflow(
53+
display_name="demo-workflow-python-example-Teradata2BQ"
54+
)
55+
56+
workflow.tasks["translation-task"] = migration_task # type: ignore
57+
58+
# Prepare the API request to create a migration workflow.
59+
request = bigquery_migration_v2.CreateMigrationWorkflowRequest(
60+
parent=parent,
61+
migration_workflow=workflow,
62+
)
63+
64+
response = client.create_migration_workflow(request=request)
65+
66+
print("Created workflow:")
67+
print(response.display_name)
68+
print("Current state:")
69+
print(response.State(response.state))
70+
71+
72+
# [END bigquery_migration_create_workflow]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2022 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+
16+
from typing import Iterable, List, Optional
17+
18+
from google.api_core.exceptions import (
19+
InternalServerError,
20+
ServiceUnavailable,
21+
TooManyRequests,
22+
)
23+
24+
from google.cloud import storage
25+
26+
import pytest
27+
28+
from test_utils.retry import RetryErrors
29+
from test_utils.system import unique_resource_id
30+
31+
from . import create_migration_workflow
32+
33+
retry_storage_errors = RetryErrors(
34+
(TooManyRequests, InternalServerError, ServiceUnavailable)
35+
)
36+
37+
storage_client = storage.Client()
38+
PROJECT_ID = storage_client.project
39+
40+
41+
def _create_bucket(bucket_name: str, location: Optional[str] = None) -> storage.Bucket:
42+
bucket = storage_client.bucket(bucket_name)
43+
retry_storage_errors(storage_client.create_bucket)(bucket_name, location=location)
44+
45+
return bucket
46+
47+
48+
@pytest.fixture
49+
def buckets_to_delete() -> Iterable[List]:
50+
doomed = []
51+
yield doomed
52+
for item in doomed:
53+
if isinstance(item, storage.Bucket):
54+
retry_storage_errors(item.delete)(force=True)
55+
56+
57+
def test_create_migration_workflow(
58+
capsys: pytest.CaptureFixture, buckets_to_delete: List[storage.Bucket]
59+
) -> None:
60+
bucket_name = "bq_migration_create_workflow_test" + unique_resource_id()
61+
path = f"gs://{PROJECT_ID}/{bucket_name}"
62+
bucket = _create_bucket(bucket_name)
63+
buckets_to_delete.extend([bucket])
64+
65+
create_migration_workflow.create_migration_workflow(path, path, PROJECT_ID)
66+
out, _ = capsys.readouterr()
67+
68+
assert "demo-workflow-python-example-Teradata2BQ" in out
69+
assert "Current state:" in out
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2021 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/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": [],
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": "GOOGLE_CLOUD_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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest==6.2.5
2+
google-cloud-testutils==1.3.0
3+
google-api-core==2.8.0
4+
google-cloud-storage==2.0.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-bigquery-migration==0.4.1
2+
protobuf==3.19.1

0 commit comments

Comments
 (0)