Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added module for FHRP groups #957

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
172 changes: 172 additions & 0 deletions plugins/modules/netbox_fhrp_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#!/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
rodvand marked this conversation as resolved.
Show resolved Hide resolved
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
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"""
- name: "Test NetBox modules"
connection: local
hosts: localhost
gather_facts: False

tasks:
- name: "Create FHRP group within netbox"
netbox_fhrp_group:
netbox_url: http://netbox.local
netbox_token: thisIsMyToken
rodvand marked this conversation as resolved.
Show resolved Hide resolved
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_fhrp_group:
netbox_url: http://netbox.local
netbox_token: thisIsMyToken
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