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

docs(samples): create connection sample for MySQL instance #147

Merged
merged 41 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5177628
setup for cloudsql connection sample
Dec 9, 2021
2fd9d0c
fleshed out sample
Dec 10, 2021
ace2625
complete sample
Dec 16, 2021
c92449c
Merge branch 'main' into feat_create_connection
parthea Jan 16, 2022
528ae61
Merge branch 'main' into feat_create_connection
parthea Jan 22, 2022
f08a351
Merge branch 'main' into feat_create_connection
meredithslota Feb 9, 2022
ba438b1
Merge branch 'main' of github.com:googleapis/python-bigquery-connecti…
Mar 30, 2022
656b335
changing region tag pattern
Mar 31, 2022
33bd57c
Merge branch 'feat_create_connection' of github.com:googleapis/python…
Mar 31, 2022
c9186f8
Merge branch 'main' into feat_create_connection
loferris Mar 31, 2022
0470125
adding new environment variables
Mar 31, 2022
b8fbd07
adding missing env variable
Mar 31, 2022
3a8c0c0
fixing fixture with correct env variable
Mar 31, 2022
5849514
refactor
Apr 5, 2022
0671b3c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 5, 2022
5b37264
Merge branch 'main' into feat_create_connection
loferris Apr 5, 2022
a7d2327
refactor test imports
Apr 5, 2022
c9ada59
refactoring test imports and merge conflicts
Apr 5, 2022
9cdacb2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 5, 2022
91b5ab5
kokoro lint errors
Apr 6, 2022
1243a12
resolving merge conflicts
Apr 6, 2022
c6c94fe
Merge branch 'main' into feat_create_connection
loferris Apr 6, 2022
fd10993
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 6, 2022
b38594f
Merge branch 'feat_create_connection' of https://github.com/googleapi…
gcf-owl-bot[bot] Apr 6, 2022
54b2f43
changing fixture use
Apr 6, 2022
6d67498
Merge branch 'feat_create_connection' of github.com:googleapis/python…
Apr 6, 2022
a5fa435
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 6, 2022
73e5702
refactoring variables
Apr 6, 2022
9df8821
Merge branch 'feat_create_connection' of github.com:googleapis/python…
Apr 6, 2022
009032d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 6, 2022
85ca087
fixing imports
Apr 6, 2022
f774ac6
merge conflicts and fixing imports
Apr 6, 2022
3da2c23
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 6, 2022
29c9505
changing fixture syntax
Apr 6, 2022
e244fbc
Merge branch 'feat_create_connection' of github.com:googleapis/python…
Apr 6, 2022
209e48c
fixing import
Apr 6, 2022
80e444e
fixing typecasting
Apr 6, 2022
01b2410
updating fixture usage
Apr 14, 2022
06d6e11
changing fixtures
Apr 14, 2022
2059016
Merge branch 'main' into feat_create_connection
loferris Apr 14, 2022
c27e66c
refactor fixtures
Apr 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions samples/snippets/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,35 @@


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


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


@pytest.fixture(scope="session")
def location() -> str:
return "US"


@pytest.fixture(scope="session")
def database() -> str:
return os.environ["MYSQL_DATABASE"]


@pytest.fixture(scope="session")
def cloud_sql_conn_name() -> str:
return os.environ["MYSQL_INSTANCE"]


@pytest.fixture(scope="session")
def mysql_username() -> str:
return os.environ["MYSQL_USER"]


@pytest.fixture(scope="session")
def mysql_password() -> str:
return os.environ["MYSQL_PASSWORD"]
64 changes: 64 additions & 0 deletions samples/snippets/create_mysql_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2021 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
#
# https://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.

# [START bigqueryconnection_connection_create]
from google.cloud import bigquery_connection_v1 as bq_connection

"""This sample shows how to create a BigQuery connection with a Cloud SQL for MySQL database"""


def main() -> None:
# TODO(developer): Set all variables for your Cloud SQL for MySQL connection.
project_id = "your-project-id" # set project_id
location = "US" # set location
# See: https://cloud.google.com/bigquery/docs/locations for a list of
# available locations.
database = "my-database" # set database name
username = "my-username" # set database username
password = "my-password" # set database password
cloud_sql_conn_name = "" # set the name of your connection

cloud_sql_credential = bq_connection.CloudSqlCredential(
{
"username": username,
"password": password,
}
)
cloud_sql_properties = bq_connection.CloudSqlProperties(
{
"type_": bq_connection.CloudSqlProperties.DatabaseType.MYSQL,
"database": database,
"instance_id": cloud_sql_conn_name,
"credential": cloud_sql_credential,
}
)
create_mysql_connection(project_id, location, cloud_sql_properties)


def create_mysql_connection(
project_id: str,
location: str,
cloud_sql_properties: bq_connection.CloudSqlProperties,
) -> None:
connection = bq_connection.types.Connection({"cloud_sql": cloud_sql_properties})
client = bq_connection.ConnectionServiceClient()
parent = client.common_location_path(project_id, location)
request = bq_connection.CreateConnectionRequest(
{"parent": parent, "connection": connection}
)
response = client.create_connection(request)
print(f"Created connection successfully: {response.name}")


# [END bigqueryconnection_connection_create]
90 changes: 90 additions & 0 deletions samples/snippets/create_mysql_connection_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright 2021 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
#
# https://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.

import google.api_core.exceptions
from google.cloud import bigquery_connection_v1 as bq_connection
from google.cloud.bigquery_connection_v1.services import connection_service
import pytest
import test_utils.prefixer

from . import create_mysql_connection

connection_prefixer = test_utils.prefixer.Prefixer("py-bq-r", "snippets", separator="-")


