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

chore: improve worker custom implementation sample #57

Merged
merged 2 commits into from
Jan 22, 2025
Merged
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
32 changes: 30 additions & 2 deletions worker/tests/nbl-custom/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
### Nbl Custom Worker
A simple implementation for testing.
## Nbl Custom Worker
A simple implementation for testing. It shows how to define your own data structures to parse `config` and `scope` data. Also returns a `Device` entity to be ingested.


## Policy samples

### Sample list scope
``` yaml
policies:
custom_1:
config:
package: nbl_custom
custom: any_value
scope:
- hostname: 10.90.0.50
password: *****
username: admin
```

### Sample map scope
``` yaml
policies:
custom_1:
config:
package: nbl_custom
custom: any_value
scope:
target: 10.90.0.50
port: 8080
```
38 changes: 37 additions & 1 deletion worker/tests/nbl-custom/nbl_custom/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@
# Copyright 2025 NetBox Labs Inc
"""NetBox Labs - Mock Impl."""

import logging
from collections.abc import Iterable
from typing import Any

from netboxlabs.diode.sdk.ingester import Device, Entity
from pydantic import BaseModel, Field, ValidationError
from worker.backend import Backend
from worker.models import Metadata, Policy

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class CustomConfig(BaseModel):
"""Validation model for config field."""

package: str = Field(..., description="Package name")
custom: str = Field(..., description="Custom field, can be of any type")


class ScopeItem(BaseModel):
"""Validation model for individual scope entries."""

hostname: str = Field(..., description="Target hostname or IP")
username: str = Field(..., description="Login username")
password: str = Field(..., description="Login password")


class ScopeMap(BaseModel):
"""Validation model for individual scope entries."""

target: str = Field(..., description="Target hostname or IP")
port: int = Field(..., description="Target port number")


class MockBackend(Backend):
"""Mock backend class."""
Expand All @@ -21,6 +48,13 @@ def run(self, policy_name: str, policy: Policy) -> Iterable[Entity]:
"""Mock run method."""
entities = []

config = CustomConfig(**policy.config.model_dump())
# Sample of how to handle a scope list
if isinstance(policy.scope, list):
scope = [ScopeItem(**item) for item in policy.scope]
# Sample of how to handle a scope dictionary
elif isinstance(policy.scope, dict):
scope = ScopeMap(**policy.scope)
device = Device(
name="Device A",
device_type="Device Type A",
Expand All @@ -35,6 +69,8 @@ def run(self, policy_name: str, policy: Policy) -> Iterable[Entity]:
status="active",
tags=["tag 1", "tag 2"],
)
logger.info(f"Policy '{policy_name}' config: {config}")
logger.info(f"Policy '{policy_name}' scope: {scope}")

entities.append(Entity(device=device))
return entities
Loading