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

Adds Location and Location Type modules #191

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/lookup/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ def get_endpoint(nautobot, term):
"interfaces": {"endpoint": nautobot.dcim.interfaces},
"inventory-items": {"endpoint": nautobot.dcim.inventory_items},
"ip-addresses": {"endpoint": nautobot.ipam.ip_addresses},
"locations": {"endpoint": nautobot.dcim.locations},
"location_types": {"endpoint": nautobot.dcim.location_types},
"manufacturers": {"endpoint": nautobot.dcim.manufacturers},
"object-changes": {"endpoint": nautobot.extras.object_changes},
"platforms": {"endpoint": nautobot.dcim.platforms},
Expand Down
2 changes: 2 additions & 0 deletions plugins/module_utils/dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
NB_INTERFACES = "interfaces"
NB_INTERFACE_TEMPLATES = "interface_templates"
NB_INVENTORY_ITEMS = "inventory_items"
NB_LOCATIONS = "locations"
NB_LOCATION_TYPES = "location_types"
NB_MANUFACTURERS = "manufacturers"
NB_PLATFORMS = "platforms"
NB_POWER_FEEDS = "power_feeds"
Expand Down
7 changes: 7 additions & 0 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"interfaces",
"interface_templates",
"inventory_items",
"locations",
"location_types",
"manufacturers",
"platforms",
"power_feeds",
Expand Down Expand Up @@ -222,6 +224,8 @@
"interface_templates": "interface_template",
"inventory_items": "inventory_item",
"ip_addresses": "ip_address",
"locations": "location",
"location_types": "location_type",
"manufacturers": "manufacturer",
"platforms": "platform",
"power_feeds": "power_feed",
Expand Down Expand Up @@ -293,6 +297,8 @@
"ip_addresses": set(["address", "vrf", "device", "interface", "vminterface"]),
"ipaddresses": set(["address", "vrf", "device", "interface", "vminterface"]),
"lag": set(["name"]),
"location": set(["name"]),
"location_type": set(["name"]),
"manufacturer": set(["slug"]),
"master": set(["name"]),
"nat_inside": set(["vrf", "address"]),
Expand Down Expand Up @@ -353,6 +359,7 @@
"interfaces": set(["form_factor", "mode", "type"]),
"interface_templates": set(["type"]),
"ip_addresses": set(["status", "role"]),
"locations": set(["status"]),
"prefixes": set(["status"]),
"power_feeds": set(["status", "type", "supply", "phase"]),
"power_outlets": set(["type", "feed_leg"]),
Expand Down
156 changes: 156 additions & 0 deletions plugins/modules/location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2023, 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: location
short_description: Creates or removes locations from Nautobot
description:
- Creates or removes locations 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: "4.3.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
- networktocode.nautobot.fragments.custom_fields
options:
name:
description:
- Name of the location to be created
required: true
type: str
slug:
description:
- URL-friendly unique shorthand
required: false
type: str
status:
description:
- Status of the location
- Required if I(state=present) and does not exist yet
required: false
type: raw
description:
description:
- Location description
required: false
type: str
location_type:
description:
- The type of location
- Required if I(state=present) and does not exist yet
required: false
type: raw
parent:
description:
- The parent location this location should be tied to
required: false
type: raw
site:
description:
- The site this location should be tied to
required: false
type: raw
"""

EXAMPLES = r"""
- name: "Test Nautobot location module"
connection: local
hosts: localhost
gather_facts: False
tasks:
- name: Create location
networktocode.nautobot.location:
url: http://nautobot.local
token: thisIsMyToken
name: My Location
status: active
location_type:
name: My Location Type
state: present

- name: Delete location
networktocode.nautobot.location:
url: http://nautobot.local
token: thisIsMyToken
name: My Location
state: absent

