Skip to content

Commit b276a30

Browse files
Added primary_ip4/6 to netbox_device, added tests, updated netbox_utils (#21)
1 parent 445664b commit b276a30

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@
269269
"parent_region": set(["slug"]),
270270
"platform": set(["slug"]),
271271
"prefix": set(["prefix", "vrf"]),
272+
"primary_ip4": set(["address", "vrf"]),
273+
"primary_ip6": set(["address", "vrf"]),
272274
"rack": set(["name", "site"]),
273275
"rack_group": set(["slug"]),
274276
"rack_role": set(["slug"]),

plugins/modules/netbox_device.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
- Staged
8787
- Failed
8888
- Inventory
89+
primary_ip4:
90+
description:
91+
- Primary IPv4 address assigned to the device
92+
primary_ip6:
93+
description:
94+
- Primary IPv6 address assigned to the device
8995
cluster:
9096
description:
9197
- Cluster that the device will be assigned to

tests/integration/integration-tests.yml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
assert:
3030
that:
3131
- test_one is changed
32-
- test_one['msg'] == "device R1 created"
3332
- test_one['diff']['before']['state'] == 'absent'
3433
- test_one['diff']['after']['state'] == 'present'
3534
- test_one['device']['name'] == "R1"
@@ -38,6 +37,7 @@
3837
- test_one['device']['site'] == 1
3938
- test_one['device']['status'] == 3
4039
- test_one['device']['name'] == "R1"
40+
- test_one['msg'] == "device R1 created"
4141

4242
- name: "2 - Duplicate device"
4343
netbox_device:
@@ -56,12 +56,12 @@
5656
assert:
5757
that:
5858
- not test_two['changed']
59-
- test_two['msg'] == "device R1 already exists"
6059
- test_two['device']['name'] == "R1"
6160
- test_two['device']['device_role'] == 1
6261
- test_two['device']['device_type'] == 1
6362
- test_two['device']['site'] == 1
6463
- test_two['device']['status'] == 3
64+
- test_two['msg'] == "device R1 already exists"
6565

6666
- name: "3 - Update device"
6767
netbox_device:
@@ -77,14 +77,14 @@
7777
assert:
7878
that:
7979
- test_three is changed
80-
- test_three['msg'] == "device R1 updated"
81-
- test_three['diff']['before']['serial'] == ""
8280
- test_three['diff']['after']['serial'] == "FXS1001"
8381
- test_three['device']['name'] == "R1"
8482
- test_three['device']['device_role'] == 1
8583
- test_three['device']['device_type'] == 1
8684
- test_three['device']['site'] == 1
8785
- test_three['device']['status'] == 3
86+
- test_three['device']['serial'] == "FXS1001"
87+
- test_three['msg'] == "device R1 updated"
8888

8989
- name: "4 - Create device with tags and assign to rack"
9090
netbox_device:
@@ -109,7 +109,6 @@
109109
assert:
110110
that:
111111
- test_four is changed
112-
- test_four['msg'] == "device TestR1 created"
113112
- test_four['diff']['before']['state'] == "absent"
114113
- test_four['diff']['after']['state'] == "present"
115114
- test_four['device']['name'] == "TestR1"
@@ -121,6 +120,7 @@
121120
- test_four['device']['tags'][0] == 'Schnozzberry'
122121
- test_four['device']['tenant'] == 1
123122
- test_four['device']['asset_tag'] == '1234'
123+
- test_four['msg'] == "device TestR1 created"
124124

125125
- name: "5 - Delete previous device"
126126
netbox_device:
@@ -155,6 +155,33 @@
155155
- test_six['diff']['before']['state'] == "present"
156156
- test_six['diff']['after']['state'] == "absent"
157157
- test_six['msg'] == "device R1 deleted"
158+
159+
- name: "7 - Add primary_ip4/6 to test100"
160+
netbox_device:
161+
netbox_url: "http://localhost:32768"
162+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
163+
data:
164+
name: "test100"
165+
primary_ip4: "172.16.180.1/24"
166+
primary_ip6: "2001::1:1/64"
167+
state: present
168+
register: test_seven
169+
170+
- name: "7 - ASSERT"
171+
assert:
172+
that:
173+
- test_seven is changed
174+
- test_seven['diff']['after']['primary_ip4'] == 1
175+
- test_seven['diff']['after']['primary_ip6'] == 2
176+
- test_seven['device']['name'] == "test100"
177+
- test_seven['device']['device_role'] == 1
178+
- test_seven['device']['device_type'] == 1
179+
- test_seven['device']['site'] == 1
180+
- test_seven['device']['status'] == 1
181+
- test_seven['device']['primary_ip4'] == 1
182+
- test_seven['device']['primary_ip6'] == 2
183+
- test_seven['msg'] == "device test100 updated"
184+
158185
##
159186
##
160187
### NETBOX_DEVICE_INTERFACE
@@ -530,7 +557,7 @@
530557
- test_eight['ip_address']['address'] == "10.10.1.30/16"
531558
- test_eight['ip_address']['family'] == 4
532559
- test_eight['ip_address']['interface'] == 1
533-
- test_eight['ip_address']['nat_inside'] == 5
560+
- test_eight['ip_address']['nat_inside'] == 7
534561
- test_eight['ip_address']['vrf'] == 1
535562

536563
- name: "9 - Create IP address on GigabitEthernet2 - test100 - State: present"

tests/integration/netbox-deploy.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
test_site2 = nb.dcim.sites.get(slug="test-site2")
3030

3131

32+
## Create VRFs
33+
vrfs = [{"name": "Test VRF", "rd": "1:1"}]
34+
created_vrfs = nb.ipam.vrfs.create(vrfs)
35+
36+
3237
## Create PREFIXES
3338
prefixes = [
3439
{"prefix": "192.168.100.0/24", "site": test_site2.id},
@@ -42,11 +47,6 @@
4247
created_regions = nb.dcim.regions.create(regions)
4348

4449

45-
## Create VRFs
46-
vrfs = [{"name": "Test VRF", "rd": "1:1"}]
47-
created_vrfs = nb.ipam.vrfs.create(vrfs)
48-
49-
5050
## Create VLAN GROUPS
5151
vlan_groups = [
5252
{
@@ -190,6 +190,17 @@
190190
{"name": "GigabitEthernet2", "device": test100.id, "form_factor": 1000},
191191
]
192192
created_interfaces = nb.dcim.interfaces.create(interfaces)
193+
## Interface variables to be used later on
194+
test100_gi1 = nb.dcim.interfaces.get(name="GigabitEthernet1", device_id=1)
195+
test100_gi2 = nb.dcim.interfaces.get(name="GigabitEthernet2", device_id=1)
196+
197+
198+
## Create IP Addresses
199+
ip_addresses = [
200+
{"address": "172.16.180.1/24", "interface": test100_gi1.id},
201+
{"address": "2001::1:1/64", "interface": test100_gi2.id},
202+
]
203+
created_ip_addresses = nb.ipam.ip_addresses.create(ip_addresses)
193204

194205

195206
## Create RIRs

tests/unit/module_utils/test_netbox_base_class.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,28 @@ def test_build_query_params_no_child(
609609
@pytest.mark.parametrize(
610610
"parent, module_data, child, expected",
611611
[
612+
(
613+
"primary_ip4",
614+
{
615+
"name": "test100",
616+
"serial": "FXS1001",
617+
"comments": "Temp device",
618+
"primary_ip4": {"address": "172.16.180.1/24", "vrf": "Test VRF"},
619+
},
620+
{"address": "172.16.180.1/24", "vrf": "Test VRF"},
621+
{"address": "172.16.180.1/24", "vrf_id": 1},
622+
),
623+
(
624+
"primary_ip6",
625+
{
626+
"name": "test100",
627+
"serial": "FXS1001",
628+
"comments": "Temp device",
629+
"primary_ip4": {"address": "2001::1:1/64", "vrf": "Test VRF"},
630+
},
631+
{"address": "2001::1:1/64", "vrf": "Test VRF"},
632+
{"address": "2001::1:1/64", "vrf_id": 1},
633+
),
612634
(
613635
"lag",
614636
{"name": "GigabitEthernet1", "device": 1, "lag": {"name": "port-channel1"}},

0 commit comments

Comments
 (0)