def import_zone(zone_id): from route53.models import ChangeBatch, Change, db Zone = get_zone_class() zone = Zone.find(zone_id) Record = get_record_class() # filter out NS records records = filter(lambda x: x.record_type != 'NS', Record.find(zone_id=zone_id)) records = sorted(records, key=lambda x: x.record_type) # order records by record_type and name into recordsets conn = get_connection() response = conn.create_hosted_zone(zone.origin) info = response['CreateHostedZoneResponse'] new_zone_id = info['HostedZone']['Id'] errors = [] for k, g in groupby(records, key=lambda x: (x.record_type, x.name)): change_batch = ChangeBatch(change_id='', status='created', comment='') db.session.add(change_batch) record_type, name = k rcds = list(g) record_name = zone.origin in name and name or name + "." + zone.origin if record_type not in ('MX', 'SRV'): values = map(lambda x: x.data, rcds) else: values = map(lambda x: "%s %s" % (x.aux, x.data), rcds) change = Change(action="CREATE", name=record_name, type=record_type, ttl=rcds[0].ttl, values={'values': values}, change_batch_id=change_batch.id) db.session.add(change) changes = [change] rendered_xml = render_change_batch({'changes': changes, 'comment': ''}) try: from route53 import shortid resp = conn.change_rrsets(shortid(new_zone_id), rendered_xml) change_batch.process_response(resp) db.session.commit() except DNSServerError as error: errors.append((record_type, name, error)) db.session.rollback() if errors: return render_template('slicehost/import_zone.html', errors=errors, zone=zone) return redirect(url_for('main.index'))
def import_zone(zone_id): from route53.models import ChangeBatch, Change, db Zone = get_zone_class() zone = Zone.find(zone_id) Record = get_record_class() # filter out NS records records = filter(lambda x: x.record_type != "NS", Record.find(zone_id=zone_id)) records = sorted(records, key=lambda x: x.record_type) # order records by record_type and name into recordsets conn = get_connection() response = conn.create_hosted_zone(zone.origin) info = response["CreateHostedZoneResponse"] new_zone_id = info["HostedZone"]["Id"] errors = [] for k, g in groupby(records, key=lambda x: (x.record_type, x.name)): change_batch = ChangeBatch(change_id="", status="created", comment="") db.session.add(change_batch) record_type, name = k rcds = list(g) record_name = zone.origin in name and name or name + "." + zone.origin if record_type not in ("MX", "SRV"): values = map(lambda x: x.data, rcds) else: values = map(lambda x: "%s %s" % (x.aux, x.data), rcds) change = Change( action="CREATE", name=record_name, type=record_type, ttl=rcds[0].ttl, values=values, change_batch_id=change_batch.id, ) db.session.add(change) changes = [change] rendered_xml = render_change_batch({"changes": changes, "comment": ""}) try: from route53 import shortid resp = conn.change_rrsets(shortid(new_zone_id), rendered_xml) change_batch.process_response(resp) db.session.commit() except DNSServerError as error: errors.append((record_type, name, error)) db.session.rollback() if errors: return render_template("slicehost/import_zone.html", errors=errors, zone=zone) return redirect(url_for("main.index"))
def clone(zone_id): conn = get_connection() zone_response = conn.get_hosted_zone(zone_id) original_zone = zone_response['GetHostedZoneResponse']['HostedZone'] form = ZoneForm() errors = [] if form.validate_on_submit(): response = conn.create_hosted_zone( form.name.data, comment=form.comment.data) info = response['CreateHostedZoneResponse'] nameservers = ', '.join(info['DelegationSet']['NameServers']) new_zone_id = info['HostedZone']['Id'] original_records = conn.get_all_rrsets(zone_id) from route53.models import ChangeBatch, Change, db for recordset in original_records: if not recordset.type in ["SOA", "NS"]: change_batch = ChangeBatch(change_id='', status='created', comment='') db.session.add(change_batch) change = Change(action="CREATE", name=recordset.name.replace(original_zone['Name'], form.name.data), type=recordset.type, ttl=recordset.ttl, values = recordset.resource_records, change_batch_id=change_batch.id) db.session.add(change) changes = [change] rendered_xml = render_change_batch({'changes': changes, 'comment': ''}) try: from route53 import shortid resp = conn.change_rrsets(shortid(new_zone_id), rendered_xml) change_batch.process_response(resp) db.session.commit() except DNSServerError as error: errors.append((recordset.type, recordset.name, error)) db.session.rollback() if not errors: flash(u"A zone with id %s has been created. " u"Use following nameservers %s" % (new_zone_id, nameservers)) return redirect(url_for('zones_list')) return render_template('zones/clone.html', form=form, errors=errors, original_zone=original_zone)
def zones_clone(zone_id): conn = get_connection() zone_response = conn.get_hosted_zone(zone_id) original_zone = zone_response['GetHostedZoneResponse']['HostedZone'] form = ZoneForm() errors = [] if form.validate_on_submit(): response = conn.create_hosted_zone( form.name.data, comment=form.comment.data) info = response['CreateHostedZoneResponse'] nameservers = ', '.join(info['DelegationSet']['NameServers']) new_zone_id = info['HostedZone']['Id'] original_records = conn.get_all_rrsets(zone_id) from route53.models import ChangeBatch, Change, db for recordset in original_records: if not recordset.type in ["SOA", "NS"]: change_batch = ChangeBatch(change_id='', status='created', comment='') db.session.add(change_batch) change = Change(action="CREATE", name=recordset.name.replace(original_zone['Name'], form.name.data), type=recordset.type, ttl=recordset.ttl, values = recordset.resource_records, change_batch_id=change_batch.id) db.session.add(change) changes = [change] rendered_xml = render_change_batch({'changes': changes, 'comment': ''}) try: from route53 import shortid resp = conn.change_rrsets(shortid(new_zone_id), rendered_xml) change_batch.process_response(resp) db.session.commit() except DNSServerError as error: errors.append((recordset.type, recordset.name, error)) db.session.rollback() if not errors: flash(u"A zone with id %s has been created. " u"Use following nameservers %s" % (new_zone_id, nameservers)) return redirect(url_for('zones_list')) return render_template('zones/clone.html', form=form, errors=errors, original_zone=original_zone)
def import_zone(zone_id): from route53.models import ChangeBatch, Change, db Zone = get_zone_class() zone = Zone.find(zone_id) Record = get_record_class() # filter out NS records records = filter(lambda x: x.record_type != 'NS', Record.find(zone_id=zone_id)) records = sorted(records, key=lambda x: x.record_type) # order records by record_type and name into recordsets conn = get_connection() response = conn.create_hosted_zone(zone.origin) info = response['CreateHostedZoneResponse'] new_zone_id = info['HostedZone']['Id'] errors = [] for k, g in groupby(records, key=lambda x: (x.record_type, x.name)): change_batch = ChangeBatch(change_id='', status='created', comment='') db.session.add(change_batch) record_type, name = k rcds = list(g) record_name = zone.origin in name and name or name + "." + zone.origin if record_type not in ('MX', 'SRV'): values = map(lambda x: x.data, rcds) else: values = map(lambda x: "%s %s" % (x.aux, x.data), rcds) change = Change(action="CREATE", name=record_name, type=record_type, ttl=rcds[0].ttl, values={'values':values}, change_batch_id=change_batch.id) db.session.add(change) changes = [change] rendered_xml = render_change_batch({'changes': changes, 'comment': ''}) try: from route53 import shortid resp = conn.change_rrsets(shortid(new_zone_id), rendered_xml) change_batch.process_response(resp) db.session.commit() except DNSServerError as error: errors.append((record_type, name, error)) db.session.rollback() if errors: return render_template('slicehost/import_zone.html', errors=errors, zone=zone) return redirect(url_for('main.index'))