Skip to content
This repository has been 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

Conversation

loferris
Copy link
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

@loferris loferris requested a review from a team December 16, 2021 04:19
@loferris loferris requested a review from a team as a code owner December 16, 2021 04:19
@snippet-bot
Copy link

snippet-bot bot commented Dec 16, 2021

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added the api: bigqueryconnection Issues related to the googleapis/python-bigquery-connection API. label Dec 16, 2021
@loferris loferris changed the title Feat create connection feat: create connection sample for MySQL instance Dec 16, 2021
@loferris loferris requested a review from tswast December 16, 2021 04:21
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Dec 16, 2021
from google.cloud import bigquery_connection_v1 as bq_connection
from google.cloud.bigquery_connection_v1 import types as connection_types

"""This sample shows how to create a BigQuery connection with a Cloud MySql database"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should be "Cloud SQL for MySQL"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in recent commit!

Comment on lines 32 to 41
# [START_EXCLUDE]
original_project_id = project_id
original_location = location
original_connection_id = connection_id
original_database = database
original_instance = instance
original_instance_location = instance_location
original_username = username
original_password = password
# [END_EXCLUDE]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with these excludes and reassignments? Can we just leave this as is so the suer can edit it, and test the behavior of the function below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pattern we've been following recently - I believe the purpose is to allow the function to be "live" and testable while still giving a clear view to the developer on how to fill in their own values. To me I think it makes more sense to have developer values set as statics above the function itself, but I'm not sure if that's consistent with what we've been doing. @tswast may have an opinion!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EXCLUDE pattern is probably not necessary if the snippet includes the whole function definition, as you've done here. Using default values there would serve the same purpose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I tend to avoid EXCLUDE even with this pattern, as it shows "..." when rendered in the docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/googleapis/python-datacatalog/blob/main/samples/snippets/data_catalog_ptm_create_taxonomy.py is a more typical example.

In core BigQuery we have a lot of samples that have the tags within the function, so we've kept with that style.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight update -- I've started to use the more standard style in some BigQuery samples: googleapis/python-bigquery#1137

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempted in recent commit! Would love feedback!

Comment on lines 74 to 84
cloud_sql_credential = bq_connection.CloudSqlCredential()
cloud_sql_credential.username = username
cloud_sql_credential.password = password
cloud_sql_properties = bq_connection.CloudSqlProperties()
cloud_sql_properties.type_ = bq_connection.CloudSqlProperties.DatabaseType.MYSQL
cloud_sql_properties.database = database
cloud_sql_properties.instance_id = instance_id
cloud_sql_properties.credential = cloud_sql_credential
connection = connection_types.Connection()
connection.cloud_sql = cloud_sql_properties
create_connection(project_id, location, connection_id, connection)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all stuff that should happen in the sample function below.

nit: you may also want to consider using the mapping value here for these protos e.g.:

cloud_sql_credential = bq_connection.CloudSqlCredential({
    "username": username,
    "password": password
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in recent commit!

return connection


def create_connection(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should it be "create_mysql_connection"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in recent commit!

connection = connection_types.Connection()
connection.cloud_sql = cloud_sql_properties
create_connection(project_id, location, connection_id, connection)
return connection

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we avoid return types in the function to force the sample to interact with the created object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in recent commit!

@parthea parthea requested a review from a team as a code owner January 16, 2022 14:18
tswast
tswast previously requested changes Feb 14, 2022
Copy link
Contributor

@tswast tswast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to Kurtis's requested changes.

Comment on lines 32 to 41
# [START_EXCLUDE]
original_project_id = project_id
original_location = location
original_connection_id = connection_id
original_database = database
original_instance = instance
original_instance_location = instance_location
original_username = username
original_password = password
# [END_EXCLUDE]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight update -- I've started to use the more standard style in some BigQuery samples: googleapis/python-bigquery#1137

@parthea parthea changed the title feat: create connection sample for MySQL instance docs(samples): create connection sample for MySQL instance Mar 5, 2022
@loferris loferris requested a review from a team as a code owner March 31, 2022 22:14
@loferris loferris requested a review from kurtisvg April 6, 2022 21:38
@loferris
Copy link
Contributor Author

loferris commented Apr 6, 2022

@kurtisvg All comments responded to!

Comment on lines 22 to 38
# TODO(developer): Set project_id to the project ID containing the
# connection.
project_id = "your-project-id"
# TODO(developer): Set location to the location of the connection.
# See: https://cloud.google.com/bigquery/docs/locations for a list of
# available locations.
location = "US"
# TODO(developer): Set database to the name of the database in which you're creating the connection.
database = "my-database"
# TODO(developer): Set instance to the instance where you're creating the connection.
instance_name = "my-instance"
# TODO(developer): Set instance_location to the location of the instance where you are creating the connection.
instance_location = "my-instance-location"
# TODO(developer): Set username to the database username.
username = "my-username"
# TODO(developer): Set password to the database password.
password = "my-password"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would try to use just 1 TODO to represent all of these inputs to reduce the vertical space this takes up. If you feel like it needs additional context, try to leave a short comment in the follow up. E.g.:

    username = "my-username" # username for your database
    password = "my-password" # password for your database user

username = "my-username"
# TODO(developer): Set password to the database password.
password = "my-password"
cloud_sql_conn_name = f"{project_id}:{instance_location}:{instance_name}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to have the user input these as 3 different fields. You can just ask them for the "instance connection name". This is a specific field in the instance metadata and is shown to the user in console, e.g.:
image

For reference, this is what we do in Cloud SQL samples:

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/HEAD/cloud-sql/mysql/sqlalchemy/main.py#L160-L173

Lo Ferris added 2 commits April 6, 2022 15:35
…-bigquery-connection into feat_create_connection

merge conflicts
@loferris loferris requested review from a team and removed request for stephaniewang526 and tswast April 6, 2022 22:37
Copy link

@shollyman shollyman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to pay more attention to how we're dealing with test resource setup/lifecycle, but that is outside the scope of this PR.

@loferris loferris added kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed kokoro:run Add this label to force Kokoro to re-run the tests. labels Apr 15, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Apr 15, 2022
@loferris loferris merged commit 8e664be into main Apr 15, 2022
@loferris loferris deleted the feat_create_connection branch April 15, 2022 19:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: bigqueryconnection Issues related to the googleapis/python-bigquery-connection API. samples Issues that are directly related to samples. size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants