Network.js 9.12 KB
Newer Older
Dietmar Maurer's avatar
Dietmar Maurer committed
1
/*jslint confusion: true */
2 3 4
Ext.define('PVE.OpenVZ.NetIfEdit', {
    extend: 'PVE.window.Edit',

5 6
    isAdd: true,

7 8 9 10
    getValues: function() {
	var me = this;

	var values = me.formPanel.getValues();
11 12 13 14

	if (!me.create) {
	    values.ifname = me.ifname;
	}
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

	var newdata = Ext.clone(me.netif);
	newdata[values.ifname] = values;
	return { netif: PVE.Parser.printOpenVZNetIf(newdata) };
    },

    initComponent : function() {
	var me = this;

	if (!me.dataCache) {
	    throw "no dataCache specified";
	}

	if (!me.nodename) {
	    throw "no node name specified";
	}
	
	me.netif = PVE.Parser.parseOpenVZNetIf(me.dataCache.netif) || {};

	var cdata = {};

	if (!me.create) {
	    if (!me.ifname) {
		throw "no interface name specified";
	    }
	    cdata = me.netif[me.ifname];
	    if (!cdata) {
		throw "no such interface '" + me.ifname + "'";
	    }
	}

	Ext.apply(me, {
47
	    subject: gettext('Network Device') + ' (veth)',
48 49 50 51 52 53 54 55 56 57
	    digest: me.dataCache.digest,
	    width: 350,
	    fieldDefaults: {
		labelWidth: 130
	    },
	    items: [
		{
		    xtype: me.create ? 'textfield' : 'displayfield',
		    name: 'ifname',
		    height: 22, // hack: set same height as text fields
58
		    fieldLabel: gettext('Name') + ' (i.e. eth0)',
59 60 61 62 63 64 65 66 67 68 69 70
		    allowBlank: false,
		    value: cdata.ifname,
		    validator: function(value) {
			if (me.create && me.netif[value]) {
			    return "interface name already in use";
			}
			return true;
		    }
		},
		{
		    xtype: 'textfield',
		    name: 'mac',
71
		    fieldLabel: 'MAC',
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		    vtype: 'MacAddress',
		    value: cdata.mac,
		    allowBlank: me.create,
		    emptyText: 'auto'
		},
		{
		    xtype: 'PVE.form.BridgeSelector',
		    name: 'bridge',
		    nodename: me.nodename,
		    fieldLabel: 'Bridge',
		    value: cdata.bridge,
		    allowBlank: false
		},
		{
		    xtype: 'textfield',
		    name: 'host_ifname',
		    fieldLabel: 'Host device name',
		    value: cdata.host_ifname,
		    allowBlank: true,
		    emptyText: 'auto'
		},
		{
		    xtype: 'textfield',
		    name: 'host_mac',
		    fieldLabel: 'Host MAC address',
		    vtype: 'MacAddress',
		    value: cdata.host_mac,
		    allowBlank: true,
		    emptyText: 'auto'
		}
	    ]
	});

	me.callParent();
    }
});

Ext.define('PVE.OpenVZ.IPAdd', {
    extend: 'PVE.window.Edit',

112 113 114 115
    isAdd: true,

    create: true, 

116 117 118 119 120 121 122 123 124 125
    getValues: function() {
	var me = this;

	var values = me.formPanel.getValues();
	console.dir(values);

	if (me.dataCache.ip_address) {
	    return { ip_address: me.dataCache.ip_address + ' ' + values.ipaddress };
	} else {  
	    return { ip_address: values.ipaddress };
Dietmar Maurer's avatar
Dietmar Maurer committed
126
	}
127 128 129 130 131 132 133 134 135 136
    },

    initComponent : function() {
	var me = this;

	if (!me.dataCache) {
	    throw "no dataCache specified";
	}

	Ext.apply(me, {
137
	    subject: gettext('IP address') + ' (venet)',
138 139 140 141 142
	    digest: me.dataCache.digest,
	    width: 350,
	    items: {
		xtype: 'textfield',
		name: 'ipaddress',
143
		fieldLabel: gettext('IP address'),
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		vtype: 'IPAddress',
		allowBlank: false
	    }
	});

	me.callParent();
    }
});


