Added images and made the visualizator work
[VSoRC/.git] / js / loadbalancer.js
1 /*
2  * Copyright (C) 2014 SDN Hub
3  *
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
7  *
8  *    http://www.gnu.org/licenses/gpl-3.0.txt
9  *
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
13  * implied.
14  */
15
16 var url = "http://" + location.hostname + ":8080";
17 var hostList = {};
18
19 function updateHosts() {
20     var serverSelect = document.getElementById("servers");
21
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");
26             el.textContent = key;
27             el.value = key;
28             serverSelect.appendChild(el);
29         });
30     });
31 }
32     
33 updateHosts();
34
35 /* Format of the POST data is as follows:
36
37 {'servers': list of {'ip': ip string, 'mac': mac string},
38 'virtual_ip': ip string,
39 'rewrite_ip': 0 or 1 }
40
41  */
42
43 function makePostData() {
44     var vip = $('#virtual-ip').val();
45     var servers = $('#servers').val();
46     var rewriteIP = $('#rewrite-ip').is(':checked');
47     var lbConfig = {};
48     lbConfig['servers'] = [];
49
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]});
54         }
55     }
56     lbConfig['virtual_ip'] = vip;
57
58     if (rewriteIP) 
59         lbConfig['rewrite_ip'] = 1;
60     else
61         lbConfig['rewrite_ip'] = 0;
62
63     return lbConfig;
64 }
65
66
67 function createLBPool() {
68     $('#post-status').html('');
69
70     var lbConfig = makePostData();
71     if (lbConfig == undefined)
72         return;
73
74     $.post(url.concat("/v1.0/loadbalancer/create"), JSON.stringify(lbConfig), function() { 
75     }, "json")
76     .done(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>');
79     })
80     .fail(function() {
81         $('#post-status').html('<p style="color:red; background:silver;">Error: Load-balancer pool creation failed. Please verify your input.');
82     });
83 }
84
85 function deleteLBPool(vip) {
86     if (typeof(vip)==='undefined') {
87         vip = $('#virtual-ip').val();
88     }
89
90     $('#post-status').html('');
91
92     lbConfig = {};
93     lbConfig['virtual_ip'] = vip;
94     lbConfig['rewrite_ip'] = 1;
95     lbConfig.servers = [];
96
97     $.post(url.concat("/v1.0/loadbalancer/delete"), JSON.stringify(lbConfig), function() { 
98     }, "json")
99     .done(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>');
105     })
106     .fail(function() {
107         $('#post-status').html('<p style="color:red; background:silver;">Error: Load-balancer pool deletion failed. Please verify your input.');
108     });
109 }
110