Ovirt vnic profile fix backport (#58058)

* Ovirt vnic profile fix (#57936)

* ovirt vnic profile fix init

* ovirt vnic profile fix init

* update syntax whitespace

* document pass_through

* create get entity method

* update example syntax

* update docs

* change force_create style

* add fragment
This commit is contained in:
Martin Nečas 2019-06-19 15:24:02 +02:00 committed by Toshio Kuratomi
parent 4a7a8ecf50
commit ab50e41a4c
2 changed files with 56 additions and 24 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Update ovirt vnic profile module BZ(https://bugzilla.redhat.com/show_bug.cgi?id=1597537)

View file

@ -66,6 +66,7 @@ options:
pass_through:
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."
choices: ['disabled', 'enabled']
migratable:
description:
@ -160,45 +161,62 @@ class EntityVnicPorfileModule(BaseModule):
def __init__(self, *args, **kwargs):
super(EntityVnicPorfileModule, self).__init__(*args, **kwargs)
def __get_dcs_service(self):
def _get_dcs_service(self):
return self._connection.system_service().data_centers_service()
def __get_dcs_id(self):
return get_id_by_name(self.__get_dcs_service(), self.param('data_center'))
def _get_dcs_id(self):
return get_id_by_name(self._get_dcs_service(), self.param('data_center'))
def __get_network_id(self):
networks_service = self.__get_dcs_service().service(self.__get_dcs_id()).networks_service()
def _get_network_id(self):
networks_service = self._get_dcs_service().service(self._get_dcs_id()).networks_service()
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()
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'))
def __get_network_filter_id(self):
def _get_network_filter_id(self):
nf_service = self._connection.system_service().network_filters_service()
return get_id_by_name(nf_service, self.param('network_filter')) if self.param('network_filter') else None
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':
network_filter = otypes.NetworkFilter()
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':
qos = otypes.Qos()
return qos
def _get_port_mirroring(self):
if self.param('pass_through') == 'enabled':
return False
return self.param('port_mirroring')
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,
port_mirroring=self.param('port_mirroring'),
pass_through=otypes.VnicPassThrough(mode=otypes.VnicPassThroughMode(self.param('pass_through')))
if self.param('pass_through') else None,
migratable=self.param('migratable'),
network=otypes.Network(id=self._get_network_id()),
description=self.param('description') if self.param('description') 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'),
regexp=cp.get('regexp'),
value=str(cp.get('value')),
) for cp in self.param('custom_properties') if cp
] if self.param('custom_properties') is not None else None,
qos=otypes.Qos(id=self.__get_qos_id())
if self.param('qos') else None,
network_filter=otypes.NetworkFilter(id=self.__get_network_filter_id())
if self.param('network_filter') is not None else None
] if self.param('custom_properties') else None,
qos=self._get_qos(),
port_mirroring=self._get_port_mirroring(),
network_filter=self._get_network_filter()
)
def update_check(self, entity):
@ -214,7 +232,7 @@ class EntityVnicPorfileModule(BaseModule):
return (
check_custom_properties() and
equal(self.param('migratable'), getattr(entity, 'migratable', None)) and
equal(self.param('pass_through'), entity.pass_through.mode.name) and
equal(self.param('pass_through'), getattr(entity.pass_through.mode, 'name', 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'), entity.qos.name) and
@ -222,6 +240,15 @@ class EntityVnicPorfileModule(BaseModule):
)
def get_entity(vnic_services, entitynics_module):
vnic_profiles = vnic_services.list()
network_id = entitynics_module._get_network_id()
for vnic in vnic_profiles:
# When vNIC already exist update it, when not create it
if vnic.name == entitynics_module.param('name') and network_id == vnic.network.id:
return vnic
def main():
argument_spec = ovirt_full_argument_spec(
state=dict(type='str', default='present', choices=['absent', 'present']),
@ -254,11 +281,14 @@ def main():
service=vnic_services,
)
state = module.params['state']
entity = get_entity(vnic_services, entitynics_module)
if state == 'present':
ret = entitynics_module.create()
ret = entitynics_module.create(entity=entity, force_create=entity is None)
elif state == 'absent':
ret = entitynics_module.remove()
if entity is not None:
ret = entitynics_module.remove(entity=entity)
else:
raise Exception("Vnic profile '%s' in network '%s' was not found." % (module.params['name'], module.params['network']))
module.exit_json(**ret)
except Exception as e:
module.fail_json(msg=str(e), exception=traceback.format_exc())