|
| 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