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

create controller_managed_device_groups #432

Open
wants to merge 32 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a3664dc
create controller_managed_device_groups
pugnacity Oct 1, 2024
b78beca
fix use correct type
pugnacity Oct 1, 2024
4f8dd5e
fix version_added
pugnacity Oct 1, 2024
ac4c601
added devices
pugnacity Oct 1, 2024
8f16eb6
assign controller_managed_device_group
pugnacity Oct 2, 2024
9dca56f
Update plugins/modules/controller_managed_device_group.py
pugnacity Oct 2, 2024
baf7812
Update plugins/modules/controller_managed_device_group.py
pugnacity Oct 2, 2024
9fe407a
Update plugins/modules/controller_managed_device_group.py
pugnacity Oct 2, 2024
eb1580f
Update plugins/modules/controller_managed_device_group.py
pugnacity Oct 2, 2024
ce4111c
Update plugins/modules/device.py
pugnacity Oct 2, 2024
e884893
added tests
pugnacity Oct 2, 2024
10ec4da
lint fix
pugnacity Oct 2, 2024
7bebac4
try to fix populate
pugnacity Oct 2, 2024
61d8e57
fix test
pugnacity Oct 7, 2024
3ddca0b
fix use location id
pugnacity Oct 7, 2024
a04775f
fix tests
pugnacity Oct 7, 2024
ab3b86d
fix: added missing parameter
pugnacity Oct 7, 2024
ce9adf4
fix: tests
pugnacity Oct 15, 2024
ec8b41a
fix: an other try
pugnacity Oct 15, 2024
241a546
fix: remove spaces
pugnacity Oct 15, 2024
b60b740
debug test
pugnacity Oct 15, 2024
7a9aaad
typo fix
pugnacity Oct 15, 2024
3acc5fa
remove debug
pugnacity Oct 15, 2024
a787e81
use CONVERT_TO_ID
pugnacity Oct 15, 2024
473e028
debug test
pugnacity Nov 20, 2024
7c14f7d
Merge branch 'develop' into fix_423
pugnacity Nov 20, 2024
d4dd5f1
Merge branch 'nautobot:develop' into fix_423
pugnacity Nov 28, 2024
6e2f274
update
pugnacity Nov 28, 2024
1e868e7
debug tests
pugnacity Nov 28, 2024
d877546
typo fix
pugnacity Nov 28, 2024
b7d6549
fix test
pugnacity Nov 28, 2024
d6ff930
Apply suggestions from code review
joewesch Dec 2, 2024
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
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 @@ -59,6 +60,7 @@ def run(self):
- console_server_ports
- console_server_port_templates
- controllers
- controller_managed_device_groups
- device_bays
- device_bay_templates
- devices
Expand Down
4 changes: 4 additions & 0 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"console_server_ports",
"console_server_port_templates",
"controllers",
"controller_managed_device_groups",
"device_bays",
"device_bay_templates",
"devices",
Expand Down Expand Up @@ -105,6 +106,7 @@
cluster_group="name",
cluster_type="name",
controller="name",
controller_managed_device_group="name",
device="name",
role="name",
device_type="model",
Expand Down Expand Up @@ -238,6 +240,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 @@ -306,6 +309,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
126 changes: 126 additions & 0 deletions plugins/modules/controller_managed_device_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/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.4.0"
joewesch marked this conversation as resolved.
Show resolved Hide resolved
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:
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
description:
- parent group of the managed device group
required: false
type: str
devices:
description:
- devices which should be assigend to this group
type: list
elements: str
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
"""

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

tasks:
- name: Create controller within Nautobot with only required information
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
networktocode.nautobot.controller_managed_device_group:
url: http://nautobot.local
token: thisIsMyToken
name: "group_1"
controller: my_controller
state: present

- name: Delete controller within nautobot
networktocode.nautobot.controller:
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
url: http://nautobot.local
token: thisIsMyToken
name: test_controller_group_3
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
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"),
devices=dict(required=False, type="list", elements="str"),
parent=dict(required=False, type="str"),
)
)

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

controller = NautobotDcimModule(module, NB_CONTROLLER_MANAGED_DEVICE_GROUPS)
controller.run()
pugnacity marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__": # pragma: no cover
main()
7 changes: 7 additions & 0 deletions plugins/modules/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@
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
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
required: false
type: raw
version_added: "5.4.0"
joewesch marked this conversation as resolved.
Show resolved Hide resolved
device_redundancy_group_priority:
description:
- Priority in the assigned device redundancy group
Expand Down Expand Up @@ -290,6 +296,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"),
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
device_redundancy_group=dict(required=False, type="raw"),
device_redundancy_group_priority=dict(required=False, type="int"),
)
Expand Down
4 changes: 2 additions & 2 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def is_truthy(arg):
namespace.configure(
{
"nautobot_ansible": {
"nautobot_ver": "2.0.0",
"nautobot_ver": "2.3.5",
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
"project_name": "nautobot_ansible",
"python_ver": "3.10",
"python_ver": "3.11",
"local": False,
"compose_dir": os.path.join(os.path.dirname(__file__), "development"),
"compose_files": ["docker-compose.yml"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
- debug:
msg: "{{ nautobot_version }}"

- block:
- set_fact:
parent_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Parent Test Location\"') }}"

- name: "CONTROLLER 1: Necessary info creation"
networktocode.nautobot.controller:
pugnacity marked this conversation as resolved.
Show resolved Hide resolved
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller One
location: "Parent Test Location"
state: present
status: "Active"
register: test_one_controller

- name: "CONTROLLER 2: Create Group"
networktocode.nautobot.controller_managed_device_groups:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: Test Controller Group One
controller: Test Controller One
state: present
register: test_two

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the integration tests, we try and stick to a pattern like this:

  • Create with minimal required fields
  • Try and create again to verify idempotency
  • Create a second object with all fields
  • Try and create again to verify idempotency
  • Update one of the previous objects
  • Try and update again to verify idempotency
  • Delete one of the previous objects
  • Try and delete again to verify idempotency

Please refactor your tests to follow this pattern.

when:
# Controllers are only available on Nautobot 2.2+
- "nautobot_version is version('2.2', '>=')"