2 * Copyright (C) 2014 SDN Hub
4 * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.
5 * You may not use this file except in compliance with this License.
6 * You may obtain a copy of the License at
8 * http://www.gnu.org/licenses/gpl-3.0.txt
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16 var url = "http://" + location.hostname + ":8080";
19 function updateHosts() {
20 var serverSelect = document.getElementById("servers");
22 $.getJSON(url.concat("/v1.0/hosts"), function(hosts){
23 $.each(hosts, function(key, value){
24 hostList[key] = value.mac
25 el = document.createElement("option");
28 serverSelect.appendChild(el);
35 /* Format of the POST data is as follows:
37 {'servers': list of {'ip': ip string, 'mac': mac string},
38 'virtual_ip': ip string,
39 'rewrite_ip': 0 or 1 }
43 function makePostData() {
44 var vip = $('#virtual-ip').val();
45 var servers = $('#servers').val();
46 var rewriteIP = $('#rewrite-ip').is(':checked');
48 lbConfig['servers'] = [];
50 if (servers != undefined) {
51 for (i=0; i<servers.length;i++) {
52 var server = servers[i];
53 lbConfig['servers'].push({'ip': server, 'mac': hostList[server]});
56 lbConfig['virtual_ip'] = vip;
59 lbConfig['rewrite_ip'] = 1;
61 lbConfig['rewrite_ip'] = 0;
67 function createLBPool() {
68 $('#post-status').html('');
70 var lbConfig = makePostData();
71 if (lbConfig == undefined)
74 $.post(url.concat("/v1.0/loadbalancer/create"), JSON.stringify(lbConfig), function() {
77 $('#post-status').html('');
78 $('#main').html('<h2>Load-balancer pool created</h2><p>Successfully created load-balancer pool. Start sending requests to the virtual IP.</p><button class="pure-button pure-button-primary" onclick="deleteLBPool(\''+lbConfig.virtual_ip+'\')">Delete LB pool</button>');
81 $('#post-status').html('<p style="color:red; background:silver;">Error: Load-balancer pool creation failed. Please verify your input.');
85 function deleteLBPool(vip) {
86 if (typeof(vip)==='undefined') {
87 vip = $('#virtual-ip').val();
90 $('#post-status').html('');
93 lbConfig['virtual_ip'] = vip;
94 lbConfig['rewrite_ip'] = 1;
95 lbConfig.servers = [];
97 $.post(url.concat("/v1.0/loadbalancer/delete"), JSON.stringify(lbConfig), function() {
100 // In direct call cases where VIP was pre-specified in onClick,
101 // it will be best to direct to the original main even before
102 // the delete pool button click
103 $('#post-status').html('');
104 $('#main').html('<h2>Load-balancer pool deleted</h2><p>Successfully deleted load-balancer pool.</p><button class="pure-button pure-button-primary" onclick="window.location.reload()">Create LB pool</button>');
107 $('#post-status').html('<p style="color:red; background:silver;">Error: Load-balancer pool deletion failed. Please verify your input.');