|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright: (c) 2024, Rich Bibby, NetBox Labs (@richbibby) |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | + |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | +DOCUMENTATION = r""" |
| 11 | +--- |
| 12 | +module: netbox_tunnel_termination |
| 13 | +short_description: Create, update or delete Tunnel terminations within NetBox |
| 14 | +description: |
| 15 | + - Creates, updates or removes tunnel terminations from NetBox |
| 16 | +notes: |
| 17 | + - Tags should be defined as a YAML list |
| 18 | + - This should be ran with connection C(local) and hosts C(localhost) |
| 19 | +author: |
| 20 | + - Rich Bibby, NetBox Labs (@richbibby) |
| 21 | +requirements: |
| 22 | + - pynetbox |
| 23 | +seealso: |
| 24 | + - name: Tunnel Termination reference |
| 25 | + description: NetBox Documentation for Tunnel Termination model. |
| 26 | + link: https://netboxlabs.com/docs/netbox/en/stable/models/vpn/tunneltermination/ |
| 27 | +version_added: '3.20.0' |
| 28 | +extends_documentation_fragment: |
| 29 | + - netbox.netbox.common |
| 30 | +options: |
| 31 | + data: |
| 32 | + type: dict |
| 33 | + description: |
| 34 | + - Defines the L2VPN termination configuration |
| 35 | + suboptions: |
| 36 | + l2vpn: |
| 37 | + description: |
| 38 | + - L2vpn object id |
| 39 | + required: true |
| 40 | + type: int |
| 41 | + assigned_object_type: |
| 42 | + description: |
| 43 | + - Assigned object type |
| 44 | + required: true |
| 45 | + choices: |
| 46 | + - dcim.interface |
| 47 | + - ipam.vlan |
| 48 | + - virtualization.vminterface |
| 49 | + type: str |
| 50 | + assigned_object_id: |
| 51 | + description: |
| 52 | + - Assigned object id |
| 53 | + required: true |
| 54 | + type: int |
| 55 | + tags: |
| 56 | + description: |
| 57 | + - Any tags that the L2VPN termination may need to be associated with |
| 58 | + required: false |
| 59 | + type: list |
| 60 | + elements: raw |
| 61 | + custom_fields: |
| 62 | + description: |
| 63 | + - Must exist in NetBox |
| 64 | + required: false |
| 65 | + type: dict |
| 66 | + required: true |
| 67 | +""" |
| 68 | + |
| 69 | +EXAMPLES = r""" |
| 70 | +- name: "Test NetBox Tunnel Termination module" |
| 71 | + connection: local |
| 72 | + hosts: localhost |
| 73 | + gather_facts: false |
| 74 | +
|
| 75 | + tasks: |
| 76 | + - name: Create tunnel termination within NetBox with only required information |
| 77 | + netbox.netbox.netbox_tunnel_termination: |
| 78 | + netbox_url: http://netbox.local |
| 79 | + netbox_token: thisIsMyToken |
| 80 | + data: |
| 81 | + state: present |
| 82 | +
|
| 83 | + - name: Delete tunnel termination within NetBox |
| 84 | + netbox.netbox.netbox_tunnel_termination: |
| 85 | + netbox_url: http://netbox.local |
| 86 | + netbox_token: thisIsMyToken |
| 87 | + data: |
| 88 | + state: absent |
| 89 | +
|
| 90 | + - name: Create tunnel termination with all information |
| 91 | + netbox.netbox.netbox_tunnel_termination: |
| 92 | + netbox_url: http://netbox.local |
| 93 | + netbox_token: thisIsMyToken |
| 94 | + data: |
| 95 | + state: present |
| 96 | +""" |
| 97 | + |
| 98 | +RETURN = r""" |
| 99 | +tunnel termination: |
| 100 | + description: Serialized object as created or already existent within NetBox |
| 101 | + returned: success (when I(state=present)) |
| 102 | + type: dict |
| 103 | +msg: |
| 104 | + description: Message indicating failure or info about what has been achieved |
| 105 | + returned: always |
| 106 | + type: str |
| 107 | +""" |
| 108 | + |
| 109 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( |
| 110 | + NetboxAnsibleModule, |
| 111 | + NETBOX_ARG_SPEC, |
| 112 | +) |
| 113 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_vpn import ( |
| 114 | + NetboxVpnModule, |
| 115 | + NB_TUNNEL_TERMINATIONS, |
| 116 | +) |
| 117 | +from copy import deepcopy |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + """ |
| 122 | + Main entry point for module execution |
| 123 | + """ |
| 124 | + argument_spec = deepcopy(NETBOX_ARG_SPEC) |
| 125 | + argument_spec.update( |
| 126 | + dict( |
| 127 | + data=dict( |
| 128 | + type="dict", |
| 129 | + required=True, |
| 130 | + options=dict( |
| 131 | + tunnel=dict(required=True, type="str"), |
| 132 | + # status=dict(required=False, type="raw"), |
| 133 | + # tunnel_group=dict(required=False, type="raw"), |
| 134 | + # encapsulation=dict( |
| 135 | + # required=True, |
| 136 | + # type="str", |
| 137 | + # choices=[ |
| 138 | + # "ipsec-transport", |
| 139 | + # "ipsec-tunnel", |
| 140 | + # "ip-ip", |
| 141 | + # "gre", |
| 142 | + # ], |
| 143 | + # ), |
| 144 | + # ipsec_profile=dict(required=False, type="raw"), |
| 145 | + # tenant=dict(required=False, type="raw"), |
| 146 | + # tunnel_id=dict(required=False, type="int"), |
| 147 | + # description=dict(required=False, type="str"), |
| 148 | + # comments=dict(required=False, type="str"), |
| 149 | + # tags=dict(required=False, type="list", elements="raw"), |
| 150 | + # custom_fields=dict(required=False, type="dict"), |
| 151 | + ), |
| 152 | + ), |
| 153 | + ) |
| 154 | + ) |
| 155 | + |
| 156 | + required_if = [ |
| 157 | + ("state", "present", ["name"]), |
| 158 | + ("state", "absent", ["name"]), |
| 159 | + ] |
| 160 | + |
| 161 | + module = NetboxAnsibleModule( |
| 162 | + argument_spec=argument_spec, supports_check_mode=True, required_if=required_if |
| 163 | + ) |
| 164 | + |
| 165 | + netbox_tunnel_termination = NetboxVpnModule(module, NB_TUNNEL_TERMINATIONS) |
| 166 | + netbox_tunnel_termination.run() |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": # pragma: no cover |
| 170 | + main() |
0 commit comments