Skip to content

Commit c39aa39

Browse files
authored
Bugfix: Build rear_port and front_port to dictionary including provided device (#267)
1 parent 45f8c22 commit c39aa39

File tree

7 files changed

+132
-19
lines changed

7 files changed

+132
-19
lines changed

plugins/module_utils/netbox_dcim.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@
5252

5353
class NetboxDcimModule(NetboxModule):
5454
def __init__(self, module, endpoint):
55+
# Front ports can only be connected to rear ports of the same device
56+
# This will prepare the rear port dict if only a string is given
57+
if endpoint == "front_ports":
58+
if isinstance(module.params.get("data").get("rear_port"), str):
59+
rear_port = {
60+
"device": module.params.get("data").get("device"),
61+
"name": module.params.get("data").get("rear_port"),
62+
}
63+
module.params["data"].update({"rear_port": rear_port})
64+
elif endpoint == "front_port_templates":
65+
if isinstance(module.params.get("data").get("rear_port_template"), str):
66+
rear_port_template = {
67+
"device_type": module.params.get("data").get("device_type"),
68+
"name": module.params.get("data").get("rear_port_template"),
69+
}
70+
module.params["data"].update({"rear_port_template": rear_port_template})
71+
5572
super().__init__(module, endpoint)
5673

5774
def run(self):

plugins/module_utils/netbox_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
rack_group="slug",
115115
rack_role="slug",
116116
rear_port="name",
117+
rear_port_template="name",
117118
region="slug",
118119
rir="slug",
119120
slug="slug",
@@ -173,6 +174,7 @@
173174
"rack_role": "rack_roles",
174175
"region": "regions",
175176
"rear_port": "rear_ports",
177+
"rear_port_template": "rear_port_templates",
176178
"rir": "rirs",
177179
"services": "services",
178180
"site": "sites",
@@ -372,6 +374,8 @@
372374
"prefix_role": "role",
373375
"rack_group": "group",
374376
"rack_role": "role",
377+
"rear_port_template": "rear_port",
378+
"rear_port_template_position": "rear_port_position",
375379
"tenant_group": "group",
376380
"termination_a": "termination_a_id",
377381
"termination_b": "termination_b_id",

plugins/modules/netbox_front_port_template.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
type: str
5858
type:
5959
description:
60-
- The type of the front port
60+
- The type of the front port template
6161
choices:
6262
- 8p8c
6363
- 110-punch
@@ -75,14 +75,14 @@
7575
- st
7676
required: true
7777
type: str
78-
rear_port:
78+
rear_port_template:
7979
description:
80-
- The rear_port the front port is attached to
80+
- The rear_port_template the front port template is attached to
8181
required: true
8282
type: raw
83-
rear_port_position:
83+
rear_port_template_position:
8484
description:
85-
- The position of the rear port this front port is connected to
85+
- The position of the rear port template this front port template is connected to
8686
required: false
8787
type: int
8888
state:
@@ -120,7 +120,7 @@
120120
name: Test Front Port Template
121121
device_type: Test Device Type
122122
type: bnc
123-
rear_port: Test Rear Port
123+
rear_port_template: Test Rear Port Template
124124
state: present
125125
126126
- name: Update front port template with other fields
@@ -131,8 +131,8 @@
131131
name: Test Front Port Template
132132
device_type: Test Device Type
133133
type: bnc
134-
rear_port: Test Rear Port
135-
rear_port_position: 5
134+
rear_port_template: Test Rear Port Template
135+
rear_port_template_position: 5
136136
state: present
137137
138138
- name: Delete front port template within netbox
@@ -143,7 +143,7 @@
143143
name: Test Front Port Template
144144
device_type: Test Device Type
145145
type: bnc
146-
rear_port: Test Rear Port
146+
rear_port_template: Test Rear Port Template
147147
state: absent
148148
"""
149149

@@ -202,16 +202,16 @@ def main():
202202
],
203203
type="str",
204204
),
205-
rear_port=dict(required=True, type="raw"),
206-
rear_port_position=dict(required=False, type="int"),
205+
rear_port_template=dict(required=True, type="raw"),
206+
rear_port_template_position=dict(required=False, type="int"),
207207
),
208208
),
209209
)
210210
)
211211

212212
required_if = [
213-
("state", "present", ["device_type", "name", "type", "rear_port"]),
214-
("state", "absent", ["device_type", "name", "type", "rear_port"]),
213+
("state", "present", ["device_type", "name", "type", "rear_port_template"]),
214+
("state", "absent", ["device_type", "name", "type", "rear_port_template"]),
215215
]
216216

217217
module = NetboxAnsibleModule(

tests/integration/targets/latest/tasks/netbox_front_port.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,27 @@
124124
- test_five['diff']['before']['state'] == "present"
125125
- test_five['diff']['after']['state'] == "absent"
126126
- test_five['msg'] == "front_port Front Port 2 deleted"
127+
128+
- name: "FRONT_PORT 6: Create duplicate with rear_port dictionary"
129+
netbox.netbox.netbox_front_port:
130+
netbox_url: http://localhost:32768
131+
netbox_token: 0123456789abcdef0123456789abcdef01234567
132+
data:
133+
name: Front Port
134+
device: test100
135+
type: bnc
136+
rear_port:
137+
device: test100
138+
name: Rear Port
139+
state: present
140+
register: test_six
141+
142+
- name: "FRONT_PORT 6: ASSERT - Create duplicate with rear_port dictionary"
143+
assert:
144+
that:
145+
- not test_six['changed']
146+
- test_six['front_port']['name'] == "Front Port"
147+
- test_six['front_port']['device'] == 1
148+
- test_six['front_port']['type'] == "bnc"
149+
- test_six['front_port']['rear_port'] == 1
150+
- test_six['msg'] == "front_port Front Port already exists"

tests/integration/targets/latest/tasks/netbox_front_port_template.yml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
name: Front Port Template
1616
device_type: Cisco Test
1717
type: bnc
18-
rear_port: Rear Port
18+
rear_port_template: Rear Port Template
1919
state: present
2020
register: test_one
2121

@@ -39,7 +39,7 @@
3939
name: Front Port Template
4040
device_type: Cisco Test
4141
type: bnc
42-
rear_port: Rear Port
42+
rear_port_template: Rear Port Template
4343
state: present
4444
register: test_two
4545

@@ -61,8 +61,8 @@
6161
name: Front Port Template
6262
device_type: Cisco Test
6363
type: bnc
64-
rear_port: Rear Port
65-
rear_port_position: 5
64+
rear_port_template: Rear Port Template
65+
rear_port_template_position: 5
6666
state: present
6767
register: test_three
6868

@@ -86,7 +86,7 @@
8686
name: Front Port Template 2
8787
device_type: Cisco Test
8888
type: bnc
89-
rear_port: Rear Port
89+
rear_port_template: Rear Port Template
9090
state: present
9191
register: test_four
9292

@@ -110,7 +110,7 @@
110110
name: Front Port Template 2
111111
device_type: Cisco Test
112112
type: bnc
113-
rear_port: Rear Port
113+
rear_port_template: Rear Port Template
114114
state: absent
115115
register: test_five
116116

@@ -121,3 +121,27 @@
121121
- test_five['diff']['before']['state'] == "present"
122122
- test_five['diff']['after']['state'] == "absent"
123123
- test_five['msg'] == "front_port_template Front Port Template 2 deleted"
124+
125+
- name: "FRONT_PORT 6: Create duplicate with rear_port_template dictionary"
126+
netbox.netbox.netbox_front_port_template:
127+
netbox_url: http://localhost:32768
128+
netbox_token: 0123456789abcdef0123456789abcdef01234567
129+
data:
130+
name: Front Port Template
131+
device_type: Cisco Test
132+
type: bnc
133+
rear_port_template:
134+
device: Cisco Test
135+
name: Rear Port Template
136+
state: present
137+
register: test_six
138+
139+
- name: "FRONT_PORT 6: ASSERT - Create duplicate with rear_port_template dictionary"
140+
assert:
141+
that:
142+
- not test_six['changed']
143+
- test_six['front_port_template']['name'] == "Front Port Template"
144+
- test_six['front_port_template']['device_type'] == 1
145+
- test_six['front_port_template']['type'] == "bnc"
146+
- test_six['front_port_template']['rear_port'] == 1
147+
- test_six['msg'] == "front_port_template Front Port Template already exists"

tests/integration/targets/latest/tasks/netbox_rear_port.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,25 @@
115115
- test_five['diff']['before']['state'] == "present"
116116
- test_five['diff']['after']['state'] == "absent"
117117
- test_five['msg'] == "rear_port Rear Port 2 deleted"
118+
119+
- name: "REAR_PORT 6: Create second Rear Port"
120+
netbox.netbox.netbox_rear_port:
121+
netbox_url: http://localhost:32768
122+
netbox_token: 0123456789abcdef0123456789abcdef01234567
123+
data:
124+
name: Rear Port
125+
device: Test Nexus One
126+
type: bnc
127+
state: present
128+
register: test_six
129+
130+
- name: "REAR_PORT 6: ASSERT - Create second Rear Port"
131+
assert:
132+
that:
133+
- test_six is changed
134+
- test_six['diff']['before']['state'] == "absent"
135+
- test_six['diff']['after']['state'] == "present"
136+
- test_six['rear_port']['name'] == "Rear Port"
137+
- test_six['rear_port']['device'] == 4
138+
- test_six['rear_port']['type'] == "bnc"
139+
- test_six['msg'] == "rear_port Rear Port created"

tests/integration/targets/latest/tasks/netbox_rear_port_template.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,25 @@
112112
- test_five['diff']['before']['state'] == "present"
113113
- test_five['diff']['after']['state'] == "absent"
114114
- test_five['msg'] == "rear_port_template Rear Port Template 2 deleted"
115+
116+
- name: "REAR_PORT_TEMPLATE 6: Create second Rear Port Template"
117+
netbox.netbox.netbox_rear_port_template:
118+
netbox_url: http://localhost:32768
119+
netbox_token: 0123456789abcdef0123456789abcdef01234567
120+
data:
121+
name: Rear Port Template 2
122+
device_type: Arista Test
123+
type: bnc
124+
state: present
125+
register: test_six
126+
127+
- name: "REAR_PORT_TEMPLATE 6: ASSERT - Create second Rear Port Template"
128+
assert:
129+
that:
130+
- test_six is changed
131+
- test_six['diff']['before']['state'] == "absent"
132+
- test_six['diff']['after']['state'] == "present"
133+
- test_six['rear_port_template']['name'] == "Rear Port Template 2"
134+
- test_six['rear_port_template']['device_type'] == 2
135+
- test_six['rear_port_template']['type'] == "bnc"
136+
- test_six['msg'] == "rear_port_template Rear Port Template 2 created"

0 commit comments

Comments
 (0)