Add VIF.runtime_properties dictionary, and use that to implement xm network-list

through the Xen-API.  Implement xm network-attach and xm network-detach also.

Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
This commit is contained in:
Ewan Mellor committed 2007-03-20 15:12:07 +00:00
1 parent 1c3dce55cd
commit 9e9090601c
3 files changed
+147 -41

No files matched your search

+43 -24
View File
@@ -1656,21 +1656,19 @@ class XendAPI(object):
xendom = XendDomain.instance()
dominfo = xendom.get_vm_with_dev_uuid('vbd', vbd_ref)
device = dominfo.get_dev_config_by_uuid('vbd', vbd_ref)
devid = int(device['id'])
device_sxps = dominfo.getDeviceSxprs('vbd')
log.debug("VBD_get_runtime_properties devid: %i device_sxps: %s",
devid, device_sxps)
device_dicts = [dict(device_sxp[1][1:]) for device_sxp in device_sxps]
try:
devid = int(device['id'])
device_sxps = dominfo.getDeviceSxprs('vbd')
device_dicts = [dict(device_sxp[1][1:]) for device_sxp in device_sxps]
device_dict = [device_dict
for device_dict in device_dicts
if int(device_dict['virtual-device']) == devid][0]
device_dict = [device_dict
for device_dict in device_dicts
if int(device_dict['virtual-device']) == devid][0]
log.debug("VBD_get_runtime_properties device_dict: %s", device_dict)
return xen_api_success(device_dict)
return xen_api_success(device_dict)
except Exception, exn:
log.exception(exn)
return xen_api_success({})
# attributes (rw)
def VBD_get_VM(self, session, vbd_ref):
@@ -1732,7 +1730,8 @@ class XendAPI(object):
# Xen API: Class VIF
# ----------------------------------------------------------------
VIF_attr_ro = ['metrics']
VIF_attr_ro = ['metrics',
'runtime_properties']
VIF_attr_rw = ['device',
'network',
'VM',
@@ -1769,18 +1768,17 @@ class XendAPI(object):
# class methods
def VIF_create(self, session, vif_struct):
xendom = XendDomain.instance()
if xendom.is_valid_vm(vif_struct['VM']):
dom = xendom.get_vm_by_uuid(vif_struct['VM'])
try:
vif_ref = dom.create_vif(vif_struct)
xendom.managed_config_save(dom)
return xen_api_success(vif_ref)
except XendError:
return xen_api_error(XEND_ERROR_TODO)
else:
if not xendom.is_valid_vm(vif_struct['VM']):
return xen_api_error(['HANDLE_INVALID', 'VM', vif_struct['VM']])
dom = xendom.get_vm_by_uuid(vif_struct['VM'])
try:
vif_ref = dom.create_vif(vif_struct)
xendom.managed_config_save(dom)
return xen_api_success(vif_ref)
except XendError:
return xen_api_error(XEND_ERROR_TODO)
def VIF_destroy(self, session, vif_ref):
xendom = XendDomain.instance()
vm = xendom.get_vm_with_dev_uuid('vif', vif_ref)
@@ -1818,6 +1816,27 @@ class XendAPI(object):
vifs = reduce(lambda x, y: x + y, vifs)
return xen_api_success(vifs)
def VIF_get_runtime_properties(self, _, vif_ref):
xendom = XendDomain.instance()
dominfo = xendom.get_vm_with_dev_uuid('vif', vif_ref)
device = dominfo.get_dev_config_by_uuid('vif', vif_ref)
try:
devid = int(device['id'])
device_sxps = dominfo.getDeviceSxprs('vif')
device_dicts = [dict(device_sxp[1][1:])
for device_sxp in device_sxps]
device_dict = [device_dict
for device_dict in device_dicts
if int(device_dict['handle']) == devid][0]
return xen_api_success(device_dict)
except Exception, exn:
log.exception(exn)
return xen_api_success({})
# Xen API: Class VIF_metrics
# ----------------------------------------------------------------
+15 -5
View File
@@ -2286,8 +2286,6 @@ class XendDomainInfo:
@return: uuid of the device
"""
xenapi_vbd['image'] = vdi_image_path
log.debug('create_vbd: %s' % xenapi_vbd)
dev_uuid = ''
if vdi_image_path.startswith('tap'):
dev_uuid = self.info.device_add('tap', cfg_xenapi = xenapi_vbd)
else:
@@ -2297,8 +2295,8 @@ class XendDomainInfo:
raise XendError('Failed to create device')
if self.state == XEN_API_VM_POWER_STATE_RUNNING:
_, config = self.info['devices'][dev_uuid]
dev_control = None
if vdi_image_path.startswith('tap'):
dev_control = self.getDeviceController('tap')
@@ -2349,9 +2347,21 @@ class XendDomainInfo:
raise XendError('Failed to create device')
if self.state == XEN_API_VM_POWER_STATE_RUNNING:
_, config = self.info['devices'][dev_uuid]
config['devid'] = self.getDeviceController('vif').createDevice(config)
_, config = self.info['devices'][dev_uuid]
dev_control = self.getDeviceController('vif')
try:
devid = dev_control.createDevice(config)
dev_control.waitForDevice(devid)
self.info.device_update(dev_uuid,
cfg_xenapi = {'devid': devid})
except Exception, exn:
log.exception(exn)
del self.info['devices'][dev_uuid]
self.info['vif_refs'].remove(dev_uuid)
raise
return dev_uuid
def create_vtpm(self, xenapi_vtpm):
+89 -12
View File
@@ -497,6 +497,10 @@ def get_default_SR():
for sr_ref in server.xenapi.SR.get_all()
if server.xenapi.SR.get_type(sr_ref) == "local"][0]
def get_default_Network():
return [network_ref
for network_ref in server.xenapi.network.get_all()][0]
def map2sxp(m):
return [[k, m[k]] for k in m.keys()]
@@ -1669,12 +1673,20 @@ def xm_network_list(args):
(use_long, params) = arg_check_for_resource_list(args, "network-list")
dom = params[0]
if use_long:
if serverType == SERVER_XEN_API:
vif_refs = server.xenapi.VM.get_VIFs(get_single_vm(dom))
vif_properties = \
map(server.xenapi.VIF.get_runtime_properties, vif_refs)
devs = map(lambda x: [x.get('handle'), map2sxp(x)], vif_properties)
else:
devs = server.xend.domain.getDeviceSxprs(dom, 'vif')
if use_long:
map(PrettyPrint.prettyprint, devs)
else:
hdr = 0
for x in server.xend.domain.getDeviceSxprs(dom, 'vif'):
for x in devs:
if hdr == 0:
print 'Idx BE MAC Addr. handle state evt-ch tx-/rx-ring-ref BE-path'
hdr = 1
@@ -1699,7 +1711,7 @@ def xm_block_list(args):
vbd_refs = server.xenapi.VM.get_VBDs(get_single_vm(dom))
vbd_properties = \
map(server.xenapi.VBD.get_runtime_properties, vbd_refs)
devs = map(lambda x: [x['virtual-device'], map2sxp(x)], vbd_properties)
devs = map(lambda x: [x.get('virtual-device'), map2sxp(x)], vbd_properties)
else:
devs = server.xend.domain.getDeviceSxprs(dom, 'vbd')
@@ -1836,15 +1848,65 @@ def xm_network_attach(args):
vif_params = ['type', 'mac', 'bridge', 'ip', 'script', \
'backend', 'vifname', 'rate', 'model']
for a in args[1:]:
vif_param = a.split("=")
if len(vif_param) != 2 or vif_param[1] == '' or \
vif_param[0] not in vif_params:
err("Invalid argument: %s" % a)
usage('network-attach')
vif.append(vif_param)
if serverType == SERVER_XEN_API:
vif_record = {
"device": "eth0",
"network": get_default_Network(),
"VM": get_single_vm(dom),
"MAC": "",
"MTU": "",
"qos_algorithm_type": "",
"qos_algorithm_params": {},
"other_config": {}
}
server.xend.domain.device_create(dom, vif)
def set(keys, val):
record = vif_record
for key in keys[:-1]:
record = record[key]
record[keys[-1]] = val
vif_conv = {
'type':
lambda x: None,
'mac':
lambda x: set(['MAC'], x),
'bridge':
lambda x: set(['network'], get_net_from_bridge(x)),
'ip':
lambda x: set(['other_config', 'ip'], x),
'script':
lambda x: set(['other_config', 'script'], x),
'backend':
lambda x: set(['other_config', 'backend'], x),
'vifname':
lambda x: set(['device'], x),
'rate':
lambda x: set(['qos_algorithm_params', 'rate'], x),
'model':
lambda x: None
}
for a in args[1:]:
vif_param = a.split("=")
if len(vif_param) != 2 or vif_param[1] == '' or \
vif_param[0] not in vif_params:
err("Invalid argument: %s" % a)
usage('network-attach')
else:
vif_conv[vif_param[0]](vif_param[1])
print str(vif_record)
server.xenapi.VIF.create(vif_record)
else:
for a in args[1:]:
vif_param = a.split("=")
if len(vif_param) != 2 or vif_param[1] == '' or \
vif_param[0] not in vif_params:
err("Invalid argument: %s" % a)
usage('network-attach')
vif.append(vif_param)
server.xend.domain.device_create(dom, vif)
def detach(args, command, deviceClass):
@@ -1890,7 +1952,22 @@ def xm_block_detach(args):
detach(args, 'block-detach', 'tap')
def xm_network_detach(args):
detach(args, 'network-detach', 'vif')
if serverType == SERVER_XEN_API:
arg_check(args, "xm_block_detach", 2, 3)
dom = args[0]
devid = args[1]
vif_refs = server.xenapi.VM.get_VIFs(get_single_vm(dom))
vif_refs = [vif_ref for vif_ref in vif_refs
if server.xenapi.VIF.\
get_runtime_properties(vif_ref)["handle"] == devid]
if len(vif_refs) > 0:
vif_ref = vif_refs[0]
server.xenapi.VIF.destroy(vif_ref)
else:
print "Cannot find device '%s' in domain '%s'" % (devid,dom)
else:
detach(args, 'network-detach', 'vif')
def xm_vnet_list(args):
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.