Skip to content

Commit

Permalink
Merge branch 'develop' into u/tsm1th-add-static-group-association
Browse files Browse the repository at this point in the history
  • Loading branch information
tsm1th committed Dec 16, 2024
2 parents 9f3ee3f + d947af4 commit c5c0d35
Show file tree
Hide file tree
Showing 11 changed files with 910 additions and 3 deletions.
3 changes: 3 additions & 0 deletions plugins/lookup/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,15 @@ def get_endpoint(nautobot, term):
"locations": {"endpoint": nautobot.dcim.locations},
"location-types": {"endpoint": nautobot.dcim.location_types},
"manufacturers": {"endpoint": nautobot.dcim.manufacturers},
"metadata-choices": {"endpoint": nautobot.extras.metadata_choices},
"metadata-types": {"endpoint": nautobot.extras.metadata_types},
"module-bay-templates": {"endpoint": nautobot.dcim.module_bay_templates},
"module-bays": {"endpoint": nautobot.dcim.module_bays},
"module-types": {"endpoint": nautobot.dcim.module_types},
"modules": {"endpoint": nautobot.dcim.modules},
"namespaces": {"endpoint": nautobot.ipam.namespaces},
"object-changes": {"endpoint": nautobot.extras.object_changes},
"object-metadata": {"endpoint": nautobot.extras.object_metadata},
"platforms": {"endpoint": nautobot.dcim.platforms},
"power-connections": {"endpoint": nautobot.dcim.power_connections},
"power-outlet-templates": {"endpoint": nautobot.dcim.power_outlet_templates},
Expand Down
7 changes: 6 additions & 1 deletion plugins/module_utils/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
NB_CUSTOM_FIELD_CHOICES = "custom_field_choices"
NB_CONTACT = "contacts"
NB_TEAM = "teams"
NB_OBJECT_METADATA = "object_metadata"
NB_METADATA_CHOICES = "metadata_choices"
NB_METADATA_TYPES = "metadata_types"


class NautobotExtrasModule(NautobotModule):
Expand Down Expand Up @@ -52,8 +55,10 @@ def run(self):
name = f"{data['dynamic_group']} -> {data['associated_object_id']}"
elif endpoint_name == "custom_field":
name = data["label"]
elif endpoint_name == "custom_field_choice":
elif endpoint_name in ["custom_field_choice", "metadata_choice"]:
name = data["value"]
elif endpoint_name in ["object_metadata"]:
name = data.get("value", data.get("contact", data.get("team")))
else:
name = data.get("id")

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 @@ -82,6 +82,9 @@
"contacts",
"custom_fields",
"custom_field_choices",
"metadata_choices",
"metadata_types",
"object_metadata",
"dynamic_groups",
"relationship_associations",
"roles",
Expand Down Expand Up @@ -215,6 +218,7 @@
"location": "locations",
"location_type": "location_types",
"manufacturer": "manufacturers",
"metadata_type": "metadata_types",
"module_bay_template": "module_bay_templates",
"module_bay": "module_bays",
"module_type": "module_types",
Expand Down Expand Up @@ -303,11 +307,14 @@
"locations": "location",
"location_types": "location_type",
"manufacturers": "manufacturer",
"metadata_choices": "metadata_choice",
"metadata_types": "metadata_type",
"module_bay_templates": "module_bay_template",
"module_bays": "module_bay",
"module_types": "module_type",
"modules": "module",
"namespaces": "namespace",
"object_metadata": "object_metadata",
"permissions": "permission",
"platforms": "platform",
"power_feeds": "power_feed",
Expand Down Expand Up @@ -397,12 +404,15 @@
"location_type": set(["name"]),
"manufacturer": set(["name"]),
"master": set(["name"]),
"metadata_choice": set(["value", "metadata_type"]),
"metadata_type": set(["name"]),
"module_bay_template": set(["name"]),
"module_bay": set(["name", "parent_device", "parent_module"]),
"module_type": set(["model"]),
"module": set(["module_type", "parent_module_bay", "location"]),
"namespace": set(["name"]),
"nat_inside": set(["namespace", "address"]),
"object_metadata": set(["metadata_type", "assigned_object_type", "assigned_object_id", "value"]),
"parent_module_bay": set(["name", "parent_device", "parent_module"]),
"parent_module": set(["module_type", "parent_module_bay"]),
"parent_rack_group": set(["name"]),
Expand Down
100 changes: 100 additions & 0 deletions plugins/modules/metadata_choice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/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: metadata_choice
short_description: Create, update or delete metadata choices within Nautobot
description:
- Creates, updates or removes metadata choices from Nautobot
notes:
- This should be ran with connection C(local) and hosts C(localhost)
author:
- Travis Smith (@tsm1th)
version_added: "5.5.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
options:
metadata_type:
description:
- The name of the metadata type
required: true
type: str
value:
description:
- The value of the metadata choice
required: true
type: str
weight:
description:
- Weight of this choice
required: false
type: int
"""

EXAMPLES = r"""
- name: Create a metadata choice
networktocode.nautobot.metadata_choice:
url: http://nautobot.local
token: thisIsMyToken
value: "Choice 1"
weight: 100
metadata_type: "TopSecretInfo"
state: present
- name: Delete a metadata choice
networktocode.nautobot.metadata_choice:
url: http://nautobot.local
token: thisIsMyToken
value: "Choice 1"
metadata_type: "TopSecretInfo"
state: absent
"""

RETURN = r"""
metadata_choice:
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_METADATA_CHOICES,
)
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(
metadata_type=dict(required=True, type="str"),
value=dict(required=True, type="str"),
weight=dict(required=False, type="int"),
)
)

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

