From 71200f4370d2577de84aa5761bf43ca37fbb2845 Mon Sep 17 00:00:00 2001 From: Leonardo Parente <23251360+leoparente@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:12:54 -0300 Subject: [PATCH 1/3] feat: allow configure diode producer_app_name prefix --- device-discovery/README.md | 2 ++ device-discovery/device_discovery/client.py | 5 +++-- device-discovery/device_discovery/main.py | 14 ++++++++++++-- device-discovery/tests/test_client.py | 18 ++++++++++++------ network-discovery/README.md | 2 ++ network-discovery/cmd/main.go | 8 +++++++- 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/device-discovery/README.md b/device-discovery/README.md index e6540bf..0e8d957 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_PREFIX_NAME, --diode-prefix-name DIODE_PREFIX_NAME + Diode prefix producer app name ``` ### 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..e1157b8 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-prefix-name", + help="Diode prefix producer app name", + 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_prefix_name, 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..f7fabce 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-prefix-name 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..84c3a02 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})") + diodePrefixName := flag.String("diode-prefix-name", "", "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 *diodePrefixName != "" { + producerName = fmt.Sprintf("%s/%s", *diodePrefixName, AppName) + } + client, err := diode.NewClient( *diodeTarget, - AppName, + producerName, version.GetBuildVersion(), diode.WithAPIKey(resolveEnv(*diodeAPIKey)), ) From 9c4fdff83b3d1fe4290de4847832d379cdf4706d Mon Sep 17 00:00:00 2001 From: Leonardo Parente <23251360+leoparente@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:17:07 -0300 Subject: [PATCH 2/3] consistency --- device-discovery/README.md | 2 +- device-discovery/device_discovery/main.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/device-discovery/README.md b/device-discovery/README.md index 0e8d957..8a1c39a 100644 --- a/device-discovery/README.md +++ b/device-discovery/README.md @@ -18,7 +18,7 @@ options: Diode API key. Environment variables can be used by wrapping them in ${} (e.g. ${MY_API_KEY}) -a DIODE_PREFIX_NAME, --diode-prefix-name DIODE_PREFIX_NAME - Diode prefix producer app name + Diode producer_app_name prefix ``` ### Policy RFC diff --git a/device-discovery/device_discovery/main.py b/device-discovery/device_discovery/main.py index e1157b8..17804e7 100644 --- a/device-discovery/device_discovery/main.py +++ b/device-discovery/device_discovery/main.py @@ -65,7 +65,7 @@ def main(): parser.add_argument( "-a", "--diode-prefix-name", - help="Diode prefix producer app name", + help="Diode producer_app_name prefix", type=str, required=False, ) From 3c47f6c365877a4565a095c689b8b949543fb9d9 Mon Sep 17 00:00:00 2001 From: Leonardo Parente <23251360+leoparente@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:41:44 -0300 Subject: [PATCH 3/3] rename flag --- device-discovery/README.md | 2 +- device-discovery/device_discovery/main.py | 4 ++-- network-discovery/README.md | 2 +- network-discovery/cmd/main.go | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/device-discovery/README.md b/device-discovery/README.md index 8a1c39a..fae1582 100644 --- a/device-discovery/README.md +++ b/device-discovery/README.md @@ -17,7 +17,7 @@ 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_PREFIX_NAME, --diode-prefix-name DIODE_PREFIX_NAME + -a DIODE_APP_NAME_PREFIX, --diode-app-name-prefix DIODE_APP_NAME_PREFIX Diode producer_app_name prefix ``` diff --git a/device-discovery/device_discovery/main.py b/device-discovery/device_discovery/main.py index 17804e7..3d6f894 100644 --- a/device-discovery/device_discovery/main.py +++ b/device-discovery/device_discovery/main.py @@ -64,7 +64,7 @@ def main(): parser.add_argument( "-a", - "--diode-prefix-name", + "--diode-app-name-prefix", help="Diode producer_app_name prefix", type=str, required=False, @@ -79,7 +79,7 @@ def main(): client = Client() client.init_client( - prefix=args.diode_prefix_name, target=args.diode_target, api_key=api_key + prefix=args.diode_app_name_prefix, target=args.diode_target, api_key=api_key ) uvicorn.run( app, diff --git a/network-discovery/README.md b/network-discovery/README.md index f7fabce..cf5a59a 100644 --- a/network-discovery/README.md +++ b/network-discovery/README.md @@ -6,7 +6,7 @@ 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-prefix-name string + -diode-app-name-prefix string diode producer_app_name prefix -diode-target string diode target (REQUIRED) diff --git a/network-discovery/cmd/main.go b/network-discovery/cmd/main.go index 84c3a02..7d756cd 100644 --- a/network-discovery/cmd/main.go +++ b/network-discovery/cmd/main.go @@ -43,7 +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})") - diodePrefixName := flag.String("diode-prefix-name", "", "diode producer_app_name prefix") + 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") @@ -60,8 +60,8 @@ func main() { } producerName := AppName - if *diodePrefixName != "" { - producerName = fmt.Sprintf("%s/%s", *diodePrefixName, AppName) + if *diodeNamePrefix != "" { + producerName = fmt.Sprintf("%s/%s", *diodeNamePrefix, AppName) } client, err := diode.NewClient(