Skip to content

Commit e52077e

Browse files
authored
Refactor Nic._handle_nic_type method (#188)
Refactor NicType class Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
1 parent 784e7c2 commit e52077e

File tree

6 files changed

+48
-70
lines changed

6 files changed

+48
-70
lines changed

plugins/module_utils/nic.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,31 @@
88

99
__metaclass__ = type
1010

11+
from typing import Optional
1112
from ..module_utils.utils import PayloadMapper
1213
from ..module_utils import errors
1314

15+
FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE = {
16+
None: None,
17+
"RTL8139": "RTL8139",
18+
"VIRTIO": "virtio",
19+
"INTEL_E1000": "INTEL_E1000",
20+
}
21+
FROM_ANSIBLE_TO_HYPERCORE_NIC_TYPE = {
22+
v: k for k, v in FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE.items()
23+
}
24+
25+
26+
# Maybe create enums.py or scale_enums.py and move all enum classes there? @Jure @Justin
27+
class NicType:
28+
@classmethod
29+
def ansible_to_hypercore(cls, ansible_value: Optional[str]) -> Optional[str]:
30+
return FROM_ANSIBLE_TO_HYPERCORE_NIC_TYPE[ansible_value]
31+
32+
@classmethod
33+
def hypercore_to_ansible(cls, hypercore_value: str) -> Optional[str]:
34+
return FROM_HYPERCORE_TO_ANSIBLE_NIC_TYPE[hypercore_value]
35+
1436

1537
class Nic(PayloadMapper):
1638
def __init__(self):
@@ -56,26 +78,14 @@ def __eq__(self, other):
5678
)
5779
return self.vlan == other.vlan and self.type == other.type
5880

59-
@classmethod
60-
def _handle_nic_type(cls, nic_type):
61-
if nic_type:
62-
if nic_type.upper() == "INTEL_E1000":
63-
actual_nic_type = nic_type.upper() # INTEL_E1000
64-
elif nic_type.upper() == "VIRTIO":
65-
actual_nic_type = nic_type.lower() # virtio
66-
else:
67-
actual_nic_type = nic_type.upper() # RTL8139
68-
return actual_nic_type
69-
return nic_type
70-
7181
@classmethod
7282
def from_hypercore(cls, hypercore_data):
7383
# If exception is thrown, there has been a change in the API or a big problem on their side.
7484
try:
7585
obj = Nic()
7686
obj.uuid = hypercore_data["uuid"]
7787
obj.vm_uuid = hypercore_data["virDomainUUID"]
78-
obj.type = Nic._handle_nic_type(hypercore_data["type"])
88+
obj.type = NicType.hypercore_to_ansible(hypercore_data["type"])
7989
obj.mac = hypercore_data["macAddress"]
8090
obj.vlan = hypercore_data["vlan"]
8191
obj.connected = hypercore_data["connected"]
@@ -88,7 +98,7 @@ def from_hypercore(cls, hypercore_data):
8898
def from_ansible(cls, ansible_data):
8999
obj = Nic()
90100
obj.vm_uuid = ansible_data.get("vm_uuid", None)
91-
obj.type = Nic._handle_nic_type(ansible_data.get("type", None))
101+
obj.type = ansible_data.get("type", None)
92102
obj.mac = ansible_data.get("mac", None)
93103
obj.mac_new = ansible_data.get("mac_new", None)
94104
obj.vlan = ansible_data.get("vlan", 0)

plugins/module_utils/type.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

plugins/module_utils/vm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from time import sleep, time
1313

1414
from ..module_utils.errors import DeviceNotUnique
15-
from ..module_utils.nic import Nic
15+
from ..module_utils.nic import Nic, NicType
1616
from ..module_utils.disk import Disk
1717
from ..module_utils.node import Node
1818
from ..module_utils.iso import ISO
@@ -30,7 +30,6 @@
3030
from ..module_utils.task_tag import TaskTag
3131
from ..module_utils import errors
3232
from ..module_utils.snapshot_schedule import SnapshotSchedule
33-
from ..module_utils.type import NicType
3433

3534
# FROM_ANSIBLE_TO_HYPERCORE_POWER_STATE and FROM_HYPERCORE_TO_ANSIBLE_POWER_STATE are mappings for how
3635
# states are stored in python/ansible and how are they stored in hypercore

