Skip to content

Commit fcdcf20

Browse files
committed
Add 'Bucket.notification' factory. (#3958)
Toward #3956.
1 parent 8df3da8 commit fcdcf20

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

storage/google/cloud/storage/bucket.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from google.cloud.storage.acl import DefaultObjectACL
3636
from google.cloud.storage.blob import Blob
3737
from google.cloud.storage.blob import _get_encryption_headers
38+
from google.cloud.storage.notification import BucketNotification
3839

3940

4041
def _blobs_page_start(iterator, page, response):
@@ -159,6 +160,28 @@ def blob(self, blob_name, chunk_size=None, encryption_key=None):
159160
return Blob(name=blob_name, bucket=self, chunk_size=chunk_size,
160161
encryption_key=encryption_key)
161162

163+
def notification(self, topic_name,
164+
topic_project=None,
165+
custom_attributes=None,
166+
event_types=None,
167+
blob_name_prefix=None,
168+
payload_format=None):
169+
"""Factory: create a notification resource for the bucket.
170+
171+
See: :class:`google.cloud.storage.notification.BucketNotification`
172+
for parameters.
173+
174+
:rtype: :class:`google.cloud.storage.notification.BucketNotification`
175+
"""
176+
return BucketNotification(
177+
self, topic_name,
178+
topic_project=topic_project,
179+
custom_attributes=custom_attributes,
180+
event_types=event_types,
181+
blob_name_prefix=blob_name_prefix,
182+
payload_format=payload_format,
183+
)
184+
162185
def exists(self, client=None):
163186
"""Determines whether or not this bucket exists.
164187

storage/tests/unit/test_bucket.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,63 @@ def test_blob(self):
7272
self.assertEqual(blob.chunk_size, CHUNK_SIZE)
7373
self.assertEqual(blob._encryption_key, KEY)
7474

75+
def test_notification_defaults(self):
76+
from google.cloud.storage.notification import BucketNotification
77+
78+
PROJECT = 'PROJECT'
79+
BUCKET_NAME = 'BUCKET_NAME'
80+
TOPIC_NAME = 'TOPIC_NAME'
81+
client = _Client(_Connection(), project=PROJECT)
82+
bucket = self._make_one(client, name=BUCKET_NAME)
83+
84+
notification = bucket.notification(TOPIC_NAME)
85+
86+
self.assertIsInstance(notification, BucketNotification)
87+
self.assertIs(notification.bucket, bucket)
88+
self.assertEqual(notification.topic_project, PROJECT)
89+
self.assertIsNone(notification.custom_attributes)
90+
self.assertIsNone(notification.event_types)
91+
self.assertIsNone(notification.blob_name_prefix)
92+
self.assertIsNone(notification.payload_format)
93+
94+
def test_notification_explicit(self):
95+
from google.cloud.storage.notification import (
96+
BucketNotification,
97+
OBJECT_FINALIZE_EVENT_TYPE,
98+
OBJECT_DELETE_EVENT_TYPE,
99+
JSON_API_V1_PAYLOAD_FORMAT)
100+
101+
PROJECT = 'PROJECT'
102+
BUCKET_NAME = 'BUCKET_NAME'
103+
TOPIC_NAME = 'TOPIC_NAME'
104+
TOPIC_ALT_PROJECT = 'topic-project-456'
105+
CUSTOM_ATTRIBUTES = {
106+
'attr1': 'value1',
107+
'attr2': 'value2',
108+
}
109+
EVENT_TYPES = [OBJECT_FINALIZE_EVENT_TYPE, OBJECT_DELETE_EVENT_TYPE]
110+
BLOB_NAME_PREFIX = 'blob-name-prefix/'
111+
client = _Client(_Connection(), project=PROJECT)
112+
bucket = self._make_one(client, name=BUCKET_NAME)
113+
114+
notification = bucket.notification(
115+
TOPIC_NAME,
116+
topic_project=TOPIC_ALT_PROJECT,
117+
custom_attributes=CUSTOM_ATTRIBUTES,
118+
event_types=EVENT_TYPES,
119+
blob_name_prefix=BLOB_NAME_PREFIX,
120+
payload_format=JSON_API_V1_PAYLOAD_FORMAT,
121+
)
122+
123+
self.assertIsInstance(notification, BucketNotification)
124+
self.assertIs(notification.bucket, bucket)
125+
self.assertEqual(notification.topic_project, TOPIC_ALT_PROJECT)
126+
self.assertEqual(notification.custom_attributes, CUSTOM_ATTRIBUTES)
127+
self.assertEqual(notification.event_types, EVENT_TYPES)
128+
self.assertEqual(notification.blob_name_prefix, BLOB_NAME_PREFIX)
129+
self.assertEqual(
130+
notification.payload_format, JSON_API_V1_PAYLOAD_FORMAT)
131+
75132
def test_bucket_name_value(self):
76133
bucket_name = 'testing123'
77134
mixin = self._make_one(name=bucket_name)

0 commit comments

Comments
 (0)