lmc/dns/domain_add/domain_add.js

244 lines
6.0 KiB
JavaScript

/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, apiPost, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.addMaster = "add-master";
elements.addSlave = "add-slave";
elements.email = "email";
elements.linode = "linode";
elements.masterDomain = "master-domain";
elements.masters = "masters";
elements.masterSection = "master-section";
elements.no = "no";
elements.slaveDomain = "slave-domain";
elements.slaveSection = "slave-section";
elements.yes = "yes";
// Data recieved from API calls
var data = {};
data.linodes = [];
// Static references to UI elements
var ui = {};
ui.addMaster = {};
ui.addSlave = {};
ui.email = {};
ui.linode = {};
ui.masterDomain = {};
ui.masters = {};
ui.masterSection = [];
ui.no = {};
ui.slaveDomain = {};
ui.slaveSection = [];
ui.yes = {};
// Temporary state
var state = {};
state.did = 0;
state.returned = 0;
state.total = 0;
// Add initial records to new domain
var addRecords = function(response)
{
// Find the selected linode
var linode = null;
for (var i = 0; i < data.linodes.length; i++) {
if (data.linodes[i].id == parseInt(ui.linode.value)) {
linode = data.linodes[i];
break;
}
}
if (!linode) {
location.href = "/dns/domain?did=" + response.id;
return;
}
// Create record requests
state.did = response.id;
var callback = function(response) {
state.returned++;
if (state.returned >= state.total)
location.href = "/dns/domain?did=" + state.did;
}
var reqs = [
{
"type": "A",
"name": "",
"target": linode.ipv4[0]
},
{
"type": "A",
"name": "mail",
"target": linode.ipv4[0]
},
{
"type": "A",
"name": "www",
"target": linode.ipv4[0]
},
{
"type": "AAAA",
"name": "",
"target": linode.ipv6.split("/")[0]
},
{
"type": "AAAA",
"name": "mail",
"target": linode.ipv6.split("/")[0]
},
{
"type": "AAAA",
"name": "www",
"target": linode.ipv6.split("/")[0]
},
{
"type": "MX",
"target": "mail.meredithlaboon.com",
"priority": 10,
"name": ""
}
];
state.total = reqs.length;
for (var i = 0; i < reqs.length; i++)
apiPost("/domains/" + response.id + "/records", reqs[i], callback);
};
// Callback for Linodes API call
var displayLinodes = function(response)
{
// Add linodes to array
data.linodes = data.linodes.concat(response.data);
// Request the next page if there are more pages
if (response.page != response.pages) {
apiGet("/linode/instances?page=" + (response.page + 1), displayLinodes, null);
return;
}
// Add linodes to selector
for (var i = 0; i < data.linodes.length; i++) {
var option = document.createElement("option");
option.value = data.linodes[i].id;
option.innerHTML = data.linodes[i].label;
ui.linode.appendChild(option);
}
ui.addMaster.disabled = false;
};
// Click handler for add master button
var handleAddMaster = function(event)
{
if (event.currentTarget.disabled)
return;
// This request takes a few seconds
ui.addMaster.disabled = true;
var req = {
"domain": ui.masterDomain.value,
"type": "master",
"soa_email": ui.email.value
};
var callback = function(response)
{
location.href = "/dns/domain?did=" + response.id;
};
if (ui.yes.checked)
callback = addRecords;
apiPost("/domains", req, callback);
};
// Click handler for add slave button
var handleAddSlave = function(event)
{
if (event.currentTarget.disabled)
return;
// This request takes a few seconds
ui.addSlave.disabled = true;
var ips = [];
var masterLines = ui.masters.value.split("\n");
for (var i = 0; i < masterLines.length; i++)
ips = ips.concat(masterLines[i].split(";"));
var req = {
"domain": ui.slaveDomain.value,
"type": "slave",
"master_ips": ips
};
apiPost("/domains", req, function(response)
{
location.href = "/dns/domain?did=" + response.id;
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.addMaster = document.getElementById(elements.addMaster);
ui.addSlave = document.getElementById(elements.addSlave);
ui.email = document.getElementById(elements.email);
ui.linode = document.getElementById(elements.linode);
ui.masterDomain = document.getElementById(elements.masterDomain);
ui.masters = document.getElementById(elements.masters);
ui.masterSection = document.getElementsByClassName(elements.masterSection);
ui.no = document.getElementById(elements.no);
ui.slaveDomain = document.getElementById(elements.slaveDomain);
ui.slaveSection = document.getElementsByClassName(elements.slaveSection);
ui.yes = document.getElementById(elements.yes);
// Display desired section
var section = null;
if (data.params.type && data.params.type == "slave")
section = ui.slaveSection;
else
section = ui.masterSection;
for (var i = 0; i < section.length; i++)
section[i].style.display = "table-row-group";
// Attach event handlers
ui.addMaster.addEventListener("click", handleAddMaster);
ui.addSlave.addEventListener("click", handleAddSlave);
// Get data from API
if (!data.params.type || data.params.type != "slave")
apiGet("/linode/instances", displayLinodes, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();