Skip to content

Commit 7c34188

Browse files
Enhancement: Add tags and ipaddresses to netbox_service (#161)
1 parent 589e211 commit 7c34188

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"aggregates",
5050
"ip_addresses",
5151
"prefixes",
52-
"services",
5352
"rirs",
5453
"roles",
5554
"vlans",
@@ -117,6 +116,7 @@
117116
installed_device="devices",
118117
interface="interfaces",
119118
ip_addresses="ip_addresses",
119+
ipaddresses="ip_addresses",
120120
lag="interfaces",
121121
manufacturer="manufacturers",
122122
nat_inside="ip_addresses",
@@ -199,6 +199,7 @@
199199
"inventory_item": set(["name", "device"]),
200200
"ip_address": set(["address", "vrf"]),
201201
"ip_addresses": set(["address", "vrf", "device"]),
202+
"ipaddresses": set(["address", "vrf", "device"]),
202203
"lag": set(["name"]),
203204
"manufacturer": set(["slug"]),
204205
"nat_inside": set(["vrf", "address"]),
@@ -214,7 +215,7 @@
214215
"region": set(["slug"]),
215216
"rir": set(["slug"]),
216217
"role": set(["slug"]),
217-
"services": set(["device", "virtual_machine", "name"]),
218+
"services": set(["device", "virtual_machine", "name", "port"]),
218219
"site": set(["slug"]),
219220
"tagged_vlans": set(["name", "site", "vlan_group", "tenant"]),
220221
"tenant": set(["slug"]),

plugins/modules/netbox_service.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,21 @@
5555
protocol:
5656
description:
5757
- Specifies which protocol used by service
58-
type: int
58+
type: str
5959
choices:
60-
- 6 TCP
61-
- 17 UDP
62-
ipaddress:
60+
- TCP
61+
- UDP
62+
ipaddresses:
6363
description:
64-
- VRF that prefix is associated with
65-
type: str
64+
- Specifies which IPaddresses to associate with service.
65+
type: dict
6666
description:
6767
description:
6868
- Service description
6969
type: str
70+
tags:
71+
description:
72+
- What tags to add/update
7073
custom_fields:
7174
description:
7275
- Must exist in Netbox and in key/value format
@@ -99,7 +102,11 @@
99102
device: Test666
100103
name: node-exporter
101104
port: 9100
102-
protocol: 6
105+
protocol: TCP
106+
ipaddresses:
107+
- address: 127.0.0.1
108+
tags:
109+
- prometheus
103110
state: present
104111
105112
- name: "Delete netbox service"
@@ -116,7 +123,7 @@
116123
device: Test666
117124
name: node-exporter
118125
port: 9100
119-
protocol: 6
126+
protocol: TCP
120127
state: absent
121128
"""
122129

@@ -149,6 +156,7 @@ def main():
149156
ipaddresses=dict(required=False, type="raw"),
150157
description=dict(required=False, type="str"),
151158
custom_fields=dict(required=False, type=dict),
159+
tags=dict(required=False, type=list),
152160
),
153161
),
154162
)

tests/integration/v2.7/tasks/netbox_service.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- test_service_idempotence['services']['port'] == 9100
5959
- test_service_idempotence['services']['protocol'] == "tcp"
6060
- test_service_idempotence['msg'] == "services node-exporter already exists"
61+
6162
- name: "NETBOX_SERVICE: Test update"
6263
netbox_service:
6364
netbox_url: "http://localhost:32768"
@@ -67,6 +68,8 @@
6768
name: "node-exporter"
6869
port: 9100
6970
protocol: UDP
71+
tags:
72+
- "Schnozzberry"
7073
state: present
7174
register: test_service_update
7275

@@ -76,6 +79,7 @@
7679
- test_service_update is changed
7780
- test_service_update['diff']['before']['protocol'] == "tcp"
7881
- test_service_update['diff']['after']['protocol'] == "udp"
82+
- test_service_update['diff']['after']['tags'][0] == "Schnozzberry"
7983
- test_service_update['msg'] == "services node-exporter updated"
8084

8185
- name: "NETBOX_SERVICE: Test service deletion"
@@ -97,3 +101,29 @@
97101
- test_service_delete['diff']['after']['state'] == "absent"
98102
- test_service_delete['diff']['before']['state'] == "present"
99103
- test_service_delete['msg'] == "services node-exporter deleted"
104+
105+
- name: "NETBOX_SERVICE: Test service IP addresses"
106+
netbox_service:
107+
netbox_url: "http://localhost:32768"
108+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
109+
data:
110+
device: "test100"
111+
name: "node-exporter"
112+
port: 9100
113+
protocol: UDP
114+
ipaddresses:
115+
- address: "172.16.180.1/24"
116+
state: present
117+
register: test_service_ip_addresses
118+
119+
- name: "NETBOX_SERVICE ASSERT - Service has been created with IP address"
120+
assert:
121+
that:
122+
- test_service_ip_addresses is changed
123+
- test_service_ip_addresses['diff']['after']['state'] == "present"
124+
- test_service_ip_addresses['diff']['before']['state'] == "absent"
125+
- test_service_ip_addresses['services']['name'] == "node-exporter"
126+
- test_service_ip_addresses['services']['port'] == 9100
127+
- test_service_ip_addresses['services']['protocol'] == "udp"
128+
- test_service_ip_addresses['services']['ipaddresses'] is defined
129+
- test_service_ip_addresses['msg'] == "services node-exporter created"

0 commit comments

Comments
 (0)