Project

General

Profile

Download (18.7 KB) Statistics
| Branch: | Revision:
1
define([
2
"dojo/_base/array",
3
"dojo/_base/connect",
4
"stabile/grid",
5
"steam2/user",
6
"steam2/models/Server",
7
"dojox/grid/cells/_base"
8
],function(arrayUtil, connect, grid, user, Server){
9

    
10
    var networks = {
11

    
12
        grid: {},
13

    
14
        _searchQuery: "name:*",
15
        _statusQuery: "status:all",
16
        _inited: false,
17
        storeQuery: "name:*",
18
        model : function(args){
19
            return dojo.mixin({
20
                uuid: Math.uuid().toLowerCase(),
21
                name: "",
22
                id: "",
23
                internalip: "",
24
                externalip: "",
25
                ports:"",
26
                domainnames: "",
27
                type: "internalip",
28
                status: "new"
29
            }, args || {});
30
        },
31

    
32
        /** object name - for reflective lookup */
33
        name : "networks",
34

    
35
        sortInfo: ((user.is_admin)?-4:-3),
36

    
37
        structure : [
38
            {
39
                field : 'id',
40
                name : "ID",
41
                width : "36px",
42
                hidden: true
43
            },
44
            {
45
                field : 'type',
46
                name : 'Type',
47
                width : "70px"
48
            },
49
            {
50
                field : 'name',
51
                name : 'Name',
52
                width : "auto"
53
            },
54
            {
55
                field : 'status',
56
                name : 'Status' //,
57
                //formatter : function(val, rowIdx, cell){
58
                //   var t = '<div class="${cssclass}">${status}</div>';
59
                //   var cssclass = "";
60
                //   if(status == "down"){
61
                //     status = "";
62
                //     cssclass = "status_indicator down_icon";
63
                //   }
64
                //   if(status == "up"){
65
                //     status = "";
66
                //     cssclass = "status_indicator up_icon";
67
                //   }
68
                //   if(status == "nat"){
69
                //     status = "";
70
                //     cssclass = "status_indicator break_icon";
71
                //   }
72
                //   return dojo.string.substitute(t, { cssclass : cssclass, status :status });
73

    
74
                ///    var item = this.grid.getItem(rowIdx);
75
                ///    if (item.type == "gateway" && val == "nat") return "up";
76
                ///    else return val;
77
                //}
78
            },
79
            {
80
                field : 'domainnames',
81
                name : 'Used by',
82
                width : "auto",
83
                formatter : function(val, rowIdx, cell) {
84
                    var item = this.grid.getItem(rowIdx);
85
                    if (val && val!='--') {
86
                        return val;
87
                    } else if (item.systemnames && item.systemnames!='--') {
88
                        return '<i>' + item.systemnames + '</i>';
89
                    } else {
90
                        return '--';
91
                    }
92
                }
93
            },
94
            {
95
                field : 'internalip',
96
                name : 'Internal IP',
97
                width : "100px"
98
            },
99
            {
100
                field : 'externalip',
101
                name : 'External IP',
102
                width : "100px"
103
            },
104
            { field : 'action', name : 'Action', width : 'auto',
105
                formatter : function(val, rowIdx, cell) {
106
                    var item = this.grid.getItem(rowIdx);
107
                    return networks.getActionButtons(item);
108
                },
109
                hidden: user.is_readonly
110
            }
111
        ],
112

    
113
        dialogStructure : [
114
            { field : "name", name : "<span style='width:115px; display:inline-block;'>Name</span>",    type : "dijit.form.TextBox"},
115
            { field : "type", name : "Type", type: "dijit.form.Select",
116
                attrs:{ store: "stores.networkTypes", searchAttr:"type", onChange: "networks.updateIPFields(this.value);"}},
117
            { field : 'id',          name : "ID", type : "dijit.form.TextBox", width : "50px", restricted: true,
118
                attrs:{ onChange: "networks.updateIPPrefix(this.value);"}},
119
            {
120
                field: "uuid",
121
                name: "UUID",
122
                type: "dijit.form.TextBox",
123
                attrs: {readonly:"readonly"}
124
            },
125
            { field : "status",      name : "Status",  type : "dijit.form.TextBox", attrs : {readonly :"readonly"}},
126
            { field : 'internalip',  type : "dijit.form.ValidationTextBox",
127
                name : 'Internal IP <span id="internalipprefix" style="color:gray;"></span>',
128
                width : "150px",
129
                attrs : {
130
                    regExp : "--|25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?",
131
                    invalidMessage : "Invalid ip."
132
                }
133
            },
134
            {
135
                field : 'externalip',
136
                type : "dijit.form.TextBox",
137
                name : 'External IP',
138
                width : "50px",
139
                attrs : {readonly :"readonly"}
140
            },
141
            {
142
                field : 'ports',
143
                type : "dijit.form.TextBox",
144
                name : 'Allowed ports',
145
                help: "networks/ports"
146
            },
147
            {
148
                formatter: function(network){
149
                    // summary: links to server dialog for each server.
150
                    if(network.status == 'new'){
151
                        return '';
152
                    }
153
                    if(network.domains != '--'){
154
                        var domainnames = network.domainnames.split(/,\s|,/);
155
                        var uuids = network.domains.split(/,\s|,/);
156
                        var domains = [];
157
                        // create a new array that we can sort by name
158
                        arrayUtil.forEach(uuids, function(d, i){
159
                            domains.push({uuid: d, name: domainnames[i]});
160
                        });
161
                        // sort the servers
162
                        domains = domains.sort(function(a,b){
163
                            if(a.name == b.name){ return 0;}
164
                            if(a.name > b.name){ return 1;}
165
                            return -1;
166
                        });
167
                        var html = [];
168
                        arrayUtil.forEach(domains, function(d, i){
169
                            var serverEditLink = '<a onclick="servers.grid.dialog.show(stores.servers.fetchItemByIdentity({identity: \'' + d.uuid  + '\'}));">' + d.name + '</a>';
170
                            html.push('<li>' + serverEditLink + '</li>');
171
                        });
172
                        return '<td valign="top" style="width:60px">Servers</td><td><ul style="list-style:none;padding:0;margin:0;">' + html.join('') + '</ul></td>';
173
                    } else if (network.systems != '--') {
174
                    //    var systemEditLink = '<a onclick="systems.grid.dialog.show(stores.systems.fetchItemByIdentity({identity: \'' + d.uuid  + '\'}));">' + d.name + '</a>';
175
                        var syshtml = network.systemnames;
176
                        return '<td valign="top" style="width:60px">Stacks</td><td><ul style="list-style:none;padding:0;margin:0;">' + syshtml + '</ul></td>';
177
                    }
178
                    return '';
179
                }  
180
            }
181
        ],
182

    
183
        store : null,
184

    
185
        getActionButtons : function(item, include_save){
186
            if (user.is_readonly) return "";
187

    
188
            var name = item.name;
189
            var type = this.name;
190
            function actionButton(args){
191
                args.name = name;
192
                args.type = type;
193
                return grid.actionButton(args);
194
            }
195

    
196
            // var store = stores.networks;
197
            // var id = store.getValue(item, 'uuid');
198
            // var status = store.getValue(item, 'status');
199

    
200
            var id = item.uuid;
201
            var status = item.status;
202

    
203
            var up = actionButton({'action' :"activate", 'id' :id});
204
            var down = actionButton({'action' :"stop", 'id' :id, 'confirm':true});
205
            var _delete = actionButton({'action' :"delete", 'id' :id, 'confirm' :true});
206
            var _break = actionButton({'action' :"deactivate", 'id' :id});
207
            var save = include_save ? grid.saveButton(type) : "";
208

    
209
            if (id == 0 || id == 1) {return "";}
210
            else if(status == "down"){
211
                return ((item.domains=="--" && item.systems=="--")?_delete + (item.type=="gateway"?up:""):up) + save;
212
            }
213
            else if(status == "up"){
214
                return (item.type=="gateway"?down:(item.type=="internalip" && item.systems!="--"?_delete:'')) + save + _break;
215
            }
216
            else if(status == "new"){
217
                return save;
218
            }
219
            else if(status == "--"){
220
                return up + ((item.domains=="--" && item.systems=="--")?_delete:"");
221
            }
222
            else if(status == "nat"){
223
                if ((item.internalip!="--" || item.externalip!="--") && item.domains!="--") return up  + (item.type=="gateway"?down:"") + save;
224
                else if ((item.internalip!="--" || item.externalip!="--") && item.systems!="--") return up + save + _delete;
225
                else return (item.type=="gateway"?down:"") + save + ((item.domains=="--" && item.systems=="--")?_delete:"");
226
            }
227
            console.error("WTF? unknown status : ", status);
228
        },
229

    
230
        onBeforeDialog : function(item){
231
            stores.networkTypes.close();
232
        },
233

    
234
        updateIPFields : function(type) {
235

    
236
            function showRow(inputId){
237
                dojo.query('#' + inputId).closest('tr').style('display', '');
238
            }
239
            function hideRow(inputId){
240
                dojo.query('#' + inputId).closest('tr').style('display', 'none');
241
            }
242

    
243
            var internalip = dijit.byId("internalip");
244
            var externalip = dijit.byId("externalip");
245
            var namefield = dijit.byId("name");
246
            var typefield = dijit.byId("type");
247
            var ports = dijit.byId("ports");
248
            var status = dijit.byId("status").value;
249
            var uuid = dijit.byId('uuid').value;
250

    
251
            if (user.is_readonly) {
252
                namefield.disabled = true;
253
            }
254
            if (status== "new" || uuid == "0" || uuid == "1") {
255
                hideRow('uuid');
256
            }
257
            if (type == "gateway") {
258
                hideRow('externalip');
259
                hideRow('internalip');
260
                hideRow('ports');
261

    
262
                internalip.set('value', '--');
263
                externalip.set('value', '--');
264
            } else if (type == "internalip") {
265
                showRow('internalip');
266
                hideRow('externalip');
267
                hideRow('ports');
268
                externalip.set('value', '--');
269
                var id = dijit.byId('id').value;
270
                networks.updateIPPrefix(id);
271
            } else if (type == "ipmapping") {
272
                showRow('internalip');
273
                showRow('externalip');
274
                showRow('ports');
275
                var id = dijit.byId('id').value;
276
                networks.updateIPPrefix(id);
277
            } else if (type == "remoteip") {
278
                showRow('internalip');
279
                showRow('externalip');
280
                showRow('ports');
281
                var id = dijit.byId('id').value;
282
                networks.updateIPPrefix(id);
283
            } else if (type == "externalip") {
284
                hideRow('internalip');
285
                showRow('externalip');
286
                showRow('ports');
287
                var id = dijit.byId('id').value;
288
                networks.updateIPPrefix(id);
289
            }
290
            if (status == "up" || user.is_readonly) {
291
                ports.disabled = true;
292
                internalip.disabled = true;
293
            }
294
        },
295

    
296
        updateIPPrefix : function(id) {
297
            if(!id || ((id==="1" || id==="0") && !user.is_admin)){return;}
298
            if (dijit.byId('type')) { // If dialog has been closed, just return
299
                var type = dijit.byId('type').value;
300
                if (type == "ipmapping" || type == "internalip") {
301
                    var first_three_ip_octets = networks.getFirstThreeOctetsbyVlan(id);
302
                    dojo.byId('internalipprefix').innerHTML = first_three_ip_octets + '.';
303
                }
304
            }
305
        },
306

    
307
        getFirstThreeOctetsbyVlan : function(vlanid){
308
            vlanid += '';
309
            if(vlanid.length > 2){
310
                vlanid = Number(vlanid.substr(0, vlanid.length-2)) + '.' + Number(vlanid.substr(vlanid.length-2));
311
            }
312
            else{
313
                vlanid = "0." + vlanid;
314
            }
315
            return "10." + vlanid;
316
        },
317

    
318
        getOctetFour : function(ip){
319
            if(ip == "--"){return "--";}
320
            return ip.split('.')[3];
321
        },
322

    
323
        onDialogButtons : function(item){
324
            //var status = this.store.getValue(item, 'status');
325
            var internalip_field = dijit.byId('internalip');
326
            var status = item.status;
327
            var disable = function(dijitId){
328
                var elm = dijit.byId(dijitId);
329
                elm && elm.set('disabled', true);
330
                return elm;
331
            };
332
            var enable = function(dijitId){
333
                var elm = dijit.byId(dijitId);
334
                elm && elm.set('disabled', false);
335
                return elm;
336
            };
337

    
338
            if(status == 'new'){
339
                //dijit.byId('internalip').set('disabled', true);
340
            } else {
341
                var id = this.store.getValue(item, 'id');
342
                networks.updateIPPrefix(id);
343
                var internalip = this.store.getValue(item, 'internalip');
344
                if (internalip_field) internalip_field.set('value', networks.getOctetFour(internalip));
345
                var domains = this.store.getValue(item, 'domains');
346
                if (id<2 || (domains && domains.indexOf(",")!=-1) || item.status == 'up' || user.is_readonly) {
347
                    if (dijit.byId("type")) disable('type');
348
                } else {
349
                     if (dijit.byId("type")) enable('type');
350
                }
351
                if(dijit.byId('id')) disable('id');
352
            }
353
        },
354

    
355
        onBeforeSave : function(item){
356
            var vlanid = stores.networks.getValue(item, "id");
357
            if(vlanid){
358
                var ip_octet_four = stores.networks.getValue(item, "internalip");
359

    
360
                var isIPSet = function(){
361
                    return ip_octet_four === '--' ? false  : true;
362
                };
363
                var internalip;
364
                if(isIPSet()) {
365
                    internalip  = networks.getFirstThreeOctetsbyVlan(vlanid) + '.' + ip_octet_four;
366
                }
367
                else{
368
                    internalip = '--';
369
                }
370
                stores.networks.setValue(item, "internalip", internalip);
371
            }
372
            var ports = dijit.byId("ports");
373
            if (ports && ports.value=="") stores.networks.setValue(item, "ports", "--");
374
        }
375
    };
376

    
377
    networks.init = function(){
378
        if (networks._inited === true) return "OK";
379
        else networks._inited = true;
380

    
381
        connect.connect(dijit.byId('networks_status_filter_select'), 'onChange', this, this.onStatusFilterChange);
382
        connect.connect(dijit.byId('networks_search_query'), 'onChange', this, this.onSearchQueryChange);
383

    
384
        this.store = stores.networks;
385
        this.domnode = "networks-grid";
386
        this.grid = grid.create(this);
387
        if (!user.is_readonly) {
388
            if (dijit.byId("networksNewButton")) dijit.byId("networksNewButton").set("style", "display:inline");
389
        }
390

    
391
        connect.connect(this.grid, '_onFetchComplete', this, function(rows){
392
            this.updateSums(rows);
393
            if (!user.is_readonly) $("#networksNewButton").show();
394
        });
395

    
396
        this.grid.startup();
397

    
398
        dojo.subscribe("networks:update", function(task){
399
            if (task.uuid) networks.grid.refreshRow(task);
400
            else networks.grid.refresh();
401
        });
402
        this.onShowItem();
403
    };
404

    
405
    networks.onShowItem = function() {
406
        if (home.networksOnShowItem != null && networks.grid.dialog) {
407
            networks.grid.dialog.show(home.networksOnShowItem);
408
            home.networksOnShowItem = null;
409
        }
410
    };
411

    
412
    networks.updateFilter = function(){
413
        var query = this._searchQuery + " AND " + this._statusQuery;
414
        this.grid.store.query = query;
415
        this.grid.filter(query, /*rerender*/true);
416
    };
417

    
418
    networks.onSearchQueryChange = function(v){
419
        if(v){
420
            /*this._searchQuery = "name: '*" + v + "*'" +
421
                " OR status: '" + v + "*'" + 
422
                " OR type: '" + v + "*'" + 
423
                " OR internalip: '" + v + "*'" + 
424
                " OR externalip: '" + v + "*'";*/
425
            this._searchQuery = "name:" +v + "*";
426
        }
427
        else{
428
            //this._searchQuery = "name: *";
429
            this._searchQuery = "name:*";
430
        }
431
        this.updateFilter();
432
    };
433

    
434
    networks.onStatusFilterChange = function(value){
435
        switch(value){
436
          case "all":
437
            //this._statusQuery = "uuid:*";
438
            this._statusQuery = "status:all";
439
            break;
440
        default:
441
            //this._statusQuery = "status:*" + value + '*';
442
            this._statusQuery = "status:" + value;
443
        }
444
        this.updateFilter();
445
    };
446

    
447
    networks.updateSums = function(rows) {
448
        var totalIntIPs = 0;
449
        var totalExtIPs = 0;
450
        var rx = 0;
451
        var tx = 0;
452
        var item;
453
        if (!rows) {
454
           for(var i = 0; i < networks.grid.rowCount; i++){
455
               sumit(networks.grid.getItem(i));
456
           }
457
        } else {
458
            for(var i in rows){
459
                sumit(rows[i]);
460
            }
461
        }
462

    
463
        function sumit(item) {
464
            if (item && item.type) {
465
                if (item.type=="internalip") {
466
                    totalIntIPs += 1;
467
                } else if (item.type=="externalip") {
468
                    totalExtIPs += 1;
469
                } else if (item.type=="ipmapping") {
470
                    totalIntIPs += 1;
471
                    totalExtIPs += 1;
472
                }
473
                if (item.rx) rx = Math.round(item.rx / 1024);
474
                if (item.tx) tx = Math.round(item.tx / 1024);
475
            }
476
        }
477

    
478
        var rxq = (user.rxquota===0)?'&infin;':Math.round(user.rxquota/1024/1024);
479
        var txq = (user.txquota===0)?'&infin;':Math.round(user.txquota/1024/1024);
480
        var exq = (user.externalipquota===0)?'&infin;' : user.externalipquota;
481
        document.getElementById("ips_sum").innerHTML =
482
                '<span title="Quotas: ' + exq + ' external IP\'s, ' + rxq + "/" + txq  + ' GB RX/TX">' +
483
                "Total external IP's: " + totalExtIPs +
484
                "&nbsp;&nbsp;Total internal IP's: " + totalIntIPs +
485
                "&nbsp;&nbsp;Total RX/TX: " + Math.round(rx/1024) + "/" + Math.round(tx/1024) + " MB" +
486
                "</span>";
487
    };
488

    
489
    window.networks = networks;
490
    return networks;
491
});
492

    
(13-13/23)