← Back to team overview

txaws-dev team mailing list archive

[Merge] lp:~dreamhosters/txaws/921352-get-bucket-notification-method into lp:txaws

 

Arsene Rei has proposed merging lp:~dreamhosters/txaws/921352-get-bucket-notification-method into lp:txaws.

Requested reviews:
  txAWS Technical List (txaws-tech)
Related bugs:
  Bug #921350 in txAWS: "S3: Add GET Bucket notification method"
  https://bugs.launchpad.net/txaws/+bug/921350

For more details, see:
https://code.launchpad.net/~dreamhosters/txaws/921352-get-bucket-notification-method/+merge/90521

Add GET Bucket notification functionality
-- 
https://code.launchpad.net/~dreamhosters/txaws/921352-get-bucket-notification-method/+merge/90521
Your team txAWS Technical List is requested to review the proposed merge of lp:~dreamhosters/txaws/921352-get-bucket-notification-method into lp:txaws.
=== modified file 'txaws/s3/client.py'
--- txaws/s3/client.py	2012-01-27 07:04:32 +0000
+++ txaws/s3/client.py	2012-01-27 20:16:22 +0000
@@ -22,7 +22,8 @@
 from txaws.s3.acls import AccessControlPolicy
 from txaws.s3.model import (
     Bucket, BucketItem, BucketListing, ItemOwner, LifecycleConfiguration,
-    LifecycleConfigurationRule, RequestPayment, WebsiteConfiguration)
+    LifecycleConfigurationRule, NotificationConfiguration, RequestPayment,
+    WebsiteConfiguration)
 from txaws.s3.exception import S3Error
 from txaws.service import AWSServiceEndpoint, S3_ENDPOINT
 from txaws.util import XML, calculate_md5
@@ -231,6 +232,27 @@
 
         return WebsiteConfiguration(index_suffix, error_key)
 
+    def get_bucket_notification_config(self, bucket):
+        """
+        Get the notification configuration of a bucket.
+
+        @param bucket: The name of the bucket.
+        @return: A C{Deferred} that will request the bucket's notification
+        configuration.
+        """
+        query = self.query_factory(
+            action='GET', creds=self.creds, endpoint=self.endpoint,
+            bucket=bucket, object_name='?notification')
+        return query.submit().addCallback(self._parse_notification_config)
+
+    def _parse_notification_config(self, xml_bytes):
+        """Parse a C{NotificationConfiguration} XML document."""
+        root = XML(xml_bytes)
+        topic = root.findtext("TopicConfiguration/Topic")
+        event = root.findtext("TopicConfiguration/Event")
+
+        return NotificationConfiguration(topic, event)
+
     def get_bucket_acl(self, bucket):
         """
         Get the access control policy for a bucket.

=== modified file 'txaws/s3/model.py'
--- txaws/s3/model.py	2012-01-27 07:04:32 +0000
+++ txaws/s3/model.py	2012-01-27 20:16:22 +0000
@@ -81,6 +81,15 @@
         self.error_key = error_key
 
 
+class NotificationConfiguration(object):
+    """
+    A mapping for the data in a bucket notification configuration.
+    """
+    def __init__(self, topic=None, event=None):
+        self.topic = topic
+        self.event = event
+
+
 class FileChunk(object):
     """
     An Amazon S3 file chunk.

=== modified file 'txaws/s3/tests/test_client.py'
--- txaws/s3/tests/test_client.py	2012-01-27 07:04:32 +0000
+++ txaws/s3/tests/test_client.py	2012-01-27 20:16:22 +0000
@@ -201,7 +201,8 @@
         """
         L{S3Client.get_bucket_location} creates a L{Query} to get a bucket's
         location.  It parses the returned C{LocationConstraint} XML document
-        and returns a C{Deferred} that fires with the bucket's region.
+        and returns a C{Deferred} that requests the bucket's location
+        constraint.
         """
 
         class StubQuery(client.Query):
@@ -235,7 +236,8 @@
         """
         L{S3Client.get_bucket_lifecycle} creates a L{Query} to get a bucket's
         lifecycle.  It parses the returned C{LifecycleConfiguration} XML
-        document and returns a C{Deferred} that fires with the bucket's region.
+        document and returns a C{Deferred} that requests the bucket's lifecycle
+        configuration with multiple rules.
         """
 
         class StubQuery(client.Query):
@@ -275,7 +277,8 @@
         """
         L{S3Client.get_bucket_lifecycle} creates a L{Query} to get a bucket's
         lifecycle.  It parses the returned C{LifecycleConfiguration} XML
-        document and returns a C{Deferred} that fires with the bucket's region.
+        document and returns a C{Deferred} that requests the bucket's lifecycle
+        configuration.
         """
 
         class StubQuery(client.Query):
