[Kimchi-devel] [PATCH v2 2/4] Handle missing parameter "network" when attaching a NIC
Crístian Deives
cristiandeives at gmail.com
Wed Apr 15 03:43:39 UTC 2015
If the user doesn't provide the parameter "network" when attaching a
network interface to a virtual machine, the server raises an unexpected
exception because it assumes that parameter will always be provided.
Check the existence of the parameter "network" instead of assuming it
always exists.
Signed-off-by: Crístian Deives <cristiandeives at gmail.com>
---
src/kimchi/model/vmifaces.py | 15 +++++++++++----
tests/test_rest.py | 5 +++++
2 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/src/kimchi/model/vmifaces.py b/src/kimchi/model/vmifaces.py
index 501336c..6a76d2d 100644
--- a/src/kimchi/model/vmifaces.py
+++ b/src/kimchi/model/vmifaces.py
@@ -22,7 +22,8 @@ import random
import libvirt
from lxml import etree, objectify
-from kimchi.exception import InvalidOperation, InvalidParameter, NotFoundError
+from kimchi.exception import InvalidOperation, InvalidParameter
+from kimchi.exception import MissingParameter, NotFoundError
from kimchi.model.config import CapabilitiesModel
from kimchi.model.vms import DOM_STATE_MAP, VMModel
from kimchi.xmlutils.interface import get_iface_xml
@@ -44,9 +45,15 @@ class VMIfacesModel(object):
networks = conn.listNetworks() + conn.listDefinedNetworks()
networks = map(lambda x: x.decode('utf-8'), networks)
- if params["type"] == "network" and params["network"] not in networks:
- raise InvalidParameter("KCHVMIF0002E",
- {'name': vm, 'network': params["network"]})
+ if params['type'] == 'network':
+ network = params.get("network")
+
+ if network is None:
+ raise MissingParameter('KCHVMIF0007E')
+
+ if network not in networks:
+ raise InvalidParameter('KCHVMIF0002E',
+ {'name': vm, 'network': network})
dom = VMModel.get_vm(vm, self.conn)
if DOM_STATE_MAP[dom.info()[0]] != "shutoff":
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 16ff41d..5788886 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -762,6 +762,11 @@ class RestTests(unittest.TestCase):
self.assertEquals(get_template_default('old', 'nic_model'),
res['model'])
+ # try to attach an interface without specifying 'model'
+ req = json.dumps({'type': 'network'})
+ resp = self.request('/vms/test-vm/ifaces', req, 'POST')
+ self.assertEquals(400, resp.status)
+
# attach network interface to vm
req = json.dumps({"type": "network",
"network": "test-network",
--
2.1.0
More information about the Kimchi-devel
mailing list