create_monitor/
create_monitor.rs

1use kuma_client::{
2    monitor::{MonitorGroup, MonitorHttp},
3    notification::Notification,
4    tag::{Tag, TagDefinition},
5    Client, Config, Url,
6};
7
8#[tokio::main()]
9async fn main() {
10    // Connect to the server
11    let client = Client::connect(Config {
12        url: Url::parse("http://localhost:3001").expect("Invalid URL"),
13        username: Some("Username".to_owned()),
14        password: Some("Password".to_owned()),
15        ..Default::default()
16    })
17    .await
18    .expect("Failed to connect to server");
19
20    // Create a tag
21    let tag_definition = client
22        .add_tag(TagDefinition {
23            name: Some("example_tag".to_owned()),
24            color: Some("red".to_owned()),
25            ..Default::default()
26        })
27        .await
28        .expect("Failed to add tag");
29
30    // Create a group
31    let group = client
32        .add_monitor(MonitorGroup {
33            name: Some("Example Group".to_owned()),
34            tags: vec![Tag {
35                tag_id: tag_definition.tag_id,
36                value: Some("example_group".to_owned()),
37                ..Default::default()
38            }],
39            ..Default::default()
40        })
41        .await
42        .expect("Failed to add group");
43
44    // Createa a notification
45    let notification = client
46        .add_notification(Notification {
47            name: Some("Example Notification".to_owned()),
48            config: Some(serde_json::json!({
49                "webhookURL": "https://webhook.site/304eeaf2-0248-49be-8985-2c86175520ca",
50                "webhookContentType": "json"
51            })),
52            ..Default::default()
53        })
54        .await
55        .expect("Failed to add notification");
56
57    // Create a monitor
58    client
59        .add_monitor(MonitorHttp {
60            name: Some("Monitor Name".to_owned()),
61            url: Some("https://example.com".to_owned()),
62            parent: group.common().id().clone(),
63            tags: vec![Tag {
64                tag_id: tag_definition.tag_id,
65                value: Some("example_monitor".to_owned()),
66                ..Default::default()
67            }],
68            notification_id_list: Some(
69                vec![(
70                    notification.id.expect("No notification ID").to_string(),
71                    true,
72                )]
73                .into_iter()
74                .collect(),
75            ),
76            ..Default::default()
77        })
78        .await
79        .expect("Failed to add monitor");
80
81    let monitors = client.get_monitors().await.expect("Failed to get monitors");
82    println!("{:?}", monitors);
83}