Skip to content

Commit

Permalink
Merge pull request #287 from joewesch/286_location-search
Browse files Browse the repository at this point in the history
Changes Location to look up by name/parent
  • Loading branch information
joewesch authored Dec 4, 2023
2 parents 6fad2c7 + 72de2dd commit b589e27
Show file tree
Hide file tree
Showing 31 changed files with 215 additions and 131 deletions.
6 changes: 2 additions & 4 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
group="name",
installed_device="name",
import_targets="name",
location="id",
location="name",
manufacturer="name",
master="name",
nat_inside="address",
Expand Down Expand Up @@ -305,7 +305,7 @@
"ipaddresses": set(["address", "namespace", "device", "interfaces", "vm_interfaces"]),
"ip_address_to_interface": set(["ip_address", "interface", "vm_interface"]),
"lag": set(["name"]),
"location": set(["name", "id"]),
"location": set(["name", "id", "parent"]),
"location_type": set(["name"]),
"manufacturer": set(["name"]),
"master": set(["name"]),
Expand Down Expand Up @@ -783,8 +783,6 @@ def _find_ids(self, data, user_query_params):
# Do not attempt to resolve if already ID/UUID is provided
if isinstance(v, int) or self.is_valid_uuid(v):
continue
elif k == "location":
self._handle_errors(msg="Location needs a valid UUID")
# Special circumstances to set endpoint to search within
elif k == "termination_a":
endpoint = CONVERT_TO_ID[data.get("termination_a_type")]
Expand Down
4 changes: 3 additions & 1 deletion plugins/modules/circuit_termination.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
token: thisIsMyToken
circuit: Test Circuit
term_side: A
location: "{{ location['key'] }}"
location:
name: My Location
parent: Parent Location
port_speed: 10000
state: present
Expand Down
4 changes: 3 additions & 1 deletion plugins/modules/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@
name: Test Cluster
cluster_type: qemu
cluster_group: GROUP
location: "{{ location['key'] }}"
location:
name: My Location
parent: Parent Location
state: present
"""

Expand Down
20 changes: 9 additions & 11 deletions plugins/modules/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
description:
- Required if I(state=present) and the device does not exist yet
required: false
type: str
type: raw
version_added: "3.0.0"
rack:
description:
Expand Down Expand Up @@ -180,11 +180,9 @@
name: Test Device
device_type: C9410R
role: Core Switch
location: "{{ my_location['key'] }}"
location: My Location
status: active
state: present
vars:
my_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Location\"') }}"
- name: Create device within Nautobot with empty string name to generate UUID
networktocode.nautobot.device:
Expand All @@ -193,11 +191,11 @@
name: ""
device_type: C9410R
role: Core Switch
location: "{{ my_location['key'] }}"
location:
name: My Location
parent: Parent Location
status: active
state: present
vars:
my_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Location\"') }}"
- name: Delete device within nautobot
networktocode.nautobot.device:
Expand All @@ -213,15 +211,15 @@
name: Another Test Device
device_type: C9410R
role: Core Switch
location: "{{ my_location['key'] }}"
location:
name: My Location
parent: Parent Location
status: active
local_config_context_data:
bgp: "65000"
tags:
- Schnozzberry
state: present
vars:
my_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Location\"') }}"
- name: Update the rack and position of an existing device
networktocode.nautobot.device:
Expand Down Expand Up @@ -269,7 +267,7 @@ def main():
platform=dict(required=False, type="raw"),
serial=dict(required=False, type="str"),
asset_tag=dict(required=False, type="str"),
location=dict(required=False, type="str"),
location=dict(required=False, type="raw"),
rack=dict(required=False, type="raw"),
position=dict(required=False, type="int"),
face=dict(
Expand Down
10 changes: 7 additions & 3 deletions plugins/modules/power_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@
url: http://nautobot.local
token: thisIsMyToken
name: Test Power Panel
location: Test location
location: My Location
state: present
- name: Update power panel with other fields
networktocode.nautobot.power_panel:
url: http://nautobot.local
token: thisIsMyToken
name: Test Power Panel
location: Test location
location:
name: My Location
parent: Parent Location
rack_group: Test Rack Group
state: present
Expand All @@ -72,7 +74,9 @@
url: http://nautobot.local
token: thisIsMyToken
name: Test Power Panel
location: Test location
location:
name: My Location
parent: Parent Location
state: absent
"""

Expand Down
8 changes: 5 additions & 3 deletions plugins/modules/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@
token: thisIsMyToken
family: 4
prefix: 10.156.32.0/19
location: Test Location
location: My Location
tenant: Test Tenant
vlan:
name: Test VLAN
location: Test Location
location: My Location
tenant: Test Tenant
vlan_group: Test Vlan Group
status: Reserved
Expand Down Expand Up @@ -191,7 +191,9 @@
token: thisIsMyToken
parent: 10.157.0.0/19
prefix_length: 24
location: Test Location
location:
name: My Location
parent: Parent Location
state: present
first_available: yes
"""
Expand Down
4 changes: 3 additions & 1 deletion plugins/modules/rack.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@
url: http://nautobot.local
token: thisIsMyToken
name: Test rack
location: Test Location
location:
name: My Location
parent: Parent Location
status: active
state: present
Expand Down
6 changes: 4 additions & 2 deletions plugins/modules/rack_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@
url: http://nautobot.local
token: thisIsMyToken
name: Test rack group
location: Test Location
location: My Location
state: present
- name: Delete rack group within nautobot
networktocode.nautobot.rack_group:
url: http://nautobot.local
token: thisIsMyToken
name: Test Rack group
location: Test Location
location:
name: My Location
parent: Parent Location
state: absent
"""

