lmc/linodes/ip_failover/ip_failover.js

226 lines
6.6 KiB
JavaScript
Raw Normal View History

/*
* 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.failoverTable = "failover-table";
elements.info = "info";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.lmcRow = "lmc-tr3";
elements.loading = "loading";
elements.saveButton = "save-button";
elements.saveRow = "save-row";
elements.subnav = "subnav-link";
elements.subnavActive = "subnav-link-active";
// Data recieved from API calls
var data = {};
data.linode = {};
data.linodes = [];
// Static references to UI elements
var ui = {};
ui.failoverTable = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.loading = {};
ui.saveButton = {};
ui.saveRow = {};
// Temporary state
var state = {};
state.lidIndex = -1;
state.netInfoCount = 0;
// Callback for linode details API call
var displayDetails = function(response)
{
data.linode = response;
// Set page title and header stuff
document.title += " // " + data.linode.label;
ui.linodeLabel.innerHTML = data.linode.label;
if (data.linode.tags.length == 1) {
ui.linodeTagLink.href = "/linodes?tag=" + data.linode.tags[0];
ui.linodeTagLink.innerHTML = "(" + data.linode.tags[0] + ")";
ui.linodeTag.style.display = "inline";
}
// Get other Linodes in the same DC
var filter = {
"region": data.linode.region
};
apiGet("/linode/instances", displayLinodes, filter);
};
// 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) {
var filter = {
"region": data.linode.region
};
apiGet("/linode/instances?page=" + (response.page + 1), displayLinodes, filter);
return;
}
// Get net info for each linode
for (var i = 0; i < data.linodes.length; i++)
apiGet("/linode/instances/" + data.linodes[i].id + "/ips", displayNetInfo, null);
};
// Callback for net info API call
var displayNetInfo = function(response)
{
// Find the associated linode
var lid = parseInt(response['_endpoint'].split("/")[3]);
var index = -1;
for (var i = 0; i < data.linodes.length; i++) {
if (data.linodes[i].id == lid) {
index = i;
break;
}
}
if (i == -1)
return;
else if (lid == data.linode.id)
state.lidIndex = i;
data.linodes[index].netInfo = response;
state.netInfoCount++;
if (state.netInfoCount < data.linodes.length)
return;
// Populate the table
ui.loading.remove();
for (var i = 0; i < data.linodes.length; i++) {
if (data.linodes[i].id == data.linode.id)
continue;
var ips = data.linodes[i].netInfo.ipv4["public"].concat(data.linodes[i].netInfo.ipv4["private"]);
for (var j = 0; j < ips.length; j++) {
var row = document.createElement("tr");
row.className = elements.lmcRow;
var nameCell = document.createElement("td");
nameCell.innerHTML = data.linodes[i].label;
var ipCell = document.createElement("td");
var box = document.createElement("input");
box.id = ips[j].address;
box.type = "checkbox";
for (var k = 0; k < data.linodes[state.lidIndex].netInfo.ipv4.shared.length; k++) {
if (data.linodes[state.lidIndex].netInfo.ipv4.shared[k].address == ips[j].address)
box.checked = true;
}
ipCell.appendChild(box);
var label = document.createElement("label");
label.htmlFor = box.id;
label.innerHTML = " " + ips[j].address;
ipCell.appendChild(label);
if (ips[j].rdns) {
var rdns = document.createElement("span");
rdns.className = elements.info;
rdns.innerHTML = " (" + ips[j].rdns + ")";
ipCell.appendChild(rdns);
}
row.appendChild(nameCell);
row.appendChild(ipCell);
ui.failoverTable.insertBefore(row, ui.saveRow);
}
}
ui.saveButton.disabled = false;
};
// Handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var ips = [];
var boxes = document.getElementsByTagName("input");
for (var i = 0; i < boxes.length; i++) {
if (boxes[i].checked)
ips.push(boxes[i].id);
}
var req = {
"ips": ips,
"linode_id": data.linode.id
};
apiPost("/networking/ipv4/share", req, function(response) {
location.href = "/linodes/remote_access?lid=" + data.params.lid;
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need a Linode ID, so die if we don't have it
if (!data.params.lid) {
alert("No Linode ID supplied!");
return;
}
setupHeader();
// Update links on page to include proper Linode ID
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++)
anchors[i].href = anchors[i].href.replace("lid=0", "lid=" + data.params.lid);
// Highlight the remote access subnav link
var subnavLinks = document.getElementsByClassName(elements.subnav);
for (var i = 0; i < subnavLinks.length; i++) {
if (subnavLinks[i].pathname == "/linodes/remote_access")
subnavLinks[i].className += " " + elements.subnavActive;
}
// Get element references
ui.failoverTable = document.getElementById(elements.failoverTable);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.loading = document.getElementById(elements.loading);
ui.saveButton = document.getElementById(elements.saveButton);
ui.saveRow = document.getElementById(elements.saveRow);
// Attach event handlers
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/linode/instances/" + data.params.lid, displayDetails, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();