Note missing credentials args in lifecycle stuff.
[zs3.git] / lifecycle.lisp
blob40126bd34ec4984116d61e6f7a08ae9372bb8fb8
1 ;;;;
2 ;;;; Copyright (c) 2012 Zachary Beane, All Rights Reserved
3 ;;;;
4 ;;;; Redistribution and use in source and binary forms, with or without
5 ;;;; modification, are permitted provided that the following conditions
6 ;;;; are met:
7 ;;;;
8 ;;;; * Redistributions of source code must retain the above copyright
9 ;;;; notice, this list of conditions and the following disclaimer.
10 ;;;;
11 ;;;; * Redistributions in binary form must reproduce the above
12 ;;;; copyright notice, this list of conditions and the following
13 ;;;; disclaimer in the documentation and/or other materials
14 ;;;; provided with the distribution.
15 ;;;;
16 ;;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
17 ;;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ;;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ;;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 ;;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ;;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 ;;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 ;;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ;;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 ;;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ;;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ;;;;
28 ;;;; lifecycle.lisp
30 (in-package #:zs3)
32 ;;; Object expiration for buckets
34 (defbinder lifecycle-configuration
35 ("LifecycleConfiguration"
36 (sequence :rules
37 ("Rule"
38 ("ID" (bind :id))
39 ("Prefix" (bind :prefix))
40 ("Status" (bind :status))
41 ("Expiration"
42 ("Days" (bind :days)))))))
44 (defclass lifecycle-rule ()
45 ((id
46 :initarg :id
47 :accessor id)
48 (prefix
49 :initarg :prefix
50 :accessor prefix)
51 (enabledp
52 :initarg :enabledp
53 :accessor enabledp)
54 (days
55 :initarg :days
56 :accessor days)))
58 (defmethod print-object ((rule lifecycle-rule) stream)
59 (print-unreadable-object (rule stream :type t)
60 (format stream "~S expire prefix ~S in ~D day~:P (~:[disabled~;enabled~])"
61 (id rule)
62 (prefix rule)
63 (days rule)
64 (enabledp rule))))
66 ;;; FIXME: The GFs for ENABLE and DISABLE should really be moved
67 ;;; somewhere out of cloudfront.lisp now that I'm adding more methods.
69 (defmethod disable ((rule lifecycle-rule))
70 (setf (enabledp rule) nil))
72 (defmethod enable ((rule lifecycle-rule))
73 (setf (enabledp rule) t))
75 (defun lifecycle-rule (&key id prefix (enabled t) days)
76 (unless id
77 (setf id (string (gensym))))
78 (unless prefix
79 (error "Missing PREFIX argument"))
80 (unless days
81 (error "Missing DAYS argument"))
82 (make-instance 'lifecycle-rule
83 :id id
84 :prefix prefix
85 :enabledp enabled
86 :days days))
88 (defun lifecycle-document (rules)
89 (cxml:with-xml-output (cxml:make-octet-vector-sink)
90 (cxml:with-element "LifecycleConfiguration"
91 (dolist (rule rules)
92 (cxml:with-element "Rule"
93 (cxml:with-element "ID"
94 (cxml:text (id rule)))
95 (cxml:with-element "Prefix"
96 (cxml:text (prefix rule)))
97 (cxml:with-element "Status"
98 (cxml:text (if (enabledp rule)
99 "Enabled"
100 "Disabled")))
101 (cxml:with-element "Expiration"
102 (cxml:with-element "Days"
103 (cxml:text (princ-to-string (days rule))))))))))
105 (defun bindings-lifecycle-rules (bindings)
106 (let ((rules '()))
107 (dolist (rule-bindings (bvalue :rules bindings) (nreverse rules))
108 (alist-bind (id prefix status days)
109 rule-bindings
110 (push (make-instance 'lifecycle-rule
111 :id id
112 :prefix prefix
113 :enabledp (string= status "Enabled")
114 :days (parse-integer days))
115 rules)))))
117 (define-specific-error (no-such-lifecycle-configuration
118 "NoSuchLifecycleConfiguration")
119 () ())
121 ;;; FIXME: Add credentials args!
123 (defun bucket-lifecycle (bucket)
124 "Return the bucket lifecycle rules for BUCKET. Signals
125 NO-SUCH-LIFECYCLE-CONFIGURATION if the bucket has no lifecycle
126 configuration."
127 (let ((response
128 (submit-request (make-instance 'request
129 :method :get
130 :bucket bucket
131 :sub-resource "lifecycle"))))
132 (bindings-lifecycle-rules
133 (xml-bind 'lifecycle-configuration (body response)))))
135 (defun delete-bucket-lifecycle (bucket)
136 "Delete the lifecycle configuration of BUCKET."
137 (submit-request (make-instance 'request
138 :method :delete
139 :bucket bucket
140 :sub-resource "lifecycle")))
142 (defun (setf bucket-lifecycle) (rules bucket)
143 "Set the lifecycle configuration of BUCKET to RULES. RULES is
144 coerced to a list if needed. If RULES is NIL, the lifecycle
145 configuration is deleted with DELETE-BUCKET-LIFECYCLE."
146 (when (null rules)
147 (return-from bucket-lifecycle
148 (delete-bucket-lifecycle bucket)))
149 (unless (listp rules)
150 (setf rules (list rules)))
151 (let* ((content (lifecycle-document rules))
152 (md5 (vector-md5/b64 content)))
153 (submit-request (make-instance 'request
154 :method :put
155 :bucket bucket
156 :sub-resource "lifecycle"
157 :content-md5 md5
158 :content content))))