Skip to content

Commit 13c6fe8

Browse files
bug_fix: Fixes issue with multiple results when attempting to create vm intfs (#41)
1 parent 3d98921 commit 13c6fe8

File tree

5 files changed

+138
-22
lines changed

5 files changed

+138
-22
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
# Changelog
22

3+
## v0.1.1
4+
5+
### Bug Fixes
6+
7+
- [#40](https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#id7) - Fixed issue with netbox_vm_interface where it would fail if different virtual machine had the same interface name
8+
- [#40](https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#id7) - Updated netbox_ip_address to find interfaces on virtual machines correctly
9+
310
## v0.1.0
411

512
### Breaking Changes
13+
614
- [#9](https://github.com/FragmentedPacket/netbox_modules/issues/9) - Changed role to prefix_role in netbox_prefix.py
715
- [#9](https://github.com/FragmentedPacket/netbox_modules/issues/9) - Changed group to tenant_group in netbox_tenant.py
816
- [#9](https://github.com/FragmentedPacket/netbox_modules/issues/9) - Renamed netbox_interface to netbox_device_interface
917
- [#24](https://github.com/FragmentedPacket/netbox_modules/issues/24) - Module failures when required fields arent provided
1018

1119
### New Modules / Plugins
20+
1221
- [#9](https://github.com/FragmentedPacket/netbox_modules/issues/9) - Added netbox_device_role
1322
- [#9](https://github.com/FragmentedPacket/netbox_modules/issues/9) - Added netbox_device_type
1423
- [#9](https://github.com/FragmentedPacket/netbox_modules/issues/9) - Added netbox_ipam_role
@@ -40,4 +49,5 @@
4049
### Bug Fixes
4150

4251
### Enhancements
52+
4353
- [#10](https://github.com/FragmentedPacket/netbox_modules/issues/10) - Add primary_ip4/6 to netbox_ip_address

plugins/module_utils/netbox_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
"device_role": set(["slug"]),
298298
"device_type": set(["slug"]),
299299
"installed_device": set(["name"]),
300-
"interface": set(["name", "device"]),
300+
"interface": set(["name", "device", "virtual_machine"]),
301301
"inventory_item": set(["name", "device"]),
302302
"ip_address": set(["address", "vrf"]),
303303
"ip_addresses": set(["address", "vrf", "device"]),
@@ -337,9 +337,10 @@
337337
"rir",
338338
"vrf",
339339
"site",
340-
"vlan_group",
341340
"tenant",
342341
"type",
342+
"vlan_group",
343+
"virtual_machine",
343344
]
344345
)
345346

@@ -578,6 +579,9 @@ def _find_ids(self, data):
578579
nb_endpoint = getattr(nb_app, endpoint)
579580

580581
if isinstance(v, dict):
582+
if k == "interface" and v.get("virtual_machine"):
583+
nb_app = getattr(self.nb, "virtualization")
584+
nb_endpoint = getattr(nb_app, endpoint)
581585
query_params = self._build_query_params(k, data, v)
582586
query_id = nb_endpoint.get(**query_params)
583587

tests/integration/integration-tests.yml

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@
313313
- test_five['interface']['enabled'] == false
314314
- test_five['interface']['form_factor'] == 1000
315315
- test_five['interface']['mgmt_only'] == false
316-
- test_five['interface']['lag'] == 4
316+
- test_five['interface']['lag'] == 14
317317
- test_five['interface']['mode'] == 100
318318
- test_five['interface']['mtu'] == 1600
319319

@@ -664,6 +664,29 @@
664664
- not test_thirteen['changed']
665665
- test_thirteen['msg'] == "ip_address 192.168.100.2/24 already exists"
666666
- test_thirteen['ip_address']['address'] == "192.168.100.2/24"
667+
668+
- name: "14 - Create IP address on Eth0 - test100-vm - State: present"
669+
netbox_ip_address:
670+
netbox_url: http://localhost.org:32768
671+
netbox_token: 0123456789abcdef0123456789abcdef01234567
672+
data:
673+
family: 4
674+
address: 10.188.1.100/24
675+
interface:
676+
name: Eth0
677+
virtual_machine: test100-vm
678+
register: test_fourteen
679+
680+
- name: "14 - ASSERT"
681+
assert:
682+
that:
683+
- test_fourteen is changed
684+
- test_fourteen['diff']['before']['state'] == "absent"
685+
- test_fourteen['diff']['after']['state'] == "present"
686+
- test_fourteen['msg'] == "ip_address 10.188.1.100/24 created"
687+
- test_fourteen['ip_address']['address'] == "10.188.1.100/24"
688+
- test_fourteen['ip_address']['family'] == 4
689+
- test_fourteen['ip_address']['interface'] == 3
667690
##
668691
##
669692
### NETBOX_PREFIX
@@ -3443,7 +3466,7 @@
34433466
netbox_token: 0123456789abcdef0123456789abcdef01234567
34443467
data:
34453468
virtual_machine: "test100-vm"
3446-
name: "Eth0"
3469+
name: "Eth10"
34473470
state: present
34483471
register: test_one
34493472

@@ -3453,35 +3476,35 @@
34533476
- test_one is changed
34543477
- test_one['diff']['before']['state'] == "absent"
34553478
- test_one['diff']['after']['state'] == "present"
3456-
- test_one['interface']['name'] == "Eth0"
3479+
- test_one['interface']['name'] == "Eth10"
34573480
- test_one['interface']['virtual_machine'] == 1
3458-
- test_one['msg'] == "interface Eth0 created"
3481+
- test_one['msg'] == "interface Eth10 created"
34593482

34603483
- name: "NETBOX_VM_INTERFACE 2: Create duplicate"
34613484
netbox_vm_interface:
34623485
netbox_url: http://localhost:32768
34633486
netbox_token: 0123456789abcdef0123456789abcdef01234567
34643487
data:
34653488
virtual_machine: "test100-vm"
3466-
name: "Eth0"
3489+
name: "Eth10"
34673490
state: present
34683491
register: test_two
34693492

34703493
- name: "NETBOX_VM_INTERFACE 2: ASSERT - Create duplicate"
34713494
assert:
34723495
that:
34733496
- not test_two['changed']
3474-
- test_two['interface']['name'] == "Eth0"
3497+
- test_two['interface']['name'] == "Eth10"
34753498
- test_two['interface']['virtual_machine'] == 1
3476-
- test_two['msg'] == "interface Eth0 already exists"
3499+
- test_two['msg'] == "interface Eth10 already exists"
34773500

34783501
- name: "NETBOX_VM_INTERFACE 3: Updated"
34793502
netbox_vm_interface:
34803503
netbox_url: http://localhost:32768
34813504
netbox_token: 0123456789abcdef0123456789abcdef01234567
34823505
data:
34833506
virtual_machine: "test100-vm"
3484-
name: "Eth0"
3507+
name: "Eth10"
34853508
enabled: false
34863509
mtu: 9000
34873510
mac_address: "00:00:00:AA:AA:01"
@@ -3510,28 +3533,26 @@
35103533
- test_three['diff']['after']['description'] == "Updated test100-vm"
35113534
- test_three['diff']['after']['mode'] == 200
35123535
- test_three['diff']['after']['untagged_vlan'] == 1
3513-
- test_three['diff']['after']['tagged_vlans'][0] == 2
3514-
- test_three['diff']['after']['tagged_vlans'][1] == 3
3536+
- test_three['diff']['after']['tagged_vlans'] == [2, 3]
35153537
- test_three['diff']['after']['tags'][0] == "Schnozzberry"
3516-
- test_three['interface']['name'] == "Eth0"
3538+
- test_three['interface']['name'] == "Eth10"
35173539
- test_three['interface']['virtual_machine'] == 1
35183540
- test_three['interface']['enabled'] == false
35193541
- test_three['interface']['mtu'] == 9000
35203542
- test_three['interface']['mac_address'] == "00:00:00:AA:AA:01"
35213543
- test_three['interface']['description'] == "Updated test100-vm"
35223544
- test_three['interface']['mode'] == 200
35233545
- test_three['interface']['untagged_vlan'] == 1
3524-
- test_three['interface']['tagged_vlans'][0] == 2
3525-
- test_three['interface']['tagged_vlans'][1] == 3
3546+
- test_three['interface']['tagged_vlans'] == [2, 3]
35263547
- test_three['interface']['tags'][0] == "Schnozzberry"
3527-
- test_three['msg'] == "interface Eth0 updated"
3548+
- test_three['msg'] == "interface Eth10 updated"
35283549

35293550
- name: "NETBOX_VM_INTERFACE 4: ASSERT - Delete"
35303551
netbox_vm_interface:
35313552
netbox_url: http://localhost:32768
35323553
netbox_token: 0123456789abcdef0123456789abcdef01234567
35333554
data:
3534-
name: "Eth0"
3555+
name: "Eth10"
35353556
virtual_machine: "test100-vm"
35363557
state: absent
35373558
register: test_four
@@ -3540,9 +3561,58 @@
35403561
assert:
35413562
that:
35423563
- test_four is changed
3543-
- test_four['interface']['name'] == "Eth0"
3564+
- test_four['interface']['name'] == "Eth10"
35443565
- test_four['interface']['virtual_machine'] == 1
3545-
- test_four['msg'] == "interface Eth0 deleted"
3566+
- test_four['msg'] == "interface Eth10 deleted"
3567+
3568+
- name: "NETBOX_VM_INTERFACE 5: Attempt to update interface with same name on other VMs"
3569+
netbox_vm_interface:
3570+
netbox_url: http://localhost:32768
3571+
netbox_token: 0123456789abcdef0123456789abcdef01234567
3572+
data:
3573+
virtual_machine: "test100-vm"
3574+
name: "Eth0"
3575+
enabled: false
3576+
mtu: 9000
3577+
mac_address: "00:00:00:AA:AA:01"
3578+
description: "Updated test100-vm Eth0 intf"
3579+
mode: Tagged
3580+
untagged_vlan:
3581+
name: Wireless
3582+
site: Test Site
3583+
tagged_vlans:
3584+
- name: Data
3585+
site: Test Site
3586+
- name: VoIP
3587+
site: Test Site
3588+
tags:
3589+
- Schnozzberry
3590+
state: present
3591+
register: test_five
3592+
3593+
- name: "NETBOX_VM_INTERFACE 5: ASSERT - Updated"
3594+
assert:
3595+
that:
3596+
- test_five is changed
3597+
- test_five['diff']['after']['enabled'] == false
3598+
- test_five['diff']['after']['mtu'] == 9000
3599+
- test_five['diff']['after']['mac_address'] == "00:00:00:AA:AA:01"
3600+
- test_five['diff']['after']['description'] == "Updated test100-vm Eth0 intf"
3601+
- test_five['diff']['after']['mode'] == 200
3602+
- test_five['diff']['after']['untagged_vlan'] == 1
3603+
- test_five['diff']['after']['tagged_vlans'] == [2, 3]
3604+
- test_five['diff']['after']['tags'][0] == "Schnozzberry"
3605+
- test_five['interface']['name'] == "Eth0"
3606+
- test_five['interface']['virtual_machine'] == 1
3607+
- test_five['interface']['enabled'] == false
3608+
- test_five['interface']['mtu'] == 9000
3609+
- test_five['interface']['mac_address'] == "00:00:00:AA:AA:01"
3610+
- test_five['interface']['description'] == "Updated test100-vm Eth0 intf"
3611+
- test_five['interface']['mode'] == 200
3612+
- test_five['interface']['untagged_vlan'] == 1
3613+
- test_five['interface']['tagged_vlans'] == [2, 3]
3614+
- test_five['interface']['tags'][0] == "Schnozzberry"
3615+
- test_five['msg'] == "interface Eth0 updated"
35463616
##
35473617
##
35483618
### NETBOX_PROVIDER

tests/integration/netbox-deploy.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@
191191

192192

193193
## Create Interfaces
194-
interfaces = [
194+
dev_interfaces = [
195195
{"name": "GigabitEthernet1", "device": test100.id, "form_factor": 1000},
196196
{"name": "GigabitEthernet2", "device": test100.id, "form_factor": 1000},
197197
]
198-
created_interfaces = nb.dcim.interfaces.create(interfaces)
198+
created_interfaces = nb.dcim.interfaces.create(dev_interfaces)
199199
## Interface variables to be used later on
200200
test100_gi1 = nb.dcim.interfaces.get(name="GigabitEthernet1", device_id=1)
201201
test100_gi2 = nb.dcim.interfaces.get(name="GigabitEthernet2", device_id=1)
@@ -229,8 +229,35 @@
229229
test_cluster = nb.virtualization.clusters.get(name="Test Cluster")
230230

231231
## Create Virtual Machine
232-
virtual_machines = [{"name": "test100-vm", "cluster": test_cluster.id}]
232+
virtual_machines = [
233+
{"name": "test100-vm", "cluster": test_cluster.id},
234+
{"name": "test101-vm", "cluster": test_cluster.id},
235+
{"name": "test102-vm", "cluster": test_cluster.id},
236+
{"name": "test103-vm", "cluster": test_cluster.id},
237+
{"name": "test104-vm", "cluster": test_cluster.id},
238+
]
233239
created_virtual_machines = nb.virtualization.virtual_machines.create(virtual_machines)
240+
test100_vm = nb.virtualization.virtual_machines.get(name="test100-vm")
241+
test101_vm = nb.virtualization.virtual_machines.get(name="test101-vm")
242+
243+
## Create Virtual Machine Interfaces
244+
virtual_machines_intfs = [
245+
# Create test100-vm intfs
246+
{"name": "Eth0", "virtual_machine": test100_vm.id},
247+
{"name": "Eth1", "virtual_machine": test100_vm.id},
248+
{"name": "Eth2", "virtual_machine": test100_vm.id},
249+
{"name": "Eth3", "virtual_machine": test100_vm.id},
250+
{"name": "Eth4", "virtual_machine": test100_vm.id},
251+
# Create test101-vm intfs
252+
{"name": "Eth0", "virtual_machine": test101_vm.id},
253+
{"name": "Eth1", "virtual_machine": test101_vm.id},
254+
{"name": "Eth2", "virtual_machine": test101_vm.id},
255+
{"name": "Eth3", "virtual_machine": test101_vm.id},
256+
{"name": "Eth4", "virtual_machine": test101_vm.id},
257+
]
258+
created_virtual_machines_intfs = nb.virtualization.interfaces.create(
259+
virtual_machines_intfs
260+
)
234261

235262
## Create Circuit Provider
236263
providers = [{"name": "Test Provider", "slug": "test-provider"}]

tests/unit/module_utils/test_netbox_base_class.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,11 @@ def test_change_choices_id(mock_netbox_module, endpoint, data, expected):
611611
{"name": "GigabitEthernet1", "device": "Test Device", "form_factor": 1000},
612612
{"name": "GigabitEthernet1", "device_id": 1},
613613
),
614+
(
615+
"interface",
616+
{"name": "Eth0", "virtual_machine": "Test Device", "type": 0},
617+
{"name": "Eth0", "virtual_machine_id": 1},
618+
),
614619
(
615620
"inventory_item",
616621
{

0 commit comments

Comments
 (0)