From 5d00d4d85e86721896f43d69d62abd48d22193b2 Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Wed, 18 Jan 2017 16:44:02 +0100 Subject: [PATCH] cloud: ovirt_vms: Filter VNIC profile by cluster (#20246) (#20384) This patch add additional filtering of VNIC profiles by the cluster parameter. It is a must, because there could be same names of the VNIC profiles in system, as every datacenter can have VNIC profile same name, which can be in other datacenter. This patch fixes issue #20246 --- lib/ansible/modules/cloud/ovirt/ovirt_vms.py | 33 +++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_vms.py b/lib/ansible/modules/cloud/ovirt/ovirt_vms.py index ff139122e39..6e5e16f4a8a 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_vms.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_vms.py @@ -643,6 +643,34 @@ class VmsModule(BaseModule): ) self.changed = True + def __get_vnic_profile_id(self, nic): + """ + Return VNIC profile ID looked up by it's name, because there can be + more VNIC profiles with same name, other criteria of filter is cluster. + """ + vnics_service = self._connection.system_service().vnic_profiles_service() + clusters_service = self._connection.system_service().clusters_service() + cluster = search_by_name(clusters_service, self.param('cluster')) + profiles = [ + profile for profile in vnics_service.list() + if profile.name == nic.get('profile_name') + ] + cluster_networks = [ + net.id for net in self._connection.follow_link(cluster.networks) + ] + try: + return next( + profile.id for profile in profiles + if profile.network.id in cluster_networks + ) + except StopIteration: + raise Exception( + "Profile '%s' was not found in cluster '%s'" % ( + nic.get('profile_name'), + self.param('cluster') + ) + ) + def __attach_nics(self, entity): # Attach NICs to VM, if specified: vnic_profiles_service = self._connection.system_service().vnic_profiles_service() @@ -657,10 +685,7 @@ class VmsModule(BaseModule): nic.get('interface', 'virtio') ), vnic_profile=otypes.VnicProfile( - id=search_by_name( - vnic_profiles_service, - nic.get('profile_name'), - ).id + id=self.__get_vnic_profile_id(nic), ) if nic.get('profile_name') else None, mac=otypes.Mac( address=nic.get('mac_address')