vm/net: move 'netvm' value validation to setter

This commit is contained in:
Marek Marczykowski-Górecki 2017-05-12 14:21:10 +02:00
parent 3bc8facc0b
commit 8c3c65f40f
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 15 additions and 13 deletions

View File

@ -322,10 +322,6 @@ class VMProperty(qubes.property):
raise TypeError(
"'type' keyword parameter is unsupported in {}".format(
self.__class__.__name__))
if 'setter' in kwargs:
raise TypeError(
"'setter' keyword parameter is unsupported in {}".format(
self.__class__.__name__))
if not issubclass(vmclass, BaseVM):
raise TypeError(
"'vmclass' should specify a subclass of qubes.vm.BaseVM")

View File

@ -63,6 +63,20 @@ def _setter_ip(self, prop, value):
return value
def _setter_netvm(self, prop, value):
if value is None:
return
if not value.provides_network:
raise qubes.exc.QubesValueError(
'The {!s} qube does not provide network'.format(value))
if value is self \
or value in self.app.domains.get_vms_connected_to(self):
raise qubes.exc.QubesValueError(
'Loops in network are unsupported')
return value
class NetVMMixin(qubes.events.Emitter):
''' Mixin containing network functionality '''
mac = qubes.property('mac', type=str,
@ -79,6 +93,7 @@ class NetVMMixin(qubes.events.Emitter):
netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
default=(lambda self: self.app.default_fw_netvm if self.provides_network
else self.app.default_netvm),
setter=_setter_netvm,
doc='''VM that provides network connection to this domain. When
`None`, machine is disconnected. When absent, domain uses default
NetVM.''')
@ -338,15 +353,6 @@ class NetVMMixin(qubes.events.Emitter):
''' Run sanity checks before setting a new NetVM '''
# pylint: disable=unused-argument
if newvalue is not None:
if not newvalue.provides_network:
raise qubes.exc.QubesValueError(
'The {!s} qube does not provide network'.format(newvalue))
if newvalue is self \
or newvalue in self.app.domains.get_vms_connected_to(self):
raise qubes.exc.QubesValueError(
'Loops in network are unsupported')
if not self.app.vmm.offline_mode \
and self.is_running() and not newvalue.is_running():
raise qubes.exc.QubesVMNotStartedError(newvalue,