Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ action_groups:
- netbox_inventory_item
- netbox_ip_address
- netbox_ipam_role
- netbox_l2vpn
- netbox_location
- netbox_manufacturer
- netbox_platform
Expand Down
4 changes: 4 additions & 0 deletions plugins/module_utils/netbox_ipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
NB_VLAN_GROUPS = "vlan_groups"
NB_VRFS = "vrfs"
NB_SERVICES = "services"
NB_L2VPNS = "l2vpns"
NB_L2VPN_TERMINATIONS = "l2vpn_terminations"


class NetboxIpamModule(NetboxModule):
Expand Down Expand Up @@ -147,6 +149,8 @@ def run(self):
- aggregates
- ipam_roles
- ip_addresses
- l2vpns
- l2vpn_terminations
- prefixes
- rirs
- route_targets
Expand Down
6 changes: 6 additions & 0 deletions plugins/module_utils/netbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
ipam=[
"aggregates",
"ip_addresses",
"l2vpns",
"l2vpn_terminations",
"prefixes",
"rirs",
"roles",
Expand Down Expand Up @@ -126,6 +128,7 @@
group="slug",
installed_device="name",
import_targets="name",
l2vpn="name",
location="slug",
manufacturer="slug",
nat_inside="address",
Expand Down Expand Up @@ -306,6 +309,7 @@
"interface_templates": "interface_template",
"inventory_items": "inventory_item",
"ip_addresses": "ip_address",
"l2vpns": "l2vpn",
"locations": "location",
"manufacturers": "manufacturer",
"platforms": "platform",
Expand Down Expand Up @@ -408,6 +412,7 @@
"ipaddresses": set(
["address", "vrf", "device", "interface", "assigned_object", "virtual_machine"]
),
"l2vpn": set(["name"]),
"lag": set(["name"]),
"location": set(["slug"]),
"manufacturer": set(["slug"]),
Expand Down Expand Up @@ -556,6 +561,7 @@
"device_roles",
"device_types",
"ipam_roles",
"l2vpns",
"locations",
"rack_groups",
"rack_roles",
Expand Down
187 changes: 187 additions & 0 deletions plugins/modules/netbox_l2vpn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = r"""
---
module: netbox_l2vpn
short_description: Create, update or delete L2VPNs within NetBox
description:
- Creates, updates or removes L2VPNs from NetBox
notes:
- Tags should be defined as a YAML list
- This should be ran with connection C(local) and hosts C(localhost)
author:
- Martin Rødvand (@rodvand)
requirements:
- pynetbox
version_added: '3.9.0'
extends_documentation_fragment:
- netbox.netbox.common
options:
data:
type: dict
description:
- Defines the L2VPN configuration
suboptions:
name:
description:
- The name of the L2VPN
required: true
type: str
type:
description:
- The type of L2VPN
required: true
type: raw
identifier:
description:
- The identifier of the L2VPN
required: false
type: int
import_targets:
description:
- Route targets to import
required: false
type: list
elements: raw
export_targets:
description:
- Route targets to export
required: false
type: list
elements: raw
description:
description:
- The description of the L2VPN
required: false
type: str
tenant:
description:
- The tenant that the L2VPN will be assigned to
required: false
type: raw
tags:
description:
- Any tags that the L2VPN may need to be associated with
required: false
type: list
elements: raw
custom_fields:
description:
- Must exist in NetBox
required: false
type: dict
required: true
"""

EXAMPLES = r"""
- name: "Test NetBox modules"
connection: local
hosts: localhost
gather_facts: False

tasks:
- name: Create L2VPN within NetBox with only required information
netbox_l2vpn:
netbox_url: http://netbox.local
netbox_token: thisIsMyToken
data:
name: Test L2VPN
type: vxlan
state: present

- name: Delete L2VPN within netbox
netbox_vlan:
netbox_url: http://netbox.local
netbox_token: thisIsMyToken
data:
name: Test L2VPN
type: vxlan
state: absent

- name: Create L2VPN with all required information
netbox_vlan:
netbox_url: http://netbox.local
netbox_token: thisIsMyToken
data:
name: Test L2VPN
type: vpls
identifier: 43256
import_targets:
- "65000:1"
export_targets:
- "65000:2"
tenant: Test Tenant
description: Just a test
tags:
- Schnozzberry
state: present
"""

RETURN = r"""
l2vpn:
description: Serialized object as created or already existent within NetBox
returned: success (when I(state=present))
type: dict
msg:
description: Message indicating failure or info about what has been achieved
returned: always
type: str
"""

from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
NetboxAnsibleModule,
NETBOX_ARG_SPEC,
)
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_ipam import (
NetboxIpamModule,
NB_L2VPNS,
)
from copy import deepcopy


