/ examples / fetch_all_entities.rs
fetch_all_entities.rs
 1  //! Example: Fetch All Entities
 2  //!
 3  //! This example demonstrates how to fetch all entities from a Backstage catalog.
 4  //!
 5  //! Usage:
 6  //!   BACKSTAGE_URL=https://your-backstage.com BACKSTAGE_TOKEN=your-token cargo run --example fetch_all_entities
 7  
 8  use backstage_client::{entities::Entity, BackstageClient};
 9  use log::{error, info};
10  use std::env;
11  
12  #[tokio::main]
13  async fn main() -> Result<(), Box<dyn std::error::Error>> {
14      // Initialize logging
15      env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("INFO")).init();
16  
17      // Get configuration from environment variables
18      let base_url =
19          env::var("BACKSTAGE_URL").unwrap_or_else(|_| "https://demo.backstage.io".to_string());
20      let token =
21          env::var("BACKSTAGE_TOKEN").expect("BACKSTAGE_TOKEN environment variable must be set");
22  
23      info!("Connecting to Backstage at: {}", base_url);
24  
25      // Create a new Backstage client
26      let client = BackstageClient::new(&base_url, &token)?;
27  
28      // Fetch all entities
29      info!("Fetching all entities from Backstage...");
30      match client.fetch_entities::<Entity>(None).await {
31          Ok(entities) => {
32              info!("Successfully fetched {} entities", entities.len());
33  
34              // Group entities by kind
35              let mut entity_counts = std::collections::HashMap::new();
36              for entity in &entities {
37                  *entity_counts.entry(entity.kind()).or_insert(0) += 1;
38              }
39  
40              info!("Entity breakdown:");
41              for (kind, count) in entity_counts {
42                  info!("  {}: {}", kind, count);
43              }
44  
45              // Show first few entities as examples
46              info!("\nFirst 5 entities:");
47              for (i, entity) in entities.iter().take(5).enumerate() {
48                  info!(
49                      "  {}. {} ({}) - {}",
50                      i + 1,
51                      entity.name(),
52                      entity.kind(),
53                      entity.description().unwrap_or("No description")
54                  );
55  
56                  // Show namespace if not default
57                  if entity.namespace() != "default" {
58                      info!("     Namespace: {}", entity.namespace());
59                  }
60  
61                  // Show tags if available
62                  if let Some(tags) = entity.tags() {
63                      if !tags.is_empty() {
64                          info!("     Tags: {}", tags.join(", "));
65                      }
66                  }
67              }
68  
69              if entities.len() > 5 {
70                  info!("... and {} more entities", entities.len() - 5);
71              }
72          }
73          Err(e) => {
74              error!("Error fetching entities: {}", e);
75              return Err(e.into());
76          }
77      }
78  
79      Ok(())
80  }