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 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,27 @@
"custom_fields": {},
"description": "",
"device": {
"display": "Test Nexus One",
"id": 4,
"name": "Test Nexus One"
"display": "Test Nexus Child One",
"id": 5,
"name": "Test Nexus Child One"
},
"display": "Ethernet1/1",
"display": "Ethernet2/1",
"duplex": null,
"enabled": true,
"id": 1,
"id": 2,
"ip_addresses": [
{
"address": "172.16.180.11/24",
"address": "172.16.180.12/24",
"comments": "",
"custom_fields": {},
"description": "",
"display": "172.16.180.11/24",
"dns_name": "",
"display": "172.16.180.12/24",
"dns_name": "nexus.example.com",
"family": {
"label": "IPv4",
"value": 4
},
"id": 3,
"id": 4,
"nat_inside": null,
"nat_outside": [],
"role": null,
Expand All @@ -88,7 +88,7 @@
"mode": null,
"module": null,
"mtu": null,
"name": "Ethernet1/1",
"name": "Ethernet2/1",
"parent": null,
"poe_mode": null,
"poe_type": null,
Expand Down Expand Up @@ -124,27 +124,27 @@
"custom_fields": {},
"description": "",
"device": {
"display": "Test Nexus Child One",
"id": 5,
"name": "Test Nexus Child One"
"display": "Test Nexus One",
"id": 4,
"name": "Test Nexus One"
},
"display": "Ethernet2/1",
"display": "Ethernet1/1",
"duplex": null,
"enabled": true,
"id": 2,
"id": 1,
"ip_addresses": [
{
"address": "172.16.180.12/24",
"address": "172.16.180.11/24",
"comments": "",
"custom_fields": {},
"description": "",
"display": "172.16.180.12/24",
"dns_name": "nexus.example.com",
"display": "172.16.180.11/24",
"dns_name": "",
"family": {
"label": "IPv4",
"value": 4
},
"id": 4,
"id": 3,
"nat_inside": null,
"nat_outside": [],
"role": null,
Expand All @@ -168,7 +168,7 @@
"mode": null,
"module": null,
"mtu": null,
"name": "Ethernet2/1",
"name": "Ethernet1/1",
"parent": null,
"poe_mode": null,
"poe_type": null,
Expand Down Expand Up @@ -417,6 +417,9 @@
},
"test100": {
"asset_tag": "123456789",
"cluster": "Test Cluster",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"config_context": {
"ntp_servers": [
"pool.ntp.org"
Expand Down Expand Up @@ -730,6 +733,7 @@
},
"test100-vm": {
"cluster": "Test Cluster",
"cluster_device": "test100",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"config_context": {},
Expand Down Expand Up @@ -1125,6 +1129,7 @@
},
"cluster_Test_Cluster": {
"hosts": [
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand All @@ -1139,6 +1144,7 @@
},
"cluster_group_test_cluster_group": {
"hosts": [
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand All @@ -1148,6 +1154,7 @@
"cluster_type_test_cluster_type": {
"hosts": [
"Test VM With Spaces",
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@
},
"test100": {
"asset_tag": "123456789",
"cluster": "Test Cluster",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"config_context": {
"ntp_servers": [
"pool.ntp.org"
Expand Down Expand Up @@ -613,6 +616,7 @@
},
"test100-vm": {
"cluster": "Test Cluster",
"cluster_device": "test100",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"config_context": {},
Expand Down Expand Up @@ -988,6 +992,7 @@
},
"cluster_Test_Cluster": {
"hosts": [
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand All @@ -996,6 +1001,7 @@
},
"cluster_group_test_cluster_group": {
"hosts": [
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand All @@ -1004,6 +1010,7 @@
},
"cluster_type_test_cluster_type": {
"hosts": [
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Test_Cluster": {
"hosts": [
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand Down Expand Up @@ -113,6 +114,9 @@
},
"test100": {
"asset_tag": "123456789",
"cluster": "Test Cluster",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"custom_fields": {},
"device_type": "cisco-test",
"is_virtual": false,
Expand Down Expand Up @@ -142,6 +146,7 @@
},
"test100-vm": {
"cluster": "Test Cluster",
"cluster_device": "test100",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"custom_fields": {},
Expand Down Expand Up @@ -331,6 +336,7 @@
},
"test_cluster_group": {
"hosts": [
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand All @@ -340,6 +346,7 @@
"test_cluster_type": {
"hosts": [
"Test VM With Spaces",
"test100",
"test100-vm",
"test101-vm",
"test102-vm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
},
"test100": {
"asset_tag": "123456789",
"cluster": "Test Cluster",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"custom_fields": {},
"device_roles": [
"core-switch"
Expand Down Expand Up @@ -278,6 +281,7 @@
},
"test100-vm": {
"cluster": "Test Cluster",
"cluster_device": "test100",
"cluster_group": "test-cluster-group",
"cluster_type": "test-cluster-type",
"custom_fields": {},
Expand Down
Loading
Loading