Skip to content

Commit

Permalink
feat: Add cloud region path handling for when this is allowed by the …
Browse files Browse the repository at this point in the history
…backend (#240)
  • Loading branch information
dpcollins-google committed Sep 15, 2021
1 parent 2b45317 commit 4cad460
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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

0 comments on commit 4cad460

Please sign in to comment.