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
loferris merged 41 commits into
mainfrom
feat_create_connection
Apr 15, 2022
Merged

docs(samples): create connection sample for MySQL instance#147
loferris merged 41 commits into
mainfrom
feat_create_connection

Conversation

@loferris

Copy link
Copy Markdown
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 teamDecember 16, 2021 04:19
@loferris
loferris requested a review from a team as a code ownerDecember 16, 2021 04:19
@snippet-bot

snippet-botBot commented Dec 16, 2021

Copy link
Copy Markdown

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-labelproduct-auto-labelBot added the api: bigqueryconnection Issues related to the googleapis/python-bigquery-connection API. label Dec 16, 2021
@loferrisloferris changed the title Feat create connectionfeat: create connection sample for MySQL instanceDec 16, 2021
@loferris
loferris requested a review from tswastDecember 16, 2021 04:21
@product-auto-labelproduct-auto-labelBot 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"""

Copy link
Copy Markdown

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
Copy Markdown
ContributorAuthor

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]

Copy link
Copy Markdown

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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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)

Copy link
Copy Markdown

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
Copy Markdown
ContributorAuthor

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(

Copy link
Copy Markdown

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
Copy Markdown
ContributorAuthor

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

Copy link
Copy Markdown

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
Copy Markdown
ContributorAuthor

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 ownerJanuary 16, 2022 14:18
tswast
tswast previously requested changes Feb 14, 2022

@tswasttswast left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

@partheaparthea changed the title feat: create connection sample for MySQL instancedocs(samples): create connection sample for MySQL instanceMar 5, 2022
@loferris
loferris requested a review from a teamMarch 31, 2022 22:14
@loferris
loferris requested review from kurtisvg and tswastMarch 31, 2022 22:31
@loferris
loferris requested a review from kurtisvgApril 6, 2022 21:38
@loferris

Copy link
Copy Markdown
ContributorAuthor

@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
Copy Markdown

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 databasepassword="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
Copy Markdown

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 tswastApril 6, 2022 22:37

@shollymanshollyman left a comment

Copy link
Copy Markdown

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.

@loferrisloferris 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-kokoroyoshi-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 mainApr 15, 2022
@loferris
loferris deleted the feat_create_connection branch April 15, 2022 19:12
@release-pleaserelease-pleaseBot mentioned this pull request Apr 15, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: bigqueryconnectionIssues related to the googleapis/python-bigquery-connection API.samplesIssues that are directly related to samples.size: lPull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@loferris@tswast@shollyman@kurtisvg@yoshi-kokoro@parthea@meredithslota