Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions plugins/module_utils/nic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,31 @@

__metaclass__ = type

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):
Expand Down Expand Up @@ -56,26 +78,14 @@ 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.
try:
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"]
Expand All @@ -88,7 +98,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)
Expand Down
32 changes: 0 additions & 32 deletions plugins/module_utils/type.py

This file was deleted.

3 changes: 1 addition & 2 deletions plugins/module_utils/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/plugins/module_utils/test_nic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
}
Expand All @@ -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"],
}
Expand All @@ -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"],
}
Expand All @@ -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"],
}
Expand All @@ -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"],
}
Expand All @@ -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"],
}
Expand All @@ -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"],
}
)
Expand All @@ -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"],
}
)
Expand All @@ -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"],
}
)
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/plugins/module_utils/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"],
Expand All @@ -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"],
Expand Down Expand Up @@ -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"],
Expand All @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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 = {
Expand All @@ -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)
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/plugins/modules/test_vm_nic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down