forked from netbox-community/ansible_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: L2vpn termination module (netbox-community#994)
* Added netbox_l2vps_termination module * Added tests for netbox_l2vps_termination module * Added L2VPNs creation to netbox-deploy.py
- Loading branch information
1 parent
6e7edaf
commit 9536dfe
Showing
9 changed files
with
402 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2023, Andrii Konts (@andrii-konts) <[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_termination | ||
short_description: Create, update or delete L2VPNs terminations within NetBox | ||
description: | ||
- Creates, updates or removes L2VPNs terminations from NetBox | ||
notes: | ||
- Tags should be defined as a YAML list | ||
- This should be ran with connection C(local) and hosts C(localhost) | ||
author: | ||
- Andrii Konts (@andrii-konts) | ||
requirements: | ||
- pynetbox | ||
seealso: | ||
- name: FHRP Group Model reference | ||
description: NetBox Documentation for FHRP Group Model. | ||
link: https://docs.netbox.dev/en/stable/models/ipam/l2vpntermination/ | ||
version_added: '3.13.0' | ||
extends_documentation_fragment: | ||
- netbox.netbox.common | ||
options: | ||
data: | ||
type: dict | ||
description: | ||
- Defines the L2VPN termination configuration | ||
suboptions: | ||
l2vpn: | ||
description: | ||
- L2vpn object id | ||
required: true | ||
type: int | ||
assigned_object_type: | ||
description: | ||
- Assigned object type | ||
required: true | ||
choices: | ||
- dcim.interface | ||
- ipam.vlan | ||
- virtualization.vminterface | ||
type: str | ||
assigned_object_id: | ||
description: | ||
- Assigned object id | ||
required: true | ||
type: int | ||
tags: | ||
description: | ||
- Any tags that the L2VPN termination 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""" | ||
- hosts: localhost | ||
connection: local | ||
module_defaults: | ||
group/netbox.netbox.netbox: | ||
netbox_url: "http://netbox.local" | ||
netbox_token: "thisIsMyToken" | ||
tasks: | ||
- name: Create L2VPN termination within NetBox with only required information | ||
netbox.netbox.netbox_l2vpn_termination: | ||
data: | ||
l2vpn: 1 | ||
assigned_object_type: dcim.interface | ||
assigned_object_id: 32 | ||
state: present | ||
- name: Delete L2VPN termination within netbox | ||
netbox.netbox.netbox_l2vpn_termination: | ||
data: | ||
l2vpn: 1 | ||
assigned_object_type: dcim.interface | ||
assigned_object_id: 32 | ||
state: absent | ||
""" | ||
|
||
RETURN = r""" | ||
l2vpn_termination: | ||
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_L2VPN_TERMINATIONS, | ||
) | ||
|
||
|
||
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( | ||
l2vpn=dict(required=True, type="int"), | ||
assigned_object_type=dict( | ||
required=True, | ||
choices=[ | ||
"dcim.interface", | ||
"ipam.vlan", | ||
"virtualization.vminterface", | ||
], | ||
), | ||
assigned_object_id=dict(required=True, type="int"), | ||
tags=dict(required=False, type="list", elements="raw"), | ||
custom_fields=dict(required=False, type="dict"), | ||
), | ||
), | ||
) | ||
) | ||
|
||
module = NetboxAnsibleModule(argument_spec=argument_spec, supports_check_mode=True) | ||
netbox_l2vpn_termination = NetboxIpamModule(module, NB_L2VPN_TERMINATIONS) | ||
netbox_l2vpn_termination.run() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
tests/integration/targets/v3.3/tasks/netbox_l2vpn_termination.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--- | ||
## | ||
## | ||
### NETBOX_L2VPN_TERMINATION | ||
## | ||
## | ||
- name: "L2VPN_TERMINATION 1: Necessary info creation" | ||
netbox.netbox.netbox_l2vpn_termination: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
l2vpn: 1 | ||
assigned_object_type: dcim.interface | ||
assigned_object_id: 1 | ||
state: present | ||
register: test_one | ||
|
||
- name: "L2VPN_TERMINATION 1: ASSERT - Necessary info creation" | ||
ansible.builtin.assert: | ||
that: | ||
- test_one is changed | ||
- test_one['diff']['before']['state'] == "absent" | ||
- test_one['diff']['after']['state'] == "present" | ||
- test_one['l2vpn_termination']['l2vpn'] == 1 | ||
- test_one['l2vpn_termination']['assigned_object_type'] == "dcim.interface" | ||
- test_one['l2vpn_termination']['assigned_object_id'] == 1 | ||
- test_one['msg'] == "l2vpn_termination l2vpn 1 <> dcim.interface 1 created" | ||
|
||
- name: "L2VPN_TERMINATION 2: Create duplicate" | ||
netbox.netbox.netbox_l2vpn_termination: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
l2vpn: 1 | ||
assigned_object_type: dcim.interface | ||
assigned_object_id: 1 | ||
state: present | ||
register: test_two | ||
|
||
- name: "L2VPN_TERMINATION 2: ASSERT - Create duplicate" | ||
ansible.builtin.assert: | ||
that: | ||
- not test_two['changed'] | ||
- test_two['l2vpn_termination']['l2vpn'] == 1 | ||
- test_two['l2vpn_termination']['assigned_object_type'] == "dcim.interface" | ||
- test_two['l2vpn_termination']['assigned_object_id'] == 1 | ||
- test_two['msg'] == "l2vpn_termination l2vpn 1 <> dcim.interface 1 already exists" | ||
|
||
- name: "L2VPN_TERMINATION 3: Update" | ||
netbox.netbox.netbox_l2vpn_termination: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
l2vpn: 1 | ||
assigned_object_type: dcim.interface | ||
assigned_object_id: 1 | ||
tags: | ||
- "Schnozzberry" | ||
state: present | ||
register: test_three | ||
|
||
- name: "L2VPN_TERMINATION 3: ASSERT - Updated" | ||
ansible.builtin.assert: | ||
that: | ||
- test_three is changed | ||
- test_three['diff']['after']['tags'][0] == 4 | ||
- test_three['l2vpn_termination']['l2vpn'] == 1 | ||
- test_three['l2vpn_termination']['assigned_object_type'] == "dcim.interface" | ||
- test_three['l2vpn_termination']['assigned_object_id'] == 1 | ||
- test_three['l2vpn_termination']['tags'][0] == 4 | ||
- test_three['msg'] == "l2vpn_termination l2vpn 1 <> dcim.interface 1 updated" | ||
|
||
- name: "L2VPN_TERMINATION 4: Delete" | ||
netbox.netbox.netbox_l2vpn_termination: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
l2vpn: 1 | ||
assigned_object_type: dcim.interface | ||
assigned_object_id: 1 | ||
state: absent | ||
register: test_four | ||
|
||
- name: "L2VPN_TERMINATION 4: ASSERT - Delete" | ||
ansible.builtin.assert: | ||
that: | ||
- test_four is changed | ||
- test_four['diff']['before']['state'] == "present" | ||
- test_four['diff']['after']['state'] == "absent" | ||
- test_four['l2vpn_termination']['l2vpn'] == 1 | ||
- test_four['l2vpn_termination']['assigned_object_type'] == "dcim.interface" | ||
- test_four['l2vpn_termination']['assigned_object_id'] == 1 | ||
- test_four['l2vpn_termination']['tags'][0] == 4 | ||
- test_four['msg'] == "l2vpn_termination l2vpn 1 <> dcim.interface 1 deleted" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.