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