1# Copyright (c) 2006-2010 Mitch Garnaat http://garnaat.org/
2# Copyright (c) 2010, Eucalyptus Systems, Inc.
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish, dis-
8# tribute, sublicense, and/or sell copies of the Software, and to permit
9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23from boto.cloudfront.identity import OriginAccessIdentity
24
25def get_oai_value(origin_access_identity):
26    if isinstance(origin_access_identity, OriginAccessIdentity):
27        return origin_access_identity.uri()
28    else:
29        return origin_access_identity
30
31class S3Origin(object):
32    """
33    Origin information to associate with the distribution.
34    If your distribution will use an Amazon S3 origin,
35    then you use the S3Origin element.
36    """
37
38    def __init__(self, dns_name=None, origin_access_identity=None):
39        """
40        :param dns_name: The DNS name of your Amazon S3 bucket to
41                         associate with the distribution.
42                         For example: mybucket.s3.amazonaws.com.
43        :type dns_name: str
44
45        :param origin_access_identity: The CloudFront origin access
46                                       identity to associate with the
47                                       distribution. If you want the
48                                       distribution to serve private content,
49                                       include this element; if you want the
50                                       distribution to serve public content,
51                                       remove this element.
52        :type origin_access_identity: str
53
54        """
55        self.dns_name = dns_name
56        self.origin_access_identity = origin_access_identity
57
58    def __repr__(self):
59        return '<S3Origin: %s>' % self.dns_name
60
61    def startElement(self, name, attrs, connection):
62        return None
63
64    def endElement(self, name, value, connection):
65        if name == 'DNSName':
66            self.dns_name = value
67        elif name == 'OriginAccessIdentity':
68            self.origin_access_identity = value
69        else:
70            setattr(self, name, value)
71
72    def to_xml(self):
73        s = '  <S3Origin>\n'
74        s += '    <DNSName>%s</DNSName>\n' % self.dns_name
75        if self.origin_access_identity:
76            val = get_oai_value(self.origin_access_identity)
77            s += '    <OriginAccessIdentity>%s</OriginAccessIdentity>\n' % val
78        s += '  </S3Origin>\n'
79        return s
80
81class CustomOrigin(object):
82    """
83    Origin information to associate with the distribution.
84    If your distribution will use a non-Amazon S3 origin,
85    then you use the CustomOrigin element.
86    """
87
88    def __init__(self, dns_name=None, http_port=80, https_port=443,
89                 origin_protocol_policy=None):
90        """
91        :param dns_name: The DNS name of your Amazon S3 bucket to
92                         associate with the distribution.
93                         For example: mybucket.s3.amazonaws.com.
94        :type dns_name: str
95
96        :param http_port: The HTTP port the custom origin listens on.
97        :type http_port: int
98
99        :param https_port: The HTTPS port the custom origin listens on.
100        :type http_port: int
101
102        :param origin_protocol_policy: The origin protocol policy to
103                                       apply to your origin. If you
104                                       specify http-only, CloudFront
105                                       will use HTTP only to access the origin.
106                                       If you specify match-viewer, CloudFront
107                                       will fetch from your origin using HTTP
108                                       or HTTPS, based on the protocol of the
109                                       viewer request.
110        :type origin_protocol_policy: str
111
112        """
113        self.dns_name = dns_name
114        self.http_port = http_port
115        self.https_port = https_port
116        self.origin_protocol_policy = origin_protocol_policy
117
118    def __repr__(self):
119        return '<CustomOrigin: %s>' % self.dns_name
120
121    def startElement(self, name, attrs, connection):
122        return None
123
124    def endElement(self, name, value, connection):
125        if name == 'DNSName':
126            self.dns_name = value
127        elif name == 'HTTPPort':
128            try:
129                self.http_port = int(value)
130            except ValueError:
131                self.http_port = value
132        elif name == 'HTTPSPort':
133            try:
134                self.https_port = int(value)
135            except ValueError:
136                self.https_port = value
137        elif name == 'OriginProtocolPolicy':
138            self.origin_protocol_policy = value
139        else:
140            setattr(self, name, value)
141
142    def to_xml(self):
143        s = '  <CustomOrigin>\n'
144        s += '    <DNSName>%s</DNSName>\n' % self.dns_name
145        s += '    <HTTPPort>%d</HTTPPort>\n' % self.http_port
146        s += '    <HTTPSPort>%d</HTTPSPort>\n' % self.https_port
147        s += '    <OriginProtocolPolicy>%s</OriginProtocolPolicy>\n' % self.origin_protocol_policy
148        s += '  </CustomOrigin>\n'
149        return s
150
151