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 cloud region path handling for when this is allowed by the backend #240

Merged
merged 1 commit into from Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions google/cloud/pubsublite/types/location.py
Expand Up @@ -30,6 +30,10 @@ def parse(to_parse: str):
raise InvalidArgument("Invalid region name: " + to_parse)
return CloudRegion(name=splits[0] + "-" + splits[1])

@property
def region(self):
return self


class CloudZone(NamedTuple):
region: CloudRegion
Expand Down
30 changes: 26 additions & 4 deletions google/cloud/pubsublite/types/paths.py
Expand Up @@ -19,17 +19,39 @@
from google.cloud.pubsublite.types.location import CloudZone, CloudRegion


def _parse_location(to_parse: str) -> Union[CloudRegion, CloudZone]:
try:
return CloudZone.parse(to_parse)
except InvalidArgument:
pass
try:
return CloudRegion.parse(to_parse)
except InvalidArgument:
pass
raise InvalidArgument("Invalid location name: " + to_parse)


class LocationPath(NamedTuple):
project: Union[int, str]
location: Union[CloudRegion, CloudZone]

def __str__(self):
return f"projects/{self.project}/locations/{self.location}"

@staticmethod
def parse(to_parse: str) -> "LocationPath":
splits = to_parse.split("/")
if len(splits) != 6 or splits[0] != "projects" or splits[2] != "locations":
raise InvalidArgument(
"Location path must be formatted like projects/{project_number}/locations/{location} but was instead "
+ to_parse
)
return LocationPath(splits[1], _parse_location(splits[3]))


class TopicPath(NamedTuple):
project: Union[int, str]
location: CloudZone
location: Union[CloudRegion, CloudZone]
name: str

def __str__(self):
Expand All @@ -51,12 +73,12 @@ def parse(to_parse: str) -> "TopicPath":
"Topic path must be formatted like projects/{project_number}/locations/{location}/topics/{name} but was instead "
+ to_parse
)
return TopicPath(splits[1], CloudZone.parse(splits[3]), splits[5])
return TopicPath(splits[1], _parse_location(splits[3]), splits[5])


class SubscriptionPath(NamedTuple):
project: Union[int, str]
location: CloudZone
location: Union[CloudRegion, CloudZone]
name: str

def __str__(self):
Expand All @@ -78,7 +100,7 @@ def parse(to_parse: str) -> "SubscriptionPath":
"Subscription path must be formatted like projects/{project_number}/locations/{location}/subscriptions/{name} but was instead "
+ to_parse
)
return SubscriptionPath(splits[1], CloudZone.parse(splits[3]), splits[5])
return SubscriptionPath(splits[1], _parse_location(splits[3]), splits[5])


class ReservationPath(NamedTuple):
Expand Down