Skip to content

Commit 8785ab1

Browse files
committed
Use NicType.hypercore_to_ansible also in Nic class
Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
1 parent 59a489a commit 8785ab1

File tree

4 files changed

+26
-36
lines changed

4 files changed

+26
-36
lines changed

plugins/module_utils/nic.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
__metaclass__ = type
1010

11+
from ..module_utils.type import NicType
1112
from ..module_utils.utils import PayloadMapper
1213
from ..module_utils import errors
1314

@@ -56,26 +57,14 @@ def __eq__(self, other):
5657
)
5758
return self.vlan == other.vlan and self.type == other.type
5859

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-
7160
@classmethod
7261
def from_hypercore(cls, hypercore_data):
7362
# If exception is thrown, there has been a change in the API or a big problem on their side.
7463
try:
7564
obj = Nic()
7665
obj.uuid = hypercore_data["uuid"]
7766
obj.vm_uuid = hypercore_data["virDomainUUID"]
78-
obj.type = Nic._handle_nic_type(hypercore_data["type"])
67+
obj.type = NicType.hypercore_to_ansible(hypercore_data["type"])
7968
obj.mac = hypercore_data["macAddress"]
8069
obj.vlan = hypercore_data["vlan"]
8170
obj.connected = hypercore_data["connected"]
@@ -88,7 +77,7 @@ def from_hypercore(cls, hypercore_data):
8877
def from_ansible(cls, ansible_data):
8978
obj = Nic()
9079
obj.vm_uuid = ansible_data.get("vm_uuid", None)
91-
obj.type = Nic._handle_nic_type(ansible_data.get("type", None))
80+
obj.type = ansible_data.get("type", None)
9281
obj.mac = ansible_data.get("mac", None)
9382
obj.mac_new = ansible_data.get("mac_new", None)
9483
obj.vlan = ansible_data.get("vlan", 0)

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)