Example #1
0
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'))
Example #2
0
def records_new(zone_id):
    from route53.models import ChangeBatch, Change, db
    conn = get_connection()
    zone = conn.get_hosted_zone(zone_id)['GetHostedZoneResponse']['HostedZone']
    form = RecordForm()
    error = None
    if form.validate_on_submit():
        change_batch = ChangeBatch(change_id='',
                                   status='created',
                                   comment=form.comment.data)
        db.session.add(change_batch)
        change = Change(action="CREATE",
                        name=form.name.data,
                        type=form.type.data,
                        ttl=form.ttl.data,
                        values=form.values,
                        change_batch_id=change_batch.id)
        db.session.add(change)
        rendered_xml = render_change_batch({'changes': [change],
                                            'comment': change_batch.comment})
        try:
            resp = conn.change_rrsets(zone_id, rendered_xml)
            change_batch.process_response(resp)
            db.session.commit()
            return redirect(url_for('zones.zones_records', zone_id=zone_id))
        except DNSServerError as error:
            error = error
            db.session.rollback()
    return render_template('records/new.html',
                           form=form,
                           zone=zone,
                           zone_id=zone_id,
                           error=error)
Example #3
0
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"))
Example #4
0
def records_new(zone_id):
    from route53.models import ChangeBatch, Change, db
    conn = get_connection()
    zone = conn.get_hosted_zone(zone_id)['GetHostedZoneResponse']['HostedZone']
    form = RecordForm()
    error = None
    if form.validate_on_submit():
        change_batch = ChangeBatch(change_id='',
                                   status='created',
                                   comment=form.comment.data)
        db.session.add(change_batch)
        change = Change(action="CREATE",
                        name=form.name.data,
                        type=form.type.data,
                        ttl=form.ttl.data,
                        values=form.values,
                        change_batch_id=change_batch.id)
        db.session.add(change)
        rendered_xml = render_change_batch({
            'changes': [change],
            'comment': change_batch.comment
        })
        try:
            resp = conn.change_rrsets(zone_id, rendered_xml)
            change_batch.process_response(resp)
            db.session.commit()
            return redirect(url_for('zones.zones_records', zone_id=zone_id))
        except DNSServerError as error:
            error = error
            db.session.rollback()
    return render_template('records/new.html',
                           form=form,
                           zone=zone,
                           zone_id=zone_id,
                           error=error)
Example #5
0
def records_update(zone_id):
    from route53.models import ChangeBatch, Change, db
    conn = get_connection()
    zone = conn.get_hosted_zone(zone_id)['GetHostedZoneResponse']['HostedZone']
    val_dict = get_record_fields()

    if request.method == "GET":
        values = request.args.getlist('value')
        if not values:
            abort(404)
        initial_data = dict(val_dict)
        initial_data['value'] = ';'.join(values)
        form = RecordForm(**initial_data)

    error = None
    if request.method == "POST":
        form = RecordForm()
        change_batch = ChangeBatch(change_id='',
                                   status='created',
                                   comment=form.comment.data)
        db.session.add(change_batch)
        values = request.form.getlist('data_value')
        delete_change = Change(action="DELETE",
                               change_batch_id=change_batch.id,
                               values=values,
                               **val_dict)
        create_change = Change(action="CREATE",
                               change_batch_id=change_batch.id,
                               values=form.values,
                               type=form.type.data,
                               ttl=form.ttl.data,
                               name=form.name.data)
        db.session.add(delete_change)
        db.session.add(create_change)
        rendered_xml = render_change_batch({
            'changes': [delete_change, create_change],
            'comment':
            change_batch.comment
        })
        try:
            resp = conn.change_rrsets(zone_id, rendered_xml)
            change_batch.process_response(resp)
            db.session.commit()
            return redirect(url_for('zones.zones_records', zone_id=zone_id))
        except DNSServerError as error:
            error = error
    return render_template('records/update.html',
                           val_dict=val_dict,
                           values=values,
                           form=form,
                           zone=zone,
                           zone_id=zone_id,
                           error=error)