def main():
"""
Main entry point for module execution
"""
argument_spec = deepcopy(NETBOX_ARG_SPEC)
argument_spec.update(
dict(
data=dict(
type="dict",
required=True,
options=dict(
name=dict(required=True, type="str"),
type=dict(required=True, type="raw"),
identifier=dict(required=False, type="int"),
import_targets=dict(required=False, type="list", elements="raw"),
export_targets=dict(required=False, type="list", elements="raw"),
description=dict(required=False, type="str"),
tenant=dict(required=False, type="raw"),
tags=dict(required=False, type="list", elements="raw"),
custom_fields=dict(required=False, type="dict"),
),
),
)
)
required_if = [
("state", "present", ["name", "type"]),
("state", "absent", ["name", "type"]),
]

module = NetboxAnsibleModule(
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
)

netbox_l2vpn = NetboxIpamModule(module, NB_L2VPNS)
netbox_l2vpn.run()


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions tests/integration/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testgroup]
testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/Users/rodvand/collections/ansible_collections/netbox/netbox/venv/bin/python3.9"
12 changes: 11 additions & 1 deletion tests/integration/targets/v3.3/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---

- name: "NETBOX_DEVICE TESTS"
include_tasks: "netbox_device.yml"

Expand Down Expand Up @@ -235,4 +236,13 @@
tags:
- netbox_webhook
tags:
- netbox_webhook
- netbox_webhook

- name: "NETBOX_L2VPN TESTS"
include_tasks:
file: "netbox_l2vpn.yml"
apply:
tags:
- netbox_l2vpn
tags:
- netbox_l2vpn
99 changes: 99 additions & 0 deletions tests/integration/targets/v3.3/tasks/netbox_l2vpn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
##
##
### NETBOX_L2VPN
##
##
- name: "L2VPN 1: Necessary info creation"
netbox.netbox.netbox_l2vpn:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
name: Test L2VPN
type: vxlan
state: present
register: test_one

- name: "L2VPN 1: ASSERT - Necessary info creation"
assert:
that:
- test_one is changed
- test_one['diff']['before']['state'] == "absent"
- test_one['diff']['after']['state'] == "present"
- test_one['l2vpn']['name'] == "Test L2VPN"
- test_one['l2vpn']['type'] == "vxlan"
- test_one['msg'] == "l2vpn Test L2VPN created"

- name: "L2VPN 2: Create duplicate"
netbox.netbox.netbox_l2vpn:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
name: Test L2VPN
type: vxlan
state: present
register: test_two

- name: "L2VPN 2: ASSERT - Create duplicate"
assert:
that:
- not test_two['changed']
- test_two['l2vpn']['name'] == "Test L2VPN"
- test_two['l2vpn']['type'] == "vxlan"
- test_two['msg'] == "l2vpn Test L2VPN already exists"

- name: "L2VPN 4: ASSERT - Update"
netbox.netbox.netbox_l2vpn:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
name: "Test L2VPN"
type: vxlan
tenant: "Test Tenant"
description: Updated description
import_targets:
- "4000:4000"
- "5000:5000"
export_targets:
- "6000:6000"
tags:
- "Schnozzberry"
state: present
register: test_four

- name: "L2VPN: ASSERT - Updated"
assert:
that:
- test_four is changed
- test_four['diff']['after']['description'] == "Updated description"
- test_four['diff']['after']['import_targets'] == [1, 2]
- test_four['diff']['after']['export_targets'] == [3]
- test_four['diff']['after']['tags'][0] == 4
- test_four['l2vpn']['name'] == "Test L2VPN"
- test_four['l2vpn']['tenant'] == 1
- test_four['l2vpn']['import_targets'] == [1, 2]
- test_four['l2vpn']['export_targets'] == [3]
- test_four['l2vpn']['description'] == "Updated description"
- test_four['l2vpn']['tags'][0] == 4
- test_four['msg'] == "l2vpn Test L2VPN updated"

- name: "L2VPN: ASSERT - Delete"
netbox.netbox.netbox_l2vpn:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
name: "Test L2VPN"
type: vxlan
state: absent
register: test_six

- name: "L2VPN 6: ASSERT - Delete"
assert:
that:
- test_six is changed
- test_six['l2vpn']['name'] == "Test L2VPN"
- test_six['l2vpn']['tenant'] == 1
- test_six['l2vpn']['type'] == "vxlan"
- test_six['l2vpn']['description'] == "Updated description"
- test_six['l2vpn']['tags'][0] == 4
- test_six['msg'] == "l2vpn Test L2VPN deleted"