Skip to content

Commit 7224900

Browse files
authored
Add module netbox_journal_entry (#961)
* Add module netbox_journal_entry
1 parent d4ca979 commit 7224900

File tree

7 files changed

+528
-309
lines changed

7 files changed

+528
-309
lines changed

meta/runtime.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ action_groups:
4242
- netbox_inventory_item_role
4343
- netbox_ip_address
4444
- netbox_ipam_role
45+
- netbox_journal_entry
4546
- netbox_l2vpn
4647
- netbox_location
4748
- netbox_manufacturer
@@ -80,4 +81,4 @@ action_groups:
8081
- netbox_webhook
8182
- netbox_wireless_lan
8283
- netbox_wireless_lan_group
83-
- netbox_wireless_link
84+
- netbox_wireless_link

plugins/module_utils/netbox_extras.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@
1616
NB_CUSTOM_FIELDS = "custom_fields"
1717
NB_CUSTOM_LINKS = "custom_links"
1818
NB_EXPORT_TEMPLATES = "export_templates"
19+
NB_JOURNAL_ENTRIES = "journal_entries"
1920
NB_WEBHOOKS = "webhooks"
2021

2122

2223
class NetboxExtrasModule(NetboxModule):
2324
def __init__(self, module, endpoint):
2425
super().__init__(module, endpoint)
2526

27+
def _handle_state_new(self, nb_app, nb_endpoint, endpoint_name, data):
28+
if self.state == "new":
29+
self.nb_object, diff = self._create_netbox_object(nb_endpoint, data)
30+
self.result["msg"] = "%s created" % (endpoint_name)
31+
self.result["changed"] = True
32+
self.result["diff"] = diff
33+
2634
def run(self):
2735
"""
2836
This function should have all necessary code for endpoints within the application
2937
to create/update/delete the endpoint objects
3038
Supported endpoints:
3139
- config_contexts
3240
- tags
41+
- journal entries
3342
"""
3443
# Used to dynamically set key when returning results
3544
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]
@@ -57,10 +66,16 @@ def run(self):
5766
if data.get("color"):
5867
data["color"] = data["color"].lower()
5968

60-
object_query_params = self._build_query_params(
61-
endpoint_name, data, user_query_params
62-
)
63-
self.nb_object = self._nb_endpoint_get(nb_endpoint, object_query_params, name)
69+
# Handle journal entry
70+
if self.state == "new" and endpoint_name == "journal_entry":
71+
self._handle_state_new(nb_app, nb_endpoint, endpoint_name, data)
72+
else:
73+
object_query_params = self._build_query_params(
74+
endpoint_name, data, user_query_params
75+
)
76+
self.nb_object = self._nb_endpoint_get(
77+
nb_endpoint, object_query_params, name
78+
)
6479

6580
if self.state == "present":
6681
self._ensure_object_exists(nb_endpoint, endpoint_name, name, data)

