devices: rng: Use CharSource

Re-use CharSource, just like libvirt does internally. Adjust all
callers to match. Rename type -> backend_model while we are here,
because type is ambiguous
This commit is contained in:
Cole Robinson 2019-05-13 14:42:50 -04:00
parent b2b9d7d366
commit 6d46e37e09
6 changed files with 23 additions and 30 deletions

View File

@ -1010,13 +1010,13 @@ class XMLParseTest(unittest.TestCase):
dev1 = guest.devices.rng[0]
check = self._make_checker(dev1)
check("type", "egd")
check("backend_model", "egd")
check("backend_type", "udp", "udp")
check("connect_host", "1.2.3.4", "1.2.3.5")
check("connect_service", "1234", "1235")
check("bind_host", None, None)
check("bind_service", "1233", "1236")
check("source.connect_host", "1.2.3.4", "1.2.3.5")
check("source.connect_service", 1234, 1235)
check("source.bind_host", None, None)
check("source.bind_service", 1233, 1236)
check("rate_bytes", "1234", "4321")
check("rate_period", "2000", "2001")
@ -1029,7 +1029,7 @@ class XMLParseTest(unittest.TestCase):
dev1 = guest.devices.rng[0]
check = self._make_checker(dev1)
check("type", "random", "random")
check("backend_model", "random", "random")
check("model", "virtio", "virtio")
check("device", "/dev/random", "/dev/hwrng")

View File

@ -1434,7 +1434,7 @@ class vmmAddHardware(vmmGObjectUI):
def _build_rng(self):
device = self.widget("rng-device").get_text()
dev = DeviceRng(self.conn.get_backend())
dev.type = DeviceRng.TYPE_RANDOM
dev.backend_model = DeviceRng.TYPE_RANDOM
dev.device = device
return dev

View File

@ -2333,10 +2333,11 @@ class vmmDetails(vmmGObjectUI):
self.widget("panic-model").set_text(pmodel)
def refresh_rng_page(self, dev):
is_random = dev.type == "random"
is_random = dev.backend_model == "random"
uiutil.set_grid_row_visible(self.widget("rng-device"), is_random)
self.widget("rng-type").set_text(dev.get_pretty_type(dev.type))
self.widget("rng-type").set_text(
dev.get_pretty_type(dev.backend_model))
self.widget("rng-device").set_text(dev.device or "")
def refresh_vsock_page(self, dev):

View File

@ -3143,16 +3143,16 @@ class ParserRNG(VirtCLIParser):
inst.backend_type = inst.cli_backend_type
if inst.cli_backend_mode == "connect":
namemap["backend_host"] = "connect_host"
namemap["backend_service"] = "connect_service"
namemap["backend_host"] = "source.connect_host"
namemap["backend_service"] = "source.connect_service"
if inst.cli_backend_mode == "bind":
namemap["backend_host"] = "bind_host"
namemap["backend_service"] = "bind_service"
namemap["backend_host"] = "source.bind_host"
namemap["backend_service"] = "source.bind_service"
if inst.cli_backend_type == "udp":
namemap["backend_connect_host"] = "connect_host"
namemap["backend_connect_service"] = "connect_service"
namemap["backend_connect_host"] = "source.connect_host"
namemap["backend_connect_service"] = "source.connect_service"
if virtarg.cliname in namemap:
util.set_prop_path(inst, namemap[virtarg.cliname], val)
@ -3182,7 +3182,7 @@ class ParserRNG(VirtCLIParser):
def _init_class(cls, **kwargs):
VirtCLIParser._init_class(**kwargs)
_add_device_address_args(cls)
cls.add_arg("type", "type")
cls.add_arg("type", "backend_model")
cls.add_arg("backend_mode", None, lookup_cb=None,
cb=cls.set_backend_cb)

View File

@ -144,7 +144,7 @@ class Device(XMLBuilder):
"smartcard": ["mode", "xmlindex"],
"redirdev": ["bus", "type", "xmlindex"],
"tpm": ["type", "xmlindex"],
"rng": ["type", "xmlindex"],
"rng": ["backend_model", "xmlindex"],
"panic": ["type", "xmlindex"],
"vsock": ["xmlindex"],
}

View File

@ -5,7 +5,8 @@
# See the COPYING file in the top-level directory.
from .device import Device
from ..xmlbuilder import XMLProperty
from .char import CharSource
from ..xmlbuilder import XMLChildProperty, XMLProperty
class DeviceRng(Device):
@ -14,12 +15,6 @@ class DeviceRng(Device):
TYPE_RANDOM = "random"
TYPE_EGD = "egd"
BACKEND_TYPE_UDP = "udp"
BACKEND_TYPE_TCP = "tcp"
BACKEND_MODE_BIND = "bind"
BACKEND_MODE_CONNECT = "connect"
@staticmethod
def get_pretty_type(rng_type):
if rng_type == DeviceRng.TYPE_RANDOM:
@ -38,16 +33,13 @@ class DeviceRng(Device):
return {"bind": _("Bind"),
"connect": _("Connect")}.get(mode) or mode
type = XMLProperty("./backend/@model")
model = XMLProperty("./@model")
backend_model = XMLProperty("./backend/@model")
backend_type = XMLProperty("./backend/@type")
bind_host = XMLProperty("./backend/source[@mode='bind']/@host")
bind_service = XMLProperty("./backend/source[@mode='bind']/@service")
connect_host = XMLProperty("./backend/source[@mode='connect']/@host")
connect_service = XMLProperty("./backend/source[@mode='connect']/@service")
source = XMLChildProperty(CharSource, is_single=True,
relative_xpath="./backend")
rate_bytes = XMLProperty("./rate/@bytes")
rate_period = XMLProperty("./rate/@period")