Merge lp:~dreamhosters/txaws/921347-get-bucket-lifecycle-3 into lp:txaws

Proposed by Arsene Rei
Status: Merged
Merged at revision: 121
Proposed branch: lp:~dreamhosters/txaws/921347-get-bucket-lifecycle-3
Merge into: lp:txaws
Diff against target: 220 lines (+165/-1)
4 files modified
txaws/s3/client.py (+31/-1)
txaws/s3/model.py (+22/-0)
txaws/s3/tests/test_client.py (+78/-0)
txaws/testing/payload.py (+34/-0)
To merge this branch: bzr merge lp:~dreamhosters/txaws/921347-get-bucket-lifecycle-3
Reviewer Review Type Date Requested Status
Duncan McGreggor Approve
Review via email: mp+90228@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Duncan McGreggor (oubiwann) wrote :

That's more like it :-)

+1, looks good! Welcome to txAWS!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'txaws/s3/client.py'
--- txaws/s3/client.py 2012-01-24 23:18:36 +0000
+++ txaws/s3/client.py 2012-01-26 00:55:10 +0000
@@ -20,7 +20,8 @@
20from txaws.client.base import BaseClient, BaseQuery, error_wrapper20from txaws.client.base import BaseClient, BaseQuery, error_wrapper
21from txaws.s3.acls import AccessControlPolicy21from txaws.s3.acls import AccessControlPolicy
22from txaws.s3.model import (22from txaws.s3.model import (
23 Bucket, BucketItem, BucketListing, ItemOwner, RequestPayment)23 Bucket, BucketItem, BucketListing, ItemOwner, LifecycleConfiguration,
24 LifecycleConfigurationRule, RequestPayment)
24from txaws.s3.exception import S3Error25from txaws.s3.exception import S3Error
25from txaws.service import AWSServiceEndpoint, S3_ENDPOINT26from txaws.service import AWSServiceEndpoint, S3_ENDPOINT
26from txaws.util import XML, calculate_md527from txaws.util import XML, calculate_md5
@@ -180,6 +181,35 @@
180 root = XML(xml_bytes)181 root = XML(xml_bytes)
181 return root.text or ""182 return root.text or ""
182183
184 def get_bucket_lifecycle(self, bucket):
185 """
186 Get the lifecycle configuration of a bucket.
187
188 @param bucket: The name of the bucket.
189 @return: A C{Deferred} that will fire with the bucket's lifecycle
190 configuration.
191 """
192 query = self.query_factory(
193 action='GET', creds=self.creds, endpoint=self.endpoint,
194 bucket=bucket, object_name='?lifecycle')
195 return query.submit().addCallback(self._parse_lifecycle_config)
196
197 def _parse_lifecycle_config(self, xml_bytes):
198 """Parse a C{LifecycleConfiguration} XML document."""
199 root = XML(xml_bytes)
200 contents = []
201 rules = []
202
203 for content_data in root.findall("Rule"):
204 id = content_data.findtext("ID")
205 prefix = content_data.findtext("Prefix")
206 status = content_data.findtext("Status")
207 expiration = int(content_data.findtext("Expiration/Days"))
208 rules.append(
209 LifecycleConfigurationRule(id, prefix, status, expiration))
210
211 return LifecycleConfiguration(rules)
212
183 def get_bucket_acl(self, bucket):213 def get_bucket_acl(self, bucket):
184 """214 """
185 Get the access control policy for a bucket.215 Get the access control policy for a bucket.
186216
=== modified file 'txaws/s3/model.py'
--- txaws/s3/model.py 2011-03-26 12:32:44 +0000
+++ txaws/s3/model.py 2012-01-26 00:55:10 +0000
@@ -34,6 +34,9 @@
3434
3535
36class BucketListing(object):36class BucketListing(object):
37 """
38 A mapping for the data in a bucket listing.
39 """
37 def __init__(self, name, prefix, marker, max_keys, is_truncated,40 def __init__(self, name, prefix, marker, max_keys, is_truncated,
38 contents=None, common_prefixes=None):41 contents=None, common_prefixes=None):
39 self.name = name42 self.name = name
@@ -45,6 +48,25 @@
45 self.common_prefixes = common_prefixes48 self.common_prefixes = common_prefixes
4649
4750
51class LifecycleConfiguration(object):
52 """
53 Returns the lifecycle configuration information set on the bucket.
54 """
55 def __init__(self, rules):
56 self.rules = rules
57
58
59class LifecycleConfigurationRule(object):
60 """
61 Container for elements that describe a lifecycle rule.
62 """
63 def __init__(self, id, prefix, status, expiration):
64 self.id = id
65 self.prefix = prefix
66 self.status = status
67 self.expiration = expiration
68
69
48class FileChunk(object):70class FileChunk(object):
49 """71 """
50 An Amazon S3 file chunk.72 An Amazon S3 file chunk.
5173
=== modified file 'txaws/s3/tests/test_client.py'
--- txaws/s3/tests/test_client.py 2012-01-24 23:18:36 +0000
+++ txaws/s3/tests/test_client.py 2012-01-26 00:55:10 +0000
@@ -231,6 +231,84 @@
231 d = s3.get_bucket_location("mybucket")231 d = s3.get_bucket_location("mybucket")
232 return d.addCallback(check_results)232 return d.addCallback(check_results)
233233
234 def test_get_bucket_lifecycle_multiple_rules(self):
235 """
236 L{S3Client.get_bucket_lifecycle} creates a L{Query} to get a bucket's
237 lifecycle. It parses the returned C{LifecycleConfiguration} XML
238 document and returns a C{Deferred} that fires with the bucket's region.
239 """
240
241 class StubQuery(client.Query):
242
243 def __init__(query, action, creds, endpoint, bucket=None,
244 object_name=None):
245 super(StubQuery, query).__init__(action=action, creds=creds,
246 bucket=bucket,
247 object_name=object_name)
248 self.assertEquals(action, "GET")
249 self.assertEqual(creds.access_key, "foo")
250 self.assertEqual(creds.secret_key, "bar")
251 self.assertEqual(query.bucket, "mybucket")
252 self.assertEqual(query.object_name, "?lifecycle")
253 self.assertEqual(query.data, "")
254 self.assertEqual(query.metadata, {})
255 self.assertEqual(query.amz_headers, {})
256
257 def submit(query, url_context=None):
258 return succeed(payload.
259 sample_s3_get_bucket_lifecycle_multiple_rules_result)
260
261 def check_results(lifecycle_config):
262 self.assertTrue(len(lifecycle_config.rules) == 2)
263 rule = lifecycle_config.rules[1]
264 self.assertEquals(rule.id, 'another-id')
265 self.assertEquals(rule.prefix, 'another-logs')
266 self.assertEquals(rule.status, 'Disabled')
267 self.assertEquals(rule.expiration, 37)
268
269 creds = AWSCredentials("foo", "bar")
270 s3 = client.S3Client(creds, query_factory=StubQuery)
271 d = s3.get_bucket_lifecycle("mybucket")
272 return d.addCallback(check_results)
273
274 def test_get_bucket_lifecycle(self):
275 """
276 L{S3Client.get_bucket_lifecycle} creates a L{Query} to get a bucket's
277 lifecycle. It parses the returned C{LifecycleConfiguration} XML
278 document and returns a C{Deferred} that fires with the bucket's region.
279 """
280
281 class StubQuery(client.Query):
282
283 def __init__(query, action, creds, endpoint, bucket=None,
284 object_name=None):
285 super(StubQuery, query).__init__(action=action, creds=creds,
286 bucket=bucket,
287 object_name=object_name)
288 self.assertEquals(action, "GET")
289 self.assertEqual(creds.access_key, "foo")
290 self.assertEqual(creds.secret_key, "bar")
291 self.assertEqual(query.bucket, "mybucket")
292 self.assertEqual(query.object_name, "?lifecycle")
293 self.assertEqual(query.data, "")
294 self.assertEqual(query.metadata, {})
295 self.assertEqual(query.amz_headers, {})
296
297 def submit(query, url_context=None):
298 return succeed(payload.sample_s3_get_bucket_lifecycle_result)
299
300 def check_results(lifecycle_config):
301 rule = lifecycle_config.rules[0]
302 self.assertEquals(rule.id, '30-day-log-deletion-rule')
303 self.assertEquals(rule.prefix, 'logs')
304 self.assertEquals(rule.status, 'Enabled')
305 self.assertEquals(rule.expiration, 30)
306
307 creds = AWSCredentials("foo", "bar")
308 s3 = client.S3Client(creds, query_factory=StubQuery)
309 d = s3.get_bucket_lifecycle("mybucket")
310 return d.addCallback(check_results)
311
234 def test_delete_bucket(self):312 def test_delete_bucket(self):
235313
236 class StubQuery(client.Query):314 class StubQuery(client.Query):
237315
=== modified file 'txaws/testing/payload.py'
--- txaws/testing/payload.py 2012-01-24 22:52:02 +0000
+++ txaws/testing/payload.py 2012-01-26 00:55:10 +0000
@@ -1007,3 +1007,37 @@
1007 </Grant>1007 </Grant>
1008 </AccessControlList>1008 </AccessControlList>
1009</AccessControlPolicy>"""1009</AccessControlPolicy>"""
1010
1011sample_s3_get_bucket_lifecycle_result = """\
1012<?xml version="1.0" encoding="UTF-8"?>
1013<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
1014 <Rule>
1015 <ID>30-day-log-deletion-rule</ID>
1016 <Prefix>logs</Prefix>
1017 <Status>Enabled</Status>
1018 <Expiration>
1019 <Days>30</Days>
1020 </Expiration>
1021 </Rule>
1022</LifecycleConfiguration>"""
1023
1024sample_s3_get_bucket_lifecycle_multiple_rules_result = """\
1025<?xml version="1.0" encoding="UTF-8"?>
1026<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
1027 <Rule>
1028 <ID>30-day-log-deletion-rule</ID>
1029 <Prefix>logs</Prefix>
1030 <Status>Enabled</Status>
1031 <Expiration>
1032 <Days>30</Days>
1033 </Expiration>
1034 </Rule>
1035 <Rule>
1036 <ID>another-id</ID>
1037 <Prefix>another-logs</Prefix>
1038 <Status>Disabled</Status>
1039 <Expiration>
1040 <Days>37</Days>
1041 </Expiration>
1042 </Rule>
1043</LifecycleConfiguration>"""

Subscribers

People subscribed via source and target branches