-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #432 from pugnacity/fix_423
create controller_managed_device_groups
- Loading branch information
Showing
9 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tests/integration/targets/latest/tasks/controller_managed_device_group.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters