-
Notifications
You must be signed in to change notification settings - Fork 83
refactor: add ConnectionName class
#1186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright 2024 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. | ||
|
|
||
| from dataclasses import dataclass | ||
| import re | ||
|
|
||
| # Instance connection name is the format <PROJECT>:<REGION>:<INSTANCE_NAME> | ||
| # Additionally, we have to support legacy "domain-scoped" projects | ||
| # (e.g. "google.com:PROJECT") | ||
| CONN_NAME_REGEX = re.compile(("([^:]+(:[^:]+)?):([^:]+):([^:]+)")) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConnectionName: | ||
| """ConnectionName represents a Cloud SQL instance's "instance connection name". | ||
|
|
||
| Takes the format "<PROJECT>:<REGION>:<INSTANCE_NAME>". | ||
| """ | ||
|
|
||
| project: str | ||
| region: str | ||
| instance_name: str | ||
|
|
||
| def __str__(self): | ||
| return f"{self.project}:{self.region}:{self.instance_name}" | ||
|
|
||
|
|
||
| def _parse_instance_connection_name(connection_name: str) -> ConnectionName: | ||
| if CONN_NAME_REGEX.fullmatch(connection_name) is None: | ||
| raise ValueError( | ||
| "Arg `instance_connection_string` must have " | ||
| "format: PROJECT:REGION:INSTANCE, " | ||
| f"got {connection_name}." | ||
| ) | ||
| connection_name_split = CONN_NAME_REGEX.split(connection_name) | ||
| return ConnectionName( | ||
| connection_name_split[1], | ||
| connection_name_split[3], | ||
| connection_name_split[4], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright 2024 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. | ||
|
|
||
| import pytest # noqa F401 Needed to run the tests | ||
|
|
||
| from google.cloud.sql.connector.connection_name import _parse_instance_connection_name | ||
| from google.cloud.sql.connector.connection_name import ConnectionName | ||
|
|
||
|
|
||
| def test_ConnectionName() -> None: | ||
| conn_name = ConnectionName("project", "region", "instance") | ||
| # test class attributes are set properly | ||
| assert conn_name.project == "project" | ||
| assert conn_name.region == "region" | ||
| assert conn_name.instance_name == "instance" | ||
| # test ConnectionName str() method prints instance connection name | ||
| assert str(conn_name) == "project:region:instance" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "connection_name, expected", | ||
| [ | ||
| ("project:region:instance", ConnectionName("project", "region", "instance")), | ||
| ( | ||
| "domain-prefix:project:region:instance", | ||
| ConnectionName("domain-prefix:project", "region", "instance"), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_parse_instance_connection_name( | ||
| connection_name: str, expected: ConnectionName | ||
| ) -> None: | ||
| """ | ||
| Test that _parse_instance_connection_name works correctly on | ||
| normal instance connection names and domain-scoped projects. | ||
| """ | ||
| assert expected == _parse_instance_connection_name(connection_name) | ||
|
|
||
|
|
||
| def test_parse_instance_connection_name_bad_conn_name() -> None: | ||
| """ | ||
| Tests that ValueError is thrown for bad instance connection names. | ||
| """ | ||
| with pytest.raises(ValueError): | ||
| _parse_instance_connection_name("project:instance") # missing region |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
black formatter uses line-length of 88 while isort was using 85, setting isort to 88 as well.
Found this as the new import for
_parse_instance_connection_namewas 87 characters and causing isort and black to argue with one another.