Skip to content

Commit

Permalink
Merge pull request #369 from nautobot/u/joewesch_324-contacts_325-teams
Browse files Browse the repository at this point in the history
Adds Contacts and Teams modules
  • Loading branch information
jvanderaa authored Jul 13, 2024
2 parents 149d4db + f8b0821 commit b345813
Show file tree
Hide file tree
Showing 9 changed files with 618 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugins/lookup/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def get_endpoint(nautobot, term):
"console-ports": {"endpoint": nautobot.dcim.console_ports},
"console-server-port-templates": {"endpoint": nautobot.dcim.console_server_port_templates},
"console-server-ports": {"endpoint": nautobot.dcim.console_server_ports},
"contacts": {"endpoint": nautobot.extras.contacts},
"custom-fields": {"endpoint": nautobot.extras.custom_fields},
"custom-field-choices": {"endpoint": nautobot.extras.custom_field_choices},
"device-bay-templates": {"endpoint": nautobot.dcim.device_bay_templates},
Expand Down Expand Up @@ -225,6 +226,7 @@ def get_endpoint(nautobot, term):
"services": {"endpoint": nautobot.ipam.services},
"statuses": {"endpoint": nautobot.extras.statuses},
"tags": {"endpoint": nautobot.extras.tags},
"teams": {"endpoint": nautobot.extras.teams},
"tenant-groups": {"endpoint": nautobot.tenancy.tenant_groups},
"tenants": {"endpoint": nautobot.tenancy.tenants},
"topology-maps": {"endpoint": nautobot.extras.topology_maps},
Expand Down
2 changes: 2 additions & 0 deletions plugins/module_utils/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
NB_RELATIONSHIP_ASSOCIATIONS = "relationship_associations"
NB_CUSTOM_FIELDS = "custom_fields"
NB_CUSTOM_FIELD_CHOICES = "custom_field_choices"
NB_CONTACT = "contacts"
NB_TEAM = "teams"


class NautobotExtrasModule(NautobotModule):
Expand Down
10 changes: 10 additions & 0 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@
"virtual_chassis",
],
extras=[
"contacts",
"custom_fields",
"custom_field_choices",
"relationship_associations",
"roles",
"statuses",
"tags",
"teams",
],
ipam=[
"ip_addresses",
Expand Down Expand Up @@ -150,6 +152,7 @@
"cluster": "clusters",
"cluster_group": "cluster_groups",
"cluster_type": "cluster_types",
"contacts": "contacts",
"dcim.consoleport": "console_ports",
"dcim.consoleserverport": "console_server_ports",
"dcim.frontport": "front_ports",
Expand Down Expand Up @@ -198,6 +201,7 @@
"status": "statuses",
"tags": "tags",
"tagged_vlans": "vlans",
"teams": "teams",
"tenant": "tenants",
"tenant_group": "tenant_groups",
"termination_a": "interfaces",
Expand All @@ -223,6 +227,7 @@
"console_port_templates": "console_port_template",
"console_server_ports": "console_server_port",
"console_server_port_templates": "console_server_port_template",
"contacts": "contact",
"custom_fields": "custom_field",
"custom_field_choices": "custom_field_choice",
"device_bays": "device_bay",
Expand Down Expand Up @@ -261,6 +266,7 @@
"services": "services",
"statuses": "statuses",
"tags": "tags",
"teams": "team",
"tenants": "tenant",
"tenant_groups": "tenant_group",
"virtual_chassis": "virtual_chassis",
Expand All @@ -284,6 +290,8 @@
"console_port_template": set(["name", "device_type"]),
"console_server_port": set(["name", "device"]),
"console_server_port_template": set(["name", "device_type"]),
"contact": set(["name", "phone", "email"]),
"contacts": set(["name", "phone", "email"]),
"custom_field": set(["label"]),
"custom_field_choice": set(["value", "custom_field"]),
"dcim.consoleport": set(["name", "device"]),
Expand Down Expand Up @@ -341,6 +349,8 @@
"statuses": set(["name"]),
"tags": set(["name"]),
"tagged_vlans": set(["group", "name", "location", "vid", "vlan_group", "tenant"]),
"team": set(["name", "phone", "email"]),
"teams": set(["name", "phone", "email"]),
"tenant": set(["name"]),
"tenant_group": set(["name"]),
"termination_a": set(["name", "device", "virtual_machine"]),
Expand Down
135 changes: 135 additions & 0 deletions plugins/modules/contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2024, 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: contact
short_description: Creates or removes contacts from Nautobot
description:
- Creates or removes contacts 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.3.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
- networktocode.nautobot.fragments.tags
- networktocode.nautobot.fragments.custom_fields
options:
name:
description:
- The name of the contact
required: true
type: str
phone:
description:
- The phone number of the contact
required: false
type: str
email:
description:
- The email of the contact
required: false
type: str
address:
description:
- The address of the contact
required: false
type: str
teams:
description:
- The teams the contact is associated with
required: false
type: list
elements: raw
comments:
description:
- Comments about the contact
required: false
type: str
"""

EXAMPLES = r"""
---
- name: Create a contact
networktocode.nautobot.contact:
url: http://nautobot.local
token: thisIsMyToken
name: My Contact
phone: 123-456-7890
email: user@example.com
address: 1234 Main St
teams:
- name: team1
- name: team2
comments: My Comments
tags:
- tag1
- tag2
custom_fields:
my_custom_field: my_value
state: present
- name: Delete a contact
networktocode.nautobot.contact:
url: http://nautobot.local
token: thisIsMyToken
name: My Contact
state: absent
"""

RETURN = r"""
contact:
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.extras import (
NautobotExtrasModule,
NB_CONTACT,
)
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"),
phone=dict(required=False, type="str"),
email=dict(required=False, type="str"),
address=dict(required=False, type="str"),
teams=dict(required=False, type="list", elements="raw"),
comments=dict(required=False, type="str"),
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)
contact = NautobotExtrasModule(module, NB_CONTACT)
contact.run()