Example #6
0
def records_delete(zone_id):
    from route53.models import ChangeBatch, Change, db
    conn = get_connection()
    zone = conn.get_hosted_zone(zone_id)['GetHostedZoneResponse']['HostedZone']
    val_dict = get_record_fields()

    if request.method == "GET":
        values = request.args.getlist('value')
        alias_hosted_zone_id = request.args.get('alias_hosted_zone_id', None)
        alias_dns_name = request.args.get('alias_dns_name', None)
        if not values and not alias_hosted_zone_id and not alias_dns_name:
            abort(404)

    error = None
    if request.method == "POST":
        change_batch = ChangeBatch(change_id='', status='created', comment='')
        db.session.add(change_batch)
        values = request.form.getlist('data_value')
        alias_hosted_zone_id = request.form.get('data_alias_hosted_zone_id',
                                                None)
        alias_dns_name = request.form.get('data_alias_dns_name', None)
        change = Change(action="DELETE",
                        change_batch_id=change_batch.id,
                        values={
                            'values': values,
                            'alias_hosted_zone_id': alias_hosted_zone_id,
                            'alias_dns_name': alias_dns_name,
                        },
                        **val_dict)
        db.session.add(change)
        rendered_xml = render_change_batch({
            'changes': [change],
            'comment': change_batch.comment
        })
        try:
            resp = conn.change_rrsets(zone_id, rendered_xml)
            change_batch.process_response(resp)
            db.session.commit()
            return redirect(url_for('zones.zones_records', zone_id=zone_id))
        except DNSServerError as error:
            error = error
    return render_template('records/delete.html',
                           val_dict=val_dict,
                           values=values,
                           alias_hosted_zone_id=alias_hosted_zone_id,
                           alias_dns_name=alias_dns_name,
                           zone=zone,
                           zone_id=zone_id,
                           error=error)
Example #7
0
def records_update(zone_id):
    from route53.models import ChangeBatch, Change, db
    conn = get_connection()
    zone = conn.get_hosted_zone(zone_id)['GetHostedZoneResponse']['HostedZone']
    val_dict = get_record_fields()

    if request.method == "GET":
        values = request.args.getlist('value')
        if not values:
            abort(404)
        initial_data = dict(val_dict)
        initial_data['value'] = ';'.join(values)
        form = RecordForm(**initial_data)

    error = None
    if request.method == "POST":
        form = RecordForm()
        change_batch = ChangeBatch(change_id='', status='created', comment=form.comment.data)
        db.session.add(change_batch)
        values = request.form.getlist('data_value')
        delete_change = Change(action="DELETE",
                               change_batch_id=change_batch.id,
                               values=values,
                               **val_dict)
        create_change = Change(action="CREATE",
                               change_batch_id=change_batch.id,
                               values=form.values,
                               type=form.type.data,
                               ttl=form.ttl.data,
                               name=form.name.data)
        db.session.add(delete_change)
        db.session.add(create_change)
        rendered_xml = render_change_batch({'changes': [delete_change, create_change],
                                            'comment': change_batch.comment})
        try:
            resp = conn.change_rrsets(zone_id, rendered_xml)
            change_batch.process_response(resp)
            db.session.commit()
            return redirect(url_for('zones.zones_records', zone_id=zone_id))
        except DNSServerError as error:
            error = error
    return render_template('records/update.html',
                           val_dict=val_dict,
                           values=values,
                           form=form,
                           zone=zone,
                           zone_id=zone_id,
                           error=error)
def records_delete(zone_id):
    from route53.models import ChangeBatch, Change, db
    conn = get_connection()
    zone = conn.get_hosted_zone(zone_id)['GetHostedZoneResponse']['HostedZone']
    val_dict = get_record_fields()

    if request.method == "GET":
        values = request.args.getlist('value')
        alias_hosted_zone_id = request.args.get('alias_hosted_zone_id', None)
        alias_dns_name = request.args.get('alias_dns_name', None)
        if not values and not alias_hosted_zone_id and not alias_dns_name:
            abort(404)

    error = None
    if request.method == "POST":
        change_batch = ChangeBatch(change_id='', status='created', comment='')
        db.session.add(change_batch)
        values = request.form.getlist('data_value')
        alias_hosted_zone_id = request.form.get('data_alias_hosted_zone_id', None)
        alias_dns_name = request.form.get('data_alias_dns_name', None)
        change = Change(action="DELETE",
                        change_batch_id=change_batch.id,
                        values={
                            'values': values,
                            'alias_hosted_zone_id': alias_hosted_zone_id,
                            'alias_dns_name': alias_dns_name,
                        },
                        **val_dict)
        db.session.add(change)
        rendered_xml = render_change_batch({'changes': [change],
                                            'comment': change_batch.comment})
        try:
            resp = conn.change_rrsets(zone_id, rendered_xml)
            change_batch.process_response(resp)
            db.session.commit()
            return redirect(url_for('zones.zones_records', zone_id=zone_id))
        except DNSServerError as error:
            error = error
    return render_template('records/delete.html',
                           val_dict=val_dict,
                           values=values,
                           alias_hosted_zone_id=alias_hosted_zone_id,
                           alias_dns_name=alias_dns_name,
                           zone=zone,
                           zone_id=zone_id,
                           error=error)
Example #9
0
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)
Example #10
0
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'))