Skip to content

Commit

Permalink
Correctly adding interface with IP address to Devices (Nodes)
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonfelipesp committed Aug 21, 2024
1 parent c3ac6d8 commit 792f8c7
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 18 deletions.
59 changes: 59 additions & 0 deletions netbox_proxbox/backend/diode/diode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from netboxlabs.diode.sdk import DiodeClient
from netboxlabs.diode.sdk.ingester import (
Device,
Entity,
IPAddress,
)


def main():
with DiodeClient(
target="grpc://localhost:8081",
app_name="my-test-app",
app_version="0.0.1",
api_key="5a52c45ee8231156cb620d193b0291912dd15433",
) as client:
entities = []

"""
Ingest device with device type, platform, manufacturer, site, role, and tags.
"""


device = Device(
name="TESTE",
device_type="Device Type A",
platform="Platform A",
manufacturer="Manufacturer A",
site="Site ABC",
role="Role ABC",
serial="123456",
asset_tag="123456",
status="active",
tags=["tag 1", "tag 2"],
)


#device = Device(name="Device A")

ip_address = IPAddress(
address="172.16.0.1/24",
)

print(f"device: {device}")

#print(f"client: {client}")
#print(f"client (dir): {dir(client)}")
#print(f"client (version): {client._target}")
#print(f"ip_address: {ip_address}")
entities.append(Entity(ip_address=ip_address))
#entities.append(Entity(ip_address=ip_address))

response = client.ingest(entities=entities)
print(f"response: {response}")
if response.errors:
print(f"Errors: {response.errors}")


if __name__ == "__main__":
main()
92 changes: 92 additions & 0 deletions netbox_proxbox/backend/diode/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: ${PROJECT_NAME:-diode}
services:
ingress-nginx:
image: nginx:latest
command: >
/bin/sh -c "echo 'upstream diode {
server diode-ingester:8081;
}
server {
listen 80;
http2 on;
server_name localhost;
client_max_body_size 25m;
location /diode {
rewrite /diode/(.*) /$$1 break;
grpc_pass grpc://diode;
}
}'
> /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
restart: always
environment:
- DIODE_NGINX_PORT=${DIODE_NGINX_PORT}
ports:
- ${DIODE_NGINX_PORT}:80
depends_on:
- diode-ingester
- diode-reconciler

diode-ingester:
image: netboxlabs/diode-ingester:${DIODE_TAG:-latest}
environment:
- API_KEY=${RECONCILER_API_KEY}
- REDIS_PASSWORD=${REDIS_PASSWORD}
- REDIS_HOST=${REDIS_HOST}
- REDIS_PORT=${REDIS_PORT}
- RECONCILER_GRPC_HOST=${RECONCILER_GRPC_HOST}
- RECONCILER_GRPC_PORT=${RECONCILER_GRPC_PORT}
- SENTRY_DSN=${SENTRY_DSN}
restart: always
ports:
- "8081:8081"
depends_on:
- diode-redis
- diode-reconciler

