]> git.proxmox.com Git - proxmox-widget-toolkit.git/blame - src/node/NetworkView.js
vlan edit: Move example from default value to tooltip
[proxmox-widget-toolkit.git] / src / node / NetworkView.js
CommitLineData
a58001dd
DM
1Ext.define('proxmox-networks', {
2 extend: 'Ext.data.Model',
3 fields: [
82b8bfb0
TL
4 'active',
5 'address',
6 'address6',
7 'autostart',
8 'bridge_ports',
9 'cidr',
10 'cidr6',
11 'comments',
12 'gateway',
13 'gateway6',
14 'iface',
15 'netmask',
16 'netmask6',
17 'slaves',
18 'type',
a58001dd 19 ],
01031528 20 idProperty: 'iface',
a58001dd
DM
21});
22
23Ext.define('Proxmox.node.NetworkView', {
24 extend: 'Ext.panel.Panel',
25
26 alias: ['widget.proxmoxNodeNetworkView'],
27
7c0e4c25
DC
28 // defines what types of network devices we want to create
29 // order is always the same
8aefd47c 30 types: ['bridge', 'bond', 'vlan', 'ovs'],
7c0e4c25 31
21cd6c09
DC
32 showApplyBtn: false,
33
01031528 34 initComponent: function() {
05a977a2 35 let me = this;
a58001dd
DM
36
37 if (!me.nodename) {
38 throw "no node name specified";
39 }
40
05a977a2 41 let baseUrl = '/nodes/' + me.nodename + '/network';
a58001dd 42
05a977a2 43 let store = Ext.create('Ext.data.Store', {
a58001dd
DM
44 model: 'proxmox-networks',
45 proxy: {
46 type: 'proxmox',
01031528 47 url: '/api2/json' + baseUrl,
a58001dd
DM
48 },
49 sorters: [
50 {
01031528
TL
51 property: 'iface',
52 direction: 'ASC',
53 },
54 ],
a58001dd
DM
55 });
56
05a977a2
TL
57 let reload = function() {
58 let changeitem = me.down('#changes');
59 let apply_btn = me.down('#apply');
60 let revert_btn = me.down('#revert');
a58001dd
DM
61 Proxmox.Utils.API2Request({
62 url: baseUrl,
63 failure: function(response, opts) {
a58001dd 64 store.loadData({});
b81cf173
DM
65 Proxmox.Utils.setErrorMask(me, response.htmlStatus);
66 changeitem.update('');
89f57452 67 changeitem.setHidden(true);
a58001dd
DM
68 },
69 success: function(response, opts) {
05a977a2 70 let result = Ext.decode(response.responseText);
a58001dd 71 store.loadData(result.data);
05a977a2 72 let changes = result.changes;
a58001dd
DM
73 if (changes === undefined || changes === '') {
74 changes = gettext("No changes");
89f57452 75 changeitem.setHidden(true);
21cd6c09 76 apply_btn.setDisabled(true);
03e93db5 77 revert_btn.setDisabled(true);
89f57452
DM
78 } else {
79 changeitem.update("<pre>" + Ext.htmlEncode(changes) + "</pre>");
80 changeitem.setHidden(false);
21cd6c09 81 apply_btn.setDisabled(false);
03e93db5 82 revert_btn.setDisabled(false);
a58001dd 83 }
01031528 84 },
a58001dd
DM
85 });
86 };
87
05a977a2
TL
88 let run_editor = function() {
89 let grid = me.down('gridpanel');
90 let sm = grid.getSelectionModel();
91 let rec = sm.getSelection()[0];
a58001dd
DM
92 if (!rec) {
93 return;
94 }
95
05a977a2 96 let win = Ext.create('Proxmox.node.NetworkEdit', {
a58001dd
DM
97 nodename: me.nodename,
98 iface: rec.data.iface,
01031528 99 iftype: rec.data.type,
a58001dd
DM
100 });
101 win.show();
102 win.on('destroy', reload);
103 };
104
05a977a2 105 let edit_btn = new Ext.Button({
a58001dd
DM
106 text: gettext('Edit'),
107 disabled: true,
01031528 108 handler: run_editor,
a58001dd
DM
109 });
110
05a977a2 111 let del_btn = new Ext.Button({
a58001dd
DM
112 text: gettext('Remove'),
113 disabled: true,
01031528 114 handler: function() {
05a977a2
TL
115 let grid = me.down('gridpanel');
116 let sm = grid.getSelectionModel();
117 let rec = sm.getSelection()[0];
a58001dd
DM
118 if (!rec) {
119 return;
120 }
121
05a977a2 122 let iface = rec.data.iface;
a58001dd
DM
123
124 Proxmox.Utils.API2Request({
125 url: baseUrl + '/' + iface,
126 method: 'DELETE',
127 waitMsgTarget: me,
128 callback: function() {
129 reload();
130 },
131 failure: function(response, opts) {
132 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
01031528 133 },
a58001dd 134 });
01031528 135 },
a58001dd
DM
136 });
137
05a977a2 138 let apply_btn = Ext.create('Proxmox.button.Button', {
21cd6c09
DC
139 text: gettext('Apply Configuration'),
140 itemId: 'apply',
141 disabled: true,
2877ddea 142 confirmMsg: 'Do you want to apply pending network changes?',
21cd6c09
DC
143 hidden: !me.showApplyBtn,
144 handler: function() {
145 Proxmox.Utils.API2Request({
146 url: baseUrl,
147 method: 'PUT',
148 waitMsgTarget: me,
149 success: function(response, opts) {
05a977a2 150 let upid = response.result.data;
21cd6c09 151
05a977a2 152 let win = Ext.create('Proxmox.window.TaskProgress', {
21cd6c09 153 taskDone: reload,
01031528 154 upid: upid,
21cd6c09
DC
155 });
156 win.show();
157 },
158 failure: function(response, opts) {
159 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
01031528 160 },
21cd6c09 161 });
01031528 162 },
21cd6c09
DC
163 });
164
05a977a2
TL
165 let set_button_status = function() {
166 let grid = me.down('gridpanel');
167 let sm = grid.getSelectionModel();
168 let rec = sm.getSelection()[0];
a58001dd
DM
169
170 edit_btn.setDisabled(!rec);
171 del_btn.setDisabled(!rec);
172 };
173
05a977a2 174 let render_ports = function(value, metaData, record) {
a58001dd
DM
175 if (value === 'bridge') {
176 return record.data.bridge_ports;
177 } else if (value === 'bond') {
178 return record.data.slaves;
179 } else if (value === 'OVSBridge') {
180 return record.data.ovs_ports;
181 } else if (value === 'OVSBond') {
182 return record.data.ovs_bonds;
183 }
05a977a2 184 return '';
a58001dd
DM
185 };
186
05a977a2
TL
187 let find_next_iface_id = function(prefix) {
188 let next;
a58001dd
DM
189 for (next = 0; next <= 9999; next++) {
190 if (!store.getById(prefix + next.toString())) {
191 break;
192 }
193 }
194 return prefix + next.toString();
195 };
196
05a977a2 197 let menu_items = [];
7c0e4c25
DC
198
199 if (me.types.indexOf('bridge') !== -1) {
200 menu_items.push({
201 text: Proxmox.Utils.render_network_iface_type('bridge'),
202 handler: function() {
05a977a2 203 let win = Ext.create('Proxmox.node.NetworkEdit', {
7c0e4c25
DC
204 nodename: me.nodename,
205 iftype: 'bridge',
25f37cbf
TL
206 iface_default: find_next_iface_id('vmbr'),
207 onlineHelp: 'sysadmin_network_configuration',
7c0e4c25
DC
208 });
209 win.on('destroy', reload);
210 win.show();
01031528 211 },
7c0e4c25
DC
212 });
213 }
214
215 if (me.types.indexOf('bond') !== -1) {
216 menu_items.push({
217 text: Proxmox.Utils.render_network_iface_type('bond'),
218 handler: function() {
05a977a2 219 let win = Ext.create('Proxmox.node.NetworkEdit', {
7c0e4c25
DC
220 nodename: me.nodename,
221 iftype: 'bond',
25f37cbf
TL
222 iface_default: find_next_iface_id('bond'),
223 onlineHelp: 'sysadmin_network_configuration',
7c0e4c25
DC
224 });
225 win.on('destroy', reload);
226 win.show();
01031528 227 },
7c0e4c25
DC
228 });
229 }
230
8aefd47c
AD
231 if (me.types.indexOf('vlan') !== -1) {
232 menu_items.push({
233 text: Proxmox.Utils.render_network_iface_type('vlan'),
234 handler: function() {
05a977a2 235 let win = Ext.create('Proxmox.node.NetworkEdit', {
8aefd47c
AD
236 nodename: me.nodename,
237 iftype: 'vlan',
25f37cbf 238 onlineHelp: 'sysadmin_network_configuration',
8aefd47c
AD
239 });
240 win.on('destroy', reload);
241 win.show();
01031528 242 },
8aefd47c
AD
243 });
244 }
245
7c0e4c25
DC
246 if (me.types.indexOf('ovs') !== -1) {
247 if (menu_items.length > 0) {
248 menu_items.push({ xtype: 'menuseparator' });
249 }
250
251 menu_items.push(
252 {
253 text: Proxmox.Utils.render_network_iface_type('OVSBridge'),
254 handler: function() {
05a977a2 255 let win = Ext.create('Proxmox.node.NetworkEdit', {
7c0e4c25
DC
256 nodename: me.nodename,
257 iftype: 'OVSBridge',
01031528 258 iface_default: find_next_iface_id('vmbr'),
7c0e4c25
DC
259 });
260 win.on('destroy', reload);
261 win.show();
01031528 262 },
7c0e4c25
DC
263 },
264 {
265 text: Proxmox.Utils.render_network_iface_type('OVSBond'),
266 handler: function() {
05a977a2 267 let win = Ext.create('Proxmox.node.NetworkEdit', {
7c0e4c25
DC
268 nodename: me.nodename,
269 iftype: 'OVSBond',
01031528 270 iface_default: find_next_iface_id('bond'),
7c0e4c25
DC
271 });
272 win.on('destroy', reload);
273 win.show();
01031528 274 },
7c0e4c25
DC
275 },
276 {
277 text: Proxmox.Utils.render_network_iface_type('OVSIntPort'),
278 handler: function() {
05a977a2 279 let win = Ext.create('Proxmox.node.NetworkEdit', {
7c0e4c25 280 nodename: me.nodename,
01031528 281 iftype: 'OVSIntPort',
7c0e4c25
DC
282 });
283 win.on('destroy', reload);
284 win.show();
01031528
TL
285 },
286 },
7c0e4c25
DC
287 );
288 }
289
05a977a2 290 let renderer_generator = function(fieldname) {
d0c2b878 291 return function(val, metaData, rec) {
05a977a2 292 let tmp = [];
d0c2b878
DC
293 if (rec.data[fieldname]) {
294 tmp.push(rec.data[fieldname]);
295 }
296 if (rec.data[fieldname + '6']) {
297 tmp.push(rec.data[fieldname + '6']);
298 }
299 return tmp.join('<br>') || '';
300 };
301 };
302
a58001dd
DM
303 Ext.apply(me, {
304 layout: 'border',
305 tbar: [
306 {
307 text: gettext('Create'),
7c0e4c25 308 menu: {
a58001dd 309 plain: true,
01031528
TL
310 items: menu_items,
311 },
1cf31d6b 312 }, '-',
a58001dd
DM
313 {
314 text: gettext('Revert'),
03e93db5 315 itemId: 'revert',
a58001dd
DM
316 handler: function() {
317 Proxmox.Utils.API2Request({
318 url: baseUrl,
319 method: 'DELETE',
320 waitMsgTarget: me,
321 callback: function() {
322 reload();
323 },
324 failure: function(response, opts) {
325 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
01031528 326 },
a58001dd 327 });
01031528 328 },
a58001dd
DM
329 },
330 edit_btn,
21cd6c09 331 del_btn,
1cf31d6b 332 '-',
01031528 333 apply_btn,
a58001dd
DM
334 ],
335 items: [
336 {
337 xtype: 'gridpanel',
338 stateful: true,
339 stateId: 'grid-node-network',
340 store: store,
341 region: 'center',
342 border: false,
343 columns: [
344 {
345 header: gettext('Name'),
a58001dd 346 sortable: true,
01031528 347 dataIndex: 'iface',
a58001dd
DM
348 },
349 {
350 header: gettext('Type'),
a58001dd 351 sortable: true,
869439b1 352 width: 120,
a58001dd 353 renderer: Proxmox.Utils.render_network_iface_type,
01031528 354 dataIndex: 'type',
a58001dd
DM
355 },
356 {
357 xtype: 'booleancolumn',
358 header: gettext('Active'),
359 width: 80,
360 sortable: true,
361 dataIndex: 'active',
9b575917
WL
362 trueText: Proxmox.Utils.yesText,
363 falseText: Proxmox.Utils.noText,
364 undefinedText: Proxmox.Utils.noText,
a58001dd
DM
365 },
366 {
367 xtype: 'booleancolumn',
368 header: gettext('Autostart'),
369 width: 80,
370 sortable: true,
371 dataIndex: 'autostart',
9b575917
WL
372 trueText: Proxmox.Utils.yesText,
373 falseText: Proxmox.Utils.noText,
01031528 374 undefinedText: Proxmox.Utils.noText,
a58001dd 375 },
8947a4fc
WL
376 {
377 xtype: 'booleancolumn',
378 header: gettext('VLAN aware'),
379 width: 80,
380 sortable: true,
381 dataIndex: 'bridge_vlan_aware',
382 trueText: Proxmox.Utils.yesText,
383 falseText: Proxmox.Utils.noText,
01031528 384 undefinedText: Proxmox.Utils.noText,
8947a4fc 385 },
a58001dd
DM
386 {
387 header: gettext('Ports/Slaves'),
388 dataIndex: 'type',
01031528 389 renderer: render_ports,
a58001dd 390 },
211bdf93
DC
391 {
392 header: gettext('Bond Mode'),
393 dataIndex: 'bond_mode',
394 renderer: Proxmox.Utils.render_bond_mode,
395 },
396 {
397 header: gettext('Hash Policy'),
398 hidden: true,
399 dataIndex: 'bond_xmit_hash_policy',
400 },
a58001dd
DM
401 {
402 header: gettext('IP address'),
403 sortable: true,
869439b1 404 width: 120,
4211996a 405 hidden: true,
a58001dd 406 dataIndex: 'address',
d0c2b878 407 renderer: renderer_generator('address'),
a58001dd
DM
408 },
409 {
410 header: gettext('Subnet mask'),
869439b1 411 width: 120,
a58001dd 412 sortable: true,
4211996a 413 hidden: true,
d0c2b878
DC
414 dataIndex: 'netmask',
415 renderer: renderer_generator('netmask'),
416 },
417 {
418 header: gettext('CIDR'),
f4d366dc 419 width: 150,
d0c2b878
DC
420 sortable: true,
421 dataIndex: 'cidr',
422 renderer: renderer_generator('cidr'),
a58001dd
DM
423 },
424 {
425 header: gettext('Gateway'),
f4d366dc 426 width: 150,
a58001dd
DM
427 sortable: true,
428 dataIndex: 'gateway',
d0c2b878 429 renderer: renderer_generator('gateway'),
a58001dd
DM
430 },
431 {
432 header: gettext('Comment'),
433 dataIndex: 'comments',
869439b1 434 flex: 1,
01031528
TL
435 renderer: Ext.String.htmlEncode,
436 },
a58001dd
DM
437 ],
438 listeners: {
439 selectionchange: set_button_status,
01031528
TL
440 itemdblclick: run_editor,
441 },
a58001dd
DM
442 },
443 {
444 border: false,
445 region: 'south',
446 autoScroll: true,
89f57452 447 hidden: true,
a58001dd
DM
448 itemId: 'changes',
449 tbar: [
450 gettext('Pending changes') + ' (' +
01031528 451 gettext("Either reboot or use 'Apply Configuration' (needs ifupdown2) to activate") + ')',
a58001dd
DM
452 ],
453 split: true,
454 bodyPadding: 5,
455 flex: 0.6,
01031528
TL
456 html: gettext("No changes"),
457 },
a58001dd 458 ],
a58001dd
DM
459 });
460
461 me.callParent();
1a68e95d 462 reload();
01031528 463 },
a58001dd 464});