-
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 branch 'develop' into u/tsm1th-add-job-button
- Loading branch information
Showing
7 changed files
with
219 additions
and
0 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,109 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2022, 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: static_group_association | ||
short_description: Creates or removes a static group association from Nautobot | ||
description: | ||
- Creates or removes a static group association from Nautobot | ||
author: | ||
- Network to Code (@networktocode) | ||
- Travis Smith (@tsm1th) | ||
version_added: "5.5.0" | ||
extends_documentation_fragment: | ||
- networktocode.nautobot.fragments.base | ||
- networktocode.nautobot.fragments.custom_fields | ||
options: | ||
dynamic_group: | ||
description: | ||
- The dynamic group to add the association to | ||
required: true | ||
type: raw | ||
associated_object_type: | ||
description: | ||
- The app_label.model for the object in the relationship | ||
required: true | ||
type: str | ||
associated_object_id: | ||
description: | ||
- The UUID of the object in the relationship | ||
required: true | ||
type: str | ||
""" | ||
|
||
EXAMPLES = r""" | ||
- name: "Test static group association creation/deletion" | ||
connection: local | ||
hosts: localhost | ||
gather_facts: False | ||
tasks: | ||
- name: Create static group association | ||
networktocode.nautobot.static_group_association: | ||
url: http://nautobot.local | ||
token: thisIsMyToken | ||
dynamic_group: 01234567-abcd-0123-abcd-012345678901 | ||
associated_object_type: dcim.device | ||
associated_object_id: abcdefgh-0123-abcd-0123-abcdefghijkl | ||
- name: Delete static group association | ||
networktocode.nautobot.static_group_association: | ||
url: http://nautobot.local | ||
token: thisIsMyToken | ||
dynamic_group: 01234567-abcd-0123-abcd-012345678901 | ||
associated_object_type: dcim.device | ||
associated_object_id: abcdefgh-0123-abcd-0123-abcdefghijkl | ||
state: absent | ||
""" | ||
|
||
RETURN = r""" | ||
static_group_association: | ||
description: Serialized object as created/existent/updated/deleted within Nautobot | ||
returned: always | ||
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, | ||
CUSTOM_FIELDS_ARG_SPEC, | ||
) | ||
from ansible_collections.networktocode.nautobot.plugins.module_utils.extras import ( | ||
NautobotExtrasModule, | ||
NB_STATIC_GROUP_ASSOCIATIONS, | ||
) | ||
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(CUSTOM_FIELDS_ARG_SPEC)) | ||
argument_spec.update( | ||
dict( | ||
dynamic_group=dict(required=True, type="raw"), | ||
associated_object_type=dict(required=True, type="str"), | ||
associated_object_id=dict(required=True, type="str"), | ||
) | ||
) | ||
|
||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) | ||
|
||
static_group_association = NautobotExtrasModule(module, NB_STATIC_GROUP_ASSOCIATIONS) | ||
static_group_association.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
89 changes: 89 additions & 0 deletions
89
tests/integration/targets/latest/tasks/static_group_association.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,89 @@ | ||
--- | ||
## | ||
## | ||
### PYNAUTOBOT_STATIC_GROUP_ASSOCIATION | ||
## | ||
## | ||
|
||
- name: "NAUTOBOT 2.3+ STATIC GROUP ASSOCIATION TESTS" | ||
when: | ||
- "nautobot_version is version('2.3', '>=')" | ||
block: | ||
- set_fact: | ||
static_dynamic_group: "{{ lookup('networktocode.nautobot.lookup', 'dynamic-groups', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"TestStaticAssociations\"') }}" | ||
static_device: "{{ lookup('networktocode.nautobot.lookup', 'devices', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=TestDeviceR1') }}" | ||
|
||
- name: "STATIC GROUP ASSOCIATION 1: Necessary info creation" | ||
networktocode.nautobot.static_group_association: | ||
url: "{{ nautobot_url }}" | ||
token: "{{ nautobot_token }}" | ||
dynamic_group: "TestStaticAssociations" | ||
associated_object_type: "dcim.device" | ||
associated_object_id: "{{ static_device['key'] }}" | ||
state: present | ||
register: test_one | ||
|
||
- name: "STATIC GROUP ASSOCIATION 1: ASSERT - Necessary info creation" | ||
assert: | ||
that: | ||
- test_one is changed | ||
- test_one['diff']['before']['state'] == "absent" | ||
- test_one['diff']['after']['state'] == "present" | ||
- test_one['static_group_association']['dynamic_group'] == static_dynamic_group['key'] | ||
- test_one['static_group_association']['associated_object_type'] == "dcim.device" | ||
- test_one['static_group_association']['associated_object_id'] == static_device['key'] | ||
- "'created' in test_one['msg']" | ||
|
||
- name: "STATIC GROUP ASSOCIATION 2: Test duplication association (Idempotency)" | ||
networktocode.nautobot.static_group_association: | ||
url: "{{ nautobot_url }}" | ||
token: "{{ nautobot_token }}" | ||
dynamic_group: "TestStaticAssociations" | ||
associated_object_type: "dcim.device" | ||
associated_object_id: "{{ static_device['key'] }}" | ||
state: present | ||
register: test_two | ||
|
||
- name: "STATIC GROUP ASSOCIATION 2: ASSERT - Item already exists (Idempotency)" | ||
assert: | ||
that: | ||
- test_two is not changed | ||
- test_two['static_group_association']['dynamic_group'] == static_dynamic_group['key'] | ||
- "'already exists' in test_two['msg']" | ||
|
||
- name: "STATIC GROUP ASSOCIATION 3: Test absent state" | ||
networktocode.nautobot.static_group_association: | ||
url: "{{ nautobot_url }}" | ||
token: "{{ nautobot_token }}" | ||
dynamic_group: "TestStaticAssociations" | ||
associated_object_type: "dcim.device" | ||
associated_object_id: "{{ static_device['key'] }}" | ||
state: absent | ||
register: test_three | ||
|
||
- name: "STATIC GROUP ASSOCIATION 3: ASSERT - Item removed" | ||
assert: | ||
that: | ||
- test_three is changed | ||
- test_three['diff']['before']['state'] == "present" | ||
- test_three['diff']['after']['state'] == "absent" | ||
- test_three['static_group_association']['dynamic_group'] == static_dynamic_group['key'] | ||
- test_three['static_group_association']['associated_object_type'] == "dcim.device" | ||
- test_three['static_group_association']['associated_object_id'] == static_device['key'] | ||
- "'deleted' in test_three['msg']" | ||
|
||
- name: "STATIC GROUP ASSOCIATION 4: Test absent state (Idempotent)" | ||
networktocode.nautobot.static_group_association: | ||
url: "{{ nautobot_url }}" | ||
token: "{{ nautobot_token }}" | ||
dynamic_group: "TestStaticAssociations" | ||
associated_object_type: "dcim.device" | ||
associated_object_id: "{{ static_device['key'] }}" | ||
state: absent | ||
register: test_four | ||
|
||
- name: "STATIC GROUP ASSOCIATION 4: ASSERT - Item not removed (Idempotent)" | ||
assert: | ||
that: | ||
- test_four is not changed | ||
- "'already absent' in test_four['msg']" |