diode-reconciler:
image: netboxlabs/diode-reconciler:${DIODE_TAG:-latest}
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
- REDIS_HOST=${REDIS_HOST}
- REDIS_PORT=${REDIS_PORT}
- NETBOX_DIODE_PLUGIN_API_BASE_URL=${NETBOX_DIODE_PLUGIN_API_BASE_URL}
- DIODE_TO_NETBOX_API_KEY=${DIODE_TO_NETBOX_API_KEY}
- NETBOX_TO_DIODE_API_KEY=${NETBOX_TO_DIODE_API_KEY}
- DIODE_API_KEY=${DIODE_API_KEY}
- LOGGING_LEVEL=${LOGGING_LEVEL}
- SENTRY_DSN=${SENTRY_DSN}
restart: always
ports: [ ]
depends_on:
- diode-redis
diode-redis:
image: redis/redis-stack-server:latest
command:
- sh
- -c
- redis-server --appendonly yes --dir /data --save 60 1 --requirepass $$REDIS_PASSWORD --loadmodule /opt/redis-stack/lib/rejson.so --loadmodule /opt/redis-stack/lib/redisearch.so --port $$REDIS_PORT
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
- REDIS_PORT=${REDIS_PORT}
ports: [ ]
volumes:
- diode-redis-data:/data
diode-redis-cli:
image: redis/redis-stack-server:latest
links:
- diode-redis
entrypoint:
- sh
- -c
- |
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" -a "$REDIS_PASSWORD" <<EOF
FT.CREATE ingest-entity ON JSON PREFIX 1 "ingest-entity:" SCHEMA \
$.data_type AS data_type TEXT \
$.state AS state NUMERIC \
$.request_id AS request_id TEXT \
$.ingestion_ts AS ingestion_ts NUMERIC SORTABLE
EOF
volumes:
diode-redis-data:
driver: local
91 changes: 74 additions & 17 deletions netbox_proxbox/backend/routes/netbox/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,10 @@ async def _check_duplicate(self, search_params: dict = None, object: dict = None
print(f"primary field: {self.primary_field} - primary_field_value: {self.primary_field_value}")

if self.primary_field == "address":

result_by_primary = await asyncio.to_thread(self.pynetbox_path.get, address=self.primary_field_value)
print(f"self.primary_field_value = {self.primary_field_value}")

else:
result_by_primary = await asyncio.to_thread(self.pynetbox_path.get,
{
Expand All @@ -447,40 +450,94 @@ async def _check_duplicate(self, search_params: dict = None, object: dict = None

logger.info("[CHECK DUPLICATE] (1.5) Checking object using NAME and DEVICE provided by the Payload and also the PROXBOX TAG. If found, return it.")

result_by_device = None

device_id = object.get("device")

print(f"object: {object}")
device_obj = None
try:
device_obj = self.nb.dcim.devices.get(id=device_id)
print(device_obj)
except:
logger.info("[CHECK DUPLICATE] (1.5.1) Device Object not found when checking for duplicated using Device as parameter.")

if device_obj != None:
logger.info("[CHECK DUPLICATE] (1.5.1) Checking duplicate using Device Object as parameter.")
device_obj = self.nb.session.dcim.devices.get(int(device_id))
print(f"device_obj: {device_obj}")

print(f"device_obj.name: {device_obj.name}")

print(f'object.get("name"): {object.get("name")}')

print(f"device_obj.id: {device_obj.id}")

result_by_device = await asyncio.to_thread(self.pynetbox_path.get,
name=object.get("name"),
#device__id=object.get("device"),
device=device_obj.name,
device__id=device_obj.id,
#device=device_obj,
tag=[self.nb.tag.slug]
)

if result_by_device:
logger.info("[CHECK DUPLICATE] Object found on Netbox. Returning it.")
return result_by_device


except:
logger.info("[CHECK DUPLICATE] (1.5.1) Device Object NOT found when checking for duplicated using Device as parameter.")


if result_by_device:

if result_by_device.device:
print(f"result_by_device: {result_by_device.device}")
print(f"result_by_device.device.id: {result_by_device.device.id}")
print(f"object: {object} \n{object.get("device")}")
print(f"object.device: {object.get("device")}")

print(f"result_by_device - object device: {result_by_device} / {result_by_device.device} / {result_by_device.device.id}")
# If this happens, it means that the interface name is equal, but device is different.
print(f"int(object.id): {int(object.get("device"))} | int(result_by_device.device.id): {int(result_by_device.device.id)}")
if int(object.get("device")) != int(result_by_device.device.id):
return None

logger.info("[CHECK DUPLICATE] (1.5.1) Object found on Netbox. Returning it.")
return result_by_device


logger.info("[CHECK DUPLICATE] (2) Checking object using only NAME and SLUG provided by the Payload and also the PROXBOX TAG. If found, return it.")
result_by_tag = await asyncio.to_thread(self.pynetbox_path.get,
name=object.get("name"),
slug=object.get("slug"),
tag=[self.nb.tag.slug]
)


result_by_tag = None
try:
result_by_tag = await asyncio.to_thread(self.pynetbox_path.get,
name=object.get("name"),
slug=object.get("slug"),
tag=[self.nb.tag.slug]
)

except Exception as error:

try:
result_by_tag = await asyncio.to_thread(self.pynetbox_path.filter,
name=object.get("name"),
slug=object.get("slug"),
tag=[self.nb.tag.slug]
)

if result_by_tag:
logger.info("[CHECK DUPLICATE] (2) More than one object found.")

for obj in result_by_tag:
print(f"obj.id: {obj.device.id} / device_obj.id: {device_obj.id}")

if int(obj.device.id) == int(device_obj.id):
logger.info("[CHECK DUPLICATE] (2) More than one object found, but returning with the same ID.")
return obj
return None
print(f"filter: {result_by_tag}")
except Exception as error:
logger.error(error)



if result_by_tag:
logger.info(f"[CHECK DUPLICATE] Object found on Netbox. Returning it.")
return result_by_tag



result_by_name_and_slug = await asyncio.to_thread(self.pynetbox_path.get,
Expand Down
2 changes: 1 addition & 1 deletion netbox_proxbox/backend/routes/proxbox/clusters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ async def get_nodes(
cidr = interface.get("cidr")
print(f"cidr: {cidr}")

if create_interface and cidr:
if cidr:
logger.info("Interface with CIDR/Network. Creating the IP Address object on Netbox...")
# If interface with network configured, create IP Address and attach interface to it.
create_ipaddress = await IPAddress(nb=nb, primary_field_value=cidr).post(data={
Expand Down

0 comments on commit 792f8c7

Please sign in to comment.