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

add support to fetch cluster device information from netbox. #1169

Open
wants to merge 15 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ venv/
changelogs/.plugin-cache.yaml
docs/_build/*
.python-version
inventory/
ansible.yml

# https://github.com/ansible/ansible/issues/68499
# ansible_collections/
5 changes: 5 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

rm netbox-netbox-3.16.0.tar.gz
ansible-galaxy collection build .
ansible-galaxy collection install -f netbox-netbox-3.16.0.tar.gz
11 changes: 11 additions & 0 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ def group_extractors(self):
"custom_fields": self.extract_custom_fields,
"region": self.extract_regions,
"cluster": self.extract_cluster,
"cluster_device": self.extract_cluster_device,
"cluster_group": self.extract_cluster_group,
"cluster_type": self.extract_cluster_type,
"is_virtual": self.extract_is_virtual,
Expand Down Expand Up @@ -943,6 +944,16 @@ def extract_cluster(self, host):
except Exception:
return

def extract_cluster_device(self, host):
try:
# cluster device does not have a slug
if host.get("device") is None:
return
else:
return host["device"].get("name")
except TypeError:
return

def extract_cluster_group(self, host):
try:
return self.clusters_group_lookup[host["cluster"]["id"]]
Expand Down
65 changes: 33 additions & 32 deletions tests/integration/netbox-deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,37 @@ def make_netbox_calls(endpoint, payload):
test_rack = nb.dcim.racks.get(name="Test Rack") # racks don't have slugs
test_rack_site2 = nb.dcim.racks.get(name="Test Rack Site 2")

## Create Cluster Group
cluster_groups = [{"name": "Test Cluster Group", "slug": "test-cluster-group"}]
created_cluster_groups = make_netbox_calls(
nb.virtualization.cluster_groups, cluster_groups
)
test_cluster_group = nb.virtualization.cluster_groups.get(slug="test-cluster-group")

## Create Cluster Type
cluster_types = [{"name": "Test Cluster Type", "slug": "test-cluster-type"}]
created_cluster_types = make_netbox_calls(
nb.virtualization.cluster_types, cluster_types
)
test_cluster_type = nb.virtualization.cluster_types.get(slug="test-cluster-type")

## Create Cluster
clusters = [
{
"name": "Test Cluster",
"type": test_cluster_type.id,
"group": test_cluster_group.id,
"site": test_site.id,
},
{
"name": "Test Cluster 2",
"type": test_cluster_type.id,
},
]
created_clusters = make_netbox_calls(nb.virtualization.clusters, clusters)
test_cluster = nb.virtualization.clusters.get(name="Test Cluster")
test_cluster2 = nb.virtualization.clusters.get(name="Test Cluster 2")


## Create Devices
devices = [
Expand All @@ -307,6 +338,7 @@ def make_netbox_calls(endpoint, payload):
"local_context_data": {"ntp_servers": ["pool.ntp.org"]},
"serial": "FAB01234567",
"asset_tag": "123456789",
"cluster": test_cluster.id,
},
{
"name": "TestDeviceR1",
Expand Down Expand Up @@ -419,40 +451,9 @@ def make_netbox_calls(endpoint, payload):
rirs = [{"name": "Example RIR", "slug": "example-rir"}]
created_rirs = make_netbox_calls(nb.ipam.rirs, rirs)

## Create Cluster Group
cluster_groups = [{"name": "Test Cluster Group", "slug": "test-cluster-group"}]
created_cluster_groups = make_netbox_calls(
nb.virtualization.cluster_groups, cluster_groups
)
test_cluster_group = nb.virtualization.cluster_groups.get(slug="test-cluster-group")

## Create Cluster Type
cluster_types = [{"name": "Test Cluster Type", "slug": "test-cluster-type"}]
created_cluster_types = make_netbox_calls(
nb.virtualization.cluster_types, cluster_types
)
test_cluster_type = nb.virtualization.cluster_types.get(slug="test-cluster-type")

## Create Cluster
clusters = [
{
"name": "Test Cluster",
"type": test_cluster_type.id,
"group": test_cluster_group.id,
"site": test_site.id,
},
{
"name": "Test Cluster 2",
"type": test_cluster_type.id,
},
]
created_clusters = make_netbox_calls(nb.virtualization.clusters, clusters)
test_cluster = nb.virtualization.clusters.get(name="Test Cluster")
test_cluster2 = nb.virtualization.clusters.get(name="Test Cluster 2")

## Create Virtual Machine
virtual_machines = [
{"name": "test100-vm", "cluster": test_cluster.id},
{"name": "test100-vm", "cluster": test_cluster.id, "device": test100.id},
{"name": "test101-vm", "cluster": test_cluster.id},
{"name": "test102-vm", "cluster": test_cluster.id},
{"name": "test103-vm", "cluster": test_cluster.id},
Expand Down
Loading
Loading