/* * 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 . */ import { settings, elements, apiGet, apiPost, apiPut, parseParams, setupHeader } from "/global.js"; (function() { // Element names specific to this page elements.address = "address"; elements.configLabel = "config-label"; elements.label = "label"; elements.mode = "mode"; elements.nodebalancerLabel = "nodebalancer-label"; elements.nodebalancerTag = "nodebalancer-tag"; elements.nodebalancerTagLink = "nodebalancer-tag-link"; elements.port = "port"; elements.saveButton = "save-button"; elements.weight = "weight"; // Data recieved from API calls var data = {}; data.config = {}; data.linodeFilters = { "ipv4": { "+contains": "192.168" } }; data.linodes = []; data.nodebalancer = {}; data.node = {}; // Static references to UI elements var ui = {}; ui.address = {}; ui.configLabel = {}; ui.label = {}; ui.mode = {}; ui.nodebalancerLabel = {}; ui.nodebalancerTag = {}; ui.nodebalancerTagLink = {}; ui.port = {}; ui.saveButton = {}; ui.weight = {}; // Callback for config API call var displayConfig = function(response) { data.config = response; ui.configLabel.innerHTML = "Port " + data.config.port; if (!parseInt(data.params.nbnid)) ui.port.value = data.config.port; }; // Callback for linodes API call var displayLinodes = function(response) { 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, data.linodeFilters); return; } // Add linodes to address selector for (var i = 0; i < data.linodes.length; i++) { var ipIndex = -1; for (var j = 0; j < data.linodes[i].ipv4.length; j++) { if (data.linodes[i].ipv4[j].indexOf("192.168.") == 0) { ipIndex = j; break; } } if (ipIndex == -1) continue; var option = document.createElement("option"); option.value = data.linodes[i].ipv4[j]; option.innerHTML = data.linodes[i].ipv4[j] + " (" + data.linodes[i].label + ")"; ui.address.appendChild(option); } if (parseInt(data.params.nbnid)) apiGet("/nodebalancers/" + data.params.nbid + "/configs/" + data.params.nbcid + "/nodes/" + data.params.nbnid, displayNode, null); else ui.saveButton.disabled = false; }; // Callback for nodebalancer API call var displayNodebalancer = function(response) { data.nodebalancer = response; // Set page title and header stuff if (document.title.indexOf("//") == -1) document.title += " // Edit " + data.nodebalancer.label; ui.nodebalancerLabel.innerHTML = data.nodebalancer.label; if (data.nodebalancer.tags.length == 1) { ui.nodebalancerTagLink.href = "/nodebalancers?tag=" + data.nodebalancer.tags[0]; ui.nodebalancerTagLink.innerHTML = "(" + data.nodebalancer.tags[0] + ")"; ui.nodebalancerTag.style.display = "inline"; } else { ui.nodebalancerTag.style.display = "none"; } // Get linodes data.linodeFilters.region = data.nodebalancer.region; apiGet("/linode/instances", displayLinodes, data.linodeFilters); }; // Callback for node API call var displayNode = function(response) { data.node = response; ui.label.value = data.node.label; var lastColon = data.node.address.lastIndexOf(":"); ui.address.value = data.node.address.slice(0, lastColon); ui.port.value = data.node.address.slice(lastColon + 1); ui.weight.value = data.node.weight; ui.mode.value = data.node.mode; ui.saveButton.disabled = false; }; // Save button handler var handleSave = function(event) { if (event.currentTarget.disabled) return; if (ui.address.value == "0") return; var req = { "label": ui.label.value, "address": ui.address.value + ":" + ui.port.value, "weight": parseInt(ui.weight.value), "mode": ui.mode.value }; if (data.params.nbnid == 0) { apiPost("/nodebalancers/" + data.params.nbid + "/configs/" + data.params.nbcid + "/nodes", req, function(response) { location.href = "/nodebalancers/config?nbid=" + data.params.nbid + "&nbcid=" + data.params.nbcid; }); } else { apiPut("/nodebalancers/" + data.params.nbid + "/configs/" + data.params.nbcid + "/nodes/" + data.params.nbnid, req, function(response) { location.href = "/nodebalancers/config?nbid=" + data.params.nbid + "&nbcid=" + data.params.nbcid; }); } }; // Initial setup var setup = function() { // Parse URL parameters data.params = parseParams(); // We need a NodeBalancer ID, so die if we don't have it if (!data.params.nbid) { alert("No NodeBalancer ID supplied!"); return; } // We also need a config ID if (!data.params.nbcid) { alert("No config ID supplied!"); return; } // We also need a node ID if (!data.params.nbnid) { alert("No node 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("nbid=0", "nbid=" + data.params.nbid); anchors[i].href = anchors[i].href.replace("nbcid=0", "nbcid=" + data.params.nbcid); } // Get element references ui.address = document.getElementById(elements.address); ui.configLabel = document.getElementById(elements.configLabel); ui.label = document.getElementById(elements.label); ui.mode = document.getElementById(elements.mode); ui.nodebalancerLabel = document.getElementById(elements.nodebalancerLabel); ui.nodebalancerTag = document.getElementById(elements.nodebalancerTag); ui.nodebalancerTagLink = document.getElementById(elements.nodebalancerTagLink); ui.port = document.getElementById(elements.port); ui.saveButton = document.getElementById(elements.saveButton); ui.weight = document.getElementById(elements.weight); // Register event handlers ui.saveButton.addEventListener("click", handleSave); // Get data from API apiGet("/nodebalancers/" + data.params.nbid, displayNodebalancer, null); apiGet("/nodebalancers/" + data.params.nbid + "/configs/" + data.params.nbcid, displayConfig, null); }; // Show/hide check rows var showHideChecks = function(event) { for (var i = 0; i < ui.checkShow.length; i++) { if (ui.checkShow[i].classList.contains(elements.checkShow + "-" + ui.checkType.value)) ui.checkShow[i].style.display = "table-row"; else ui.checkShow[i].style.display = "none"; } }; // Show/hide protocol stuff var showHideProtocol = function(event) { for (var i = 0; i < ui.protocolShow.length; i++) { if (ui.protocolShow[i].classList.contains(elements.protocolShow + "-" + ui.protocol.value)) { if (ui.protocolShow[i].classList.contains(elements.replaceCert)) { if (ui.protocolShow[i].classList.contains(elements.replaceCertOnly) == state.replaceCert) ui.protocolShow[i].style.display = "table-row"; else ui.protocolShow[i].style.display = "none"; } else { ui.protocolShow[i].style.display = "table-row"; } } else { ui.protocolShow[i].style.display = "none"; } } }; // Attach onload handler window.addEventListener("load", setup); })();