Skip to content

Commit

Permalink
Merge pull request #1592 from tseaver/fix-topic-subscription-path-pat…
Browse files Browse the repository at this point in the history
…terns

Fix topic subscription path patterns
  • Loading branch information
tseaver committed Mar 9, 2016
2 parents 7104253 + 2f1350e commit 72617b3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
8 changes: 5 additions & 3 deletions gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,14 @@ def _name_from_project_path(path, project, template):
match = template.match(path)

if not match:
raise ValueError('path did not match: %s' % (template.pattern,))
raise ValueError('path "%s" did not match expected pattern "%s"' % (
path, template.pattern,))

found_project = match.group('project')
if found_project != project:
raise ValueError('Project from client should agree with '
'project from resource.')
raise ValueError(
'Project from client (%s) should agree with '
'project from resource(%s).' % (project, found_project))

return match.group('name')

Expand Down
24 changes: 20 additions & 4 deletions gcloud/pubsub/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,27 @@

"""Helper functions for shared behavior."""

import re

from gcloud._helpers import _name_from_project_path


_TOPIC_TEMPLATE = re.compile(r"""
projects/ # static prefix
(?P<project>[^/]+) # initial letter, wordchars + hyphen
/topics/ # static midfix
(?P<name>[^/]+) # initial letter, wordchars + allowed punc
""", re.VERBOSE)


_SUBSCRIPTION_TEMPLATE = re.compile(r"""
projects/ # static prefix
(?P<project>[^/]+) # initial letter, wordchars + hyphen
/subscriptions/ # static midfix
(?P<name>[^/]+) # initial letter, wordchars + allowed punc
""", re.VERBOSE)


def topic_name_from_path(path, project):
"""Validate a topic URI path and get the topic name.
Expand All @@ -33,8 +51,7 @@ def topic_name_from_path(path, project):
the project from the ``path`` does not agree with the
``project`` passed in.
"""
template = r'projects/(?P<project>\w+)/topics/(?P<name>\w+)'
return _name_from_project_path(path, project, template)
return _name_from_project_path(path, project, _TOPIC_TEMPLATE)


def subscription_name_from_path(path, project):
Expand All @@ -53,5 +70,4 @@ def subscription_name_from_path(path, project):
the project from the ``path`` does not agree with the
``project`` passed in.
"""
template = r'projects/(?P<project>\w+)/subscriptions/(?P<name>\w+)'
return _name_from_project_path(path, project, template)
return _name_from_project_path(path, project, _SUBSCRIPTION_TEMPLATE)
28 changes: 21 additions & 7 deletions gcloud/pubsub/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ def _callFUT(self, path, project):
from gcloud.pubsub._helpers import topic_name_from_path
return topic_name_from_path(path, project)

def test_it(self):
def test_w_simple_name(self):
TOPIC_NAME = 'TOPIC_NAME'
PROJECT = 'PROJECT'
PROJECT = 'my-project-1234'
PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
topic_name = self._callFUT(PATH, PROJECT)
self.assertEqual(topic_name, TOPIC_NAME)

def test_w_name_w_all_extras(self):
TOPIC_NAME = 'TOPIC_NAME-part.one~part.two%part-three'
PROJECT = 'my-project-1234'
PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
topic_name = self._callFUT(PATH, PROJECT)
self.assertEqual(topic_name, TOPIC_NAME)
Expand All @@ -35,9 +42,16 @@ def _callFUT(self, path, project):
from gcloud.pubsub._helpers import subscription_name_from_path
return subscription_name_from_path(path, project)

def test_it(self):
TOPIC_NAME = 'TOPIC_NAME'
PROJECT = 'PROJECT'
PATH = 'projects/%s/subscriptions/%s' % (PROJECT, TOPIC_NAME)
def test_w_simple_name(self):
SUBSCRIPTION_NAME = 'SUBSCRIPTION_NAME'
PROJECT = 'my-project-1234'
PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUBSCRIPTION_NAME)
subscription_name = self._callFUT(PATH, PROJECT)
self.assertEqual(subscription_name, TOPIC_NAME)
self.assertEqual(subscription_name, SUBSCRIPTION_NAME)

def test_w_name_w_all_extras(self):
SUBSCRIPTION_NAME = 'SUBSCRIPTION_NAME-part.one~part.two%part-three'
PROJECT = 'my-project-1234'
PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUBSCRIPTION_NAME)
topic_name = self._callFUT(PATH, PROJECT)
self.assertEqual(topic_name, SUBSCRIPTION_NAME)

0 comments on commit 72617b3

Please sign in to comment.