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

Adds device redundancy group module #282

Merged
merged 2 commits into from
Nov 29, 2023
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
3 changes: 3 additions & 0 deletions plugins/lookup/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def get_endpoint(nautobot, term):
"device-bay-templates": {"endpoint": nautobot.dcim.device_bay_templates},
"device-bays": {"endpoint": nautobot.dcim.device_bays},
"device-types": {"endpoint": nautobot.dcim.device_types},
"device-redundancy-groups": {"endpoint": nautobot.dcim.device_redundancy_groups},
"devices": {"endpoint": nautobot.dcim.devices},
"export-templates": {"endpoint": nautobot.dcim.export_templates},
"front-port-templates": {"endpoint": nautobot.dcim.front_port_templates},
Expand Down Expand Up @@ -193,6 +194,8 @@ def get_endpoint(nautobot, term):
"reports": {"endpoint": nautobot.extras.reports},
"rirs": {"endpoint": nautobot.ipam.rirs},
"roles": {"endpoint": nautobot.extras.roles},
"secrets": {"endpoint": nautobot.extras.secrets},
"secrets-groups": {"endpoint": nautobot.extras.secrets_groups},
"services": {"endpoint": nautobot.ipam.services},
"statuses": {"endpoint": nautobot.extras.statuses},
"tags": {"endpoint": nautobot.extras.tags},
Expand Down
1 change: 1 addition & 0 deletions plugins/module_utils/dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
NB_CONSOLE_SERVER_PORT_TEMPLATES = "console_server_port_templates"
NB_DEVICE_BAYS = "device_bays"
NB_DEVICE_BAY_TEMPLATES = "device_bay_templates"
NB_DEVICE_REDUNDANCY_GROUPS = "device_redundancy_groups"
NB_DEVICES = "devices"
NB_ROLES = "roles"
NB_DEVICE_TYPES = "device_types"
Expand Down
3 changes: 3 additions & 0 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"device_bay_templates",
"devices",
"device_types",
"device_redundancy_groups",
"front_ports",
"front_port_templates",
"interfaces",
Expand Down Expand Up @@ -215,6 +216,7 @@
"device_bay_templates": "device_bay_template",
"devices": "device",
"device_types": "device_type",
"device_redundancy_groups": "device_redundancy_group",
"front_ports": "front_port",
"front_port_templates": "front_port_template",
"interfaces": "interface",
Expand Down Expand Up @@ -279,6 +281,7 @@
"device_bay": set(["name", "device"]),
"device_bay_template": set(["name", "device_type"]),
"device": set(["name"]),
"device_redundancy_group": set(["name"]),
"device_type": set(["model"]),
"front_port": set(["name", "device", "rear_port"]),
"front_port_template": set(["name", "device_type", "rear_port_template"]),
Expand Down
14 changes: 14 additions & 0 deletions plugins/modules/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@
required: false
type: dict
version_added: "3.0.0"
device_redundancy_group:
description:
- Device redundancy group the device will be assigned to
required: false
type: raw
version_added: "5.1.0"
device_redundancy_group_priority:
description:
- Priority in the assigned device redundancy group
required: false
type: int
version_added: "5.1.0"
"""

EXAMPLES = r"""
Expand Down Expand Up @@ -276,6 +288,8 @@ def main():
tags=dict(required=False, type="list", elements="raw"),
local_config_context_data=dict(required=False, type="dict"),
custom_fields=dict(required=False, type="dict"),
device_redundancy_group=dict(required=False, type="raw"),
device_redundancy_group_priority=dict(required=False, type="int"),
)
)

Expand Down
153 changes: 153 additions & 0 deletions plugins/modules/device_redundancy_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2023, Network to Code (@networktocode) <info@networktocode.com>
# 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: device_redundancy_group
short_description: Creates or removes device redundancy groups from Nautobot
description:
- Creates or removes device redundancy groups from Nautobot
notes:
- Tags should be defined as a YAML list
- This should be ran with connection C(local) and hosts C(localhost)
author:
- Joe Wesch (@joewesch)
requirements:
- pynautobot
version_added: "5.1.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
- networktocode.nautobot.fragments.tags
- networktocode.nautobot.fragments.custom_fields
options:
name:
description:
- The name of the device redundancy group
required: true
type: str
version_added: "5.1.0"
status:
description:
- The status of the device redundancy group
- Required if I(state=present) and the device redundancy group does not exist yet
required: false
type: raw
version_added: "5.1.0"
description:
description:
- The description of the device redundancy group
required: false
type: str
version_added: "5.1.0"
failover_strategy:
description:
- The failover strategy of the device redundancy group
required: false
choices:
- active-active
- active-passive
type: str
version_added: "5.1.0"
secrets_group:
description:
- The secrets group of the device redundancy group
required: false
type: raw
version_added: "5.1.0"
"""

EXAMPLES = r"""
- name: "Test Nautobot device_redundancy_group module"
connection: local
hosts: localhost
gather_facts: False

