]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/lxc/Network.js
ui: lxc/CreateWizard: add a 'nesting' checkbox and enable it by default
[pve-manager.git] / www / manager6 / lxc / Network.js
CommitLineData
5a691a50 1Ext.define('PVE.lxc.NetworkInputPanel', {
ef4ef788 2 extend: 'Proxmox.panel.InputPanel',
5a691a50
DM
3 alias: 'widget.pveLxcNetworkInputPanel',
4
5 insideWizard: false,
6
c8802a60 7 onlineHelp: 'pct_container_network',
7f1ac74c 8
5a691a50 9 setNodename: function(nodename) {
c511ca9c 10 let me = this;
99541dcc 11
53e3ea84 12 if (!nodename || me.nodename === nodename) {
5a691a50
DM
13 return;
14 }
5a691a50
DM
15 me.nodename = nodename;
16
c511ca9c
TL
17 let bridgeSelector = me.query("[isFormField][name=bridge]")[0];
18 bridgeSelector.setNodename(nodename);
5a691a50 19 },
99541dcc 20
5a691a50 21 onGetValues: function(values) {
c511ca9c 22 let me = this;
5a691a50 23
c511ca9c 24 let id;
d5e771ce 25 if (me.isCreate) {
5a691a50
DM
26 id = values.id;
27 delete values.id;
28 } else {
29 id = me.ifname;
30 }
c511ca9c
TL
31 let newdata = {};
32 if (id) {
33 if (values.ipv6mode !== 'static') {
34 values.ip6 = values.ipv6mode;
35 }
36 if (values.ipv4mode !== 'static') {
37 values.ip = values.ipv4mode;
38 }
39 newdata[id] = PVE.Parser.printLxcNetwork(values);
84de645d 40 }
5a691a50
DM
41 return newdata;
42 },
43
8058410f 44 initComponent: function() {
c511ca9c 45 let me = this;
5a691a50 46
c511ca9c 47 let cdata = {};
5a691a50
DM
48 if (me.insideWizard) {
49 me.ifname = 'net0';
50 cdata.name = 'eth0';
d8e3d77c 51 me.dataCache = {};
5a691a50 52 }
53e3ea84 53 cdata.firewall = me.insideWizard || me.isCreate;
d8e3d77c
TL
54
55 if (!me.dataCache) {
56 throw "no dataCache specified";
57 }
58
d5e771ce 59 if (!me.isCreate) {
5a691a50
DM
60 if (!me.ifname) {
61 throw "no interface name specified";
62 }
63 if (!me.dataCache[me.ifname]) {
64 throw "no such interface '" + me.ifname + "'";
65 }
5a691a50
DM
66 cdata = PVE.Parser.parseLxcNetwork(me.dataCache[me.ifname]);
67 }
68
c511ca9c
TL
69 for (let i = 0; i < 32; i++) {
70 let ifname = 'net' + i.toString();
71 if (me.isCreate && !me.dataCache[ifname]) {
72 me.ifname = ifname;
2517c76b
DC
73 break;
74 }
5a691a50 75 }
5a691a50 76
7c7ae44f 77 me.column1 = [
c511ca9c
TL
78 {
79 xtype: 'hidden',
80 name: 'id',
81 value: me.ifname,
82 },
5a691a50
DM
83 {
84 xtype: 'textfield',
85 name: 'name',
160673c1
TL
86 fieldLabel: gettext('Name'),
87 emptyText: '(e.g., eth0)',
5a691a50
DM
88 allowBlank: false,
89 value: cdata.name,
90 validator: function(value) {
c511ca9c 91 for (const [key, netRaw] of Object.entries(me.dataCache)) {
5a691a50 92 if (!key.match(/^net\d+/) || key === me.ifname) {
c511ca9c 93 continue;
5a691a50 94 }
c511ca9c 95 let net = PVE.Parser.parseLxcNetwork(netRaw);
5a691a50 96 if (net.name === value) {
c511ca9c 97 return "interface name already in use";
5a691a50 98 }
ec0bd652 99 }
ec0bd652 100 return true;
f6710aac 101 },
5a691a50
DM
102 },
103 {
104 xtype: 'textfield',
105 name: 'hwaddr',
106 fieldLabel: gettext('MAC address'),
107 vtype: 'MacAddress',
108 value: cdata.hwaddr,
41bfbd0a 109 allowBlank: true,
f6710aac 110 emptyText: 'auto',
5a691a50
DM
111 },
112 {
113 xtype: 'PVE.form.BridgeSelector',
114 name: 'bridge',
115 nodename: me.nodename,
116 fieldLabel: gettext('Bridge'),
117 value: cdata.bridge,
f6710aac 118 allowBlank: false,
5a691a50
DM
119 },
120 {
121 xtype: 'pveVlanField',
122 name: 'tag',
f6710aac 123 value: cdata.tag,
5a691a50
DM
124 },
125 {
519ca7fa
DM
126 xtype: 'numberfield',
127 name: 'rate',
128 fieldLabel: gettext('Rate limit') + ' (MB/s)',
129 minValue: 0,
130 maxValue: 10*1024,
131 value: cdata.rate,
132 emptyText: 'unlimited',
f6710aac 133 allowBlank: true,
519ca7fa
DM
134 },
135 {
896c0d50 136 xtype: 'proxmoxcheckbox',
5a691a50
DM
137 fieldLabel: gettext('Firewall'),
138 name: 'firewall',
f6710aac
TL
139 value: cdata.firewall,
140 },
5a691a50
DM
141 ];
142
c511ca9c 143 let dhcp4 = cdata.ip === 'dhcp';
5a691a50
DM
144 if (dhcp4) {
145 cdata.ip = '';
146 cdata.gw = '';
147 }
148
c511ca9c
TL
149 let auto6 = cdata.ip6 === 'auto';
150 let dhcp6 = cdata.ip6 === 'dhcp';
5a691a50
DM
151 if (auto6 || dhcp6) {
152 cdata.ip6 = '';
153 cdata.gw6 = '';
154 }
2a4971d8 155
5a691a50
DM
156 me.column2 = [
157 {
158 layout: {
159 type: 'hbox',
f6710aac 160 align: 'middle',
5a691a50
DM
161 },
162 border: false,
163 margin: '0 0 5 0',
5a691a50
DM
164 items: [
165 {
166 xtype: 'label',
f6710aac 167 text: 'IPv4:', // do not localize
5a691a50
DM
168 },
169 {
170 xtype: 'radiofield',
171 boxLabel: gettext('Static'),
172 name: 'ipv4mode',
173 inputValue: 'static',
174 checked: !dhcp4,
175 margin: '0 0 0 10',
176 listeners: {
177 change: function(cb, value) {
a2163454 178 me.down('field[name=ip]').setEmptyText(
d2021707 179 value ? Proxmox.Utils.NoneText : "",
a2163454 180 );
5a691a50
DM
181 me.down('field[name=ip]').setDisabled(!value);
182 me.down('field[name=gw]').setDisabled(!value);
f6710aac
TL
183 },
184 },
5a691a50
DM
185 },
186 {
187 xtype: 'radiofield',
2ce6111f 188 boxLabel: 'DHCP', // do not localize
5a691a50
DM
189 name: 'ipv4mode',
190 inputValue: 'dhcp',
191 checked: dhcp4,
f6710aac
TL
192 margin: '0 0 0 10',
193 },
194 ],
5a691a50
DM
195 },
196 {
197 xtype: 'textfield',
198 name: 'ip',
199 vtype: 'IPCIDRAddress',
200 value: cdata.ip,
b552db18 201 emptyText: dhcp4 ? '' : Proxmox.Utils.NoneText,
5a691a50 202 disabled: dhcp4,
f6710aac 203 fieldLabel: 'IPv4/CIDR', // do not localize
5a691a50
DM
204 },
205 {
206 xtype: 'textfield',
207 name: 'gw',
208 value: cdata.gw,
209 vtype: 'IPAddress',
210 disabled: dhcp4,
2ce6111f 211 fieldLabel: gettext('Gateway') + ' (IPv4)',
f6710aac 212 margin: '0 0 3 0', // override bottom margin to account for the menuseparator
5a691a50
DM
213 },
214 {
215 xtype: 'menuseparator',
216 height: '3',
f6710aac 217 margin: '0',
5a691a50
DM
218 },
219 {
220 layout: {
221 type: 'hbox',
f6710aac 222 align: 'middle',
5a691a50
DM
223 },
224 border: false,
225 margin: '0 0 5 0',
5a691a50
DM
226 items: [
227 {
228 xtype: 'label',
f6710aac 229 text: 'IPv6:', // do not localize
5a691a50
DM
230 },
231 {
232 xtype: 'radiofield',
233 boxLabel: gettext('Static'),
234 name: 'ipv6mode',
235 inputValue: 'static',
236 checked: !(auto6 || dhcp6),
237 margin: '0 0 0 10',
238 listeners: {
239 change: function(cb, value) {
a2163454 240 me.down('field[name=ip6]').setEmptyText(
d2021707 241 value ? Proxmox.Utils.NoneText : "",
a2163454 242 );
5a691a50
DM
243 me.down('field[name=ip6]').setDisabled(!value);
244 me.down('field[name=gw6]').setDisabled(!value);
f6710aac
TL
245 },
246 },
5a691a50
DM
247 },
248 {
249 xtype: 'radiofield',
2ce6111f 250 boxLabel: 'DHCP', // do not localize
5a691a50
DM
251 name: 'ipv6mode',
252 inputValue: 'dhcp',
253 checked: dhcp6,
f6710aac 254 margin: '0 0 0 10',
5a691a50
DM
255 },
256 {
257 xtype: 'radiofield',
2ce6111f 258 boxLabel: 'SLAAC', // do not localize
5a691a50
DM
259 name: 'ipv6mode',
260 inputValue: 'auto',
261 checked: auto6,
f6710aac
TL
262 margin: '0 0 0 10',
263 },
264 ],
5a691a50
DM
265 },
266 {
267 xtype: 'textfield',
268 name: 'ip6',
269 value: cdata.ip6,
b552db18 270 emptyText: dhcp6 || auto6 ? '' : Proxmox.Utils.NoneText,
5a691a50 271 vtype: 'IP6CIDRAddress',
53e3ea84 272 disabled: dhcp6 || auto6,
f6710aac 273 fieldLabel: 'IPv6/CIDR', // do not localize
5a691a50
DM
274 },
275 {
276 xtype: 'textfield',
277 name: 'gw6',
278 vtype: 'IP6Address',
279 value: cdata.gw6,
53e3ea84 280 disabled: dhcp6 || auto6,
f6710aac
TL
281 fieldLabel: gettext('Gateway') + ' (IPv6)',
282 },
5a691a50
DM
283 ];
284
285 me.callParent();
f6710aac 286 },
5a691a50 287});
d5e771ce 288
5a691a50 289Ext.define('PVE.lxc.NetworkEdit', {
9fccc702 290 extend: 'Proxmox.window.Edit',
5a691a50
DM
291
292 isAdd: true,
293
8058410f 294 initComponent: function() {
c511ca9c 295 let me = this;
5a691a50
DM
296
297 if (!me.dataCache) {
298 throw "no dataCache specified";
299 }
5a691a50
DM
300 if (!me.nodename) {
301 throw "no node name specified";
302 }
303
5a691a50
DM
304 Ext.apply(me, {
305 subject: gettext('Network Device') + ' (veth)',
306 digest: me.dataCache.digest,
c511ca9c
TL
307 items: [
308 {
309 xtype: 'pveLxcNetworkInputPanel',
310 ifname: me.ifname,
311 nodename: me.nodename,
312 dataCache: me.dataCache,
313 isCreate: me.isCreate,
314 },
315 ],
5a691a50
DM
316 });
317
318 me.callParent();
f6710aac 319 },
5a691a50
DM
320});
321
322Ext.define('PVE.lxc.NetworkView', {
323 extend: 'Ext.grid.GridPanel',
d5e771ce 324 alias: 'widget.pveLxcNetworkView',
5a691a50 325
ba93a9c6
DC
326 onlineHelp: 'pct_container_network',
327
5a691a50
DM
328 dataCache: {}, // used to store result of last load
329
af603c15
DC
330 stateful: true,
331 stateId: 'grid-lxc-network',
332
5a691a50 333 load: function() {
c511ca9c 334 let me = this;
5a691a50 335
e7ade592 336 Proxmox.Utils.setErrorMask(me, true);
5a691a50 337
e7ade592 338 Proxmox.Utils.API2Request({
5a691a50
DM
339 url: me.url,
340 failure: function(response, opts) {
e7ade592 341 Proxmox.Utils.setErrorMask(me, gettext('Error') + ': ' + response.htmlStatus);
5a691a50
DM
342 },
343 success: function(response, opts) {
e7ade592 344 Proxmox.Utils.setErrorMask(me, false);
c511ca9c
TL
345 let result = Ext.decode(response.responseText);
346 me.dataCache = result.data || {};
347 let records = [];
348 for (const [key, value] of Object.entries(me.dataCache)) {
349 if (key.match(/^net\d+/)) {
350 let net = PVE.Parser.parseLxcNetwork(value);
351 net.id = key;
352 records.push(net);
5a691a50 353 }
c511ca9c 354 }
5a691a50 355 me.store.loadData(records);
53e3ea84 356 me.down('button[name=addButton]').setDisabled(records.length >= 32);
f6710aac 357 },
5a691a50
DM
358 });
359 },
360
8058410f 361 initComponent: function() {
c511ca9c 362 let me = this;
5a691a50 363
c511ca9c 364 let nodename = me.pveSelNode.data.node;
5a691a50
DM
365 if (!nodename) {
366 throw "no node name specified";
367 }
368
c511ca9c 369 let vmid = me.pveSelNode.data.vmid;
5a691a50
DM
370 if (!vmid) {
371 throw "no VM ID specified";
372 }
373
c511ca9c 374 let caps = Ext.state.Manager.get('GuiCap');
5a691a50 375
c511ca9c 376 me.url = `/nodes/${nodename}/lxc/${vmid}/config`;
5a691a50 377
c511ca9c 378 let store = new Ext.data.Store({
5a691a50
DM
379 model: 'pve-lxc-network',
380 sorters: [
381 {
8058410f 382 property: 'id',
f6710aac
TL
383 direction: 'ASC',
384 },
385 ],
5a691a50
DM
386 });
387
c511ca9c 388 let sm = Ext.create('Ext.selection.RowModel', {});
5a691a50 389
c511ca9c
TL
390 let run_editor = function() {
391 let rec = sm.getSelection()[0];
392 if (!rec || !caps.vms['VM.Config.Network']) {
393 return false; // disable default-propagation when triggered by grid dblclick
5a691a50 394 }
c511ca9c 395 Ext.create('PVE.lxc.NetworkEdit', {
5a691a50
DM
396 url: me.url,
397 nodename: nodename,
398 dataCache: me.dataCache,
f6710aac 399 ifname: rec.data.id,
c511ca9c
TL
400 listeners: {
401 destroy: () => me.load(),
402 },
403 autoShow: true,
5a691a50 404 });
c511ca9c 405 return undefined; // make eslint happier
5a691a50
DM
406 };
407
f7993618 408 Ext.apply(me, {
5a691a50
DM
409 store: store,
410 selModel: sm,
5a691a50
DM
411 tbar: [
412 {
413 text: gettext('Add'),
2517c76b 414 name: 'addButton',
5a691a50
DM
415 disabled: !caps.vms['VM.Config.Network'],
416 handler: function() {
c511ca9c 417 Ext.create('PVE.lxc.NetworkEdit', {
5a691a50
DM
418 url: me.url,
419 nodename: nodename,
d5e771ce 420 isCreate: true,
f6710aac 421 dataCache: me.dataCache,
c511ca9c
TL
422 listeners: {
423 destroy: () => me.load(),
424 },
425 autoShow: true,
426 });
427 },
428 },
429 {
430 xtype: 'proxmoxButton',
431 text: gettext('Remove'),
432 disabled: true,
433 selModel: sm,
434 enableFn: function(rec) {
435 return !!caps.vms['VM.Config.Network'];
436 },
437 confirmMsg: ({ data }) =>
438 Ext.String.format(gettext('Are you sure you want to remove entry {0}'), `'${data.id}'`),
439 handler: function(btn, e, rec) {
440 Proxmox.Utils.API2Request({
441 url: me.url,
442 waitMsgTarget: me,
443 method: 'PUT',
444 params: {
445 'delete': rec.data.id,
446 digest: me.dataCache.digest,
447 },
448 callback: () => me.load(),
449 failure: (response, opts) => Ext.Msg.alert(gettext('Error'), response.htmlStatus),
5a691a50 450 });
f6710aac 451 },
5a691a50 452 },
c511ca9c
TL
453 {
454 xtype: 'proxmoxButton',
455 text: gettext('Edit'),
456 selModel: sm,
457 disabled: true,
458 enableFn: rec => !!caps.vms['VM.Config.Network'],
459 handler: run_editor,
460 },
5a691a50
DM
461 ],
462 columns: [
463 {
a29ff1bc 464 header: 'ID',
5a691a50 465 width: 50,
f6710aac 466 dataIndex: 'id',
5a691a50
DM
467 },
468 {
469 header: gettext('Name'),
470 width: 80,
f6710aac 471 dataIndex: 'name',
5a691a50
DM
472 },
473 {
474 header: gettext('Bridge'),
475 width: 80,
f6710aac 476 dataIndex: 'bridge',
5a691a50
DM
477 },
478 {
479 header: gettext('Firewall'),
480 width: 80,
481 dataIndex: 'firewall',
f6710aac 482 renderer: Proxmox.Utils.format_boolean,
5a691a50
DM
483 },
484 {
485 header: gettext('VLAN Tag'),
486 width: 80,
f6710aac 487 dataIndex: 'tag',
5a691a50
DM
488 },
489 {
490 header: gettext('MAC address'),
491 width: 110,
f6710aac 492 dataIndex: 'hwaddr',
5a691a50
DM
493 },
494 {
495 header: gettext('IP address'),
496 width: 150,
497 dataIndex: 'ip',
498 renderer: function(value, metaData, rec) {
499 if (rec.data.ip && rec.data.ip6) {
500 return rec.data.ip + "<br>" + rec.data.ip6;
501 } else if (rec.data.ip6) {
502 return rec.data.ip6;
503 } else {
504 return rec.data.ip;
505 }
f6710aac 506 },
5a691a50
DM
507 },
508 {
509 header: gettext('Gateway'),
510 width: 150,
511 dataIndex: 'gw',
512 renderer: function(value, metaData, rec) {
513 if (rec.data.gw && rec.data.gw6) {
514 return rec.data.gw + "<br>" + rec.data.gw6;
515 } else if (rec.data.gw6) {
516 return rec.data.gw6;
517 } else {
518 return rec.data.gw;
519 }
f6710aac
TL
520 },
521 },
5a691a50
DM
522 ],
523 listeners: {
4b488565 524 activate: me.load,
f6710aac
TL
525 itemdblclick: run_editor,
526 },
5a691a50
DM
527 });
528
529 me.callParent();
f6710aac 530 },
5a691a50 531}, function() {
5a691a50
DM
532 Ext.define('pve-lxc-network', {
533 extend: "Ext.data.Model",
534 proxy: { type: 'memory' },
c511ca9c
TL
535 fields: [
536 'id',
537 'name',
538 'hwaddr',
539 'bridge',
540 'ip',
541 'gw',
542 'ip6',
543 'gw6',
544 'tag',
545 'firewall',
546 ],
5a691a50 547 });
5a691a50
DM
548});
549