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
Changes from 3 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
46 changes: 32 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,10 @@

logger = logging.getLogger(name=__name__)

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


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

# 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._instance_uri,
self._project,
self._region,
self._cluster,
self._name,
) = self._parse_instance_uri(instance_uri)
jackwotherspoon marked this conversation as resolved.
Show resolved Hide resolved

self._client = client
self._keys = keys
Expand All @@ -83,6 +82,25 @@ def __init__(
self._current: asyncio.Task = self._schedule_refresh(0)
self._next: asyncio.Task = self._current

def _parse_instance_uri(self, instance_uri: str) -> Tuple[str, 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>, "
f"got {instance_uri}."
)

instance_uri_split = INSTANCE_URI_REGEX.split(instance_uri)

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

async def _perform_refresh(self) -> RefreshResult:
"""
Perform a refresh operation on an AlloyDB instance.
Expand Down