1# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
2#
3# Permission is hereby granted, free of charge, to any person obtaining a
4# copy of this software and associated documentation files (the
5# "Software"), to deal in the Software without restriction, including
6# without limitation the rights to use, copy, modify, merge, publish, dis-
7# tribute, sublicense, and/or sell copies of the Software, and to permit
8# persons to whom the Software is furnished to do so, subject to the fol-
9# lowing conditions:
10#
11# The above copyright notice and this permission notice shall be included
12# in all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20# IN THE SOFTWARE.
21
22import uuid
23
24class OriginAccessIdentity(object):
25    def __init__(self, connection=None, config=None, id='',
26                 s3_user_id='', comment=''):
27        self.connection = connection
28        self.config = config
29        self.id = id
30        self.s3_user_id = s3_user_id
31        self.comment = comment
32        self.etag = None
33
34    def startElement(self, name, attrs, connection):
35        if name == 'CloudFrontOriginAccessIdentityConfig':
36            self.config = OriginAccessIdentityConfig()
37            return self.config
38        else:
39            return None
40
41    def endElement(self, name, value, connection):
42        if name == 'Id':
43            self.id = value
44        elif name == 'S3CanonicalUserId':
45            self.s3_user_id = value
46        elif name == 'Comment':
47            self.comment = value
48        else:
49            setattr(self, name, value)
50
51    def update(self, comment=None):
52        new_config = OriginAccessIdentityConfig(self.connection,
53                                                self.config.caller_reference,
54                                                self.config.comment)
55        if comment is not None:
56            new_config.comment = comment
57        self.etag = self.connection.set_origin_identity_config(self.id, self.etag, new_config)
58        self.config = new_config
59
60    def delete(self):
61        return self.connection.delete_origin_access_identity(self.id, self.etag)
62
63    def uri(self):
64        return 'origin-access-identity/cloudfront/%s' % self.id
65
66
67class OriginAccessIdentityConfig(object):
68    def __init__(self, connection=None, caller_reference='', comment=''):
69        self.connection = connection
70        if caller_reference:
71            self.caller_reference = caller_reference
72        else:
73            self.caller_reference = str(uuid.uuid4())
74        self.comment = comment
75
76    def to_xml(self):
77        s = '<?xml version="1.0" encoding="UTF-8"?>\n'
78        s += '<CloudFrontOriginAccessIdentityConfig xmlns="http://cloudfront.amazonaws.com/doc/2009-09-09/">\n'
79        s += '  <CallerReference>%s</CallerReference>\n' % self.caller_reference
80        if self.comment:
81            s += '  <Comment>%s</Comment>\n' % self.comment
82        s += '</CloudFrontOriginAccessIdentityConfig>\n'
83        return s
84
85    def startElement(self, name, attrs, connection):
86        return None
87
88    def endElement(self, name, value, connection):
89        if name == 'Comment':
90            self.comment = value
91        elif name == 'CallerReference':
92            self.caller_reference = value
93        else:
94            setattr(self, name, value)
95
96
97class OriginAccessIdentitySummary(object):
98    def __init__(self, connection=None, id='',
99                 s3_user_id='', comment=''):
100        self.connection = connection
101        self.id = id
102        self.s3_user_id = s3_user_id
103        self.comment = comment
104        self.etag = None
105
106    def startElement(self, name, attrs, connection):
107        return None
108
109    def endElement(self, name, value, connection):
110        if name == 'Id':
111            self.id = value
112        elif name == 'S3CanonicalUserId':
113            self.s3_user_id = value
114        elif name == 'Comment':
115            self.comment = value
116        else:
117            setattr(self, name, value)
118
119    def get_origin_access_identity(self):
120        return self.connection.get_origin_access_identity_info(self.id)
121
122