tests/unit/plugins/module_utils/test_nic.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _get_nic_1(cls):
6565
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
6666
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
6767
"vlan": 1,
68-
"type": "virtio",
68+
"type": "VIRTIO",
6969
"macAddress": "00-00-00-00-00",
7070
"connected": True,
7171
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
@@ -123,7 +123,7 @@ def test_compare_same(self):
123123
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
124124
"vlan": 1,
125125
"connected": True,
126-
"type": "virtio",
126+
"type": "VIRTIO",
127127
"macAddress": "00-00-00-00-00",
128128
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
129129
}
@@ -146,7 +146,7 @@ def test_compare_different(self):
146146
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
147147
"vlan": 1,
148148
"connected": True,
149-
"type": "virtio",
149+
"type": "VIRTIO",
150150
"macAddress": "00-00-00-00-00",
151151
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
152152
}
@@ -168,7 +168,7 @@ def test_compare_vlan_new_other(self):
168168
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
169169
"vlan": 1,
170170
"connected": True,
171-
"type": "virtio",
171+
"type": "VIRTIO",
172172
"macAddress": "00-00-00-00-00",
173173
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
174174
}
@@ -193,7 +193,7 @@ def test_compare_vlan_new_self(self):
193193
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
194194
"vlan": 1,
195195
"connected": True,
196-
"type": "virtio",
196+
"type": "VIRTIO",
197197
"macAddress": "00-00-00-00-00",
198198
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
199199
}
@@ -216,7 +216,7 @@ def test_compare_mac_new(self):
216216
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
217217
"vlan": 1,
218218
"connected": True,
219-
"type": "virtio",
219+
"type": "VIRTIO",
220220
"macAddress": "00-00-00-00-00",
221221
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
222222
}
@@ -239,7 +239,7 @@ def test_compare_mac_new_and_vlan_new(self):
239239
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
240240
"vlan": 1,
241241
"connected": True,
242-
"type": "virtio",
242+
"type": "VIRTIO",
243243
"macAddress": "00-00-00-00-00",
244244
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
245245
}
@@ -264,7 +264,7 @@ def test_compare_vlan_new_same(self):
264264
"vlan": 1,
265265
"macAddress": "12:34:56:78:AB",
266266
"connected": True,
267-
"type": "virtio",
267+
"type": "VIRTIO",
268268
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
269269
}
270270
)
@@ -287,7 +287,7 @@ def test_compare_mac_new_same(self):
287287
"vlan": 1,
288288
"macAddress": "12:34:56:78:AB",
289289
"connected": True,
290-
"type": "virtio",
290+
"type": "VIRTIO",
291291
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
292292
}
293293
)
@@ -310,7 +310,7 @@ def test_compare_mac_new_and_vlan_new_same(self):
310310
"vlan": 2,
311311
"macAddress": "12:34:56:78:AB",
312312
"connected": True,
313-
"type": "virtio",
313+
"type": "VIRTIO",
314314
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
315315
}
316316
)

tests/unit/plugins/module_utils/test_vm.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ def _get_test_vm(cls, rest_client, mocker):
819819
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
820820
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
821821
"vlan": 1,
822-
"type": "virtio",
822+
"type": "VIRTIO",
823823
"macAddress": "12-34-56-78-AB",
824824
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
825825
"connected": True,
@@ -935,7 +935,7 @@ def test_delete_unused_nics_to_hypercore_vm_when_one_nic_deleted(
935935
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
936936
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
937937
"vlan": 1,
938-
"type": "virtio",
938+
"type": "VIRTIO",
939939
"macAddress": "12-34-56-78-CD",
940940
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
941941
"connected": True,
@@ -1005,7 +1005,7 @@ def test_delete_unused_nics_to_hypercore_vm_when_multiple_nic_deleted(
10051005
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
10061006
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
10071007
"vlan": 1,
1008-
"type": "virtio",
1008+
"type": "VIRTIO",
10091009
"macAddress": "00-00-00-00-00",
10101010
"connected": True,
10111011
"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(
10141014
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
10151015
"virDomainUUID": "8542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
10161016
"vlan": 2,
1017-
"type": "virtio",
1017+
"type": "VIRTIO",
10181018
"macAddress": "00-00-00-00-00",
10191019
"connected": True,
10201020
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
@@ -3692,7 +3692,7 @@ def _get_nic_1(cls):
36923692
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
36933693
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
36943694
"vlan": 1,
3695-
"type": "virtio",
3695+
"type": "VIRTIO",
36963696
"macAddress": "00-00-00-00-00",
36973697
"connected": True,
36983698
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
@@ -3705,7 +3705,7 @@ def _get_nic_1_dict(cls):
37053705
uuid="6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
37063706
virDomainUUID="7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
37073707
vlan=1,
3708-
type="virtio",
3708+
type="VIRTIO",
37093709
macAddress="00-00-00-00-00",
37103710
connected=True,
37113711
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):
37743774
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
37753775
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
37763776
"vlan": 1,
3777-
"type": "virtio",
3777+
"type": "VIRTIO",
37783778
"macAddress": "00-00-00-00-00",
37793779
"connected": True,
37803780
"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
38473847
"vlan_new": 3,
38483848
"macAddress": "12:34:56:78:AB",
38493849
"connected": True,
3850-
"type": "virtio",
3850+
"type": "VIRTIO",
38513851
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
38523852
}
38533853
existing_nic = {
@@ -3856,7 +3856,7 @@ def test_update_nic_when_one_nic_updated(self, rest_client, create_module, mocke
38563856
"vlan": 1,
38573857
"macAddress": "12:34:56:78:AB",
38583858
"connected": True,
3859-
"type": "virtio",
3859+
"type": "VIRTIO",
38603860
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
38613861
}
38623862
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):
39133913
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
39143914
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
39153915
"vlan": 1,
3916-
"type": "virtio",
3916+
"type": "VIRTIO",
39173917
"macAddress": "00-00-00-00-00",
39183918
"connected": True,
39193919
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
@@ -4025,7 +4025,7 @@ def _get_test_vm(cls):
40254025
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
40264026
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
40274027
"vlan": 1,
4028-
"type": "virtio",
4028+
"type": "VIRTIO",
40294029
"connected": True,
40304030
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
40314031
"macAddress": "00-00-00-00-00",

tests/unit/plugins/modules/test_vm_nic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ def _get_empty_test_vm(cls):
5151

5252
@classmethod
5353
def _get_test_vm(cls):
54+
# Returned value is mock HyperCore API response (not ansible dict).
5455
nic_dict_1 = {
5556
"uuid": "6756f2hj-6u9a-90ff-6g91-7jeahgf47aab",
5657
"virDomainUUID": "7542f2gg-5f9a-51ff-8a91-8ceahgf47ghg",
5758
"vlan": 1,
58-
"type": "virtio",
59+
"type": "VIRTIO",
5960
"connected": True,
6061
"ipv4Addresses": ["10.0.0.1", "10.0.0.2"],
6162
"macAddress": "00-00-00-00-00",

0 commit comments

Comments
 (0)