Skip to content
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

feat: add support for domain-scoped projects #185

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions google/cloud/alloydb/connector/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import asyncio
import logging
import re
from typing import Tuple, TYPE_CHECKING

from google.cloud.alloydb.connector.exceptions import RefreshError
Expand All @@ -33,6 +34,29 @@

logger = logging.getLogger(name=__name__)

INSTANCE_URI_REGEX = re.compile(
"projects/([^:]+(:[^:]+)?)/locations/([^:]+)/clusters/([^:]+)/instances/([^:]+)"
)


def parse_instance_uri(instance_uri: str) -> Tuple[str, str, str, str]:
jackwotherspoon marked this conversation as resolved.
Show resolved Hide resolved
# should take form "projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>"
if INSTANCE_URI_REGEX.fullmatch(instance_uri) is None:
raise ValueError(
"Arg `instance_uri` must have "
"format: projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>, projects/<DOMAIN>:<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>"
f"got {instance_uri}."
)

instance_uri_split = INSTANCE_URI_REGEX.split(instance_uri)

return (
jackwotherspoon marked this conversation as resolved.
Show resolved Hide resolved
instance_uri_split[1],
instance_uri_split[3],
instance_uri_split[4],
instance_uri_split[5],
)


class Instance:
"""
Expand All @@ -55,21 +79,16 @@ def __init__(
client: AlloyDBClient,
keys: asyncio.Future[Tuple[rsa.RSAPrivateKey, str]],
) -> None:

self._instance_uri = instance_uri
jackwotherspoon marked this conversation as resolved.
Show resolved Hide resolved

# validate and parse instance_uri
instance_uri_split = instance_uri.split("/")
# should take form "projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>"
if len(instance_uri_split) == 8:
self._instance_uri = instance_uri
self._project = instance_uri_split[1]
self._region = instance_uri_split[3]
self._cluster = instance_uri_split[5]
self._name = instance_uri_split[7]
else:
raise ValueError(
"Arg `instance_uri` must have "
"format: projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>, "
f"got {instance_uri}."
)
(
self._project,
self._region,
self._cluster,
self._name,
) = parse_instance_uri(instance_uri)

self._client = client
self._keys = keys
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,32 @@
import pytest

from google.cloud.alloydb.connector.exceptions import RefreshError
from google.cloud.alloydb.connector.instance import Instance
from google.cloud.alloydb.connector.instance import Instance, parse_instance_uri
from google.cloud.alloydb.connector.refresh import _is_valid, RefreshResult
from google.cloud.alloydb.connector.utils import generate_keys


@pytest.mark.asycio
async def test_parser_instance_uri() -> None:
"""
Test to check whether the __init__ method of Instance
can tell if the instance URI that's passed in is formatted correctly.
"""
instance_uri = [
"projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance",
"projects/google.com:test-project/locations/test-region/clusters/test-cluster/instances/test-instance",
]
jackwotherspoon marked this conversation as resolved.
Show resolved Hide resolved

for uri in instance_uri:
project, region, cluster, name = parse_instance_uri(uri)
assert (
project in ["test-project", "google.com:test-project"]
and region == "test-region"
and cluster == "test-cluster"
and name == "test-instance"
)


@pytest.mark.asyncio
async def test_Instance_init() -> None:
"""
Expand Down