-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add
ConnectionName
class (#1186)
This PR refactors all instance connection name related code into its own file connection_name.py It introduces the ConnectionName class which will make tracking if a DNS name was given to the Connector easier in the future.
- Loading branch information
1 parent
3b24c10
commit ef7d8fe
Showing
15 changed files
with
147 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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) -> str: | ||
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 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 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 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 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 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 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 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 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.