tasks:
- name: Create device redundancy group within Nautobot with only required information
networktocode.nautobot.device_redundancy_group:
url: http://nautobot.local
token: thisIsMyToken
name: My Redundancy Group
status: Active
state: present

- name: Create device redundancy group within Nautobot with all information
networktocode.nautobot.device_redundancy_group:
url: http://nautobot.local
token: thisIsMyToken
name: My Redundancy Group
status: Active
description: My Description
failover_strategy: active-active
secrets_group: "{{ my_secrets_group['key'] }}"
tags:
- My Tag
custom_fields:
my_field: my_value
state: present
vars:
my_secrets_group: "{{ lookup('networktocode.nautobot.lookup', 'secrets-groups', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Secrets Group\"') }}"

- name: Delete device redundancy group within nautobot
networktocode.nautobot.device_redundancy_group:
url: http://nautobot.local
token: thisIsMyToken
name: My Redundancy Group
state: absent

"""

RETURN = r"""
device_redundancy_group:
description: Serialized object as created or already existent within Nautobot
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.networktocode.nautobot.plugins.module_utils.utils import NAUTOBOT_ARG_SPEC
from ansible_collections.networktocode.nautobot.plugins.module_utils.dcim import (
NautobotDcimModule,
NB_DEVICE_REDUNDANCY_GROUPS,
)
from ansible.module_utils.basic import AnsibleModule
from copy import deepcopy


def main():
"""
Main entry point for module execution
"""
argument_spec = deepcopy(NAUTOBOT_ARG_SPEC)
argument_spec.update(
dict(
name=dict(required=True, type="str"),
status=dict(required=False, type="raw"),
description=dict(required=False, type="str"),
failover_strategy=dict(
required=False,
type="str",
choices=["active-active", "active-passive"],
),
secrets_group=dict(required=False, type="raw"),
tags=dict(required=False, type="list", elements="raw"),
custom_fields=dict(required=False, type="dict"),
)
)

module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
device_redundancy_group = NautobotDcimModule(module, NB_DEVICE_REDUNDANCY_GROUPS)
device_redundancy_group.run()


if __name__ == "__main__": # pragma: no cover
main()
36 changes: 36 additions & 0 deletions tests/integration/nautobot-populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,5 +538,41 @@ def make_nautobot_calls(endpoint, payload):
]
created_relationships = make_nautobot_calls(nb.extras.relationships, relationships)

# Create Secrets
secrets = [
{
"name": "Test Secret",
"provider": "environment-variable",
"parameters": {
"variable": "TEST_ENV_VAR",
},
}
]
created_secrets = make_nautobot_calls(nb.extras.secrets, secrets)
test_secret = nb.extras.secrets.get(name="Test Secret")

secrets_groups = [
{
"name": "Test Secrets Group",
"secrets": [
{
"secret": test_secret.id,
"access_type": "Generic",
"secret_type": "secret",
}
],
}
]
created_secrets_groups = make_nautobot_calls(nb.extras.secrets_groups, secrets_groups)

# Create Device Redundancy Groups
device_redundancy_groups = [
{
"name": "My Device Redundancy Group",
"status": "Active",
}
]
created_device_redundancy_groups = make_nautobot_calls(nb.dcim.device_redundancy_groups, device_redundancy_groups)

if ERRORS:
sys.exit("Errors have occurred when creating objects, and should have been printed out. Check previous output.")
5 changes: 5 additions & 0 deletions tests/integration/targets/latest/tasks/device.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
vc1: "{{ lookup('networktocode.nautobot.lookup', 'virtual-chassis', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=VC1') }}"
staged: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Staged') }}"
active: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Active') }}"
device_redundancy_group: "{{ lookup('networktocode.nautobot.lookup', 'device-redundancy-groups', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Device Redundancy Group\"') }}"

- name: "1 - Device with required information"
networktocode.nautobot.device:
Expand Down Expand Up @@ -95,6 +96,8 @@
virtual_chassis: "VC1"
vc_position: 3
vc_priority: 15
device_redundancy_group: "{{ device_redundancy_group['key'] }}"
device_redundancy_group_priority: 10
state: present
register: test_three

Expand All @@ -117,6 +120,8 @@
- test_three['device']['virtual_chassis'] == vc1['key']
- test_three['device']['vc_position'] == 3
- test_three['device']['vc_priority'] == 15
- test_three['device']['device_redundancy_group'] == device_redundancy_group['key']
- test_three['device']['device_redundancy_group_priority'] == 10
- test_three['msg'] == "device R1 updated"

- name: "3.1 - Update device status"
Expand Down
Loading