Ext.define('PVE.openvz.NetworkView', {
    extend: 'Ext.grid.GridPanel',
    alias: ['widget.pveOpenVZNetworkView'],

    dataCache: {}, // used to store result of last load

160 161 162 163
    ipAddressText: gettext('IP address'),
    networkText: gettext('Network'),
    networkDeviceText: gettext('Network Device'),

164
    renderType: function(value, metaData, record, rowIndex, colIndex, store) {
165
	var me = this;
166
	if (value === 'ip') {
167
	    return me.ipAddressText;
168
	} else if (value === 'net') {
169
	    return me.networkText;
170
	} else if (value === 'veth') {
171
	    return me.networkDeviceText;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	} else {
	    return value;
	}
    },

    renderValue: function(value, metaData, record, rowIndex, colIndex, store) {
	var type = record.data.type;
	if (type === 'veth') {
	    return record.data.ifname;
	} else {
	    return value;
	}
    },

    load: function() {
	var me = this;

189
	PVE.Utils.setErrorMask(me, true);
190 191 192 193

	PVE.Utils.API2Request({
	    url: me.url,
	    failure: function(response, opts) {
194
		PVE.Utils.setErrorMask(me, gettext('Error') + ': ' + response.htmlStatus);
195 196
	    },
	    success: function(response, opts) {
197
		PVE.Utils.setErrorMask(me, false);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		var result = Ext.decode(response.responseText);
		var data = result.data || {};
		me.dataCache = data;
		var ipAddress = data.ip_address;
		var records = [];
		if (ipAddress) {
		    var ind = 0;
		    Ext.Array.each(ipAddress.split(' '), function(value) {
			if (value) {
			    records.push({
				type: 'ip',
				id: 'ip' + ind,
				value: value
			    });
			    ind++;
			}
		    });
		}
		var netif = PVE.Parser.parseOpenVZNetIf(me.dataCache.netif);
		if (netif) {
		    Ext.Object.each(netif, function(iface, data) {
			
			records.push(Ext.apply({
			    type: 'veth',
			    id: iface,
			    value: data.raw
			}, data));
		    });
		}
		me.store.loadData(records);
	    }
	});
    },

    initComponent : function() {
	var me = this;

Dietmar Maurer's avatar
Dietmar Maurer committed
235
	var nodename = me.pveSelNode.data.node;
236 237 238 239
	if (!nodename) {
	    throw "no node name specified";
	}

Dietmar Maurer's avatar
Dietmar Maurer committed
240
	var vmid = me.pveSelNode.data.vmid;
241 242 243 244
	if (!vmid) {
	    throw "no VM ID specified";
	}

245 246
	var caps = Ext.state.Manager.get('GuiCap');

247 248 249 250 251 252
	me.url = '/nodes/' + nodename + '/openvz/' + vmid + '/config';

	var store = new Ext.data.Store({
	    model: 'pve-openvz-network'
	});

253
	var sm = Ext.create('Ext.selection.RowModel', {});
254

255 256 257 258
	var remove_btn = new PVE.button.Button({
	    text: gettext('Remove'),
	    disabled: true,
	    selModel: sm,
259 260 261
	    enableFn: function(rec) {
		return !!caps.vms['VM.Config.Network'];
	    },
262 263 264 265 266 267
	    confirmMsg: function (rec) {
		var idtext = rec.id;
		if (rec.data.type === 'ip') {
		    idtext = rec.data.value;
		} else if (rec.data.type === 'veth') {
		    idtext = rec.data.id;
268
		}
269 270 271 272 273
		return Ext.String.format(gettext('Are you sure you want to remove entry {0}'),
					 "'" + idtext + "'");
	    },
	    handler: function(btn, event, rec) {
		var values = { digest: me.dataCache.digest };
274 275

		if (rec.data.type === 'ip') {
276 277 278 279 280 281 282
		    var ipa = [];
		    Ext.Array.each(me.dataCache.ip_address.split(' '), function(value) {
			if (value && value !== rec.data.value) {
			    ipa.push(value);
			}
		    });
		    values.ip_address = ipa.join(' ');
283
		} else if (rec.data.type === 'veth') {
284 285 286
		    var netif = PVE.Parser.parseOpenVZNetIf(me.dataCache.netif);
		    delete netif[rec.data.id];
		    values.netif = PVE.Parser.printOpenVZNetIf(netif);
287
		} else {
288
		    return; // not implemented
289 290
		}

291 292 293 294 295 296 297 298 299 300
		PVE.Utils.API2Request({
		    url: me.url,
		    waitMsgTarget: me,
		    method: 'PUT',
		    params: values,
		    callback: function() {
			me.load();
		    },
		    failure: function (response, opts) {
			Ext.Msg.alert(gettext('Error'), response.htmlStatus);
301 302 303 304 305 306 307 308 309 310 311
		    }
		});
	    }
	});

	var run_editor = function() {
	    var rec = sm.getSelection()[0];
	    if (!rec || rec.data.type !== 'veth') {
		return;
	    }

312 313 314 315
	    if (!caps.vms['VM.Config.Network']) {
		return false;
	    }

316 317 318 319 320 321 322 323
	    var win = Ext.create('PVE.OpenVZ.NetIfEdit', {
		url: me.url,
		nodename: nodename,
		dataCache: me.dataCache,
		ifname: rec.data.id
	    });
	    win.on('destroy', me.load, me);
	    win.show();
Dietmar Maurer's avatar
Dietmar Maurer committed
324
	};
325

326 327 328
	var edit_btn = new PVE.button.Button({
	    text: gettext('Edit'),
	    selModel: sm,
329
	    disabled: true,
330
	    enableFn: function(rec) {
331 332 333
		if (!caps.vms['VM.Config.Network']) {
		    return false;
		}
334 335
		return rec.data.type === 'veth';
	    },
336 337 338 339 340 341
	    handler: run_editor
	});


	Ext.applyIf(me, {
	    store: store,
342
	    selModel: sm,
343 344 345
	    stateful: false,
	    tbar: [
		{
346
		    text: gettext('Add'),
347 348 349
		    menu: new Ext.menu.Menu({
			items: [
			    {
350
				text: gettext('IP address') + ' (venet)',
351
				disabled: !caps.vms['VM.Config.Network'],
352 353 354 355 356 357 358 359 360 361 362 363
				//plain: true,
				//iconCls: 'pve-itype-icon-storage',
				handler: function() {
				    var win = Ext.create('PVE.OpenVZ.IPAdd', {
					url: me.url,
					dataCache: me.dataCache
				    });
				    win.on('destroy', me.load, me);
				    win.show();
				}
			    },
			    {
364
				text: gettext('Network Device') + ' (veth)',
365
				disabled: !caps.vms['VM.Config.Network'],
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
				//plain: true,
				//iconCls: 'pve-itype-icon-storage',
				handler: function() {
				    var win = Ext.create('PVE.OpenVZ.NetIfEdit', {
					url: me.url,
					nodename: nodename,
					create: true,
					dataCache: me.dataCache
				    });
				    win.on('destroy', me.load, me);
				    win.show();
				}
			    }
			]
		    })
		},
		remove_btn,
		edit_btn
	    ],
	    columns: [
		{
387
		    header: gettext('Type'),
388 389 390 391 392
		    width: 110,
		    dataIndex: 'type',
		    renderer: me.renderType
		},
		{
393
		    header: gettext('IP address') +'/' + gettext('Name'),
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
		    width: 110,
		    dataIndex: 'value',
		    renderer: me.renderValue
		},
		{
		    header: 'Bridge',
		    width: 110,
		    dataIndex: 'bridge'
		},
		{
		    header: 'MAC',
		    width: 110,
		    dataIndex: 'mac'
		},
		{
		    header: 'Host ifname',
		    width: 110,
		    dataIndex: 'host_ifname'
		},
		{
		    header: 'Host MAC',
		    width: 110,
		    dataIndex: 'host_mac'
		}
	    ],
	    listeners: {
		show: me.load,
421
		itemdblclick: run_editor
422 423 424
	    }
	});

Dietmar Maurer's avatar
Dietmar Maurer committed
425
	me.callParent();
426 427 428 429 430 431 432 433 434 435 436 437 438

	me.load();
   }
}, function() {

    Ext.define('pve-openvz-network', {
	extend: "Ext.data.Model",
	proxy: { type: 'memory' },
	fields: [ 'id', 'type', 'value', 'ifname', 'mac', 'bridge', 'host_ifname', 'host_mac' ]
    });

});