Skip to main content

01_hello_wami/
01_hello_wami.rs

1//! Hello WAMI - Absolute Basics
2//!
3//! This example demonstrates:
4//! - Initializing an in-memory store
5//! - Creating a single user
6//! - Retrieving the user
7//!
8//! Scenario: Your first steps with WAMI - create and retrieve a user.
9//!
10//! Run with: `cargo run --example 01_hello_wami`
11
12use wami::arn::{TenantPath, WamiArn};
13use wami::context::WamiContext;
14use wami::store::memory::InMemoryWamiStore;
15use wami::store::traits::UserStore;
16use wami::wami::identity::user::builder::build_user;
17
18#[tokio::main]
19async fn main() -> Result<(), Box<dyn std::error::Error>> {
20    println!("=== Hello WAMI ===\n");
21
22    // Step 1: Initialize the store
23    println!("Step 1: Initializing in-memory store...");
24    let mut store = InMemoryWamiStore::default();
25    println!("✓ Store initialized");
26
27    // Step 2: Create a WamiContext for operations
28    println!("\nStep 2: Creating WAMI context...");
29    let context = WamiContext::builder()
30        .instance_id("123456789012")
31        .tenant_path(TenantPath::single(0)) // Root tenant ID is 0
32        .caller_arn(
33            WamiArn::builder()
34                .service(wami::arn::Service::Iam)
35                .tenant_path(TenantPath::single(0))
36                .wami_instance("123456789012")
37                .resource("user", "admin")
38                .build()?,
39        )
40        .is_root(false)
41        .build()?;
42    println!("✓ Context created");
43
44    // Step 3: Build a user using pure functions
45    println!("\nStep 3: Building user 'alice'...");
46    let user = build_user("alice".to_string(), Some("/".to_string()), &context)?;
47    println!("✓ User built with ARN: {}", user.wami_arn);
48
49    // Step 4: Store the user
50    println!("\nStep 4: Storing user in the store...");
51    let created_user = store.create_user(user).await?;
52    println!("✓ User stored successfully");
53    println!("  - Name: {}", created_user.user_name);
54    println!("  - User ID: {}", created_user.user_id);
55    println!("  - ARN: {}", created_user.wami_arn);
56
57    // Step 5: Retrieve the user
58    println!("\nStep 5: Retrieving user from store...");
59    let retrieved = store.get_user("alice").await?;
60    match retrieved {
61        Some(user) => {
62            println!("✓ User retrieved successfully");
63            println!("  - Name: {}", user.user_name);
64            println!("  - Path: {}", user.path);
65        }
66        None => println!("✗ User not found"),
67    }
68
69    println!("\n✅ Example completed successfully!");
70    println!("Key takeaways:");
71    println!("- InMemoryWamiStore provides a simple storage backend");
72    println!("- Providers (AWS, GCP, Azure) handle platform-specific details");
73    println!("- Pure functions create domain objects without side effects");
74
75    Ok(())
76}