Skip to content

Commit

Permalink
Merge pull request #432 from pugnacity/fix_423
Browse files Browse the repository at this point in the history
create controller_managed_device_groups
  • Loading branch information
joewesch authored Jan 15, 2025
2 parents 077a62f + 4821d1c commit 3921a0a
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 2 deletions.
2 changes: 2 additions & 0 deletions plugins/lookup/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def get_endpoint(nautobot, term):
"circuits": {"endpoint": nautobot.circuits.circuits},
"circuit-providers": {"endpoint": nautobot.circuits.providers},
"cables": {"endpoint": nautobot.dcim.cables},
"controllers": {"endpoint": nautobot.dcim.controllers},
"controller-managed-device-groups": {"endpoint": nautobot.dcim.controller_managed_device_groups},
"cloud-accounts": {"endpoint": nautobot.cloud.cloud_accounts},
"cloud-networks": {"endpoint": nautobot.cloud.cloud_networks},
"cloud-network-prefix-assignments": {"endpoint": nautobot.cloud.cloud_network_prefix_assignments},
Expand Down
2 changes: 2 additions & 0 deletions plugins/module_utils/dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
NB_CONSOLE_SERVER_PORTS = "console_server_ports"
NB_CONSOLE_SERVER_PORT_TEMPLATES = "console_server_port_templates"
NB_CONTROLLERS = "controllers"
NB_CONTROLLER_MANAGED_DEVICE_GROUPS = "controller_managed_device_groups"
NB_DEVICE_BAYS = "device_bays"
NB_DEVICE_BAY_TEMPLATES = "device_bay_templates"
NB_DEVICE_REDUNDANCY_GROUPS = "device_redundancy_groups"
Expand Down Expand Up @@ -63,6 +64,7 @@ def run(self):
- console_server_ports
- console_server_port_templates
- controllers
- controller_managed_device_groups
- device_bays
- device_bay_templates
- devices
Expand Down
5 changes: 5 additions & 0 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"console_server_ports",
"console_server_port_templates",
"controllers",
"controller_managed_device_groups",
"device_bays",
"device_bay_templates",
"devices",
Expand Down Expand Up @@ -129,6 +130,7 @@
cluster_group="name",
cluster_type="name",
controller="name",
controller_managed_device_group="name",
device="name",
dynamic_group="name",
role="name",
Expand Down Expand Up @@ -196,6 +198,7 @@
"cluster_group": "cluster_groups",
"cluster_type": "cluster_types",
"contacts": "contacts",
"controller": "controllers",
"dcim.consoleport": "console_ports",
"dcim.consoleserverport": "console_server_ports",
"dcim.frontport": "front_ports",
Expand Down Expand Up @@ -291,6 +294,7 @@
"console_server_port_templates": "console_server_port_template",
"contacts": "contact",
"controllers": "controller",
"controller_managed_device_groups": "controller_managed_device_group",
"custom_fields": "custom_field",
"custom_field_choices": "custom_field_choice",
"device_bays": "device_bay",
Expand Down Expand Up @@ -375,6 +379,7 @@
"contact": set(["name", "phone", "email"]),
"contacts": set(["name", "phone", "email"]),
"controller": set(["name"]),
"controller_managed_device_group": set(["name"]),
"custom_field": set(["label"]),
"custom_field_choice": set(["value", "custom_field"]),
"dcim.consoleport": set(["name", "device"]),
Expand Down
123 changes: 123 additions & 0 deletions plugins/modules/controller_managed_device_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2024, Network to Code (@networktocode) <[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: controller_managed_device_group
short_description: Create, update or delete managed device groups within Nautobot
description:
- Creates, updates or removes managed device 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:
- Sven Winkelmann (@pugnacity)
version_added: "5.7.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
- networktocode.nautobot.fragments.tags
- networktocode.nautobot.fragments.custom_fields
options:
name:
description:
- The name of the controller managed device groups
required: true
type: str
controller:
description:
- The name of the controller for this group
required: true
type: str
weight:
description:
- weight of the managed device group
required: false
type: int
parent_cloud_network:
aliases:
- parent
description:
- The parent cloud network this network should be child to
required: false
type: raw
"""

EXAMPLES = r"""
- name: "Test Nautobot modules"
connection: local
hosts: localhost
gather_facts: False
tasks:
- name: Create controller managed device group within Nautobot with only required information
networktocode.nautobot.controller_managed_device_group:
url: http://nautobot.local
token: thisIsMyToken
name: "group_1"
controller: my_controller
state: present
- name: Delete controller managed device group within nautobot
networktocode.nautobot.controller_managed_device_group:
url: http://nautobot.local
token: thisIsMyToken
name: "group_1"
controller: test_controller_group_3
state: absent
"""

RETURN = r"""
controller_managed_device_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,
TAGS_ARG_SPEC,
CUSTOM_FIELDS_ARG_SPEC,
)
from ansible_collections.networktocode.nautobot.plugins.module_utils.dcim import (
NautobotDcimModule,
NB_CONTROLLER_MANAGED_DEVICE_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(deepcopy(TAGS_ARG_SPEC))
argument_spec.update(deepcopy(CUSTOM_FIELDS_ARG_SPEC))
argument_spec.update(
dict(
name=dict(required=True, type="str"),
controller=dict(required=True, type="str"),
weight=dict(required=False, type="int"),
parent_cloud_network=dict(required=False, type="raw", aliases=["parent"]),
)
)

module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

controller_group = NautobotDcimModule(module, NB_CONTROLLER_MANAGED_DEVICE_GROUPS)
controller_group.run()


if __name__ == "__main__": # pragma: no cover
main()
8 changes: 8 additions & 0 deletions plugins/modules/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@
required: false
type: raw
version_added: "5.1.0"
controller_managed_device_group:
description:
- Device controller_managed_device_group the device will be assigned to
- Requires Nautobot C(v2.2) or later
required: false
type: raw
version_added: "5.7.0"
device_redundancy_group_priority:
description:
- Priority in the assigned device redundancy group
Expand Down Expand Up @@ -290,6 +297,7 @@ def main():
vc_priority=dict(required=False, type="int"),
comments=dict(required=False, type="str"),
local_config_context_data=dict(required=False, type="dict"),
controller_managed_device_group=dict(required=False, type="raw"),
device_redundancy_group=dict(required=False, type="raw"),
device_redundancy_group_priority=dict(required=False, type="int"),
)
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/nautobot-populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,18 @@ def make_nautobot_calls(endpoint, payload):
contacts = [{"name": "My Contact"}, {"name": "My Contact 2"}]
created_contacts = make_nautobot_calls(nb.extras.contacts, contacts)

