← Back to team overview

txaws-dev team mailing list archive

[Merge] lp:~dreamhosters/txaws/921354-get-bucket-website-method-4 into lp:txaws

 

Arsene Rei has proposed merging lp:~dreamhosters/txaws/921354-get-bucket-website-method-4 into lp:txaws.

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

For more details, see:
https://code.launchpad.net/~dreamhosters/txaws/921354-get-bucket-website-method-4/+merge/90358
-- 
https://code.launchpad.net/~dreamhosters/txaws/921354-get-bucket-website-method-4/+merge/90358
Your team txAWS Technical List is requested to review the proposed merge of lp:~dreamhosters/txaws/921354-get-bucket-website-method-4 into lp:txaws.
=== modified file 'txaws/s3/client.py'
--- txaws/s3/client.py	2012-01-26 23:05:01 +0000
+++ txaws/s3/client.py	2012-01-27 00:57:24 +0000
@@ -22,7 +22,7 @@
 from txaws.s3.acls import AccessControlPolicy
 from txaws.s3.model import (
     Bucket, BucketItem, BucketListing, ItemOwner, LifecycleConfiguration,
-    LifecycleConfigurationRule, RequestPayment)
+    LifecycleConfigurationRule, RequestPayment, WebsiteConfiguration)
 from txaws.s3.exception import S3Error
 from txaws.service import AWSServiceEndpoint, S3_ENDPOINT
 from txaws.util import XML, calculate_md5
@@ -210,6 +210,27 @@
 
         return LifecycleConfiguration(rules)
 
+    def get_bucket_website_config(self, bucket):
+        """
+        Get the website configuration of a bucket.
+
+        @param bucket: The name of the bucket.
+        @return: A C{Deferred} that will fire with the bucket's website
+        configuration.
+        """
+        query = self.query_factory(
+            action='GET', creds=self.creds, endpoint=self.endpoint,
+            bucket=bucket, object_name='?website')
+        return query.submit().addCallback(self._parse_website_config)
+
+    def _parse_website_config(self, xml_bytes):
+        """Parse a C{WebsiteConfiguration} XML document."""
+        root = XML(xml_bytes)
+        index_suffix = root.findtext("IndexDocument/Suffix")
+        error_key = root.findtext("ErrorDocument/Key")
+
+        return WebsiteConfiguration(index_suffix, error_key)
+
     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-26 02:19:17 +0000
+++ txaws/s3/model.py	2012-01-27 00:57:24 +0000
@@ -72,6 +72,14 @@
         self.expiration = expiration
 
 
+class WebsiteConfiguration(object):
+    """
+    A mapping for the data in a bucket website configuration.
+    """
+    def __init__(self, index_suffix, error_key=None):
+        self.index_suffix = index_suffix
+        self.error_key = error_key
+
 class FileChunk(object):
     """
     An Amazon S3 file chunk.

=== modified file 'txaws/s3/tests/test_client.py'
--- txaws/s3/tests/test_client.py	2012-01-26 00:42:15 +0000
+++ txaws/s3/tests/test_client.py	2012-01-27 00:57:24 +0000
@@ -309,6 +309,79 @@
         d = s3.get_bucket_lifecycle("mybucket")
         return d.addCallback(check_results)
 
+    def test_get_bucket_website_config(self):
+        """
+        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.
+        """
+
+        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, "?website")
+                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_website_result)
+
+        def check_results(website_config):
+            self.assertEquals(website_config.index_suffix, "index.html")
+            self.assertEquals(website_config.error_key, "404.html")
+
+        creds = AWSCredentials("foo", "bar")
+        s3 = client.S3Client(creds, query_factory=StubQuery)
+        d = s3.get_bucket_website_config("mybucket")
+        return d.addCallback(check_results)
+
+    def test_get_bucket_website_config_no_error_doc(self):
+        """
+        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.
+        """
+
+        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, "?website")
+                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_website_no_error_result)
+
+        def check_results(website_config):
+            self.assertEquals(website_config.index_suffix, "index.html")
+            self.assertEquals(website_config.error_key, None)
+
+        creds = AWSCredentials("foo", "bar")
+        s3 = client.S3Client(creds, query_factory=StubQuery)
+        d = s3.get_bucket_website_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-26 00:42:15 +0000
+++ txaws/testing/payload.py	2012-01-27 00:57:24 +0000
@@ -1041,3 +1041,22 @@
       </Expiration>
    </Rule>
 </LifecycleConfiguration>"""
+
+sample_s3_get_bucket_website_result = """\
+<?xml version="1.0" encoding="UTF-8"?>
+<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/";>
+  <IndexDocument>
+    <Suffix>index.html</Suffix>
+  </IndexDocument>
+  <ErrorDocument>
+    <Key>404.html</Key>
+  </ErrorDocument>
+</WebsiteConfiguration>"""
+
+sample_s3_get_bucket_website_no_error_result = """\
+<?xml version="1.0" encoding="UTF-8"?>
+<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/";>
+  <IndexDocument>
+    <Suffix>index.html</Suffix>
+  </IndexDocument>
+</WebsiteConfiguration>"""