Skip to content

Commit 9449f88

Browse files
k-304Kenneth Joss
andauthored
Allow adding power port templates to modules (#1105)
* Allow adding power port templates to modules --------- Co-authored-by: Kenneth Joss <kenneth@cloudscale.ch>
1 parent 78f4c86 commit 9449f88

File tree

5 files changed

+217
-7
lines changed

5 files changed

+217
-7
lines changed

plugins/lookup/nb_lookup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def get_endpoint(netbox, term):
212212
"module-bays": {"endpoint": netbox.dcim.module_bays},
213213
"module-bay-templates": {"endpoint": netbox.dcim.module_bay_templates},
214214
"module-bay-types": {"endpoint": netbox.dcim.module_bay_types},
215+
"module-types": {"endpoint": netbox.dcim.module_types},
215216
"modules": {"endpoint": netbox.dcim.modules},
216217
"object-changes": {"endpoint": netbox.extras.object_changes},
217218
"permissions": {"endpoint": netbox.users.permissions},

plugins/module_utils/netbox_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
"lag": "interfaces",
240240
"manufacturer": "manufacturers",
241241
"master": "devices",
242+
"module_type": "module_types",
242243
"nat_inside": "ip_addresses",
243244
"nat_outside": "ip_addresses",
244245
"platform": "platforms",

plugins/modules/netbox_power_port_template.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
device_type:
3535
description:
3636
- The device type the power port is attached to
37-
required: true
37+
- Either I(device_type) or I(module_type) are required
38+
type: raw
39+
module_type:
40+
description:
41+
- The module type the power port is attached to
42+
- Either I(device_type) or I(module_type) are required
3843
type: raw
3944
name:
4045
description:
@@ -175,6 +180,17 @@
175180
device_type: Test Device Type
176181
state: present
177182
183+
- name: Create power port for a module type within NetBox
184+
netbox.netbox.netbox_power_port_template:
185+
netbox_url: http://netbox.local
186+
netbox_token: thisIsMyToken
187+
data:
188+
name: Test Power Port Template
189+
module_type: Test Module Type
190+
type: iec-60320-c6
191+
maximum_draw: 750
192+
state: present
193+
178194
- name: Update power port with other fields
179195
netbox.netbox.netbox_power_port_template:
180196
netbox_url: http://netbox.local
@@ -230,7 +246,8 @@ def main():
230246
type="dict",
231247
required=True,
232248
options=dict(
233-
device_type=dict(required=True, type="raw"),
249+
device_type=dict(required=False, type="raw"),
250+
module_type=dict(required=False, type="raw"),
234251
name=dict(required=True, type="str"),
235252
type=dict(
236253
required=False,
@@ -346,12 +363,19 @@ def main():
346363
)
347364

348365
required_if = [
349-
("state", "present", ["device_type", "name"]),
350-
("state", "absent", ["device_type", "name"]),
366+
("state", "present", ["name"]),
367+
("state", "absent", ["name"]),
368+
]
369+
370+
required_one_of = [
371+
("device_type", "module_type"),
351372
]
352373

353374
module = NetboxAnsibleModule(
354-
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
375+
argument_spec=argument_spec,
376+
supports_check_mode=True,
377+
required_if=required_if,
378+
required_one_of=required_one_of,
355379
)
356380

357381
netbox_power_port_template = NetboxDcimModule(module, NB_POWER_PORT_TEMPLATES)

tests/integration/targets/v3.5/tasks/netbox_power_port_template.yml

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
### NETBOX_POWER_PORT_TEMPLATE
88
##
99
##
10-
- name: "POWER_PORT_TEMPLATE 0: Create device type for testing power ports"
10+
- name: "POWER_PORT_TEMPLATE 0.1: Create device type for testing power ports on device types"
1111
netbox.netbox.netbox_device_type:
1212
netbox_url: http://localhost:32768
1313
netbox_token: 0123456789abcdef0123456789abcdef01234567
@@ -16,6 +16,15 @@
1616
manufacturer: Test Manufacturer
1717
state: present
1818

19+
- name: "POWER_PORT_TEMPLATE 0.2: Create module type for testing power ports on module types"
20+
netbox.netbox.netbox_module_type:
21+
netbox_url: http://localhost:32768
22+
netbox_token: 0123456789abcdef0123456789abcdef01234567
23+
data:
24+
model: Module Type Power Tests
25+
manufacturer: Test Manufacturer
26+
state: present
27+
1928
- name: "POWER_PORT_TEMPLATE 1: Necessary info creation"
2029
netbox.netbox.netbox_power_port_template:
2130
netbox_url: http://localhost:32768
@@ -118,3 +127,86 @@
118127
- test_five['diff']['before']['state'] == "present"
119128
- test_five['diff']['after']['state'] == "absent"
120129
- test_five['msg'] == "power_port_template Power Port Template 2 deleted"
130+
131+
- name: "POWER_PORT_TEMPLATE 6: Necessary info creation"
132+
netbox.netbox.netbox_power_port_template:
133+
netbox_url: http://localhost:32768
134+
netbox_token: 0123456789abcdef0123456789abcdef01234567
135+
data:
136+
name: Module Power Port Template
137+
module_type: Module Type Power Tests
138+
state: present
139+
register: test_six
140+
141+
- name: "POWER_PORT_TEMPLATE 6: ASSERT - Necessary info creation"
142+
assert:
143+
that:
144+
- test_six is changed
145+
- test_six['diff']['before']['state'] == "absent"
146+
- test_six['diff']['after']['state'] == "present"
147+
- test_six['power_port_template']['name'] == "Module Power Port Template"
148+
- test_six['power_port_template']['module_type'] == 1
149+
- test_six['msg'] == "power_port_template Module Power Port Template created"
150+
151+
- name: "POWER_PORT_TEMPLATE 7: Create duplicate"
152+
netbox.netbox.netbox_power_port_template:
153+
netbox_url: http://localhost:32768
154+
netbox_token: 0123456789abcdef0123456789abcdef01234567
155+
data:
156+
name: Module Power Port Template
157+
module_type: Module Type Power Tests
158+
state: present
159+
register: test_seven
160+
161+
- name: "POWER_PORT_TEMPLATE 7: ASSERT - Create duplicate"
162+
assert:
163+
that:
164+
- not test_seven['changed']
165+
- test_seven['power_port_template']['name'] == "Module Power Port Template"
166+
- test_seven['power_port_template']['module_type'] == 1
167+
- test_seven['msg'] == "power_port_template Module Power Port Template already exists"
168+
169+
- name: "POWER_PORT_TEMPLATE 8: Update power_port_template with other fields"
170+
netbox.netbox.netbox_power_port_template:
171+
netbox_url: http://localhost:32768
172+
netbox_token: 0123456789abcdef0123456789abcdef01234567
173+
data:
174+
name: Module Power Port Template
175+
module_type: Module Type Power Tests
176+
type: ita-e
177+
allocated_draw: 10
178+
maximum_draw: 20
179+
state: present
180+
register: test_eight
181+
182+
- name: "POWER_PORT_TEMPLATE 8: ASSERT - Update power_port_template with other fields"
183+
assert:
184+
that:
185+
- test_eight is changed
186+
- test_eight['diff']['after']['type'] == "ita-e"
187+
- test_eight['diff']['after']['allocated_draw'] == 10
188+
- test_eight['diff']['after']['maximum_draw'] == 20
189+
- test_eight['power_port_template']['name'] == "Module Power Port Template"
190+
- test_eight['power_port_template']['module_type'] == 1
191+
- test_eight['power_port_template']['type'] == "ita-e"
192+
- test_eight['power_port_template']['allocated_draw'] == 10
193+
- test_eight['power_port_template']['maximum_draw'] == 20
194+
- test_eight['msg'] == "power_port_template Module Power Port Template updated"
195+
196+
- name: "POWER_PORT_TEMPLATE 9: Delete Power Port Template"
197+
netbox.netbox.netbox_power_port_template:
198+
netbox_url: http://localhost:32768
199+
netbox_token: 0123456789abcdef0123456789abcdef01234567
200+
data:
201+
name: Module Power Port Template
202+
module_type: Module Type Power Tests
203+
state: absent
204+
register: test_nine
205+
206+
- name: "POWER_PORT_TEMPLATE 9: ASSERT - Delete Power Port Template"
207+
assert:
208+
that:
209+
- test_nine is changed
210+
- test_nine['diff']['before']['state'] == "present"
211+
- test_nine['diff']['after']['state'] == "absent"
212+
- test_nine['msg'] == "power_port_template Module Power Port Template deleted"

tests/integration/targets/v3.6/tasks/netbox_power_port_template.yml

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
### NETBOX_POWER_PORT_TEMPLATE
88
##
99
##
10-
- name: "POWER_PORT_TEMPLATE 0: Create device type for testing power ports"
10+
- name: "POWER_PORT_TEMPLATE 0.1: Create device type for testing power ports on device types"
1111
netbox.netbox.netbox_device_type:
1212
netbox_url: http://localhost:32768
1313
netbox_token: 0123456789abcdef0123456789abcdef01234567
@@ -16,6 +16,15 @@
1616
manufacturer: Test Manufacturer
1717
state: present
1818

19+
- name: "POWER_PORT_TEMPLATE 0.2: Create module type for testing power ports on module types"
20+
netbox.netbox.netbox_module_type:
21+
netbox_url: http://localhost:32768
22+
netbox_token: 0123456789abcdef0123456789abcdef01234567
23+
data:
24+
model: Module Type Power Tests
25+
manufacturer: Test Manufacturer
26+
state: present
27+
1928
- name: "POWER_PORT_TEMPLATE 1: Necessary info creation"
2029
netbox.netbox.netbox_power_port_template:
2130
netbox_url: http://localhost:32768
@@ -118,3 +127,86 @@
118127
- test_five['diff']['before']['state'] == "present"
119128
- test_five['diff']['after']['state'] == "absent"
120129
- test_five['msg'] == "power_port_template Power Port Template 2 deleted"
130+
131+
- name: "POWER_PORT_TEMPLATE 6: Necessary info creation"
132+
netbox.netbox.netbox_power_port_template:
133+
netbox_url: http://localhost:32768
134+
netbox_token: 0123456789abcdef0123456789abcdef01234567
135+
data:
136+
name: Module Power Port Template
137+
module_type: Module Type Power Tests
138+
state: present
139+
register: test_six
140+
141+
- name: "POWER_PORT_TEMPLATE 6: ASSERT - Necessary info creation"
142+
assert:
143+
that:
144+
- test_six is changed
145+
- test_six['diff']['before']['state'] == "absent"
146+
- test_six['diff']['after']['state'] == "present"
147+
- test_six['power_port_template']['name'] == "Module Power Port Template"
148+
- test_six['power_port_template']['module_type'] == 1
149+
- test_six['msg'] == "power_port_template Module Power Port Template created"
150+
151+
- name: "POWER_PORT_TEMPLATE 7: Create duplicate"
152+
netbox.netbox.netbox_power_port_template:
153+
netbox_url: http://localhost:32768
154+
netbox_token: 0123456789abcdef0123456789abcdef01234567
155+
data:
156+
name: Module Power Port Template
157+
module_type: Module Type Power Tests
158+
state: present
159+
register: test_seven
160+
161+
- name: "POWER_PORT_TEMPLATE 7: ASSERT - Create duplicate"
162+
assert:
163+
that:
164+
- not test_seven['changed']
165+
- test_seven['power_port_template']['name'] == "Module Power Port Template"
166+
- test_seven['power_port_template']['module_type'] == 1
167+
- test_seven['msg'] == "power_port_template Module Power Port Template already exists"
168+
169+
- name: "POWER_PORT_TEMPLATE 8: Update power_port_template with other fields"
170+
netbox.netbox.netbox_power_port_template:
171+
netbox_url: http://localhost:32768
172+
netbox_token: 0123456789abcdef0123456789abcdef01234567
173+
data:
174+
name: Module Power Port Template
175+
module_type: Module Type Power Tests
176+
type: ita-e
177+
allocated_draw: 10
178+
maximum_draw: 20
179+
state: present
180+
register: test_eight
181+
182+
- name: "POWER_PORT_TEMPLATE 8: ASSERT - Update power_port_template with other fields"
183+
assert:
184+
that:
185+
- test_eight is changed
186+
- test_eight['diff']['after']['type'] == "ita-e"
187+
- test_eight['diff']['after']['allocated_draw'] == 10
188+
- test_eight['diff']['after']['maximum_draw'] == 20
189+
- test_eight['power_port_template']['name'] == "Module Power Port Template"
190+
- test_eight['power_port_template']['module_type'] == 1
191+
- test_eight['power_port_template']['type'] == "ita-e"
192+
- test_eight['power_port_template']['allocated_draw'] == 10
193+
- test_eight['power_port_template']['maximum_draw'] == 20
194+
- test_eight['msg'] == "power_port_template Module Power Port Template updated"
195+
196+
- name: "POWER_PORT_TEMPLATE 9: Delete Power Port Template"
197+
netbox.netbox.netbox_power_port_template:
198+
netbox_url: http://localhost:32768
199+
netbox_token: 0123456789abcdef0123456789abcdef01234567
200+
data:
201+
name: Module Power Port Template
202+
module_type: Module Type Power Tests
203+
state: absent
204+
register: test_nine
205+
206+
- name: "POWER_PORT_TEMPLATE 9: ASSERT - Delete Power Port Template"
207+
assert:
208+
that:
209+
- test_nine is changed
210+
- test_nine['diff']['before']['state'] == "present"
211+
- test_nine['diff']['after']['state'] == "absent"
212+
- test_nine['msg'] == "power_port_template Module Power Port Template deleted"

0 commit comments

Comments
 (0)