Expand Down
4 changes: 3 additions & 1 deletion plugins/modules/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@
token: thisIsMyToken
name: Test VLAN
vid: 400
location: Test Site
location:
name: My Location
parent: Parent Location
group: Test VLAN Group
tenant: Test Tenant
status: Deprecated
Expand Down
4 changes: 3 additions & 1 deletion plugins/modules/vlan_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
url: http://nautobot.local
token: thisIsMyToken
name: Test vlan group
location: Test Location
location:
name: My Location
parent: Parent Location
state: present
- name: Delete vlan group within nautobot
Expand Down
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ exclude = '''
# the root of the project
)
'''

[tool.pylint.messages_control]
# Line length is enforced by Black, so pylint doesn't need to check it.
# Pylint and Black disagree about how to format multi-line arrays; Black wins.
disable = """,
line-too-long
"""
6 changes: 5 additions & 1 deletion tests/integration/nautobot-populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,20 @@ def make_nautobot_calls(endpoint, payload):
# Create locations
parent_location_attrs = [
{"name": "Parent Test Location", "location_type": parent_location_type.id, "tenant": test_tenant.id, "status": {"name": "Active"}},
{"name": "Parent Test Location 2", "location_type": parent_location_type.id, "tenant": test_tenant.id, "status": {"name": "Active"}},
]
make_nautobot_calls(nb.dcim.locations, parent_location_attrs)

# Location variables to be used later on
location_parent = nb.dcim.locations.get(name="Parent Test Location")
location_parent2 = nb.dcim.locations.get(name="Parent Test Location 2")
child_location_attrs = [
{"name": "Child Test Location", "location_type": child_location_type.id, "parent": location_parent.id, "status": {"name": "Active"}},
# Creating an intentionally duplicate location name with different parent to test looking up by parent
{"name": "Child Test Location", "location_type": child_location_type.id, "parent": location_parent2.id, "status": {"name": "Active"}},
]
make_nautobot_calls(nb.dcim.locations, child_location_attrs)
location_child = nb.dcim.locations.get(name="Child Test Location")
location_child = nb.dcim.locations.get(name="Child Test Location", parent="Parent Test Location")

child_child_location_attrs = [
{"name": "Child-Child Test Location", "location_type": child_location_type.id, "parent": location_child.id, "status": {"name": "Active"}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
"nexus_parent",
"parent_rack_group",
"parent_test_location",
"parent_test_location_2",
"sub_test_rack",
"test_cluster",
"test_cluster_2",
Expand Down Expand Up @@ -410,6 +411,11 @@
"test103-vm"
]
},
"parent_test_location_2": {
"children": [
"child_test_location"
]
},
"sub_test_rack": {
"hosts": [
"TestDeviceR1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,7 @@
"device_types_nexus_parent",
"is_virtual",
"location_parent_test_location",
"location_parent_test_location_2",
"locations_child_child_test_location",
"locations_child_test_location",
"locations_parent_test_location",
Expand Down Expand Up @@ -2201,6 +2202,11 @@
"location_child_test_location"
]
},
"location_parent_test_location_2": {
"children": [
"location_child_test_location"
]
},
"locations_child_child_test_location": {
"hosts": [
"TestDeviceR1"
Expand Down
10 changes: 7 additions & 3 deletions tests/integration/targets/latest/tasks/circuit_termination.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
##
##
- set_fact:
test_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Child Test Location\"') }}"
test_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Child Test Location\" parent=\"Parent Test Location\"') }}"
circuit: '{{ lookup(''networktocode.nautobot.lookup'', ''circuits'', api_endpoint=nautobot_url, token=nautobot_token, api_filter=''cid="Test Circuit"'') }}'
circuit2: '{{ lookup(''networktocode.nautobot.lookup'', ''circuits'', api_endpoint=nautobot_url, token=nautobot_token, api_filter=''cid="Test Circuit Two"'') }}'
provider_network: '{{ lookup(''networktocode.nautobot.lookup'', ''provider-networks'', api_endpoint=nautobot_url, token=nautobot_token, api_filter=''name="Test Provider Network"'') }}'
Expand All @@ -16,7 +16,9 @@
token: "{{ nautobot_token }}"
circuit: Test Circuit
term_side: A
location: "{{ test_location['key'] }}"
location:
name: "Child Test Location"
parent: "Parent Test Location"
port_speed: 10000
state: present
register: test_one
Expand Down Expand Up @@ -89,7 +91,9 @@
token: "{{ nautobot_token }}"
circuit: Test Circuit
term_side: Z
location: "{{ test_location['key'] }}"
location:
name: "Child Test Location"
parent: "Parent Test Location"
port_speed: 10000
state: present
register: test_four
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/targets/latest/tasks/cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- set_fact:
test_cluster_type: "{{ lookup('networktocode.nautobot.lookup', 'cluster-types', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Test Cluster Type\"') }}"
test_cluster_group: "{{ lookup('networktocode.nautobot.lookup', 'cluster-groups', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Test Cluster Group\"') }}"
test_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Child Test Location\"') }}"
test_location: "{{ lookup('networktocode.nautobot.lookup', 'locations', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Child Test Location\" parent=\"Parent Test Location\"') }}"
tenant: "{{ lookup('networktocode.nautobot.lookup', 'tenants', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Test Tenant\"') }}"
tag_schnozzberry: "{{ lookup('networktocode.nautobot.lookup', 'tags', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Schnozzberry') }}"

Expand Down Expand Up @@ -54,7 +54,9 @@
name: "Test Cluster One"
cluster_type: "Test Cluster Type"
cluster_group: "Test Cluster Group"
location: "{{ test_location['key'] }}"
location:
name: "Child Test Location"
parent: "Parent Test Location"
comments: "Updated cluster"
tenant: "Test Tenant"
tags:
Expand Down
Loading

0 comments on commit b589e27

Please sign in to comment.