From 035c348ffec991d91581c793eb1300bce26ccc1f Mon Sep 17 00:00:00 2001 From: Justin Cinkelj Date: Thu, 30 Mar 2023 09:28:45 +0200 Subject: [PATCH 1/2] Use NicType.hypercore_to_ansible also in Nic class Signed-off-by: Justin Cinkelj --- plugins/module_utils/nic.py | 17 +++------------- tests/unit/plugins/module_utils/test_nic.py | 20 +++++++++---------- tests/unit/plugins/module_utils/test_vm.py | 22 ++++++++++----------- tests/unit/plugins/modules/test_vm_nic.py | 3 ++- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/plugins/module_utils/nic.py b/plugins/module_utils/nic.py index 7be485e62..4633c546d 100644 --- a/plugins/module_utils/nic.py +++ b/plugins/module_utils/nic.py @@ -8,6 +8,7 @@ __metaclass__ = type +from ..module_utils.type import NicType from ..module_utils.utils import PayloadMapper from ..module_utils import errors @@ -56,18 +57,6 @@ def __eq__(self, other): ) return self.vlan == other.vlan and self.type == other.type - @classmethod - def _handle_nic_type(cls, nic_type): - if nic_type: - if nic_type.upper() == "INTEL_E1000": - actual_nic_type = nic_type.upper() # INTEL_E1000 - elif nic_type.upper() == "VIRTIO": - actual_nic_type = nic_type.lower() # virtio - else: - actual_nic_type = nic_type.upper() # RTL8139 - return actual_nic_type - return nic_type - @classmethod def from_hypercore(cls, hypercore_data): # If exception is thrown, there has been a change in the API or a big problem on their side. @@ -75,7 +64,7 @@ def from_hypercore(cls, hypercore_data): obj = Nic() obj.uuid = hypercore_data["uuid"] obj.vm_uuid = hypercore_data["virDomainUUID"] - obj.type = Nic._handle_nic_type(hypercore_data["type"]) + obj.type = NicType.hypercore_to_ansible(hypercore_data["type"]) obj.mac = hypercore_data["macAddress"] obj.vlan = hypercore_data["vlan"] obj.connected = hypercore_data["connected"] @@ -88,7 +77,7 @@ def from_hypercore(cls, hypercore_data): def from_ansible(cls, ansible_data): obj = Nic() obj.vm_uuid = ansible_data.get("vm_uuid", None) - obj.type = Nic._handle_nic_type(ansible_data.get("type", None)) + obj.type = ansible_data.get("type", None) obj.mac = ansible_data.get("mac", None) obj.mac_new = ansible_data.get("mac_new", None) obj.vlan = ansible_data.get("vlan", 0) diff --git a/tests/unit/plugins/module_utils/test_nic.py b/tests/unit/plugins/module_utils/test_nic.py index 26a37db33..38f25bc04 100644 --- a/tests/unit/plugins/module_utils/test_nic.py +++ b/tests/unit/plugins/module_utils/test_nic.py @@ -65,7 +65,7 @@ def _get_nic_1(cls): "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], @@ -123,7 +123,7 @@ def test_compare_same(self): "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, "connected": True, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } @@ -146,7 +146,7 @@ def test_compare_different(self): "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, "connected": True, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } @@ -168,7 +168,7 @@ def test_compare_vlan_new_other(self): "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, "connected": True, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } @@ -193,7 +193,7 @@ def test_compare_vlan_new_self(self): "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, "connected": True, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } @@ -216,7 +216,7 @@ def test_compare_mac_new(self): "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, "connected": True, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } @@ -239,7 +239,7 @@ def test_compare_mac_new_and_vlan_new(self): "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, "connected": True, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } @@ -264,7 +264,7 @@ def test_compare_vlan_new_same(self): "vlan": 1, "macAddress": "12:34:56:78:AB", "connected": True, - "type": "virtio", + "type": "VIRTIO", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } ) @@ -287,7 +287,7 @@ def test_compare_mac_new_same(self): "vlan": 1, "macAddress": "12:34:56:78:AB", "connected": True, - "type": "virtio", + "type": "VIRTIO", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } ) @@ -310,7 +310,7 @@ def test_compare_mac_new_and_vlan_new_same(self): "vlan": 2, "macAddress": "12:34:56:78:AB", "connected": True, - "type": "virtio", + "type": "VIRTIO", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } ) diff --git a/tests/unit/plugins/module_utils/test_vm.py b/tests/unit/plugins/module_utils/test_vm.py index 2ee53518b..cc4baaf23 100644 --- a/tests/unit/plugins/module_utils/test_vm.py +++ b/tests/unit/plugins/module_utils/test_vm.py @@ -819,7 +819,7 @@ def _get_test_vm(cls, rest_client, mocker): "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "macAddress": "12-34-56-78-AB", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], "connected": True, @@ -935,7 +935,7 @@ def test_delete_unused_nics_to_hypercore_vm_when_one_nic_deleted( "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "macAddress": "12-34-56-78-CD", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], "connected": True, @@ -1005,7 +1005,7 @@ def test_delete_unused_nics_to_hypercore_vm_when_multiple_nic_deleted( "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], @@ -1014,7 +1014,7 @@ def test_delete_unused_nics_to_hypercore_vm_when_multiple_nic_deleted( "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "8542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 2, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], @@ -3692,7 +3692,7 @@ def _get_nic_1(cls): "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], @@ -3705,7 +3705,7 @@ def _get_nic_1_dict(cls): uuid="6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", virDomainUUID="7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", vlan=1, - type="virtio", + type="VIRTIO", macAddress="00-00-00-00-00", connected=True, ipv4Addresses=["10.0.0.1", "10.0.0.2"], @@ -3774,7 +3774,7 @@ def test_send_update_nic_to_hypercore(self, rest_client, create_module, mocker): "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], @@ -3847,7 +3847,7 @@ def test_update_nic_when_one_nic_updated(self, rest_client, create_module, mocke "vlan_new": 3, "macAddress": "12:34:56:78:AB", "connected": True, - "type": "virtio", + "type": "VIRTIO", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } existing_nic = { @@ -3856,7 +3856,7 @@ def test_update_nic_when_one_nic_updated(self, rest_client, create_module, mocke "vlan": 1, "macAddress": "12:34:56:78:AB", "connected": True, - "type": "virtio", + "type": "VIRTIO", "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], } new_nic_obj = Nic.from_hypercore(hypercore_data=new_nic) @@ -3913,7 +3913,7 @@ def test_send_create_nic_to_hypercore(self, rest_client, create_module, mocker): "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "macAddress": "00-00-00-00-00", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], @@ -4025,7 +4025,7 @@ def _get_test_vm(cls): "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], "macAddress": "00-00-00-00-00", diff --git a/tests/unit/plugins/modules/test_vm_nic.py b/tests/unit/plugins/modules/test_vm_nic.py index fe86bc75b..9d5437737 100644 --- a/tests/unit/plugins/modules/test_vm_nic.py +++ b/tests/unit/plugins/modules/test_vm_nic.py @@ -51,11 +51,12 @@ def _get_empty_test_vm(cls): @classmethod def _get_test_vm(cls): + # Returned value is mock HyperCore API response (not ansible dict). nic_dict_1 = { "uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab", "virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg", "vlan": 1, - "type": "virtio", + "type": "VIRTIO", "connected": True, "ipv4Addresses": ["10.0.0.1", "10.0.0.2"], "macAddress": "00-00-00-00-00", From 30009487e7a5f7c1209d56c7f6596b051c638562 Mon Sep 17 00:00:00 2001 From: Justin Cinkelj Date: Thu, 30 Mar 2023 14:52:36 +0200 Subject: [PATCH 2/2] Move NicType to nic.py Signed-off-by: Justin Cinkelj --- plugins/module_utils/nic.py | 23 ++++++++++++++++++++++- plugins/module_utils/type.py | 32 -------------------------------- plugins/module_utils/vm.py | 3 +-- 3 files changed, 23 insertions(+), 35 deletions(-) delete mode 100644 plugins/module_utils/type.py diff --git a/plugins/module_utils/nic.py b/plugins/module_utils/nic.py index 4633c546d..215522c99 100644 --- a/plugins/module_utils/nic.py +++ b/plugins/module_utils/nic.py @@ -8,10 +8,31 @@ __metaclass__ = type -from ..module_utils.type import NicType +from typing import Optional from ..module_utils.utils import PayloadMapper from ..module_utils import errors +FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE = { + None: None, + "RTL8139": "RTL8139", + "VIRTIO": "virtio", + "INTEL_E1000": "INTEL_E1000", +} +FROM_ANSIBLE_TO_HYPERCORE_NIC_TYPE = { + v: k for k, v in FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE.items() +} + + +# Maybe create enums.py or scale_enums.py and move all enum classes there? @Jure @Justin +class NicType: + @classmethod + def ansible_to_hypercore(cls, ansible_value: Optional[str]) -> Optional[str]: + return FROM_ANSIBLE_TO_HYPERCORE_NIC_TYPE[ansible_value] + + @classmethod + def hypercore_to_ansible(cls, hypercore_value: str) -> Optional[str]: + return FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE[hypercore_value] + class Nic(PayloadMapper): def __init__(self): diff --git a/plugins/module_utils/type.py b/plugins/module_utils/type.py deleted file mode 100644 index fa1b3c4fa..000000000 --- a/plugins/module_utils/type.py +++ /dev/null @@ -1,32 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright: (c) 2022, XLAB Steampunk -# -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - - -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -from typing import Optional - -FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE = { - None: None, - "RTL8139": "RTL8139", - "VIRTIO": "virtio", - "INTEL_E1000": "INTEL_E1000", -} -FROM_ANSIBLE_TO_HYPERCORE_NIC_TYPE = { - v: k for k, v in FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE.items() -} - - -# Maybe create enums.py or scale_enums.py and move all enum classes there? @Jure @Justin -class NicType: - @classmethod - def ansible_to_hypercore(cls, ansible_value: Optional[str]) -> Optional[str]: - return FROM_ANSIBLE_TO_HYPERCORE_NIC_TYPE[ansible_value] - - @classmethod - def hypercore_to_ansible(cls, hypercore_value: str) -> Optional[str]: - return FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE[hypercore_value] diff --git a/plugins/module_utils/vm.py b/plugins/module_utils/vm.py index 56650985e..49ed8d62f 100644 --- a/plugins/module_utils/vm.py +++ b/plugins/module_utils/vm.py @@ -12,7 +12,7 @@ from time import sleep, time from ..module_utils.errors import DeviceNotUnique -from ..module_utils.nic import Nic +from ..module_utils.nic import Nic, NicType from ..module_utils.disk import Disk from ..module_utils.node import Node from ..module_utils.iso import ISO @@ -30,7 +30,6 @@ from ..module_utils.task_tag import TaskTag from ..module_utils import errors from ..module_utils.snapshot_schedule import SnapshotSchedule -from ..module_utils.type import NicType # FROM_ANSIBLE_TO_HYPERCORE_POWER_STATE and FROM_HYPERCORE_TO_ANSIBLE_POWER_STATE are mappings for how # states are stored in python/ansible and how are they stored in hypercore