Skip to content

Commit 67f7739

Browse files
BugFix: Fix device type slug (#77)
1 parent 9940849 commit 67f7739

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

plugins/module_utils/netbox_dcim.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def run(self):
7474
# Used for msg output
7575
if data.get("name"):
7676
name = data["name"]
77+
elif data.get("model") and not data.get("slug"):
78+
name = data["model"]
7779
elif data.get("slug"):
7880
name = data["slug"]
7981

plugins/module_utils/netbox_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
"cluster_groups",
382382
"cluster_types",
383383
"device_roles",
384+
"device_types",
384385
"ipam_roles",
385386
"rack_groups",
386387
"rack_roles",

plugins/modules/netbox_device_type.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
slug:
5252
description:
5353
- The slug of the device type. Must follow slug formatting (URL friendly)
54+
- If not specified, it will slugify the model
5455
- ex. test-device-type
55-
required: true
56+
required: false
5657
part_number:
5758
description:
5859
- The part number of the device type
@@ -164,8 +165,8 @@ def main():
164165
required=True,
165166
options=dict(
166167
manufacturer=dict(required=False, type="raw"),
167-
model=dict(required=False, type="raw"),
168-
slug=dict(required=True, type="str"),
168+
model=dict(required=True, type="raw"),
169+
slug=dict(required=False, type="str"),
169170
part_number=dict(required=False, type="str"),
170171
u_height=dict(required=False, type="int"),
171172
is_full_depth=dict(required=False, type="bool"),
@@ -180,7 +181,7 @@ def main():
180181
)
181182
)
182183

183-
required_if = [("state", "present", ["slug"]), ("state", "absent", ["slug"])]
184+
required_if = [("state", "present", ["model"]), ("state", "absent", ["model"])]
184185

185186
module = NetboxAnsibleModule(
186187
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if

tests/integration/integration-tests.yml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@
18461846
netbox_token: 0123456789abcdef0123456789abcdef01234567
18471847
data:
18481848
slug: test-device-type
1849-
model: ws-test-3750
1849+
model: "ws-test-3750"
18501850
manufacturer: Test Manufacturer
18511851
state: present
18521852
register: test_two
@@ -1895,7 +1895,7 @@
18951895
netbox_url: http://localhost:32768
18961896
netbox_token: 0123456789abcdef0123456789abcdef01234567
18971897
data:
1898-
slug: test-device-type
1898+
model: test-device-type
18991899
state: absent
19001900
register: test_four
19011901

@@ -1912,7 +1912,7 @@
19121912
netbox_url: http://localhost:32768
19131913
netbox_token: 0123456789abcdef0123456789abcdef01234567
19141914
data:
1915-
slug: test-device-type
1915+
model: "Test Device Type"
19161916
state: absent
19171917
register: test_five
19181918

@@ -1921,7 +1921,28 @@
19211921
that:
19221922
- not test_five['changed']
19231923
- test_five['device_type'] == None
1924-
- test_five['msg'] == "device_type test-device-type already absent"
1924+
- test_five['msg'] == "device_type Test Device Type already absent"
1925+
1926+
- name: "DEVICE_TYPE 6: Without Slug"
1927+
netbox_device_type:
1928+
netbox_url: http://localhost:32768
1929+
netbox_token: 0123456789abcdef0123456789abcdef01234567
1930+
data:
1931+
model: "WS Test 3850"
1932+
manufacturer: Test Manufacturer
1933+
state: present
1934+
register: test_six
1935+
1936+
- name: "DEVICE_TYPE 6: ASSERT - Without Slug"
1937+
assert:
1938+
that:
1939+
- test_six is changed
1940+
- test_six['diff']['before']['state'] == "absent"
1941+
- test_six['diff']['after']['state'] == "present"
1942+
- test_six['device_type']['slug'] == "ws-test-3850"
1943+
- test_six['device_type']['model'] == "WS Test 3850"
1944+
- test_six['device_type']['manufacturer'] == 3
1945+
- test_six['msg'] == "device_type WS Test 3850 created"
19251946

19261947
##
19271948
##

0 commit comments

Comments
 (0)