if __name__ == "__main__": # pragma: no cover
main()
135 changes: 135 additions & 0 deletions plugins/modules/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2024, 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: team
short_description: Creates or removes teams from Nautobot
description:
- Creates or removes teams 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.3.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
- networktocode.nautobot.fragments.tags
- networktocode.nautobot.fragments.custom_fields
options:
name:
description:
- The name of the team
required: true
type: str
phone:
description:
- The phone number of the team
required: false
type: str
email:
description:
- The email of the team
required: false
type: str
address:
description:
- The address of the team
required: false
type: str
contacts:
description:
- The contacts the team is associated with
required: false
type: list
elements: raw
comments:
description:
- Comments about the team
required: false
type: str
"""

EXAMPLES = r"""
---
- name: Create a team
networktocode.nautobot.team:
url: http://nautobot.local
token: thisIsMyToken
name: My Team
phone: 123-456-7890
email: user@example.com
address: 1234 Main St
contacts:
- name: contact1
- name: contact2
comments: My Comments
tags:
- tag1
- tag2
custom_fields:
my_custom_field: my_value
state: present
- name: Delete a team
networktocode.nautobot.team:
url: http://nautobot.local
token: thisIsMyToken
name: My Team
state: absent
"""

RETURN = r"""
team:
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.extras import (
NautobotExtrasModule,
NB_TEAM,
)
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"),
phone=dict(required=False, type="str"),
email=dict(required=False, type="str"),
address=dict(required=False, type="str"),
contacts=dict(required=False, type="list", elements="raw"),
comments=dict(required=False, type="str"),
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)
team = NautobotExtrasModule(module, NB_TEAM)
team.run()


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

###############
# v2.2+ items #
###############
if nautobot_version > version.parse("2.1"):
# Create Teams
teams = [{"name": "My Test Team"}]
created_teams = make_nautobot_calls(nb.extras.teams, teams)

# Create Contacts
contacts = [{"name": "My Contact"}, {"name": "My Contact 2"}]
created_contacts = make_nautobot_calls(nb.extras.contacts, contacts)

if ERRORS:
sys.exit("Errors have occurred when creating objects, and should have been printed out. Check previous output.")
Loading

0 comments on commit b345813

Please sign in to comment.