@@ -314,7 +317,7 @@
         L{S3Client.get_bucket_website_config} creates a L{Query} to get a
         bucket's website configurtion.  It parses the returned
         C{WebsiteConfiguration} XML document and returns a C{Deferred} that
-        fires with the bucket's region.
+        requests the bucket's website configuration.
         """
 
         class StubQuery(client.Query):
@@ -351,7 +354,7 @@
         L{S3Client.get_bucket_website_config} creates a L{Query} to get a
         bucket's website configurtion.  It parses the returned
         C{WebsiteConfiguration} XML document and returns a C{Deferred} that
-        fires with the bucket's region.
+        requests the bucket's website configuration with the error document.
         """
 
         class StubQuery(client.Query):
@@ -382,6 +385,82 @@
         d = s3.get_bucket_website_config("mybucket")
         return d.addCallback(check_results)
 
+    def test_get_bucket_notification_config(self):
+        """
+        L{S3Client.get_bucket_notification_config} creates a L{Query} to get a
+        bucket's notification configuration.  It parses the returned
+        C{NotificationConfiguration} XML document and returns a C{Deferred}
+        that requests the bucket's notification configuration.
+        """
+
+        class StubQuery(client.Query):
+
+            def __init__(query, action, creds, endpoint, bucket=None,
+                         object_name=None):
+                super(StubQuery, query).__init__(action=action, creds=creds,
+                                                 bucket=bucket,
+                                                 object_name=object_name)
+                self.assertEquals(action, "GET")
+                self.assertEqual(creds.access_key, "foo")
+                self.assertEqual(creds.secret_key, "bar")
+                self.assertEqual(query.bucket, "mybucket")
+                self.assertEqual(query.object_name, "?notification")
+                self.assertEqual(query.data, "")
+                self.assertEqual(query.metadata, {})
+                self.assertEqual(query.amz_headers, {})
+
+            def submit(query, url_context=None):
+                return succeed(payload.
+                               sample_s3_get_bucket_notification_result)
+
+        def check_results(notification_config):
+            self.assertEquals(notification_config.topic, None)
+            self.assertEquals(notification_config.event, None)
+
+        creds = AWSCredentials("foo", "bar")
+        s3 = client.S3Client(creds, query_factory=StubQuery)
+        d = s3.get_bucket_notification_config("mybucket")
+        return d.addCallback(check_results)
+
+    def test_get_bucket_notification_config_with_topic(self):
+        """
+        L{S3Client.get_bucket_notification_config} creates a L{Query} to get a
+        bucket's notification configuration.  It parses the returned
+        C{NotificationConfiguration} XML document and returns a C{Deferred}
+        that requests the bucket's notification configuration with a topic.
+        """
+
+        class StubQuery(client.Query):
+
+            def __init__(query, action, creds, endpoint, bucket=None,
+                         object_name=None):
+                super(StubQuery, query).__init__(action=action, creds=creds,
+                                                 bucket=bucket,
+                                                 object_name=object_name)
+                self.assertEquals(action, "GET")
+                self.assertEqual(creds.access_key, "foo")
+                self.assertEqual(creds.secret_key, "bar")
+                self.assertEqual(query.bucket, "mybucket")
+                self.assertEqual(query.object_name, "?notification")
+                self.assertEqual(query.data, "")
+                self.assertEqual(query.metadata, {})
+                self.assertEqual(query.amz_headers, {})
+
+            def submit(query, url_context=None):
+                return succeed(payload.
+                    sample_s3_get_bucket_notification_with_topic_result)
+
+        def check_results(notification_config):
+            self.assertEquals(notification_config.topic,
+                              "arn:aws:sns:us-east-1:123456789012:myTopic")
+            self.assertEquals(notification_config.event,
+                              "s3:ReducedRedundancyLostObject")
+
+        creds = AWSCredentials("foo", "bar")
+        s3 = client.S3Client(creds, query_factory=StubQuery)
+        d = s3.get_bucket_notification_config("mybucket")
+        return d.addCallback(check_results)
+
     def test_delete_bucket(self):
 
         class StubQuery(client.Query):

=== modified file 'txaws/testing/payload.py'
--- txaws/testing/payload.py	2012-01-27 00:50:35 +0000
+++ txaws/testing/payload.py	2012-01-27 20:16:22 +0000
@@ -1060,3 +1060,14 @@
     <Suffix>index.html</Suffix>
   </IndexDocument>
 </WebsiteConfiguration>"""
+
+sample_s3_get_bucket_notification_result = """\
+<NotificationConfiguration />"""
+
+sample_s3_get_bucket_notification_with_topic_result = """\
+<NotificationConfiguration>
+   <TopicConfiguration>
+       <Topic>arn:aws:sns:us-east-1:123456789012:myTopic</Topic>
+       <Event>s3:ReducedRedundancyLostObject</Event>
+   </TopicConfiguration>
+</NotificationConfiguration>"""