can delete zone

This commit is contained in:
AJ ONeal 2018-01-31 19:09:26 -07:00
parent 92ad26d18e
commit 16bceb8a8a
4 changed files with 125 additions and 47 deletions

View File

@ -187,7 +187,7 @@ function getNs(engine, zs, results, cb) {
return -1 !== engine.primaryNameservers.indexOf(ns.data.toLowerCase()); return -1 !== engine.primaryNameservers.indexOf(ns.data.toLowerCase());
})) { })) {
results.authority.length = 0; results.authority.length = 0;
results.authority.push(engine.zoneToSoa(z)); results.authority.push(engine.zones._toSoa(z));
results.header.rcode = NXDOMAIN; results.header.rcode = NXDOMAIN;
} }
cb(null, results); cb(null, results);
@ -199,9 +199,9 @@ function getSoa(engine, domain, results, cb, answerSoa) {
console.log('[DEV] getSoa entered'); console.log('[DEV] getSoa entered');
if (!answerSoa) { if (!answerSoa) {
results.authority.push(engine.zoneToSoa(domain)); results.authority.push(engine.zones._toSoa(domain));
} else { } else {
results.answer.push(engine.zoneToSoa(domain)); results.answer.push(engine.zones._toSoa(domain));
} }
cb(null, results); cb(null, results);

View File

@ -189,6 +189,14 @@ module.exports.create = function (cli, engine/*, dnsd*/) {
res.send(zone); res.send(zone);
}); });
}); });
app.delete('/api/zones/:id', hasClaim('+rw@adns.org'), function (req, res) {
var zoneId = req.params.id;
engine.zones.destroy(zoneId, function (err, zone) {
if (err) { res.send({ error: { message: err.message } }); return; }
// zone + records
res.send(zone);
});
});
function mapRecord(r) { function mapRecord(r) {
return { return {
id: r.id id: r.id
@ -217,8 +225,7 @@ module.exports.create = function (cli, engine/*, dnsd*/) {
engine.zones.get({ names: [ zonename ] }, function (err, zones) { engine.zones.get({ names: [ zonename ] }, function (err, zones) {
console.log('zone:'); console.log('zone:');
console.log(zones[0]); console.log(zones[0]);
var zone = engine.zoneToSoa(zones[0]); var zone = engine.zones._toSoa(zones[0]);
console.log(zone);
zone.class = zone.className; zone.class = zone.className;
zone.type = zone.typeName; zone.type = zone.typeName;
engine.records.all(function (err, records) { engine.records.all(function (err, records) {

View File

@ -225,7 +225,48 @@
$qs('a.js-name').href = $qs('a.js-name').href =
$qs('a.js-name').dataset.href.replace(/:name/, ev.target.value || ':name'); $qs('a.js-name').dataset.href.replace(/:name/, ev.target.value || ':name');
}); });
$on('button.js-zone-destroy', 'click', function (ev) {
var zoneId = ev.target.parentElement.dataset.id;
var zoneName = ev.target.parentElement.dataset.name;
if (!window.confirm("Remove zone '" + zoneName + "' and all associated records?")) {
return;
}
return window.fetch(
'/api/zones/' + zoneId
, { method: 'DELETE'
, headers: new window.Headers({
'Authorization': 'Bearer ' + auth
, 'Content-Type': 'application/json;charset=UTF-8'
})
}
).then(function (resp) {
return resp.json().then(function (data) {
var zone;
if (data.error) {
console.error(data);
window.alert(data.error.message);
return;
}
console.log('zone undo data:');
console.log(data);
zone = cache.recordsMap[zoneId];
delete cache.recordsMap[zoneId];
function removeRecord(r, i) {
if (r.zone === zone.name) {
cache.records.splice(i, 1);
return true;
}
}
while (cache.records.some(removeRecord)) { continue; }
renderZones();
renderRecords();
console.log('result:', data);
});
});
});
$on('button.js-zone-view', 'click', function (ev) { $on('button.js-zone-view', 'click', function (ev) {
var zone = ev.target.parentElement.dataset.name; var zone = ev.target.parentElement.dataset.name;
myZone = zone; myZone = zone;

View File

@ -73,7 +73,39 @@ module.exports.create = function (opts) {
db.save._pending = []; db.save._pending = [];
engine.primaryNameservers = db.primaryNameservers; engine.primaryNameservers = db.primaryNameservers;
engine.zoneToSoa = function (domain) { engine.peers = {
all: function (cb) {
var dns = require('dns');
var count = db.primaryNameservers.length;
function gotRecord() {
count -= 1;
if (!count) {
cb(null, db.primaryNameservers);
}
}
function getRecord(ns) {
dns.resolve4(ns.name, function (err, addresses) {
console.log('ns addresses:');
console.log(addresses);
if (err) { console.error(err); gotRecord(); return; }
ns.type = 'A';
ns.address = addresses[0];
gotRecord();
});
}
db.primaryNameservers.forEach(getRecord);
}
};
engine.zones = {
_immutableKeys: [ 'id', 'name', 'primary', 'serial', 'revokedAt', 'changedAt', 'insertedAt', 'updatedAt', 'deletedAt' ]
, _mutableKeys: [ 'admin', 'expiration', 'minimum', 'refresh', 'retry', 'ttl', 'vanity' ]
, _dateToSerial: function (date) {
// conventionally the format is YYYYMMDDxx,
// but since it's an integer and I don't want to keep track of incrementing xx,
// epoch in seconds will do
return parseInt(Math.round(date/1000).toString().slice(-10), 10);
}
, _toSoa: function (domain) {
var nameservers = domain.vanityNs || engine.primaryNameservers.map(function (n) { return n.name; }); var nameservers = domain.vanityNs || engine.primaryNameservers.map(function (n) { return n.name; });
var index = Math.floor(Math.random() * nameservers.length) % nameservers.length; var index = Math.floor(Math.random() * nameservers.length) % nameservers.length;
@ -113,38 +145,6 @@ module.exports.create = function (opts) {
, minimum: domain.minimum || 5 , minimum: domain.minimum || 5
, nx: domain.minimum || 5 , nx: domain.minimum || 5
}; };
};
engine.peers = {
all: function (cb) {
var dns = require('dns');
var count = db.primaryNameservers.length;
function gotRecord() {
count -= 1;
if (!count) {
cb(null, db.primaryNameservers);
}
}
function getRecord(ns) {
dns.resolve4(ns.name, function (err, addresses) {
console.log('ns addresses:');
console.log(addresses);
if (err) { console.error(err); gotRecord(); return; }
ns.type = 'A';
ns.address = addresses[0];
gotRecord();
});
}
db.primaryNameservers.forEach(getRecord);
}
};
engine.zones = {
_immutableKeys: [ 'id', 'name', 'primary', 'serial', 'revokedAt', 'changedAt', 'insertedAt', 'updatedAt', 'deletedAt' ]
, _mutableKeys: [ 'admin', 'expiration', 'minimum', 'refresh', 'retry', 'ttl', 'vanity' ]
, _dateToSerial: function (date) {
// conventionally the format is YYYYMMDDxx,
// but since it's an integer and I don't want to keep track of incrementing xx,
// epoch in seconds will do
return parseInt(Math.round(date/1000).toString().slice(-10), 10);
} }
, all: function (cb) { , all: function (cb) {
process.nextTick(function () { process.nextTick(function () {
@ -307,11 +307,41 @@ module.exports.create = function (opts) {
db.records.push(ns); db.records.push(ns);
}); });
console.log('saving...'); console.log('[zone] [create] saving...');
db.save(function (err) { db.save(function (err) {
cb(err, !err && newZone || null); cb(err, !err && newZone || null);
}); });
} }
, destroy: function (zoneId, cb) {
var zone;
var records;
db.zones.filter(notDeleted).some(function (z) {
if (zoneId === z.id) {
zone = z;
return true;
}
});
if (!zone) {
process.nextTick(function () {
cb(null, null);
});
return;
}
records = [];
db.records.filter(notDeleted).forEach(function (r) {
if (zone.name === r.zone) {
records.push(r);
}
});
console.log('[zone] [destroy] saving...');
db.save(function (err) {
zone.records = records;
cb(err, !err && zone || null);
});
}
}; };
engine.records = { engine.records = {
all: function (cb) { all: function (cb) {