diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_vnic_profile.py b/lib/ansible/modules/cloud/ovirt/ovirt_vnic_profile.py index 3d8ef78205c..f2f37a73978 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_vnic_profile.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_vnic_profile.py @@ -67,6 +67,7 @@ options: description: - "Enables passthrough to an SR-IOV-enabled host NIC." - "When enabled C(qos) and C(network_filter) are automatically set to None and C(port_mirroring) to False." + - "Port mirroring, QoS and network filters are not supported on passthrough profiles." choices: ['disabled', 'enabled'] migratable: description: @@ -170,8 +171,10 @@ class EntityVnicPorfileModule(BaseModule): return get_id_by_name(networks_service, self.param('network')) def _get_qos_id(self): - qoss_service = self._get_dcs_service().service(self._get_dcs_id()).qoss_service() - return get_id_by_name(qoss_service, self.param('qos')) + if self.param('qos'): + qoss_service = self._get_dcs_service().service(self._get_dcs_id()).qoss_service() + return get_id_by_name(qoss_service, self.param('qos')) if self.param('qos') else None + return None def _get_network_filter_id(self): nf_service = self._connection.system_service().network_filters_service() @@ -179,18 +182,21 @@ class EntityVnicPorfileModule(BaseModule): def _get_network_filter(self): network_filter = None - if self.param('network_filter'): - network_filter = otypes.NetworkFilter(id=self._get_network_filter_id()) - elif self.param('network_filter') == '' or self.param('pass_through') == 'enabled': + # The order of these condition is necessary. + # When would network_filter and pass_through specified it would try to create and network_filter and fail on engine. + if self.param('network_filter') == '' or self.param('pass_through') == 'enabled': network_filter = otypes.NetworkFilter() + elif self.param('network_filter'): + network_filter = otypes.NetworkFilter(id=self._get_network_filter_id()) return network_filter def _get_qos(self): qos = None - if self.param('qos'): - qos = otypes.Qos(id=self._get_qos_id()) - elif self.param('qos') == '' or self.param('pass_through') == 'enabled': + # The order of these condition is necessary. When would qos and pass_through specified it would try to create and qos and fail on engine. + if self.param('qos') == '' or self.param('pass_through') == 'enabled': qos = otypes.Qos() + elif self.param('qos'): + qos = otypes.Qos(id=self._get_qos_id()) return qos def _get_port_mirroring(self): @@ -198,13 +204,18 @@ class EntityVnicPorfileModule(BaseModule): return False return self.param('port_mirroring') + def _get_migratable(self): + if self.param('migratable') is not None: + return self.param('migratable') + if self.param('pass_through') == 'enabled': + return True + def build_entity(self): return otypes.VnicProfile( name=self.param('name'), network=otypes.Network(id=self._get_network_id()), - description=self.param('description') if self.param('description') else None, + description=self.param('description') if self.param('description') is not None else None, pass_through=otypes.VnicPassThrough(mode=otypes.VnicPassThroughMode(self.param('pass_through'))) if self.param('pass_through') else None, - migratable=self.param('migratable') if self.param('migratable') else None, custom_properties=[ otypes.CustomProperty( name=cp.get('name'), @@ -212,6 +223,7 @@ class EntityVnicPorfileModule(BaseModule): value=str(cp.get('value')), ) for cp in self.param('custom_properties') if cp ] if self.param('custom_properties') else None, + migratable=self._get_migratable(), qos=self._get_qos(), port_mirroring=self._get_port_mirroring(), network_filter=self._get_network_filter() @@ -227,13 +239,14 @@ class EntityVnicPorfileModule(BaseModule): return sorted(current) == sorted(passed) return True + pass_through = getattr(entity.pass_through.mode, 'name', None) return ( check_custom_properties() and + self._get_network_filter_id() == getattr(entity.network_filter, 'id', None) and + self._get_qos_id() == getattr(entity.qos, 'id', None) and equal(self.param('migratable'), getattr(entity, 'migratable', None)) and - equal(self.param('pass_through'), getattr(entity.pass_through.mode, 'name', None)) and + equal(self.param('pass_through'), pass_through.lower() if pass_through else None) and equal(self.param('description'), entity.description) and - equal(self.param('network_filter'), getattr(entity.network_filter, 'name', None)) and - equal(self.param('qos'), getattr(entity.qos, 'name', None)) and equal(self.param('port_mirroring'), getattr(entity, 'port_mirroring', None)) ) @@ -247,6 +260,12 @@ def get_entity(vnic_services, entitynics_module): return vnic +def check_params(module): + if (module.params.get('port_mirroring') or module.params.get('network_filter') or module.params.get('qos'))\ + and module.params.get('pass_through') == 'enabled': + module.fail_json(msg="Cannot edit VM network interface profile. 'Port Mirroring,'Qos' and 'Network Filter' are not supported on passthrough profiles.") + + def main(): argument_spec = ovirt_full_argument_spec( state=dict(type='str', default='present', choices=['absent', 'present']), @@ -267,6 +286,7 @@ def main(): ) check_sdk(module) + check_params(module) try: auth = module.params.pop('auth') connection = create_connection(auth)