- name: Create location with all parameters
networktocode.nautobot.location:
url: http://nautobot.local
token: thisIsMyToken
name: My Nested Location
status: active
location_type:
name: My Location Type
description: My Nested Location Description
parent:
name: My Location
site:
name: My Site
state: present
"""

RETURN = r"""
location:
description: Serialized object as created or already existent within Nautobot
returned: on creation
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.dcim import (
NautobotDcimModule,
NB_LOCATIONS,
)
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"),
slug=dict(required=False, type="str"),
status=dict(required=False, type="raw"),
description=dict(required=False, type="str"),
location_type=dict(required=False, type="raw"),
parent=dict(required=False, type="raw"),
site=dict(required=False, type="raw"),
custom_fields=dict(required=False, type="dict"),
)
)

required_if = [
("state", "present", ["name", "location_type", "status"]),
("state", "absent", ["name"]),
]

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

location = NautobotDcimModule(module, NB_LOCATIONS)
location.run()


if __name__ == "__main__": # pragma: no cover
main()
140 changes: 140 additions & 0 deletions plugins/modules/location_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2023, 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: location_type
short_description: Creates or removes location types from Nautobot
description:
- Creates or removes location types 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: "4.3.0"
extends_documentation_fragment:
- networktocode.nautobot.fragments.base
- networktocode.nautobot.fragments.custom_fields
options:
name:
description:
- Name of the location type to be created
required: true
type: str
slug:
description:
- URL-friendly unique shorthand
required: false
type: str
description:
description:
- Location Type description
required: false
type: str
parent:
description:
- The parent location type this location type should be tied to
required: false
type: raw
nestable:
description:
- Allow Locations of this type to be parents/children of other Locations of this same type
- Requires C(nautobot >= 1.5)
type: bool
content_types:
description:
- Location Type content type(s). These match app.endpoint and the endpoint is singular.
- e.g. dcim.device, ipam.ipaddress (more can be found in the examples)
required: false
type: list
elements: str
"""

EXAMPLES = r"""
- name: "Test Nautobot location type module"
connection: local
hosts: localhost
gather_facts: False
tasks:
- name: Create location type
networktocode.nautobot.location_type:
url: http://nautobot.local
token: thisIsMyToken
name: My Location Type
state: present

- name: Delete location type
networktocode.nautobot.location_type:
url: http://nautobot.local
token: thisIsMyToken
name: My Location Type
state: absent

- name: Create location type with all parameters
networktocode.nautobot.location_type:
url: http://nautobot.local
token: thisIsMyToken
name: My Nested Location Type
description: My Nested Location Type Description
parent:
name: My Location Type
nestable: false
content_types:
- "dcim.device"
state: present
"""

RETURN = r"""
location_type:
description: Serialized object as created or already existent within Nautobot
returned: on creation
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.dcim import (
NautobotDcimModule,
NB_LOCATION_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(
dict(
name=dict(required=True, type="str"),
slug=dict(required=False, type="str"),
description=dict(required=False, type="str"),
parent=dict(required=False, type="raw"),
nestable=dict(required=False, type="bool"),
content_types=dict(required=False, type="list", elements="str"),
custom_fields=dict(required=False, type="dict"),
)
)

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

location_type = NautobotDcimModule(module, NB_LOCATION_TYPES)
location_type.run()


if __name__ == "__main__": # pragma: no cover
main()
1 change: 1 addition & 0 deletions tests/integration/integration_config.tmpl.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
nautobot_url: ${NAUTOBOT_URL:-"http://nautobot:8000"}
nautobot_token: ${NAUTOBOT_TOKEN:-"0123456789abcdef0123456789abcdef01234567"}
nautobot_version: ${NAUTOBOT_VER:-"1.3"}
4 changes: 4 additions & 0 deletions tests/integration/nautobot-populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def make_nautobot_calls(endpoint, payload):
test_site = nb.dcim.sites.get(slug="test-site")
test_site2 = nb.dcim.sites.get(slug="test-site2")

# Create location type
location_types = [{"name": "My Location Type", "slug": "my-location-type"}]
created_location_types = make_nautobot_calls(nb.dcim.location_types, location_types)

# Create power panel
power_panels = [{"name": "Test Power Panel", "site": test_site.id}]
created_power_panels = make_nautobot_calls(nb.dcim.power_panels, power_panels)
Expand Down
Loading