list_analyzers/
list_analyzers.rs

1//! # Cortex List Analyzers Example
2//!
3//! This example demonstrates how to use the Rust client for the Cortex API
4//! to fetch and list all available analyzer instances configured in your Cortex setup.
5//!
6//! ## Functionality
7//!
8//! 1.  **Configuration**: Sets up the API client configuration using `common::setup_configuration()`,
9//!     which relies on `CORTEX_ENDPOINT` and `CORTEX_API_KEY` environment variables.
10//! 2.  **Fetch Analyzers**: Calls `analyzer_api::find_analyzers()` to retrieve a list
11//!     of all analyzer instances.
12//! 3.  **Display Information**: Iterates through the fetched analyzer instances and prints
13//!     key details for each, including:
14//!     *   Name (e.g., "AbuseIPDB_1_0", "VirusTotal_GetReport_3_0")
15//!     *   Instance ID (`_id`): This is the crucial ID required to run a job with a specific analyzer.
16//!     *   Worker Definition ID
17//!     *   Type (Analyzer/Responder)
18//!
19//! ## Importance of Instance ID
20//!
21//! When you want to run an analysis (i.e., create an analyzer job), you need to provide
22//! the specific `Instance ID` (referred to as `analyzer_worker_id` or similar in other examples)
23//! of the analyzer you wish to use. This example helps you discover these IDs by listing
24//! all configured analyzer instances and their names.
25//!
26//! ## Prerequisites
27//!
28//! -   A running Cortex instance.
29//! -   Environment variables `CORTEX_ENDPOINT` and `CORTEX_API_KEY` must be set.
30//!
31//! ## Usage
32//!
33//! ```sh
34//! # Set your Cortex API endpoint and key
35//! export CORTEX_ENDPOINT="http://your-cortex-host:9000/api"
36//! export CORTEX_API_KEY="your_cortex_api_key"
37//!
38//! # Run the example
39//! cargo run --example list_analyzers
40//! ```
41//!
42//! The output will be a list of analyzer instances. Look for the analyzer you intend to use
43//! (e.g., an AbuseIPDB analyzer) and note its `Instance ID (_id)`. This ID can then be
44//! used in other examples or your own applications to run jobs with that analyzer.
45use cortex_client::apis::analyzer_api;
46mod common; 
47
48#[tokio::main]
49async fn main() -> Result<(), Box<dyn std::error::Error>> {
50    let config = match common::setup_configuration() {
51        Ok(cfg) => cfg,
52        Err(e) => {
53            eprintln!("Configuration error: {}", e);
54            return Err(e.into());
55        }
56    };
57
58    println!("Fetching list of all analyzer instances...");
59
60    let find_request = Some(cortex_client::models::AnalyzerFindRequest::default());
61
62    match analyzer_api::find_analyzers(&config, find_request).await {
63        Ok(analyzers) => {
64            println!("\nSuccessfully fetched analyzer instances:");
65            if analyzers.is_empty() {
66                println!("No analyzer instances found in your Cortex organization.");
67            } else {
68                println!("--------------------------------------------------");
69                println!("Available Analyzer Instances:");
70                println!("--------------------------------------------------");
71                for worker_instance in &analyzers {
72                    // Iterate over the direct vector
73                    println!(
74                        "  Name:                 {}",
75                        worker_instance.name.as_deref().unwrap_or("N/A")
76                    );
77                    println!(
78                        "  Instance ID (_id):    {}", // THIS IS WHAT YOU NEED
79                        worker_instance._id.as_deref().unwrap_or("MISSING_ID")
80                    );
81                    println!(
82                        "  Definition ID:        {}",
83                        worker_instance
84                            .worker_definition_id
85                            .as_deref()
86                            .unwrap_or("N/A")
87                    );
88                    println!(
89                        "  Type:                 {:?}",
90                        worker_instance
91                            .r#type
92                            .unwrap_or(cortex_client::models::worker::Type::Analyzer) // Provide a default if None
93                    );
94                    println!("  Enabled (Implicitly): If listed, it's an instance. Check Cortex UI for explicit enabled/disabled status if unsure.");
95                    println!("  ---");
96                }
97                println!("\nTotal analyzer instances found: {}", analyzers.len());
98                println!("--------------------------------------------------");
99                println!("\nLook for your AbuseIPDB analyzer in the list above and use its 'Instance ID (_id)'");
100                println!(
101                    "in your abuseipdb_example.rs file for the 'analyzer_worker_id' variable."
102                );
103            }
104        }
105        Err(e) => {
106            eprintln!("\nError fetching analyzer instances: {:?}", e);
107            eprintln!("Please check your Cortex connection and API key permissions.");
108            if let cortex_client::apis::Error::Serde(serde_err) = e {
109                eprintln!("Serde error details: {}", serde_err);
110            }
111        }
112    }
113
114    Ok(())
115}