-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1596 from tseaver/logging-sink_create
Add 'Sink.create' API wrapper and 'Client.sink' factory.
- Loading branch information
Showing
6 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,7 @@ | |
Client <logging-client> | ||
logging-logger | ||
logging-entries | ||
logging-sink | ||
|
||
.. toctree:: | ||
:maxdepth: 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Sinks | ||
===== | ||
|
||
.. automodule:: gcloud.logging.sink | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Copyright 2016 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Define Logging API Sinks.""" | ||
|
||
|
||
class Sink(object): | ||
"""Sinks represent filtered exports for log entries. | ||
See: | ||
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks | ||
:type name: string | ||
:param name: the name of the sink | ||
:type filter_: string | ||
:param filter_: the advanced logs filter expression defining the entries | ||
exported by the sink. | ||
:type destination: string | ||
:param destination: destination URI for the entries exported by the sink. | ||
:type client: :class:`gcloud.logging.client.Client` | ||
:param client: A client which holds credentials and project configuration | ||
for the sink (which requires a project). | ||
""" | ||
def __init__(self, name, filter_, destination, client): | ||
self.name = name | ||
self.filter_ = filter_ | ||
self.destination = destination | ||
self._client = client | ||
|
||
@property | ||
def client(self): | ||
"""Clent bound to the sink.""" | ||
return self._client | ||
|
||
@property | ||
def project(self): | ||
"""Project bound to the sink.""" | ||
return self._client.project | ||
|
||
@property | ||
def full_name(self): | ||
"""Fully-qualified name used in sink APIs""" | ||
return 'projects/%s/sinks/%s' % (self.project, self.name) | ||
|
||
@property | ||
def path(self): | ||
"""URL path for the sink's APIs""" | ||
return '/%s' % (self.full_name) | ||
|
||
def _require_client(self, client): | ||
"""Check client or verify over-ride. | ||
:type client: :class:`gcloud.logging.client.Client` or ``NoneType`` | ||
:param client: the client to use. If not passed, falls back to the | ||
``client`` stored on the current sink. | ||
:rtype: :class:`gcloud.logging.client.Client` | ||
:returns: The client passed in or the currently bound client. | ||
""" | ||
if client is None: | ||
client = self._client | ||
return client | ||
|
||
def create(self, client=None): | ||
"""API call: create the sink via a PUT request | ||
See: | ||
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks/create | ||
:type client: :class:`gcloud.logging.client.Client` or ``NoneType`` | ||
:param client: the client to use. If not passed, falls back to the | ||
``client`` stored on the current sink. | ||
""" | ||
client = self._require_client(client) | ||
data = { | ||
'name': self.name, | ||
'filter': self.filter_, | ||
'destination': self.destination, | ||
} | ||
client.connection.api_request(method='PUT', path=self.path, data=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Copyright 2016 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest2 | ||
|
||
|
||
class TestSink(unittest2.TestCase): | ||
|
||
PROJECT = 'test-project' | ||
SINK_NAME = 'sink-name' | ||
FILTER = 'logName:syslog AND severity>=INFO' | ||
DESTINATION_URI = 'faux.googleapis.com/destination' | ||
|
||
def _getTargetClass(self): | ||
from gcloud.logging.sink import Sink | ||
return Sink | ||
|
||
def _makeOne(self, *args, **kw): | ||
return self._getTargetClass()(*args, **kw) | ||
|
||
def test_ctor(self): | ||
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME) | ||
conn = _Connection() | ||
client = _Client(self.PROJECT, conn) | ||
sink = self._makeOne(self.SINK_NAME, self.FILTER, self.DESTINATION_URI, | ||
client=client) | ||
self.assertEqual(sink.name, self.SINK_NAME) | ||
self.assertEqual(sink.filter_, self.FILTER) | ||
self.assertEqual(sink.destination, self.DESTINATION_URI) | ||
self.assertTrue(sink.client is client) | ||
self.assertEqual(sink.project, self.PROJECT) | ||
self.assertEqual(sink.full_name, FULL) | ||
self.assertEqual(sink.path, '/%s' % (FULL,)) | ||
|
||
def test_create_w_bound_client(self): | ||
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME) | ||
RESOURCE = { | ||
'name': self.SINK_NAME, | ||
'filter': self.FILTER, | ||
'destination': self.DESTINATION_URI, | ||
} | ||
conn = _Connection({'name': FULL}) | ||
client = _Client(project=self.PROJECT, connection=conn) | ||
sink = self._makeOne(self.SINK_NAME, self.FILTER, self.DESTINATION_URI, | ||
client=client) | ||
sink.create() | ||
self.assertEqual(len(conn._requested), 1) | ||
req = conn._requested[0] | ||
self.assertEqual(req['method'], 'PUT') | ||
self.assertEqual(req['path'], '/%s' % FULL) | ||
self.assertEqual(req['data'], RESOURCE) | ||
|
||
def test_create_w_alternate_client(self): | ||
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME) | ||
RESOURCE = { | ||
'name': self.SINK_NAME, | ||
'filter': self.FILTER, | ||
'destination': self.DESTINATION_URI, | ||
} | ||
conn1 = _Connection({'name': FULL}) | ||
client1 = _Client(project=self.PROJECT, connection=conn1) | ||
conn2 = _Connection({'name': FULL}) | ||
client2 = _Client(project=self.PROJECT, connection=conn2) | ||
sink = self._makeOne(self.SINK_NAME, self.FILTER, self.DESTINATION_URI, | ||
client=client1) | ||
sink.create(client=client2) | ||
self.assertEqual(len(conn1._requested), 0) | ||
self.assertEqual(len(conn2._requested), 1) | ||
req = conn2._requested[0] | ||
self.assertEqual(req['method'], 'PUT') | ||
self.assertEqual(req['path'], '/%s' % FULL) | ||
self.assertEqual(req['data'], RESOURCE) | ||
|
||
|
||
class _Connection(object): | ||
|
||
def __init__(self, *responses): | ||
self._responses = responses | ||
self._requested = [] | ||
|
||
def api_request(self, **kw): | ||
from gcloud.exceptions import NotFound | ||
self._requested.append(kw) | ||
|
||
try: | ||
response, self._responses = self._responses[0], self._responses[1:] | ||
except: # pragma: NO COVER | ||
raise NotFound('miss') | ||
else: | ||
return response | ||
|
||
|
||
class _Client(object): | ||
|
||
def __init__(self, project, connection=None): | ||
self.project = project | ||
self.connection = connection |