<s> from boto . route53 . exception import DNSServerError <EOL> from itertools import groupby <EOL> from flask import Module <EOL> from flask import url_for , render_template , redirect , flash , request <EOL> from route53 . forms import ZoneForm <EOL> from route53 . connection import get_connection <EOL> from route53 . xmltools import render_change_batch <EOL> zones = Module ( __name__ ) <EOL> @ zones . route ( '<STR_LIT:/>' ) <EOL> def zones_list ( ) : <EOL> conn = get_connection ( ) <EOL> response = conn . get_all_hosted_zones ( ) <EOL> zones = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return render_template ( '<STR_LIT>' , zones = zones ) <EOL> @ zones . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def zones_new ( ) : <EOL> conn = get_connection ( ) <EOL> form = ZoneForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> response = conn . create_hosted_zone ( <EOL> form . name . data , <EOL> comment = form . comment . data ) <EOL> info = response [ '<STR_LIT>' ] <EOL> nameservers = '<STR_LIT:U+002CU+0020>' . join ( info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> zone_id = info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> flash ( u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> % ( zone_id , nameservers ) ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ zones . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def zones_delete ( zone_id ) : <EOL> conn = get_connection ( ) <EOL> zone = conn . get_hosted_zone ( zone_id ) [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> error = None <EOL> if request . method == '<STR_LIT:POST>' and '<STR_LIT>' in request . form : <EOL> try : <EOL> conn . delete_hosted_zone ( zone_id ) <EOL> flash ( u"<STR_LIT>" % zone_id ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> except DNSServerError as error : <EOL> error = error <EOL> return render_template ( '<STR_LIT>' , <EOL> zone_id = zone_id , <EOL> zone = zone , <EOL> error = error ) <EOL> @ zones . route ( '<STR_LIT>' ) <EOL> def zones_detail ( zone_id ) : <EOL> conn = get_connection ( ) <EOL> resp = conn . get_hosted_zone ( zone_id ) <EOL> zone = resp [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> nameservers = resp [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return render_template ( '<STR_LIT>' , <EOL> zone_id = zone_id , <EOL> zone = zone , <EOL> nameservers = nameservers ) <EOL> @ zones . route ( '<STR_LIT>' ) <EOL> def zones_records ( zone_id ) : <EOL> conn = get_connection ( ) <EOL> resp = conn . get_hosted_zone ( zone_id ) <EOL> zone = resp [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> record_resp = sorted ( conn . get_all_rrsets ( zone_id ) , key = lambda x : x . type ) <EOL> groups = groupby ( record_resp , key = lambda x : x . type ) <EOL> groups = [ ( k , list ( v ) ) for k , v in groups ] <EOL> return render_template ( '<STR_LIT>' , <EOL> zone_id = zone_id , <EOL> zone = zone , <EOL> groups = groups ) <EOL> @ zones . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def zones_clone ( zone_id ) : <EOL> conn = get_connection ( ) <EOL> zone_response = conn . get_hosted_zone ( zone_id ) <EOL> original_zone = zone_response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> form = ZoneForm ( ) <EOL> errors = [ ] <EOL> if form . validate_on_submit ( ) : <EOL> response = conn . create_hosted_zone ( <EOL> form . name . data , <EOL> comment = form . comment . data ) <EOL> info = response [ '<STR_LIT>' ] <EOL> nameservers = '<STR_LIT:U+002CU+0020>' . join ( info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> new_zone_id = info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> original_records = conn . get_all_rrsets ( zone_id ) <EOL> from route53 . models import ChangeBatch , Change , db <EOL> for recordset in original_records : <EOL> if not recordset . type in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> change_batch = ChangeBatch ( change_id = '<STR_LIT>' , <EOL> status = '<STR_LIT>' , <EOL> comment = '<STR_LIT>' ) <EOL> db . session . add ( change_batch ) <EOL> change = Change ( action = "<STR_LIT>" , <EOL> name = recordset . name . replace ( original_zone [ '<STR_LIT:Name>' ] , <EOL> form . name . data ) , <EOL> type = recordset . type , <EOL> ttl = recordset . ttl , <EOL> values = recordset . resource_records , <EOL> change_batch_id = change_batch . id ) <EOL> db . session . add ( change ) <EOL> changes = [ change ] <EOL> rendered_xml = render_change_batch ( { '<STR_LIT>' : changes , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> try : <EOL> from route53 import shortid <EOL> resp = conn . change_rrsets ( shortid ( new_zone_id ) , rendered_xml ) <EOL> change_batch . process_response ( resp ) <EOL> db . session . commit ( ) <EOL> except DNSServerError as error : <EOL> errors . append ( ( recordset . type , recordset . name , error ) ) <EOL> db . session . rollback ( ) <EOL> if not errors : <EOL> flash ( u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> % ( new_zone_id , nameservers ) ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , <EOL> form = form , errors = errors , original_zone = original_zone ) </s>
