From nobody Mon Sep 16 20:34:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of ovirt.org designates 66.187.230.42 as permitted sender) client-ip=66.187.230.42; envelope-from=kimchi-devel-bounces@ovirt.org; helo=lists.ovirt.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of ovirt.org designates 66.187.230.42 as permitted sender) smtp.mailfrom=kimchi-devel-bounces@ovirt.org; Return-Path: Received: from lists.ovirt.org (lists.phx.ovirt.org [66.187.230.42]) by mx.zohomail.com with SMTPS id 1486492628621253.35011617833788; Tue, 7 Feb 2017 10:37:08 -0800 (PST) Received: from lists.phx.ovirt.org (localhost [127.0.0.1]) by lists.ovirt.org (Postfix) with ESMTP id CA3238205C1; Tue, 7 Feb 2017 18:37:07 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lists.ovirt.org (Postfix) with ESMTPS id EE02A82054C for ; Tue, 7 Feb 2017 18:35:46 +0000 (UTC) Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v17ISqS4064314 for ; Tue, 7 Feb 2017 13:35:46 -0500 Received: from e24smtp01.br.ibm.com (e24smtp01.br.ibm.com [32.104.18.85]) by mx0a-001b2d01.pphosted.com with ESMTP id 28fgmt1cv8-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 07 Feb 2017 13:35:45 -0500 Received: from localhost by e24smtp01.br.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 7 Feb 2017 16:35:43 -0200 Received: from d24dlp01.br.ibm.com (9.18.248.204) by e24smtp01.br.ibm.com (10.172.0.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 7 Feb 2017 16:35:41 -0200 Received: from d24relay01.br.ibm.com (d24relay01.br.ibm.com [9.8.31.16]) by d24dlp01.br.ibm.com (Postfix) with ESMTP id 96D44352006C for ; Tue, 7 Feb 2017 13:35:07 -0500 (EST) Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.8.31.91]) by d24relay01.br.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v17IZedt4047030 for ; Tue, 7 Feb 2017 16:35:40 -0200 Received: from d24av01.br.ibm.com (localhost [127.0.0.1]) by d24av01.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id v17IZeCm003451 for ; Tue, 7 Feb 2017 16:35:40 -0200 Received: from alinefm-TP440.ibmmodules.com ([9.85.187.18]) by d24av01.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id v17IZRN4003298 for ; Tue, 7 Feb 2017 16:35:39 -0200 X-Original-To: kimchi-devel@ovirt.org From: Aline Manera To: Kimchi Devel Date: Tue, 7 Feb 2017 16:35:16 -0200 X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170207183516.5661-1-alinefm@linux.vnet.ibm.com> References: <20170207183516.5661-1-alinefm@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17020718-1523-0000-0000-0000027CEAD9 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17020718-1524-0000-0000-00002A110C0F Message-Id: <20170207183516.5661-7-alinefm@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-07_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1702070175 Subject: [Kimchi-devel] [PATCH] [Kimchi 6/6] Improve logic to identify if a network is in use or not X-BeenThere: kimchi-devel@ovirt.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: kimchi-devel-bounces@ovirt.org Errors-To: kimchi-devel-bounces@ovirt.org X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The function get_vms_attach_to_a_network() to identify which virtual machines are using a given network was being called multiple times without need. So improve the logic. Signed-off-by: Aline Manera --- i18n.py | 4 ++-- model/networks.py | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/i18n.py b/i18n.py index f505693..4460ce5 100644 --- a/i18n.py +++ b/i18n.py @@ -300,8 +300,8 @@ messages =3D { "KCHNET0014E": _("Network interfaces must be an array."), "KCHNET0015E": _("Network VLAN ID must be an integer between 1 and 409= 4"), "KCHNET0016E": _("Specify name and type to create a Network"), - "KCHNET0017E": _("Unable to delete or update network %(name)s. There a= re some virtual machines %(vms)s and/or templates linked to this network."), - "KCHNET0018E": _("Unable to deactivate network %(name)s. There are som= e virtual machines %(vms)s and/or templates linked to this network."), + "KCHNET0017E": _("Unable to delete or update network %(name)s as it is= linked to some virtual machines (%(vms)s) and/or templates (%(tmpls)s)."), + "KCHNET0018E": _("Unable to deactivate network %(name)s as it is linke= d to are some virtual machines (%(vms)s) and/or templates (%(tmpls)s)."), "KCHNET0019E": _("Bridge device %(name)s can not be the trunk device o= f a VLAN."), "KCHNET0020E": _("Failed to activate interface %(iface)s: %(err)s."), "KCHNET0021E": _("Failed to activate interface %(iface)s. Please check= the physical link status."), diff --git a/model/networks.py b/model/networks.py index be4eec2..eb1590e 100644 --- a/model/networks.py +++ b/model/networks.py @@ -1,7 +1,7 @@ # # Project Kimchi # -# Copyright IBM Corp, 2015-2016 +# Copyright IBM Corp, 2015-2017 # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -383,12 +383,14 @@ class NetworkModel(object): else: interfaces =3D [interface] =20 + network_in_use, used_by_vms, _ =3D self._is_network_in_use(name) + return {'connection': connection, 'interfaces': interfaces, 'subnet': subnet, 'dhcp': dhcp, - 'vms': self._get_vms_attach_to_a_network(name), - 'in_use': self._is_network_in_use(name), + 'vms': used_by_vms, + 'in_use': network_in_use, 'autostart': network.autostart() =3D=3D 1, 'state': network.isActive() and "active" or "inactive", 'persistent': True if network.isPersistent() else False} @@ -397,20 +399,24 @@ class NetworkModel(object): # All the networks listed as default in template.conf file should = not # be deactivate or deleted. Otherwise, we will allow user create # inconsistent templates from scratch + vms =3D self._get_vms_attach_to_a_network(name) + tmpls =3D self._is_network_used_by_template(name) + if name in tmpl_defaults['networks']: - return True + return (True, vms, tmpls) =20 - vms =3D self._get_vms_attach_to_a_network(name) - return bool(vms) or self._is_network_used_by_template(name) + return (bool(vms) or bool(tmpls), vms, tmpls) =20 def _is_network_used_by_template(self, network): + tmpl_list =3D [] + with self.objstore as session: templates =3D session.get_list('template') for tmpl in templates: tmpl_net =3D session.get('template', tmpl)['networks'] if network in tmpl_net: - return True - return False + tmpl_list.append(tmpl) + return tmpl_list =20 def _get_vms_attach_to_a_network(self, network, filter=3D"all"): DOM_STATE_MAP =3D {'nostate': 0, 'running': 1, 'blocked': 2, @@ -440,21 +446,25 @@ class NetworkModel(object): 'err': e.message}) =20 def deactivate(self, name): - if self._is_network_in_use(name): - vms =3D self._get_vms_attach_to_a_network(name) - vms.sort() + in_use, used_by_vms, used_by_tmpls =3D self._is_network_in_use(nam= e) + vms =3D 'N/A' if len(used_by_vms) =3D=3D 0 else ', '.join(used_by_= vms) + tmpls =3D 'N/A' if len(used_by_tmpls) =3D=3D 0 else ', '.join(used= _by_tmpls) + if in_use: raise InvalidOperation("KCHNET0018E", {'name': name, - 'vms': ', '.join(vms)}) + 'vms': vms, + 'tmpls': tmpls}) =20 network =3D self.get_network(self.conn.get(), name) network.destroy() =20 def delete(self, name): - if self._is_network_in_use(name): - vms =3D self._get_vms_attach_to_a_network(name) - vms.sort() + in_use, used_by_vms, used_by_tmpls =3D self._is_network_in_use(nam= e) + vms =3D 'N/A' if len(used_by_vms) =3D=3D 0 else ', '.join(used_by_= vms) + tmpls =3D 'N/A' if len(used_by_tmpls) =3D=3D 0 else ', '.join(used= _by_tmpls) + if in_use: raise InvalidOperation("KCHNET0017E", {'name': name, - 'vms': ', '.join(vms)}) + 'vms': vms, + 'tmpls': tmpls}) =20 network =3D self.get_network(self.conn.get(), name) if network.isActive(): --=20 2.9.3 _______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel