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: Added module for FHRP group assignments (netbox-community#974)
* Added fhrp_group_assignment module * Added fhrp groups creation to netbox-deploy * Added tests for fhrp_group_assignment module * Added netbox_fhrp_group_assignment to runtime.yml
- Loading branch information
1 parent
f839158
commit 4916f48
Showing
11 changed files
with
474 additions
and
0 deletions.
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,143 @@ | ||
#!/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_fhrp_group_assignment | ||
short_description: Create, update or delete FHRP group assignments within NetBox | ||
description: | ||
- Creates, updates or removes FHRP group assignments 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/fhrpgroupassignment/ | ||
extends_documentation_fragment: | ||
- netbox.netbox.common | ||
options: | ||
data: | ||
type: dict | ||
description: | ||
- Defines the FHRP group assignment configuration | ||
suboptions: | ||
fhrp_group: | ||
description: | ||
- FHRP Group ID | ||
required: True | ||
type: int | ||
interface_type: | ||
description: | ||
- Interface type | ||
required: True | ||
choices: | ||
- dcim.interface | ||
- virtualization.vminterface | ||
type: str | ||
interface_id: | ||
description: | ||
- Interface ID | ||
type: int | ||
required: True | ||
priority: | ||
description: | ||
- Priority (0 .. 255) | ||
type: int | ||
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 FHRP group assignment within netbox" | ||
netbox.netbox.netbox_fhrp_group_assignment: | ||
data: | ||
fhrp_group: 3 | ||
interface_type: dcim.interface | ||
interface_id: 5 | ||
priority: 1 | ||
state: present | ||
- name: Delete FHRP group assignment within netbox | ||
netbox.netbox.netbox_fhrp_group_assignment: | ||
data: | ||
fhrp_group: 3 | ||
interface_type: dcim.interface | ||
interface_id: 5 | ||
state: absent | ||
""" | ||
|
||
RETURN = r""" | ||
fhrp_group: | ||
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_FHRP_GROUP_ASSIGNMENTS, | ||
) | ||
|
||
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( | ||
fhrp_group=dict(required=True, type="int"), | ||
interface_type=dict( | ||
required=True, | ||
type="str", | ||
choices=["dcim.interface", "virtualization.vminterface"], | ||
), | ||
interface_id=dict(required=True, type="int"), | ||
priority=dict(type="int"), | ||
), | ||
), | ||
) | ||
) | ||
required_if = [("state", "present", ["priority"])] | ||
|
||
module = NetboxAnsibleModule(argument_spec=argument_spec, required_if=required_if) | ||
|
||
netbox_fhrp_group = NetboxIpamModule(module, NB_FHRP_GROUP_ASSIGNMENTS) | ||
netbox_fhrp_group.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
92 changes: 92 additions & 0 deletions
92
tests/integration/targets/v3.2/tasks/netbox_fhrp_group_assignment.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,92 @@ | ||
--- | ||
## | ||
## | ||
### NETBOX_FHRP_GROUP_ASSIGNMENT | ||
## | ||
## | ||
- name: "FHRP group assignment 1: Test FHRP group assignment creation" | ||
netbox.netbox.netbox_fhrp_group_assignment: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
fhrp_group: 1 | ||
interface_type: dcim.interface | ||
interface_id: 1 | ||
priority: 1 | ||
state: present | ||
register: test_one | ||
|
||
- name: "FHRP group assignment: 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['fhrp_group_assignment']['group'] == 1 | ||
- test_one['fhrp_group_assignment']['interface_type'] == "dcim.interface" | ||
- test_one['fhrp_group_assignment']['interface_id'] == 1 | ||
- test_one['fhrp_group_assignment']['priority'] == 1 | ||
- test_one['msg'] == "fhrp_group_assignment fhrp_group 1 > dcim.interface 1 created" | ||
|
||
- name: "FHRP group assignment 2: Create duplicate" | ||
netbox.netbox.netbox_fhrp_group_assignment: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
fhrp_group: 1 | ||
interface_type: dcim.interface | ||
interface_id: 1 | ||
priority: 1 | ||
state: present | ||
register: test_two | ||
|
||
- name: "FHRP group assignment 2: ASSERT - Create duplicate" | ||
ansible.builtin.assert: | ||
that: | ||
- not test_two['changed'] | ||
- test_two['fhrp_group_assignment']['group'] == 1 | ||
- test_two['fhrp_group_assignment']['interface_type'] == "dcim.interface" | ||
- test_two['fhrp_group_assignment']['interface_id'] == 1 | ||
- test_two['fhrp_group_assignment']['priority'] == 1 | ||
- test_two['msg'] == "fhrp_group_assignment fhrp_group 1 > dcim.interface 1 already exists" | ||
|
||
- name: "FHRP group assignment 3: Update FHRP group assignment" | ||
netbox.netbox.netbox_fhrp_group_assignment: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
fhrp_group: 1 | ||
interface_type: dcim.interface | ||
interface_id: 1 | ||
priority: 2 | ||
state: present | ||
register: test_three | ||
|
||
- name: "FHRP group assignment 3: ASSERT - Update FHRP group assignment" | ||
ansible.builtin.assert: | ||
that: | ||
- test_three is changed | ||
- test_three['fhrp_group_assignment']['group'] == 1 | ||
- test_three['fhrp_group_assignment']['interface_type'] == "dcim.interface" | ||
- test_three['fhrp_group_assignment']['interface_id'] == 1 | ||
- test_three['fhrp_group_assignment']['priority'] == 2 | ||
- test_three['msg'] == "fhrp_group_assignment fhrp_group 1 > dcim.interface 1 updated" | ||
|
||
- name: "FHRP group assignment 4: Delete FHRP group assignment" | ||
netbox.netbox.netbox_fhrp_group_assignment: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
fhrp_group: 1 | ||
interface_type: dcim.interface | ||
interface_id: 1 | ||
state: absent | ||
register: test_four | ||
|
||
- name: "FHRP group assignment 3: ASSERT - Delete FHRP group assignment" | ||
ansible.builtin.assert: | ||
that: | ||
- test_four is changed | ||
- test_four['diff']['before']['state'] == "present" | ||
- test_four['diff']['after']['state'] == "absent" | ||
- test_four['msg'] == "fhrp_group_assignment fhrp_group 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.