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

feat: allow configure diode producer_app_name prefix #35

Merged
merged 3 commits into from
Dec 13, 2024
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
2 changes: 2 additions & 0 deletions device-discovery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ options:
-k DIODE_API_KEY, --diode-api-key DIODE_API_KEY
Diode API key. Environment variables can be used by wrapping them in ${} (e.g.
${MY_API_KEY})
-a DIODE_APP_NAME_PREFIX, --diode-app-name-prefix DIODE_APP_NAME_PREFIX
Diode producer_app_name prefix
```

### Policy RFC
Expand Down
5 changes: 3 additions & 2 deletions device-discovery/device_discovery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,21 @@ def __init__(self):
if not hasattr(self, "diode_client"): # Prevent reinitialization
self.diode_client = None

def init_client(self, target: str, api_key: str | None = None):
def init_client(self, prefix: str, target: str, api_key: str | None = None):
"""
Initialize the Diode client with the specified target, API key, and TLS verification.

Args:
----
prefix (str): The prefix for the producer app name.
target (str): The target endpoint for the Diode client.
api_key (Optional[str]): The API key for authentication (default is None).

"""
with self._lock:
self.diode_client = DiodeClient(
target=target,
app_name=APP_NAME,
app_name=f"{prefix}/{APP_NAME}" if prefix else APP_NAME,
app_version=APP_VERSION,
api_key=api_key,
)
Expand Down
14 changes: 12 additions & 2 deletions device-discovery/device_discovery/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def main():
"--port",
default=8072,
help="Server port",
type=str,
type=int,
required=False,
)
parser.add_argument(
Expand All @@ -62,6 +62,14 @@ def main():
required=True,
)

parser.add_argument(
"-a",
"--diode-app-name-prefix",
help="Diode producer_app_name prefix",
type=str,
required=False,
)

try:
args = parser.parse_args()
api_key = args.diode_api_key
Expand All @@ -70,7 +78,9 @@ def main():
api_key = os.getenv(env_var, api_key)

client = Client()
client.init_client(target=args.diode_target, api_key=args.diode_api_key)
client.init_client(
prefix=args.diode_app_name_prefix, target=args.diode_target, api_key=api_key
)
uvicorn.run(
app,
host=args.host,
Expand Down
18 changes: 12 additions & 6 deletions device-discovery/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ def mock_diode_client_class():
def test_init_client(mock_diode_client_class, mock_version_semver):
"""Test the initialization of the Diode client."""
client = Client()
client.init_client(target="https://example.com", api_key="dummy_api_key")
client.init_client(
prefix="prefix", target="https://example.com", api_key="dummy_api_key"
)

mock_diode_client_class.assert_called_once_with(
target="https://example.com",
app_name="device-discovery",
app_name="prefix/device-discovery",
app_version=mock_version_semver(),
api_key="dummy_api_key",
)
Expand All @@ -69,14 +71,15 @@ def test_init_client(mock_diode_client_class, mock_version_semver):
def test_ingest_success(mock_diode_client_class, sample_data):
"""Test successful data ingestion."""
client = Client()
client.init_client(target="https://example.com", api_key="dummy_api_key")
client.init_client(prefix="", target="https://example.com", api_key="dummy_api_key")

mock_diode_instance = mock_diode_client_class.return_value
mock_diode_instance.ingest.return_value.errors = []
hostname = sample_data["device"]["hostname"]

with patch(
"device_discovery.client.translate_data", return_value=translate_data(sample_data)
"device_discovery.client.translate_data",
return_value=translate_data(sample_data),
) as mock_translate_data:
client.ingest(hostname, sample_data)
mock_translate_data.assert_called_once_with(sample_data)
Expand All @@ -86,14 +89,17 @@ def test_ingest_success(mock_diode_client_class, sample_data):
def test_ingest_failure(mock_diode_client_class, sample_data):
"""Test data ingestion with errors."""
client = Client()
client.init_client(target="https://example.com", api_key="dummy_api_key")
client.init_client(
prefix="prefix", target="https://example.com", api_key="dummy_api_key"
)

mock_diode_instance = mock_diode_client_class.return_value
mock_diode_instance.ingest.return_value.errors = ["Error1", "Error2"]
hostname = sample_data["device"]["hostname"]

with patch(
"device_discovery.client.translate_data", return_value=translate_data(sample_data)
"device_discovery.client.translate_data",
return_value=translate_data(sample_data),
) as mock_translate_data:
client.ingest(hostname, sample_data)
mock_translate_data.assert_called_once_with(sample_data)
Expand Down
2 changes: 2 additions & 0 deletions network-discovery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Orb network discovery backend
Usage of network-discovery:
-diode-api-key string
diode api key (REQUIRED). Environment variables can be used by wrapping them in ${} (e.g. ${MY_API_KEY})
-diode-app-name-prefix string
diode producer_app_name prefix
-diode-target string
diode target (REQUIRED)
-help
Expand Down
8 changes: 7 additions & 1 deletion network-discovery/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
diodeTarget := flag.String("diode-target", "", "diode target (REQUIRED)")
diodeAPIKey := flag.String("diode-api-key", "", "diode api key (REQUIRED)."+
" Environment variables can be used by wrapping them in ${} (e.g. ${MY_API_KEY})")
diodeNamePrefix := flag.String("diode-app-name-prefix", "", "diode producer_app_name prefix")
logLevel := flag.String("log-level", "INFO", "log level")
logFormat := flag.String("log-format", "TEXT", "log format")
help := flag.Bool("help", false, "show this help")
Expand All @@ -58,9 +59,14 @@ func main() {
os.Exit(1)
}

producerName := AppName
if *diodeNamePrefix != "" {
producerName = fmt.Sprintf("%s/%s", *diodeNamePrefix, AppName)
}

client, err := diode.NewClient(
*diodeTarget,
AppName,
producerName,
version.GetBuildVersion(),
diode.WithAPIKey(resolveEnv(*diodeAPIKey)),
)
Expand Down
Loading