metadata_choice = NautobotExtrasModule(module, NB_METADATA_CHOICES)
metadata_choice.run()


if __name__ == "__main__": # pragma: no cover
main()
132 changes: 132 additions & 0 deletions plugins/modules/metadata_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/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: metadata_type
short_description: Create, update or delete metadata types within Nautobot
description:
- Creates, updates or removes metadata types from Nautobot
notes:
- This should be ran with connection C(local) and hosts C(localhost)
author:
- Travis Smith (@tsm1th)
version_added: "5.5.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
- networktocode.nautobot.fragments.custom_fields
options:
name:
description:
- The name of the metadata type
required: true
type: str
description:
description:
- The description of the metdata type
required: false
type: str
data_type:
description:
- Data type of this field
- Required if I(state=present) and the metadata type does not exist yet
required: false
choices:
- text
- integer
- boolean
- date
- url
- select
- multi-select
- json
- markdown
- contact-or-team
- datetime
- float
type: str
content_types:
description:
- Content types that this metadata type should be available for
- Required if I(state=present) and the metadata type does not exist yet
required: false
type: list
elements: str
"""

EXAMPLES = r"""
- name: Create a metadata type
networktocode.nautobot.metadata_type:
url: http://nautobot.local
token: thisIsMyToken
name: TopSecretInfo
description: The topest secretest info
data_type: text
content_types:
- dcim.device
state: present
- name: Delete a metadata type
networktocode.nautobot.metadata_type:
url: http://nautobot.local
token: thisIsMyToken
name: TopSecretInfo
state: absent
"""

RETURN = r"""
metadata_type:
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,
CUSTOM_FIELDS_ARG_SPEC,
)
from ansible_collections.networktocode.nautobot.plugins.module_utils.extras import (
NautobotExtrasModule,
NB_METADATA_TYPES,
)
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(
name=dict(required=True, type="str"),
description=dict(required=False, type="str"),
data_type=dict(
required=False,
choices=["text", "integer", "boolean", "date", "url", "select", "multi-select", "json", "markdown", "contact-or-team", "datetime", "float"],
type="str",
),
content_types=dict(required=False, type="list", elements="str"),
)
)

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

metadata_type = NautobotExtrasModule(module, NB_METADATA_TYPES)
metadata_type.run()


if __name__ == "__main__": # pragma: no cover
main()
Loading

0 comments on commit c5c0d35

Please sign in to comment.