Skip to content

Commit

Permalink
Feature: Added module for FHRP groups (netbox-community#957)
Browse files Browse the repository at this point in the history
* Feature: Added module for FHRP groups
* Added tests for the FHRP groups module
* Updated documentation for fhrp_group module
  • Loading branch information
andrii-konts authored and rodvand committed May 1, 2023
1 parent 4117a89 commit 028924d
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/module_utils/netbox_ipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

NB_AGGREGATES = "aggregates"
NB_ASNS = "asns"
NB_FHRP_GROUPS = "fhrp_groups"
NB_IP_ADDRESSES = "ip_addresses"
NB_PREFIXES = "prefixes"
NB_IPAM_ROLES = "roles"
Expand Down Expand Up @@ -150,6 +151,7 @@ def run(self):
Supported endpoints:
- aggregates
- asns
- fhrp_groups
- ipam_roles
- ip_addresses
- l2vpns
Expand Down Expand Up @@ -186,6 +188,8 @@ def run(self):
name = data.get("prefix")
elif self.endpoint == "asns":
name = data.get("asn")
elif self.endpoint == "fhrp_groups":
name = data.get("group_id")
else:
name = data.get("name")

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 @@ -87,6 +87,7 @@
ipam=[
"aggregates",
"asns",
"fhrp_groups",
"ip_addresses",
"l2vpns",
"l2vpn_terminations",
Expand Down Expand Up @@ -130,6 +131,7 @@
device_type="slug",
export_targets="name",
export_template="name",
fhrp_groups="group_id",
group="slug",
installed_device="name",
inventory_item_role="name",
Expand Down Expand Up @@ -313,6 +315,7 @@
"device_roles": "device_role",
"device_types": "device_type",
"export_templates": "export_template",
"fhrp_groups": "fhrp_group",
"front_ports": "front_port",
"front_port_templates": "front_port_template",
"interfaces": "interface",
Expand Down Expand Up @@ -413,6 +416,9 @@
"device_role": set(["slug"]),
"device_type": set(["slug"]),
"export_template": set(["name"]),
"fhrp_group": set(
["id", "group_id", "interface_type", "device", "virtual_machine"]
),
"front_port": set(["name", "device", "rear_port"]),
"front_port_template": set(["name", "device_type", "rear_port"]),
"installed_device": set(["name"]),
Expand Down
173 changes: 173 additions & 0 deletions plugins/modules/netbox_fhrp_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/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
short_description: Create, update or delete FHRP groups within NetBox
description:
- Creates, updates or removes FHRP groups 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/fhrpgroup/
version_added: '3.12.0'
extends_documentation_fragment:
- netbox.netbox.common
options:
data:
type: dict
description:
- Defines the FHRP group configuration
suboptions:
protocol:
description:
- Protocol
required: False
type: str
choices:
- vrrp2
- vrrp3
- carp
- clusterxl
- hsrp
- glbp
- other
group_id:
description:
- Group ID (0 .. 32767)
type: int
required: true
auth_type:
description:
- Authentication type
choices:
- plaintext
- md5
type: str
auth_key:
description:
- Authentication key (max length 255)
type: str
description:
description:
- Description (max length 200)
required: false
type: str
tags:
description:
- Any tags that the FHRP group 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 FHRP group within netbox"
netbox.netbox.netbox_fhrp_group:
data:
protocol: "glbp"
group_id: 111
auth_type: md5
auth_key: 11111
description: test FHRP group
state: present
- name: Delete FHRP group within netbox
netbox.netbox.netbox_fhrp_group:
data:
group_id: 111
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_GROUPS,
)

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(
protocol=dict(
type="str",
choices=[
"vrrp2",
"vrrp3",
"carp",
"clusterxl",
"hsrp",
"glbp",
"other",
],
),
group_id=dict(required=True, type="int"),
auth_type=dict(type="str", choices=["plaintext", "md5"]),
auth_key=dict(type="str", no_log=True),
description=dict(type="str"),
tags=dict(required=False, type="list", elements="raw"),
custom_fields=dict(required=False, type="dict"),
),
),
)
)
required_if = [("state", "present", ["protocol"])]
module = NetboxAnsibleModule(argument_spec=argument_spec, required_if=required_if)
netbox_fhrp_group = NetboxIpamModule(module, NB_FHRP_GROUPS)
netbox_fhrp_group.run()


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions tests/integration/targets/v3.2/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,6 @@

- name: "NETBOX_ASN TESTS"
include_tasks: "netbox_asn.yml"

- name: "NETBOX_FHRP_GROUP TESTS"
include_tasks: "netbox_fhrp_group.yml"
91 changes: 91 additions & 0 deletions tests/integration/targets/v3.2/tasks/netbox_fhrp_group.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
##
##
### NETBOX_FHRP_GROUP
##
##
- name: "FHRP group 1: Test FHRP group creation"
netbox.netbox.netbox_fhrp_group:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
protocol: "glbp"
group_id: 111
state: present
register: test_one

- name: "FHRP group: 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']['group_id'] == 111
- test_one['fhrp_group']['protocol'] == "glbp"
- test_one['msg'] == "fhrp_group 111 created"

- name: "FHRP group 2: Create duplicate"
netbox.netbox.netbox_fhrp_group:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
protocol: "glbp"
group_id: 111
state: present
register: test_two

- name: "FHRP group 2: ASSERT - Create duplicate"
ansible.builtin.assert:
that:
- not test_two['changed']
- test_two['fhrp_group']['group_id'] == 111
- test_two['fhrp_group']['protocol'] == "glbp"
- test_two['msg'] == "fhrp_group 111 already exists"

- name: "FHRP group 3: Update FHRP group with other fields"
netbox.netbox.netbox_fhrp_group:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
protocol: "glbp"
group_id: 111
auth_type: md5
auth_key: 11111
description: Test description
tags:
- "Schnozzberry"
state: present
register: test_three

- name: "FHRP group 3: ASSERT - Update FHRP group with other fields"
ansible.builtin.assert:
that:
- test_three is changed
- test_three['diff']['after']['auth_type'] == "md5"
- test_three['diff']['after']['auth_key'] == "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
- test_three['diff']['after']['description'] == "Test description"
- test_three['diff']['after']['tags'][0] == 4
- test_three['fhrp_group']['group_id'] == 111
- test_three['fhrp_group']['protocol'] == "glbp"
- test_three['fhrp_group']['auth_type'] == "md5"
- test_three['fhrp_group']['auth_key'] == "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
- test_three['fhrp_group']['description'] == "Test description"
- test_three['fhrp_group']['tags'][0] == 4
- test_three['msg'] == "fhrp_group 111 updated"

- name: "FHRP group 4: ASSERT - Delete"
netbox.netbox.netbox_fhrp_group:
netbox_url: http://localhost:32768
netbox_token: 0123456789abcdef0123456789abcdef01234567
data:
group_id: 111
state: absent
register: test_four

- name: "FHRP group 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['msg'] == "fhrp_group 111 deleted"
3 changes: 3 additions & 0 deletions tests/integration/targets/v3.3/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,6 @@

- name: "NETBOX_ASN TESTS"
include_tasks: "netbox_asn.yml"

- name: "NETBOX_FHRP_GROUP TESTS"
include_tasks: "netbox_fhrp_group.yml"
Loading

0 comments on commit 028924d

Please sign in to comment.