plugins/module_utils/netbox_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"custom_fields",
8383
"custom_links",
8484
"export_templates",
85+
"journal_entries",
8586
"webhooks",
8687
],
8788
ipam=[
@@ -318,6 +319,7 @@
318319
"fhrp_groups": "fhrp_group",
319320
"front_ports": "front_port",
320321
"front_port_templates": "front_port_template",
322+
"journal_entries": "journal_entry",
321323
"interfaces": "interface",
322324
"interface_templates": "interface_template",
323325
"inventory_items": "inventory_item",
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright: (c) 2023, Martin Rødvand (@rodvand) <martin@rodvand.net>
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
7+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
DOCUMENTATION = r"""
12+
---
13+
module: netbox_journal_entry
14+
short_description: Creates a journal entry
15+
description:
16+
- Creates a journal entry in NetBox
17+
notes:
18+
- Tags should be defined as a YAML list
19+
- This should be ran with connection C(local) and hosts C(localhost)
20+
author:
21+
- Martin Rødvand (@rodvand)
22+
requirements:
23+
- pynetbox
24+
version_added: '3.12.0'
25+
extends_documentation_fragment:
26+
- netbox.netbox.common
27+
options:
28+
data:
29+
type: dict
30+
description:
31+
- Defines the journal entry
32+
suboptions:
33+
created_by:
34+
description:
35+
- The user ID of the user creating the journal entry. Omit to use the API token user
36+
required: false
37+
type: int
38+
kind:
39+
description:
40+
- The kind of journal entry
41+
required: false
42+
type: str
43+
assigned_object_id:
44+
description:
45+
- ID of the object to create the journal entry on
46+
required: true
47+
type: int
48+
assigned_object_type:
49+
description:
50+
- The object type of the model
51+
required: true
52+
type: str
53+
comments:
54+
description:
55+
- The comment associated with the journal entry
56+
required: true
57+
type: str
58+
tags:
59+
description:
60+
- Any tags that the journal entry may need to be associated with
61+
required: false
62+
type: list
63+
elements: raw
64+
custom_fields:
65+
description:
66+
- Must exist in NetBox
67+
required: false
68+
type: dict
69+
required: true
70+
state:
71+
description:
72+
- |
73+
Use C(new) for adding a journal entry.
74+
choices: [new]
75+
default: new
76+
type: str
77+
"""
78+
79+
EXAMPLES = r"""
80+
- name: "Test NetBox Module"
81+
hosts: localhost
82+
connection: local
83+
gather_facts: false
84+
module_defaults:
85+
group/netbox.netbox.netbox:
86+
netbox_url: MYURL
87+
netbox_token: MYTOKEN
88+
tasks:
89+
- name: Create an IP Address
90+
netbox.netbox.netbox_ip_address:
91+
data:
92+
address: 192.168.8.14/24
93+
register: ip
94+
95+
- name: Create a journal entry
96+
netbox.netbox.netbox_journal_entry:
97+
data:
98+
assigned_object_type: ipam.ipaddress
99+
assigned_object_id: "{{ ip.ip_address.id }}"
100+
kind: success
101+
comments: |
102+
This is a journal entry
103+
when: ip.changed
104+
"""
105+
106+
RETURN = r"""
107+
journal_entry:
108+
description: Serialized object as created or already existent within NetBox
109+
returned: on creation
110+
type: dict
111+
msg:
112+
description: Message indicating failure or info about what has been achieved
113+
returned: always
114+
type: str
115+
"""
116+
117+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
118+
NetboxAnsibleModule,
119+
NETBOX_ARG_SPEC,
120+
)
121+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_extras import (
122+
NetboxExtrasModule,
123+
NB_JOURNAL_ENTRIES,
124+
)
125+
from copy import deepcopy
126+
127+
128+
def main():
129+
"""
130+
Main entry point for module execution
131+
"""
132+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
133+
argument_spec["state"] = dict(required=False, default="new", choices=["new"])
134+
argument_spec.update(
135+
dict(
136+
data=dict(
137+
type="dict",
138+
required=True,
139+
options=dict(
140+
created_by=dict(required=False, type="int"),
141+
kind=dict(required=False, type="str"),
142+
assigned_object_type=dict(required=True, type="str"),
143+
assigned_object_id=dict(required=True, type="int"),
144+
comments=dict(required=True, type="str"),
145+
tags=dict(required=False, type="list", elements="raw"),
146+
custom_fields=dict(required=False, type="dict"),
147+
),
148+
),
149+
)
150+
)
151+
152+
required_if = [
153+
(
154+
"state",
155+
"new",
156+
["comments", "assigned_object_type", "assigned_object_id"],
157+
True,
158+
),
159+
]
160+
161+
module = NetboxAnsibleModule(
162+
argument_spec=argument_spec,
163+
supports_check_mode=True,
164+
required_if=required_if,
165+
)
166+
167+
netbox_journal_entry = NetboxExtrasModule(module, NB_JOURNAL_ENTRIES)
168+
netbox_journal_entry.run()
169+
170+
171+
if __name__ == "__main__": # pragma: no cover
172+
main()

0 commit comments

Comments
 (0)