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

Don't enforce project ID validation when unmarshallig log entries #1808

Merged
merged 2 commits into from
May 23, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,10 @@ def _name_from_project_path(path, project, template):
:type path: string
:param path: URI path containing the name.

:type project: string
:type project: string or NoneType
:param project: The project associated with the request. It is
included for validation purposes.
included for validation purposes. If passed as None,
disables validation.

:type template: string
:param template: Template regex describing the expected form of the path.
Expand All @@ -457,11 +458,12 @@ def _name_from_project_path(path, project, template):
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 (%s) should agree with '
'project from resource(%s).' % (project, found_project))
if project is not None:
found_project = match.group('project')
if found_project != project:
raise ValueError(
'Project from client (%s) should agree with '
'project from resource(%s).' % (project, found_project))

return match.group('name')

Expand Down
8 changes: 2 additions & 6 deletions gcloud/logging/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@
""", re.VERBOSE)


def logger_name_from_path(path, project):
def logger_name_from_path(path):
"""Validate a logger URI path and get the logger name.

:type path: string
:param path: URI path for a logger API request.

:type project: string
:param project: The project associated with the request. It is
included for validation purposes.

:rtype: string
:returns: Logger name parsed from ``path``.
:raises: :class:`ValueError` if the ``path`` is ill-formed or if
the project from the ``path`` does not agree with the
``project`` passed in.
"""
return _name_from_project_path(path, project, _LOGGER_TEMPLATE)
return _name_from_project_path(path, None, _LOGGER_TEMPLATE)
3 changes: 1 addition & 2 deletions gcloud/logging/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def from_api_repr(cls, resource, client, loggers=None):
logger_fullname = resource['logName']
logger = loggers.get(logger_fullname)
if logger is None:
logger_name = logger_name_from_path(
logger_fullname, client.project)
logger_name = logger_name_from_path(logger_fullname)
logger = loggers[logger_fullname] = client.logger(logger_name)
payload = resource[cls._PAYLOAD_KEY]
insert_id = resource.get('insertId')
Expand Down
8 changes: 4 additions & 4 deletions gcloud/logging/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

class Test_logger_name_from_path(unittest2.TestCase):

def _callFUT(self, path, project):
def _callFUT(self, path):
from gcloud.logging._helpers import logger_name_from_path
return logger_name_from_path(path, project)
return logger_name_from_path(path)

def test_w_simple_name(self):
LOGGER_NAME = 'LOGGER_NAME'
PROJECT = 'my-project-1234'
PATH = 'projects/%s/logs/%s' % (PROJECT, LOGGER_NAME)
logger_name = self._callFUT(PATH, PROJECT)
logger_name = self._callFUT(PATH)
self.assertEqual(logger_name, LOGGER_NAME)

def test_w_name_w_all_extras(self):
LOGGER_NAME = 'LOGGER_NAME-part.one~part.two%part-three'
PROJECT = 'my-project-1234'
PATH = 'projects/%s/logs/%s' % (PROJECT, LOGGER_NAME)
logger_name = self._callFUT(PATH, PROJECT)
logger_name = self._callFUT(PATH)
self.assertEqual(logger_name, LOGGER_NAME)
7 changes: 7 additions & 0 deletions gcloud/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ def test_w_valid_data_w_compiled_regex(self):
name = self._callFUT(PATH, self.PROJECT, template)
self.assertEqual(name, self.THING_NAME)

def test_w_project_passed_as_none(self):
PROJECT1 = 'PROJECT1'
PATH = 'projects/%s/things/%s' % (PROJECT1, self.THING_NAME)
self._callFUT(PATH, None, self.TEMPLATE)
name = self._callFUT(PATH, None, self.TEMPLATE)
self.assertEqual(name, self.THING_NAME)


class _AppIdentity(object):

Expand Down