Skip to content

Commit 74f66c2

Browse files
loferrispartheameredithslotagcf-owl-bot[bot]
authored andcommitted
docs(samples): create connection sample for MySQL instance (GoogleCloudPlatform#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 <[email protected]> Co-authored-by: meredithslota <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent daa5034 commit 74f66c2

File tree

3 files changed

+178
-4
lines changed

3 files changed

+178
-4
lines changed

bigquery-connection/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

0 commit comments

Comments
 (0)