Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit 8e664be

Browse files
loferrispartheameredithslotagcf-owl-bot[bot]
authored
docs(samples): create connection sample for MySQL instance (#147)
* setup for cloudsql connection sample * fleshed out sample * complete sample * changing region tag pattern * adding new environment variables * adding missing env variable * fixing fixture with correct env variable * refactor * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor test imports * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * kokoro lint errors * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * changing fixture use * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactoring variables * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fixing imports * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * changing fixture syntax * fixing import * fixing typecasting * updating fixture usage * changing fixtures * refactor fixtures Co-authored-by: Anthonios Partheniou <partheniou@google.com> Co-authored-by: meredithslota <meredithslota@google.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent cf2ac37 commit 8e664be

File tree

4 files changed

+288
-4
lines changed

4 files changed

+288
-4
lines changed

samples/snippets/conftest.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,35 @@
1919

2020

2121
@pytest.fixture(scope="session")
22-
def project_id() -> str:
23-
return os.environ["GOOGLE_CLOUD_PROJECT"]
22+
def connection_client() -> connection_service.ConnectionServiceClient:
23+
return connection_service.ConnectionServiceClient()
2424

2525

2626
@pytest.fixture(scope="session")
27-
def connection_client() -> connection_service.ConnectionServiceClient:
28-
return connection_service.ConnectionServiceClient()
27+
def project_id() -> str:
28+
return os.environ["GOOGLE_CLOUD_PROJECT"]
2929

3030

3131
@pytest.fixture(scope="session")
3232
def location() -> str:
3333
return "US"
34+
35+
36+
@pytest.fixture(scope="session")
37+
def database() -> str:
38+
return os.environ["MYSQL_DATABASE"]
39+
40+
41+
@pytest.fixture(scope="session")
42+
def cloud_sql_conn_name() -> str:
43+
return os.environ["MYSQL_INSTANCE"]
44+
45+
46+
@pytest.fixture(scope="session")
47+
def mysql_username() -> str:
48+
return os.environ["MYSQL_USER"]
49+
50+
51+
@pytest.fixture(scope="session")
52+
def mysql_password() -> str:
53+
return os.environ["MYSQL_PASSWORD"]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
# 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 bigqueryconnection_connection_create]
16+
from google.cloud import bigquery_connection_v1 as bq_connection
17+
18+
"""This sample shows how to create a BigQuery connection with a Cloud SQL for MySQL database"""
19+
20+
21+
def main() -> None:
22+
# TODO(developer): Set all variables for your Cloud SQL for MySQL connection.
23+
project_id = "your-project-id" # set project_id
24+
location = "US" # set location
25+
# See: https://cloud.google.com/bigquery/docs/locations for a list of
26+
# available locations.
27+
database = "my-database" # set database name
28+
username = "my-username" # set database username
29+
password = "my-password" # set database password
30+
cloud_sql_conn_name = "" # set the name of your connection
31+
32+
cloud_sql_credential = bq_connection.CloudSqlCredential(
33+
{
34+
"username": username,
35+
"password": password,
36+
}
37+
)
38+
cloud_sql_properties = bq_connection.CloudSqlProperties(
39+
{
40+
"type_": bq_connection.CloudSqlProperties.DatabaseType.MYSQL,
41+
"database": database,
42+
"instance_id": cloud_sql_conn_name,
43+
"credential": cloud_sql_credential,
44+
}
45+
)
46+
create_mysql_connection(project_id, location, cloud_sql_properties)
47+
48+
49+
def create_mysql_connection(
50+
project_id: str,
51+
location: str,
52+
cloud_sql_properties: bq_connection.CloudSqlProperties,
53+
) -> None:
54+
connection = bq_connection.types.Connection({"cloud_sql": cloud_sql_properties})
55+
client = bq_connection.ConnectionServiceClient()
56+
parent = client.common_location_path(project_id, location)
57+
request = bq_connection.CreateConnectionRequest(
58+
{"parent": parent, "connection": connection}
59+
)
60+
response = client.create_connection(request)
61+
print(f"Created connection successfully: {response.name}")
62+
63+
64+
# [END bigqueryconnection_connection_create]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
# 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 google.api_core.exceptions
16+
from google.cloud import bigquery_connection_v1 as bq_connection
17+
from google.cloud.bigquery_connection_v1.services import connection_service
18+
import pytest
19+
import test_utils.prefixer
20+
21+
from . import create_mysql_connection
22+
23+
connection_prefixer = test_utils.prefixer.Prefixer("py-bq-r", "snippets", separator="-")
24+
25+
26+
@pytest.fixture(scope="session")
27+
def location_path(
28+
connection_client: connection_service.ConnectionServiceClient(),
29+
project_id: str,
30+
location: str,
31+
) -> str:
32+
return connection_client.common_location_path(project_id, location)
33+
34+
35+
@pytest.fixture(scope="module", autouse=True)
36+
def cleanup_connection(
37+
connection_client: connection_service.ConnectionServiceClient, location_path: str
38+
) -> None:
39+
for connection in connection_client.list_connections(parent=location_path):
40+
connection_id = connection.name.split("/")[-1]
41+
if connection_prefixer.should_cleanup(connection_id):
42+
connection_client.delete_connection(name=connection.name)
43+
44+
45+
@pytest.fixture(scope="session")
46+
def connection_id(
47+
connection_client: connection_service.ConnectionServiceClient,
48+
project_id: str,
49+
location: str,
50+
) -> str:
51+
id_ = connection_prefixer.create_prefix()
52+
yield id_
53+
54+
connection_name = connection_client.connection_path(project_id, location, id_)
55+
try:
56+
connection_client.delete_connection(name=connection_name)
57+
except google.api_core.exceptions.NotFound:
58+
pass
59+
60+
61+
def test_create_mysql_connection(
62+
capsys: pytest.CaptureFixture,
63+
mysql_username: str,
64+
mysql_password: str,
65+
database: str,
66+
cloud_sql_conn_name: str,
67+
project_id: str,
68+
location: str,
69+
) -> None:
70+
cloud_sql_credential = bq_connection.CloudSqlCredential(
71+
{
72+
"username": mysql_username,
73+
"password": mysql_password,
74+
}
75+
)
76+
cloud_sql_properties = bq_connection.CloudSqlProperties(
77+
{
78+
"type_": bq_connection.CloudSqlProperties.DatabaseType.MYSQL,
79+
"database": database,
80+
"instance_id": cloud_sql_conn_name,
81+
"credential": cloud_sql_credential,
82+
}
83+
)
84+
create_mysql_connection.create_mysql_connection(
85+
project_id=project_id,
86+
location=location,
87+
cloud_sql_properties=cloud_sql_properties,
88+
)
89+
out, _ = capsys.readouterr()
90+
assert "Created connection successfully:" in out

testing/test-env.tmpl.sh

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
# 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+
# Environment variables for system tests.
16+
export GOOGLE_CLOUD_PROJECT=your-project-id
17+
export GCP_PROJECT=$GOOGLE_CLOUD_PROJECT
18+
export FIRESTORE_PROJECT=
19+
20+
export CLOUD_STORAGE_BUCKET=$GOOGLE_CLOUD_PROJECT
21+
export REQUESTER_PAYS_TEST_BUCKET="${CLOUD_STORAGE_BUCKET}-requester-pays-test"
22+
export API_KEY=
23+
export BIGTABLE_CLUSTER=bigtable-test
24+
export BIGTABLE_ZONE=us-central1-c
25+
export BIGTABLE_INSTANCE=
26+
export SPANNER_INSTANCE=
27+
export COMPOSER_LOCATION=us-central1
28+
export COMPOSER_ENVIRONMENT=
29+
export COMPOSER2_ENVIRONMENT=
30+
# Webserver for COMPOSER2_ENVIRONMENT
31+
export COMPOSER2_WEB_SERVER_URL=
32+
export CLOUD_KMS_KEY=
33+
34+
export MYSQL_INSTANCE=
35+
export MYSQL_INSTANCE_ID=
36+
export MYSQL_INSTANCE_LOCATION=
37+
export MYSQL_USER=
38+
export MYSQL_PASSWORD=
39+
export MYSQL_DATABASE=
40+
export MYSQL_HOST=localhost:3306
41+
export POSTGRES_INSTANCE=
42+
export POSTGRES_USER=
43+
export POSTGRES_PASSWORD=
44+
export POSTGRES_DATABASE=
45+
export POSTGRES_HOST=localhost:5432
46+
export SQLSERVER_INSTANCE=
47+
export SQLSERVER_USER=
48+
export SQLSERVER_PASSWORD=
49+
export SQLSERVER_DATABASE=
50+
export SQLSERVER_HOST=127.0.0.1:1433
51+
export DB_SOCKET_DIR=
52+
53+
export KG_API_KEY=
54+
export SLACK_TEST_SIGNATURE=
55+
export SLACK_SECRET=
56+
export FUNCTIONS_TOPIC=
57+
58+
# Service account for HMAC samples
59+
export HMAC_KEY_TEST_SERVICE_ACCOUNT=
60+
61+
# Environment variables for App Engine Flexible system tests.
62+
export GA_TRACKING_ID=
63+
export SQLALCHEMY_DATABASE_URI=sqlite://
64+
export PUBSUB_TOPIC=gae-mvm-pubsub-topic
65+
export PUBSUB_VERIFICATION_TOKEN=1234abc
66+
67+
# Secret Manager Test Vars
68+
export GCLOUD_SECRETS_SERVICE_ACCOUNT=
69+
70+
# Automl
71+
# A centralized project is used to remove duplicate work across all 7 languages
72+
# and reduce the management of these resources.
73+
# https://docs.google.com/document/d/1-E7zTNqBm9ex7XIOhzMHCupwKWieyMKgAVwrRK5JTVY
74+
export AUTOML_PROJECT_ID=
75+
76+
export ENTITY_EXTRACTION_DATASET_ID=
77+
export ENTITY_EXTRACTION_MODEL_ID=
78+
79+
export SENTIMENT_ANALYSIS_DATASET_ID=
80+
export SENTIMENT_ANALYSIS_MODEL_ID=
81+
82+
export TEXT_CLASSIFICATION_DATASET_ID=
83+
export TEXT_CLASSIFICATION_MODEL_ID=
84+
85+
export TRANSLATION_DATASET_ID=
86+
export TRANSLATION_MODEL_ID=
87+
88+
export VISION_CLASSIFICATION_DATASET_ID=
89+
export VISION_CLASSIFICATION_MODEL_ID=
90+
91+
export OBJECT_DETECTION_DATASET_ID=
92+
# AutoML model takes 8-24 hours to create, having predefined
93+
# and centralized models remove duplicate work across all languages.
94+
export OBJECT_DETECTION_MODEL_ID=
95+
96+
# For git operations in the test driver(testing/run_tests.sh).
97+
# These are optional, but for avoiding flakes in Kokoro builds.
98+
export GITHUB_ACCESS_TOKEN=
99+
export GITHUB_USERNAME=
100+
101+
# Cloud Run
102+
# For run/idp example, a Firebase IDP token
103+
export IDP_KEY=
104+
# For run/filesystem
105+
export IP_ADDRESS=
106+
export CONNECTOR=
107+
108+
# Dialogflow examples.
109+
export SMART_REPLY_MODEL=
110+
export SMART_REPLY_ALLOWLIST=

0 commit comments

Comments
 (0)