Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
7 changes: 7 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,12 @@ def extract_cluster(self, host):
except Exception:
return

def extract_cluster_device(self, host):
# cluster device does not have a slug
if host.get("device", None) is None:
return ""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you certain this is the correct thing to return? This doesn't seem like a good idea to me

return host.get("device", {"name": ""}).get("name", "")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awful similar to the above return when there is no device key in the dictionary.

Overall I think this needs to be improved

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your opinion should we return, if there is no value assigned. Should it be null/None?
Also, is there a handy way to determine the object type of the current host. Then i can only return the cluster_device variable, when the object is an virtual machine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your opinion should we return, if there is no value assigned. Should it be null/None?

I would follow the pattern set by the other functions in this code. Specifically it appears that most would return (which is short hand for returning None) although some return an empty list where it makes sense to.


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