# Create Controller
controller = [
{"name": "controller_one", "location": location_child.id, "status": {"name": "Active"}},
{"name": "controller_two", "location": location_child.id, "status": {"name": "Active"}},
]
created_controller = make_nautobot_calls(nb.dcim.controllers, controller)

# Create Controller Managed Device Groups
test_controller_one = nb.dcim.controllers.get(name="controller_one")
controller_device_group = [{"name": "controller_group_one", "weight": "1000", "controller": test_controller_one.id}]
created_controller_device_group = make_nautobot_calls(nb.dcim.controller_managed_device_groups, controller_device_group)

###############
# v2.3+ items #
###############
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
- name: "CONTROLLER GROUP 1: Create Group"
networktocode.nautobot.controller_managed_device_group:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller Group One
controller: controller_one
weight: 1000
state: present
register: test_one

- name: "CONTROLLER 1: ASSERT - Create Group"
assert:
that:
- test_one['changed']
- test_one['controller_managed_device_group']['name'] == "Test Controller Group One"

- name: "CONTROLLER GROUP 2: Create duplicate"
networktocode.nautobot.controller_managed_device_group:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller Group One
controller: controller_one
weight: 1000
state: present
register: test_two

- name: "CONTROLLER 2: ASSERT - Create duplicate"
assert:
that:
- not test_two['changed']
- test_two['controller_managed_device_group']['name'] == "Test Controller Group One"
- test_two['msg'] == "controller_managed_device_group Test Controller Group One already exists"

- name: "CONTROLLER GROUP 3: Update"
networktocode.nautobot.controller_managed_device_group:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller Group One
weight: 2000
controller: controller_two
register: test_three

- name: "CONTROLLER GROUP 3: ASSERT - Update"
assert:
that:
- test_three['changed']
- test_three['controller_managed_device_group']['name'] == "Test Controller Group One"
- test_three['diff']['after']['weight'] == 2000
- test_three['msg'] == "controller_managed_device_group Test Controller Group One updated"

- name: "CONTROLLER GROUP 4: idempotent"
networktocode.nautobot.controller_managed_device_group:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller Group One
weight: 2000
controller: controller_two
register: test_four

- name: "CONTROLLER GROUP 4: ASSERT - idempotent"
assert:
that:
- not test_four['changed']
- test_four['controller_managed_device_group']['name'] == "Test Controller Group One"
- test_four['msg'] == "controller_managed_device_group Test Controller Group One already exists"

- name: "CONTROLLER GROUP 5: ASSERT - Delete"
networktocode.nautobot.controller_managed_device_group:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller Group One
controller: controller_two
state: absent
register: test_five

- name: "CONTROLLER 5: ASSERT - Delete"
assert:
that:
- test_five is changed
- test_five['diff']['before']['state'] == "present"
- test_five['diff']['after']['state'] == "absent"
- test_five['msg'] == "controller_managed_device_group Test Controller Group One deleted"

- name: "CONTROLLER GROUP 6: ASSERT - Delete non existing"
networktocode.nautobot.controller_managed_device_group:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller Group One
controller: controller_two
state: absent
register: test_six

- name: "CONTROLLER GROUP 6: ASSERT - Delete non existing"
assert:
that:
- not test_six['changed']
- test_six['controller_managed_device_group'] == None
- test_six['msg'] == "controller_managed_device_group Test Controller Group One already absent"

1 change: 1 addition & 0 deletions tests/integration/targets/latest/tasks/device.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
parent: "Parent Test Location"
rack: "Main Test Rack"
status: "Active"
controller_managed_device_group: "{{ 'controller_group_one' if nautobot_version is version('2.2', '>=') else omit }}"
position: 35
face: "Front"
tags:
Expand Down
13 changes: 11 additions & 2 deletions tests/integration/targets/latest/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
##########################

- name: "NAUTOBOT 2.2+ TESTS"
when:
when:
- "nautobot_version is version('2.2', '>=')"
block:
- name: "PYNAUTOBOT_CONTACT TESTS"
Expand Down Expand Up @@ -610,6 +610,15 @@
tags:
- controller

- name: "PYNAUTOBOT_CONTROLLER_MANAGED_DEVICE_GROUP TESTS"
include_tasks:
file: "controller_managed_device_group.yml"
apply:
tags:
- controller_managed_device_group
tags:
- controller_managed_device_group

- name: "PYNAUTOBOT_VLAN_LOCATION TESTS"
include_tasks:
file: "vlan_location.yml"
Expand All @@ -627,7 +636,7 @@
##########################

- name: "NAUTOBOT 2.3+ TESTS"
when:
when:
- "nautobot_version is version('2.3', '>=')"
block:
- name: "PYNAUTOBOT_CLOUD_ACCOUNT TESTS"
Expand Down

0 comments on commit 3921a0a

Please sign in to comment.