Skip to content

Commit 0fe6657

Browse files
committed
Merge pull request #1825 from tseaver/1817-logging-fix-sink-from_api_repr
Fix 'Sink.from_api_repr'.
2 parents d4ff022 + e63ce94 commit 0fe6657

6 files changed

Lines changed: 19 additions & 142 deletions

File tree

gcloud/logging/metric.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,9 @@
1414

1515
"""Define Logging API Metrics."""
1616

17-
import re
18-
19-
from gcloud._helpers import _name_from_project_path
2017
from gcloud.exceptions import NotFound
2118

2219

23-
_METRIC_TEMPLATE = re.compile(r"""
24-
projects/ # static prefix
25-
(?P<project>[^/]+) # initial letter, wordchars + hyphen
26-
/metrics/ # static midfix
27-
(?P<name>[^/]+) # initial letter, wordchars + allowed punc
28-
""", re.VERBOSE)
29-
30-
31-
def _metric_name_from_path(path, project):
32-
"""Validate a metric URI path and get the metric name.
33-
34-
:type path: string
35-
:param path: URI path for a metric API request.
36-
37-
:type project: string
38-
:param project: The project associated with the request. It is
39-
included for validation purposes.
40-
41-
:rtype: string
42-
:returns: Metric name parsed from ``path``.
43-
:raises: :class:`ValueError` if the ``path`` is ill-formed or if
44-
the project from the ``path`` does not agree with the
45-
``project`` passed in.
46-
"""
47-
return _name_from_project_path(path, project, _METRIC_TEMPLATE)
48-
49-
5020
class Metric(object):
5121
"""Metrics represent named filters for log entries.
5222

gcloud/logging/sink.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,9 @@
1414

1515
"""Define Logging API Sinks."""
1616

17-
import re
18-
19-
from gcloud._helpers import _name_from_project_path
2017
from gcloud.exceptions import NotFound
2118

2219

23-
_SINK_TEMPLATE = re.compile(r"""
24-
projects/ # static prefix
25-
(?P<project>[^/]+) # initial letter, wordchars + hyphen
26-
/sinks/ # static midfix
27-
(?P<name>[^/]+) # initial letter, wordchars + allowed punc
28-
""", re.VERBOSE)
29-
30-
31-
def _sink_name_from_path(path, project):
32-
"""Validate a sink URI path and get the sink name.
33-
:type path: string
34-
:param path: URI path for a sink API request.
35-
:type project: string
36-
:param project: The project associated with the request. It is
37-
included for validation purposes.
38-
:rtype: string
39-
:returns: Metric name parsed from ``path``.
40-
:raises: :class:`ValueError` if the ``path`` is ill-formed or if
41-
the project from the ``path`` does not agree with the
42-
``project`` passed in.
43-
"""
44-
return _name_from_project_path(path, project, _SINK_TEMPLATE)
45-
46-
4720
class Sink(object):
4821
"""Sinks represent filtered exports for log entries.
4922
@@ -107,7 +80,7 @@ def from_api_repr(cls, resource, client):
10780
project from the resource does not agree with the project
10881
from the client.
10982
"""
110-
sink_name = _sink_name_from_path(resource['name'], client.project)
83+
sink_name = resource['name']
11184
filter_ = resource['filter']
11285
destination = resource['destination']
11386
return cls(sink_name, filter_, destination, client=client)

gcloud/logging/test_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ def test_list_sinks_no_paging(self):
210210
TOKEN = 'TOKEN'
211211
SINK_NAME = 'sink_name'
212212
FILTER = 'logName:syslog AND severity>=ERROR'
213-
SINK_PATH = 'projects/%s/sinks/%s' % (PROJECT, SINK_NAME)
214213
SINKS = [{
215-
'name': SINK_PATH,
214+
'name': SINK_NAME,
216215
'filter': FILTER,
217216
'destination': self.DESTINATION_URI,
218217
}]
@@ -238,11 +237,10 @@ def test_list_sinks_with_paging(self):
238237
PROJECT = 'PROJECT'
239238
SINK_NAME = 'sink_name'
240239
FILTER = 'logName:syslog AND severity>=ERROR'
241-
SINK_PATH = 'projects/%s/sinks/%s' % (PROJECT, SINK_NAME)
242240
TOKEN = 'TOKEN'
243241
PAGE_SIZE = 42
244242
SINKS = [{
245-
'name': SINK_PATH,
243+
'name': SINK_NAME,
246244
'filter': FILTER,
247245
'destination': self.DESTINATION_URI,
248246
}]

gcloud/logging/test_metric.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,6 @@
1515
import unittest2
1616

1717

18-
class Test__metric_name_from_path(unittest2.TestCase):
19-
20-
def _callFUT(self, path, project):
21-
from gcloud.logging.metric import _metric_name_from_path
22-
return _metric_name_from_path(path, project)
23-
24-
def test_invalid_path_length(self):
25-
PATH = 'projects/foo'
26-
PROJECT = None
27-
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT)
28-
29-
def test_invalid_path_format(self):
30-
METRIC_NAME = 'METRIC_NAME'
31-
PROJECT = 'PROJECT'
32-
PATH = 'foo/%s/bar/%s' % (PROJECT, METRIC_NAME)
33-
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT)
34-
35-
def test_invalid_project(self):
36-
METRIC_NAME = 'METRIC_NAME'
37-
PROJECT1 = 'PROJECT1'
38-
PROJECT2 = 'PROJECT2'
39-
PATH = 'projects/%s/metrics/%s' % (PROJECT1, METRIC_NAME)
40-
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT2)
41-
42-
def test_valid_data(self):
43-
METRIC_NAME = 'METRIC_NAME'
44-
PROJECT = 'PROJECT'
45-
PATH = 'projects/%s/metrics/%s' % (PROJECT, METRIC_NAME)
46-
metric_name = self._callFUT(PATH, PROJECT)
47-
self.assertEqual(metric_name, METRIC_NAME)
48-
49-
5018
class TestMetric(unittest2.TestCase):
5119

5220
PROJECT = 'test-project'

gcloud/logging/test_sink.py

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,6 @@
1515
import unittest2
1616

1717

18-
class Test__sink_name_from_path(unittest2.TestCase):
19-
20-
def _callFUT(self, path, project):
21-
from gcloud.logging.sink import _sink_name_from_path
22-
return _sink_name_from_path(path, project)
23-
24-
def test_invalid_path_length(self):
25-
PATH = 'projects/foo'
26-
PROJECT = None
27-
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT)
28-
29-
def test_invalid_path_format(self):
30-
SINK_NAME = 'SINK_NAME'
31-
PROJECT = 'PROJECT'
32-
PATH = 'foo/%s/bar/%s' % (PROJECT, SINK_NAME)
33-
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT)
34-
35-
def test_invalid_project(self):
36-
SINK_NAME = 'SINK_NAME'
37-
PROJECT1 = 'PROJECT1'
38-
PROJECT2 = 'PROJECT2'
39-
PATH = 'projects/%s/sinks/%s' % (PROJECT1, SINK_NAME)
40-
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT2)
41-
42-
def test_valid_data(self):
43-
SINK_NAME = 'SINK_NAME'
44-
PROJECT = 'PROJECT'
45-
PATH = 'projects/%s/sinks/%s' % (PROJECT, SINK_NAME)
46-
sink_name = self._callFUT(PATH, PROJECT)
47-
self.assertEqual(sink_name, SINK_NAME)
48-
49-
5018
class TestSink(unittest2.TestCase):
5119

5220
PROJECT = 'test-project'
@@ -78,7 +46,7 @@ def test_from_api_repr_minimal(self):
7846
client = _Client(project=self.PROJECT)
7947
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
8048
RESOURCE = {
81-
'name': FULL,
49+
'name': self.SINK_NAME,
8250
'filter': self.FILTER,
8351
'destination': self.DESTINATION_URI,
8452
}
@@ -95,7 +63,7 @@ def test_from_api_repr_w_description(self):
9563
client = _Client(project=self.PROJECT)
9664
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
9765
RESOURCE = {
98-
'name': FULL,
66+
'name': self.SINK_NAME,
9967
'filter': self.FILTER,
10068
'destination': self.DESTINATION_URI,
10169
}
@@ -108,20 +76,6 @@ def test_from_api_repr_w_description(self):
10876
self.assertEqual(sink.project, self.PROJECT)
10977
self.assertEqual(sink.full_name, FULL)
11078

111-
def test_from_api_repr_with_mismatched_project(self):
112-
PROJECT1 = 'PROJECT1'
113-
PROJECT2 = 'PROJECT2'
114-
client = _Client(project=PROJECT1)
115-
FULL = 'projects/%s/sinks/%s' % (PROJECT2, self.SINK_NAME)
116-
RESOURCE = {
117-
'name': FULL,
118-
'filter': self.FILTER,
119-
'destination': self.DESTINATION_URI,
120-
}
121-
klass = self._getTargetClass()
122-
self.assertRaises(ValueError, klass.from_api_repr,
123-
RESOURCE, client=client)
124-
12579
def test_create_w_bound_client(self):
12680
client = _Client(project=self.PROJECT)
12781
api = client.sinks_api = _DummySinksAPI()

system_tests/logging_.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,20 @@ def test_create_sink_bigquery_dataset(self):
277277
self.to_delete.append(sink)
278278
self.assertTrue(sink.exists())
279279

280+
def test_list_sinks(self):
281+
uri = self._init_storage_bucket()
282+
sink = Config.CLIENT.sink(DEFAULT_SINK_NAME, DEFAULT_FILTER, uri)
283+
self.assertFalse(sink.exists())
284+
before_sinks, _ = Config.CLIENT.list_sinks()
285+
before_names = set(sink.name for sink in before_sinks)
286+
sink.create()
287+
self.to_delete.append(sink)
288+
self.assertTrue(sink.exists())
289+
after_sinks, _ = Config.CLIENT.list_sinks()
290+
after_names = set(sink.name for sink in after_sinks)
291+
self.assertEqual(after_names - before_names,
292+
set([DEFAULT_SINK_NAME]))
293+
280294
def test_reload_sink(self):
281295
uri = self._init_bigquery_dataset()
282296
sink = Config.CLIENT.sink(DEFAULT_SINK_NAME, DEFAULT_FILTER, uri)

0 commit comments

Comments
 (0)