@pytest.fixture(scope="session")
def location_path(
connection_client: connection_service.ConnectionServiceClient(),
project_id: str,
location: str,
) -> str:
return connection_client.common_location_path(project_id, location)


@pytest.fixture(scope="module", autouse=True)
def cleanup_connection(
connection_client: connection_service.ConnectionServiceClient, location_path: str
) -> None:
for connection in connection_client.list_connections(parent=location_path):
connection_id = connection.name.split("/")[-1]
if connection_prefixer.should_cleanup(connection_id):
connection_client.delete_connection(name=connection.name)


@pytest.fixture(scope="session")
def connection_id(
connection_client: connection_service.ConnectionServiceClient,
project_id: str,
location: str,
) -> str:
id_ = connection_prefixer.create_prefix()
yield id_

connection_name = connection_client.connection_path(project_id, location, id_)
try:
connection_client.delete_connection(name=connection_name)
except google.api_core.exceptions.NotFound:
pass


def test_create_mysql_connection(
capsys: pytest.CaptureFixture,
mysql_username: str,
mysql_password: str,
database: str,
cloud_sql_conn_name: str,
project_id: str,
location: str,
) -> None:
cloud_sql_credential = bq_connection.CloudSqlCredential(
{
"username": mysql_username,
"password": mysql_password,
}
)
cloud_sql_properties = bq_connection.CloudSqlProperties(
{
"type_": bq_connection.CloudSqlProperties.DatabaseType.MYSQL,
"database": database,
"instance_id": cloud_sql_conn_name,
"credential": cloud_sql_credential,
}
)
create_mysql_connection.create_mysql_connection(
project_id=project_id,
location=location,
cloud_sql_properties=cloud_sql_properties,
)
out, _ = capsys.readouterr()
assert "Created connection successfully:" in out
110 changes: 110 additions & 0 deletions testing/test-env.tmpl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright 2022 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.

# Environment variables for system tests.
export GOOGLE_CLOUD_PROJECT=your-project-id
export GCP_PROJECT=$GOOGLE_CLOUD_PROJECT
export FIRESTORE_PROJECT=

export CLOUD_STORAGE_BUCKET=$GOOGLE_CLOUD_PROJECT
export REQUESTER_PAYS_TEST_BUCKET="${CLOUD_STORAGE_BUCKET}-requester-pays-test"
export API_KEY=
export BIGTABLE_CLUSTER=bigtable-test
export BIGTABLE_ZONE=us-central1-c
export BIGTABLE_INSTANCE=
export SPANNER_INSTANCE=
export COMPOSER_LOCATION=us-central1
export COMPOSER_ENVIRONMENT=
export COMPOSER2_ENVIRONMENT=
# Webserver for COMPOSER2_ENVIRONMENT
export COMPOSER2_WEB_SERVER_URL=
export CLOUD_KMS_KEY=

export MYSQL_INSTANCE=
export MYSQL_INSTANCE_ID=
export MYSQL_INSTANCE_LOCATION=
export MYSQL_USER=
export MYSQL_PASSWORD=
export MYSQL_DATABASE=
export MYSQL_HOST=localhost:3306
export POSTGRES_INSTANCE=
export POSTGRES_USER=
export POSTGRES_PASSWORD=
export POSTGRES_DATABASE=
export POSTGRES_HOST=localhost:5432
export SQLSERVER_INSTANCE=
export SQLSERVER_USER=
export SQLSERVER_PASSWORD=
export SQLSERVER_DATABASE=
export SQLSERVER_HOST=127.0.0.1:1433
export DB_SOCKET_DIR=

export KG_API_KEY=
export SLACK_TEST_SIGNATURE=
export SLACK_SECRET=
export FUNCTIONS_TOPIC=

# Service account for HMAC samples
export HMAC_KEY_TEST_SERVICE_ACCOUNT=

# Environment variables for App Engine Flexible system tests.
export GA_TRACKING_ID=
export SQLALCHEMY_DATABASE_URI=sqlite://
export PUBSUB_TOPIC=gae-mvm-pubsub-topic
export PUBSUB_VERIFICATION_TOKEN=1234abc

# Secret Manager Test Vars
export GCLOUD_SECRETS_SERVICE_ACCOUNT=

# Automl
# A centralized project is used to remove duplicate work across all 7 languages
# and reduce the management of these resources.
# https://docs.google.com/document/d/1-E7zTNqBm9ex7XIOhzMHCupwKWieyMKgAVwrRK5JTVY
export AUTOML_PROJECT_ID=

export ENTITY_EXTRACTION_DATASET_ID=
export ENTITY_EXTRACTION_MODEL_ID=

export SENTIMENT_ANALYSIS_DATASET_ID=
export SENTIMENT_ANALYSIS_MODEL_ID=

export TEXT_CLASSIFICATION_DATASET_ID=
export TEXT_CLASSIFICATION_MODEL_ID=

export TRANSLATION_DATASET_ID=
export TRANSLATION_MODEL_ID=

export VISION_CLASSIFICATION_DATASET_ID=
export VISION_CLASSIFICATION_MODEL_ID=

export OBJECT_DETECTION_DATASET_ID=
# AutoML model takes 8-24 hours to create, having predefined
# and centralized models remove duplicate work across all languages.
export OBJECT_DETECTION_MODEL_ID=

# For git operations in the test driver(testing/run_tests.sh).
# These are optional, but for avoiding flakes in Kokoro builds.
export GITHUB_ACCESS_TOKEN=
export GITHUB_USERNAME=

# Cloud Run
# For run/idp example, a Firebase IDP token
export IDP_KEY=
# For run/filesystem
export IP_ADDRESS=
export CONNECTOR=

# Dialogflow examples.
export SMART_REPLY_MODEL=
export SMART_REPLY_ALLOWLIST=