[Kimchi-devel] [PATCH 3/5] Update users/groups verification based on new API

Aline Manera alinefm at linux.vnet.ibm.com
Thu Apr 17 21:04:42 UTC 2014


From: Aline Manera <alinefm at br.ibm.com>

Use the APIs /host/users and /host/groups to check user input
That way we can check the input data at once instead of checking item by item.

Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 src/kimchi/i18n.py      |    4 ++--
 src/kimchi/mockmodel.py |   24 ++++++++++++++++++------
 src/kimchi/model/vms.py |   20 ++++++++++++--------
 3 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 3fc3013..89bcd02 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -84,8 +84,8 @@ messages = {
     "KCHVM0024E": _("User name must be a string"),
     "KCHVM0025E": _("Group names list must be an array"),
     "KCHVM0026E": _("Group name must be a string"),
-    "KCHVM0027E": _("User %(user)s does not exist"),
-    "KCHVM0028E": _("Group %(group)s does not exist"),
+    "KCHVM0027E": _("User(s) '%(users)s' do not exist"),
+    "KCHVM0028E": _("Group(s) '%(groups)s' do not exist"),
 
     "KCHVMIF0001E": _("Interface %(iface)s does not exist in virtual machine %(name)s"),
     "KCHVMIF0002E": _("Network %(network)s specified for virtual machine %(name)s does not exist"),
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index b235e1e..866ad2c 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -91,14 +91,26 @@ class MockModel(object):
             if state == 'running' or params['name'] in self.vms_get_list():
                 msg_args = {'name': dom.name, 'new_name': params['name']}
                 raise InvalidParameter("KCHVM0003E", msg_args)
-            else:
-                del self._mock_vms[dom.name]
-                dom.name = params['name']
-                self._mock_vms[dom.name] = dom
+
+            del self._mock_vms[dom.name]
+            dom.name = params['name']
+            self._mock_vms[dom.name] = dom
 
         for key, val in params.items():
-            if key in dom.info:
-                dom.info[key] = val
+            if key == 'users':
+                invalid_users = set(val) - set(self.users_get_list())
+                if len(invalid_users) != 0:
+                    raise InvalidParameter("KCHVM0027E",
+                                           {'users': ", ".join(invalid_users)})
+
+            elif key == 'groups':
+                invalid_groups = set(val) - set(self.groups_get_list())
+                if len(invalid_groups) != 0:
+                    raise InvalidParameter("KCHVM0028E",
+                                           {'groups':
+                                            ", ".join(invalid_groups)})
+
+            dom.info[key] = val
 
     def _live_vm_update(self, dom, params):
         pass
diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index 90e9537..ede54a3 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -29,7 +29,6 @@ from lxml.builder import E
 
 from kimchi import vnc
 from kimchi import xmlutils
-from kimchi.auth import Group, User
 from kimchi.config import READONLY_POOL_TYPE
 from kimchi.exception import InvalidOperation, InvalidParameter
 from kimchi.exception import NotFoundError, OperationFailed
@@ -37,7 +36,7 @@ from kimchi.model.config import CapabilitiesModel
 from kimchi.model.templates import TemplateModel
 from kimchi.model.utils import get_vm_name
 from kimchi.screenshot import VMScreenshot
-from kimchi.utils import kimchi_log, run_setfacl_set_attr
+from kimchi.utils import import_class, kimchi_log, run_setfacl_set_attr
 from kimchi.utils import template_name_from_uri
 from kimchi.xmlutils import xpath_get_text
 
@@ -236,6 +235,8 @@ class VMModel(object):
         self.conn = kargs['conn']
         self.objstore = kargs['objstore']
         self.vmscreenshot = VMScreenshotModel(**kargs)
+        self.users = import_class('kimchi.model.host.UsersModel')(**kargs)
+        self.groups = import_class('kimchi.model.host.GroupsModel')(**kargs)
 
     def update(self, name, params):
         dom = self.get_vm(name, self.conn)
@@ -264,14 +265,17 @@ class VMModel(object):
 
         for key, val in params.items():
             if key == 'users':
-                for user in val:
-                    if not User(user).exists():
-                        raise OperationFailed("KCHVM0027E", {'user': user})
+                invalid_users = set(val) - set(self.users.get_list())
+                if len(invalid_users) != 0:
+                    raise InvalidParameter("KCHVM0027E",
+                                           {'users': ", ".join(invalid_users)})
                 users = val
             elif key == 'groups':
-                for group in val:
-                    if not Group(group).exists():
-                        raise OperationFailed("KCHVM0028E", {'group': group})
+                invalid_groups = set(val) - set(self.groups.get_list())
+                if len(invalid_groups) != 0:
+                    raise InvalidParameter("KCHVM0028E",
+                                           {'groups':
+                                            ", ".join(invalid_groups)})
                 groups = val
             else:
                 if key in VM_STATIC_UPDATE_PARAMS:
-- 
1.7.10.4




More information about the Kimchi-devel mailing list