Skip to content

Commit

Permalink
docs(samples): create connection sample for MySQL instance (GoogleClo…
Browse files Browse the repository at this point in the history
…udPlatform#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>
  • Loading branch information
4 people authored and Avani-Thakker-Crest committed Jun 21, 2023
1 parent daa5034 commit 74f66c2
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 4 deletions.
28 changes: 24 additions & 4 deletions bigquery-connection/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 bigquery-connection/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 bigquery-connection/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

0 comments on commit 74f66c2

Please sign in to comment.