diff --git a/device-discovery/README.md b/device-discovery/README.md index e6540bf..fae1582 100644 --- a/device-discovery/README.md +++ b/device-discovery/README.md @@ -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 diff --git a/device-discovery/device_discovery/client.py b/device-discovery/device_discovery/client.py index cc8a6a1..1778153 100644 --- a/device-discovery/device_discovery/client.py +++ b/device-discovery/device_discovery/client.py @@ -54,12 +54,13 @@ 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). @@ -67,7 +68,7 @@ def init_client(self, target: str, api_key: str | None = 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, ) diff --git a/device-discovery/device_discovery/main.py b/device-discovery/device_discovery/main.py index 828d11c..3d6f894 100644 --- a/device-discovery/device_discovery/main.py +++ b/device-discovery/device_discovery/main.py @@ -43,7 +43,7 @@ def main(): "--port", default=8072, help="Server port", - type=str, + type=int, required=False, ) parser.add_argument( @@ -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 @@ -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, diff --git a/device-discovery/tests/test_client.py b/device-discovery/tests/test_client.py index f0d33f9..3fae9a5 100644 --- a/device-discovery/tests/test_client.py +++ b/device-discovery/tests/test_client.py @@ -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", ) @@ -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) @@ -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) diff --git a/network-discovery/README.md b/network-discovery/README.md index 228ada6..cf5a59a 100644 --- a/network-discovery/README.md +++ b/network-discovery/README.md @@ -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 diff --git a/network-discovery/cmd/main.go b/network-discovery/cmd/main.go index ef54682..7d756cd 100644 --- a/network-discovery/cmd/main.go +++ b/network-discovery/cmd/main.go @@ -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") @@ -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)), )