diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index d1c7b49c8be7..c6cb406334c6 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -71,6 +71,7 @@ /texttospeech/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers /translate/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers /video/transcoder/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers +/bigquery-connection/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers # Cloud SDK Databases & Data Analytics teams # ---* Cloud Native DB diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index 23bca271ca56..a4c13da37e93 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -70,6 +70,7 @@ assign_issues_by: - "api: texttospeech" - "api: translate" - "api: vision" + - "api: bigquery" to: - GoogleCloudPlatform/dee-data-ai diff --git a/bigquery-connection/snippets/__init__.py b/bigquery-connection/snippets/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/bigquery-connection/snippets/conftest.py b/bigquery-connection/snippets/conftest.py new file mode 100644 index 000000000000..eca2a7fd12d0 --- /dev/null +++ b/bigquery-connection/snippets/conftest.py @@ -0,0 +1,53 @@ +# 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 os + +from google.cloud.bigquery_connection_v1.services import connection_service +import pytest + + +@pytest.fixture(scope="session") +def connection_client() -> connection_service.ConnectionServiceClient: + return connection_service.ConnectionServiceClient() + + +@pytest.fixture(scope="session") +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"] diff --git a/bigquery-connection/snippets/create_mysql_connection.py b/bigquery-connection/snippets/create_mysql_connection.py new file mode 100644 index 000000000000..f4577afbb703 --- /dev/null +++ b/bigquery-connection/snippets/create_mysql_connection.py @@ -0,0 +1,66 @@ +# 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_create_connection] +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 + transport = "grpc" # Set the transport to either "grpc" or "rest" + + 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, transport) + + +def create_mysql_connection( + project_id: str, + location: str, + cloud_sql_properties: bq_connection.CloudSqlProperties, + transport: str, +) -> None: + connection = bq_connection.types.Connection({"cloud_sql": cloud_sql_properties}) + client = bq_connection.ConnectionServiceClient(transport=transport) + 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_create_connection] diff --git a/bigquery-connection/snippets/create_mysql_connection_test.py b/bigquery-connection/snippets/create_mysql_connection_test.py new file mode 100644 index 000000000000..501af447d8b6 --- /dev/null +++ b/bigquery-connection/snippets/create_mysql_connection_test.py @@ -0,0 +1,93 @@ +# 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 + + +@pytest.mark.parametrize("transport", ["grpc", "rest"]) +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, + transport: 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, + transport=transport, + ) + out, _ = capsys.readouterr() + assert "Created connection successfully:" in out diff --git a/bigquery-connection/snippets/noxfile_config.py b/bigquery-connection/snippets/noxfile_config.py new file mode 100644 index 000000000000..4abe361c51f5 --- /dev/null +++ b/bigquery-connection/snippets/noxfile_config.py @@ -0,0 +1,38 @@ +# 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 +# +# 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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be inported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.6"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/bigquery-connection/snippets/quickstart.py b/bigquery-connection/snippets/quickstart.py new file mode 100644 index 000000000000..463bd8a87ccc --- /dev/null +++ b/bigquery-connection/snippets/quickstart.py @@ -0,0 +1,33 @@ +# 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_quickstart] + +from google.cloud import bigquery_connection_v1 as bq_connection + + +def main( + project_id: str = "your-project-id", location: str = "US", transport: str = "grpc" +) -> None: + """Prints details and summary information about connections for a given admin project and location""" + client = bq_connection.ConnectionServiceClient(transport=transport) + print(f"List of connections in project {project_id} in location {location}") + req = bq_connection.ListConnectionsRequest( + parent=client.common_location_path(project_id, location) + ) + for connection in client.list_connections(request=req): + print(f"\tConnection {connection.friendly_name} ({connection.name})") + + +# [END bigqueryconnection_quickstart] diff --git a/bigquery-connection/snippets/quickstart_test.py b/bigquery-connection/snippets/quickstart_test.py new file mode 100644 index 000000000000..2a8df8f59eaf --- /dev/null +++ b/bigquery-connection/snippets/quickstart_test.py @@ -0,0 +1,26 @@ +# 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 pytest + +from . import quickstart + + +@pytest.mark.parametrize("transport", ["grpc", "rest"]) +def test_quickstart( + capsys: pytest.CaptureFixture, project_id: str, location: str, transport: str +) -> None: + quickstart.main(project_id, location, transport) + out, _ = capsys.readouterr() + assert "List of connections in project" in out diff --git a/bigquery-connection/snippets/requirements-test.txt b/bigquery-connection/snippets/requirements-test.txt new file mode 100644 index 000000000000..8e8f5c2707e9 --- /dev/null +++ b/bigquery-connection/snippets/requirements-test.txt @@ -0,0 +1,2 @@ +pytest==7.3.2 +google-cloud-testutils==1.3.3 \ No newline at end of file diff --git a/bigquery-connection/snippets/requirements.txt b/bigquery-connection/snippets/requirements.txt new file mode 100644 index 000000000000..9339b226aafc --- /dev/null +++ b/bigquery-connection/snippets/requirements.txt @@ -0,0 +1 @@ +google-cloud-bigquery-connection==1.12.0 \ No newline at end of file