| OLD | NEW |
| (Empty) | |
| 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 # |
| 23 import xml.sax |
| 24 import time |
| 25 import uuid |
| 26 import urllib |
| 27 |
| 28 import boto |
| 29 from boto.connection import AWSAuthConnection |
| 30 from boto import handler |
| 31 from boto.resultset import ResultSet |
| 32 import boto.jsonresponse |
| 33 import exception |
| 34 import hostedzone |
| 35 |
| 36 HZXML = """<?xml version="1.0" encoding="UTF-8"?> |
| 37 <CreateHostedZoneRequest xmlns="%(xmlns)s"> |
| 38 <Name>%(name)s</Name> |
| 39 <CallerReference>%(caller_ref)s</CallerReference> |
| 40 <HostedZoneConfig> |
| 41 <Comment>%(comment)s</Comment> |
| 42 </HostedZoneConfig> |
| 43 </CreateHostedZoneRequest>""" |
| 44 |
| 45 #boto.set_stream_logger('dns') |
| 46 |
| 47 |
| 48 class Route53Connection(AWSAuthConnection): |
| 49 DefaultHost = 'route53.amazonaws.com' |
| 50 """The default Route53 API endpoint to connect to.""" |
| 51 |
| 52 Version = '2012-02-29' |
| 53 """Route53 API version.""" |
| 54 |
| 55 XMLNameSpace = 'https://route53.amazonaws.com/doc/2012-02-29/' |
| 56 """XML schema for this Route53 API version.""" |
| 57 |
| 58 def __init__(self, aws_access_key_id=None, aws_secret_access_key=None, |
| 59 port=None, proxy=None, proxy_port=None, |
| 60 host=DefaultHost, debug=0, security_token=None, |
| 61 validate_certs=True): |
| 62 AWSAuthConnection.__init__(self, host, |
| 63 aws_access_key_id, aws_secret_access_key, |
| 64 True, port, proxy, proxy_port, debug=debug, |
| 65 security_token=security_token, |
| 66 validate_certs=validate_certs) |
| 67 |
| 68 def _required_auth_capability(self): |
| 69 return ['route53'] |
| 70 |
| 71 def make_request(self, action, path, headers=None, data='', params=None): |
| 72 if params: |
| 73 pairs = [] |
| 74 for key, val in params.iteritems(): |
| 75 if val is None: |
| 76 continue |
| 77 pairs.append(key + '=' + urllib.quote(str(val))) |
| 78 path += '?' + '&'.join(pairs) |
| 79 return AWSAuthConnection.make_request(self, action, path, |
| 80 headers, data) |
| 81 |
| 82 # Hosted Zones |
| 83 |
| 84 def get_all_hosted_zones(self, start_marker=None, zone_list=None): |
| 85 """ |
| 86 Returns a Python data structure with information about all |
| 87 Hosted Zones defined for the AWS account. |
| 88 |
| 89 :param int start_marker: start marker to pass when fetching additional |
| 90 results after a truncated list |
| 91 :param list zone_list: a HostedZones list to prepend to results |
| 92 """ |
| 93 params = {} |
| 94 if start_marker: |
| 95 params = {'marker': start_marker} |
| 96 response = self.make_request('GET', '/%s/hostedzone' % self.Version, |
| 97 params=params) |
| 98 body = response.read() |
| 99 boto.log.debug(body) |
| 100 if response.status >= 300: |
| 101 raise exception.DNSServerError(response.status, |
| 102 response.reason, |
| 103 body) |
| 104 e = boto.jsonresponse.Element(list_marker='HostedZones', |
| 105 item_marker=('HostedZone',)) |
| 106 h = boto.jsonresponse.XmlHandler(e, None) |
| 107 h.parse(body) |
| 108 if zone_list: |
| 109 e['ListHostedZonesResponse']['HostedZones'].extend(zone_list) |
| 110 while 'NextMarker' in e['ListHostedZonesResponse']: |
| 111 next_marker = e['ListHostedZonesResponse']['NextMarker'] |
| 112 zone_list = e['ListHostedZonesResponse']['HostedZones'] |
| 113 e = self.get_all_hosted_zones(next_marker, zone_list) |
| 114 return e |
| 115 |
| 116 def get_hosted_zone(self, hosted_zone_id): |
| 117 """ |
| 118 Get detailed information about a particular Hosted Zone. |
| 119 |
| 120 :type hosted_zone_id: str |
| 121 :param hosted_zone_id: The unique identifier for the Hosted Zone |
| 122 |
| 123 """ |
| 124 uri = '/%s/hostedzone/%s' % (self.Version, hosted_zone_id) |
| 125 response = self.make_request('GET', uri) |
| 126 body = response.read() |
| 127 boto.log.debug(body) |
| 128 if response.status >= 300: |
| 129 raise exception.DNSServerError(response.status, |
| 130 response.reason, |
| 131 body) |
| 132 e = boto.jsonresponse.Element(list_marker='NameServers', |
| 133 item_marker=('NameServer',)) |
| 134 h = boto.jsonresponse.XmlHandler(e, None) |
| 135 h.parse(body) |
| 136 return e |
| 137 |
| 138 def get_hosted_zone_by_name(self, hosted_zone_name): |
| 139 """ |
| 140 Get detailed information about a particular Hosted Zone. |
| 141 |
| 142 :type hosted_zone_name: str |
| 143 :param hosted_zone_name: The fully qualified domain name for the Hosted |
| 144 Zone |
| 145 |
| 146 """ |
| 147 if hosted_zone_name[-1] != '.': |
| 148 hosted_zone_name += '.' |
| 149 all_hosted_zones = self.get_all_hosted_zones() |
| 150 for zone in all_hosted_zones['ListHostedZonesResponse']['HostedZones']: |
| 151 #check that they gave us the FQDN for their zone |
| 152 if zone['Name'] == hosted_zone_name: |
| 153 return self.get_hosted_zone(zone['Id'].split('/')[-1]) |
| 154 |
| 155 def create_hosted_zone(self, domain_name, caller_ref=None, comment=''): |
| 156 """ |
| 157 Create a new Hosted Zone. Returns a Python data structure with |
| 158 information about the newly created Hosted Zone. |
| 159 |
| 160 :type domain_name: str |
| 161 :param domain_name: The name of the domain. This should be a |
| 162 fully-specified domain, and should end with a final period |
| 163 as the last label indication. If you omit the final period, |
| 164 Amazon Route 53 assumes the domain is relative to the root. |
| 165 This is the name you have registered with your DNS registrar. |
| 166 It is also the name you will delegate from your registrar to |
| 167 the Amazon Route 53 delegation servers returned in |
| 168 response to this request.A list of strings with the image |
| 169 IDs wanted. |
| 170 |
| 171 :type caller_ref: str |
| 172 :param caller_ref: A unique string that identifies the request |
| 173 and that allows failed CreateHostedZone requests to be retried |
| 174 without the risk of executing the operation twice. If you don't |
| 175 provide a value for this, boto will generate a Type 4 UUID and |
| 176 use that. |
| 177 |
| 178 :type comment: str |
| 179 :param comment: Any comments you want to include about the hosted |
| 180 zone. |
| 181 |
| 182 """ |
| 183 if caller_ref is None: |
| 184 caller_ref = str(uuid.uuid4()) |
| 185 params = {'name': domain_name, |
| 186 'caller_ref': caller_ref, |
| 187 'comment': comment, |
| 188 'xmlns': self.XMLNameSpace} |
| 189 xml = HZXML % params |
| 190 uri = '/%s/hostedzone' % self.Version |
| 191 response = self.make_request('POST', uri, |
| 192 {'Content-Type': 'text/xml'}, xml) |
| 193 body = response.read() |
| 194 boto.log.debug(body) |
| 195 if response.status == 201: |
| 196 e = boto.jsonresponse.Element(list_marker='NameServers', |
| 197 item_marker=('NameServer',)) |
| 198 h = boto.jsonresponse.XmlHandler(e, None) |
| 199 h.parse(body) |
| 200 return e |
| 201 else: |
| 202 raise exception.DNSServerError(response.status, |
| 203 response.reason, |
| 204 body) |
| 205 |
| 206 def delete_hosted_zone(self, hosted_zone_id): |
| 207 uri = '/%s/hostedzone/%s' % (self.Version, hosted_zone_id) |
| 208 response = self.make_request('DELETE', uri) |
| 209 body = response.read() |
| 210 boto.log.debug(body) |
| 211 if response.status not in (200, 204): |
| 212 raise exception.DNSServerError(response.status, |
| 213 response.reason, |
| 214 body) |
| 215 e = boto.jsonresponse.Element() |
| 216 h = boto.jsonresponse.XmlHandler(e, None) |
| 217 h.parse(body) |
| 218 return e |
| 219 |
| 220 # Resource Record Sets |
| 221 |
| 222 def get_all_rrsets(self, hosted_zone_id, type=None, |
| 223 name=None, identifier=None, maxitems=None): |
| 224 """ |
| 225 Retrieve the Resource Record Sets defined for this Hosted Zone. |
| 226 Returns the raw XML data returned by the Route53 call. |
| 227 |
| 228 :type hosted_zone_id: str |
| 229 :param hosted_zone_id: The unique identifier for the Hosted Zone |
| 230 |
| 231 :type type: str |
| 232 :param type: The type of resource record set to begin the record |
| 233 listing from. Valid choices are: |
| 234 |
| 235 * A |
| 236 * AAAA |
| 237 * CNAME |
| 238 * MX |
| 239 * NS |
| 240 * PTR |
| 241 * SOA |
| 242 * SPF |
| 243 * SRV |
| 244 * TXT |
| 245 |
| 246 Valid values for weighted resource record sets: |
| 247 |
| 248 * A |
| 249 * AAAA |
| 250 * CNAME |
| 251 * TXT |
| 252 |
| 253 Valid values for Zone Apex Aliases: |
| 254 |
| 255 * A |
| 256 * AAAA |
| 257 |
| 258 :type name: str |
| 259 :param name: The first name in the lexicographic ordering of domain |
| 260 names to be retrieved |
| 261 |
| 262 :type identifier: str |
| 263 :param identifier: In a hosted zone that includes weighted resource |
| 264 record sets (multiple resource record sets with the same DNS |
| 265 name and type that are differentiated only by SetIdentifier), |
| 266 if results were truncated for a given DNS name and type, |
| 267 the value of SetIdentifier for the next resource record |
| 268 set that has the current DNS name and type |
| 269 |
| 270 :type maxitems: int |
| 271 :param maxitems: The maximum number of records |
| 272 |
| 273 """ |
| 274 from boto.route53.record import ResourceRecordSets |
| 275 params = {'type': type, 'name': name, |
| 276 'Identifier': identifier, 'maxitems': maxitems} |
| 277 uri = '/%s/hostedzone/%s/rrset' % (self.Version, hosted_zone_id) |
| 278 response = self.make_request('GET', uri, params=params) |
| 279 body = response.read() |
| 280 boto.log.debug(body) |
| 281 if response.status >= 300: |
| 282 raise exception.DNSServerError(response.status, |
| 283 response.reason, |
| 284 body) |
| 285 rs = ResourceRecordSets(connection=self, hosted_zone_id=hosted_zone_id) |
| 286 h = handler.XmlHandler(rs, self) |
| 287 xml.sax.parseString(body, h) |
| 288 return rs |
| 289 |
| 290 def change_rrsets(self, hosted_zone_id, xml_body): |
| 291 """ |
| 292 Create or change the authoritative DNS information for this |
| 293 Hosted Zone. |
| 294 Returns a Python data structure with information about the set of |
| 295 changes, including the Change ID. |
| 296 |
| 297 :type hosted_zone_id: str |
| 298 :param hosted_zone_id: The unique identifier for the Hosted Zone |
| 299 |
| 300 :type xml_body: str |
| 301 :param xml_body: The list of changes to be made, defined in the |
| 302 XML schema defined by the Route53 service. |
| 303 |
| 304 """ |
| 305 uri = '/%s/hostedzone/%s/rrset' % (self.Version, hosted_zone_id) |
| 306 response = self.make_request('POST', uri, |
| 307 {'Content-Type': 'text/xml'}, |
| 308 xml_body) |
| 309 body = response.read() |
| 310 boto.log.debug(body) |
| 311 if response.status >= 300: |
| 312 raise exception.DNSServerError(response.status, |
| 313 response.reason, |
| 314 body) |
| 315 e = boto.jsonresponse.Element() |
| 316 h = boto.jsonresponse.XmlHandler(e, None) |
| 317 h.parse(body) |
| 318 return e |
| 319 |
| 320 def get_change(self, change_id): |
| 321 """ |
| 322 Get information about a proposed set of changes, as submitted |
| 323 by the change_rrsets method. |
| 324 Returns a Python data structure with status information about the |
| 325 changes. |
| 326 |
| 327 :type change_id: str |
| 328 :param change_id: The unique identifier for the set of changes. |
| 329 This ID is returned in the response to the change_rrsets method. |
| 330 |
| 331 """ |
| 332 uri = '/%s/change/%s' % (self.Version, change_id) |
| 333 response = self.make_request('GET', uri) |
| 334 body = response.read() |
| 335 boto.log.debug(body) |
| 336 if response.status >= 300: |
| 337 raise exception.DNSServerError(response.status, |
| 338 response.reason, |
| 339 body) |
| 340 e = boto.jsonresponse.Element() |
| 341 h = boto.jsonresponse.XmlHandler(e, None) |
| 342 h.parse(body) |
| 343 return e |
| OLD | NEW |