Skip to content
Closed
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
55 changes: 55 additions & 0 deletions gcloud/pubsub/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,61 @@ def test_ctor_w_explicit_timestamp(self):
'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME))
self.assertTrue(topic.timestamp_messages)

def test_ctor_auto_create_new_topic(self):
TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
conn = _Connection({'name': PATH})
CLIENT = _Client(project=PROJECT, connection=conn)
topic = self._makeOne(TOPIC_NAME, CLIENT,
auto_create=True)
req, = conn._requested # Asserts length 1.
self.assertEqual(req['method'], 'PUT')
self.assertEqual(req['path'], '/%s' % PATH)
self.assertEqual(topic.name, TOPIC_NAME)
self.assertEqual(topic.project, PROJECT)
self.assertEqual(topic.full_name,
'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME))

def test_ctor_auto_create_existing_topic(self):
class _ConflictConnection(object):

def __init__(self):
self._requested = []

def api_request(self, **kw):
from gcloud.exceptions import Conflict
self._requested.append(kw)
raise Conflict('Always fail.')

TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
conn = _ConflictConnection()
CLIENT = _Client(project=PROJECT, connection=conn)

PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
topic = self._makeOne(TOPIC_NAME, CLIENT, auto_create=True)
req, = conn._requested # Asserts length 1.
self.assertEqual(req['method'], 'PUT')
self.assertEqual(req['path'], '/%s' % PATH)
self.assertEqual(topic.name, TOPIC_NAME)
self.assertEqual(topic.project, PROJECT)
self.assertEqual(topic.full_name, PATH)

def test_ctor_auto_create_uncaught_error(self):
from gcloud.exceptions import NotFound
TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
conn = _Connection()
CLIENT = _Client(project=PROJECT, connection=conn)
self.assertRaises(NotFound, self._makeOne, TOPIC_NAME,
CLIENT, auto_create=True)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'PUT')
self.assertEqual(req['path'], '/%s' % PATH)

def test_from_api_repr(self):
TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
Expand Down
16 changes: 15 additions & 1 deletion gcloud/pubsub/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import datetime

from gcloud._helpers import _RFC3339_MICROS
from gcloud.exceptions import Conflict
from gcloud.exceptions import NotFound

_NOW = datetime.datetime.utcnow
Expand All @@ -42,11 +43,24 @@ class Topic(object):
:param timestamp_messages: If true, the topic will add a ``timestamp`` key
to the attributes of each published message:
the value will be an RFC 3339 timestamp.

:type auto_create: boolean
:param auto_create: If true, will attempt to create the topic on
:param auto_create: If true, will attempt to create the topic on
instantiation. If the topic already exists, we'll
catch and ignore a 409 error (Conflict). Any other
errors will not be caught.
"""
def __init__(self, name, client, timestamp_messages=False):
def __init__(self, name, client, timestamp_messages=False,
auto_create=False):
self.name = name
self._client = client
self.timestamp_messages = timestamp_messages
if auto_create:
try:
self.create()
except Conflict:
pass

@classmethod
def from_api_repr(cls, resource, client):
Expand Down