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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use derive_new::new;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::{HttpMethods, KeyValue, Service, NAMESPACE_ENDPOINT, RULES_ENDPOINT};
use crate::client::Context;
#[derive(new,Default, Debug, Clone)]
pub struct RuleService<T> {
client: T,
context: Context,
}
#[derive(new, Debug, Clone, Serialize, Deserialize, Default)]
pub struct Rule {
#[serde(default)]
pub namespace: String,
#[serde(default)]
pub name: String,
#[serde(default)]
pub version: String,
#[serde(default)]
pub annotations: Vec<KeyValue>,
pub status: String,
#[serde(default)]
pub trigger: Value,
#[serde(default)]
pub action: Value,
#[serde(default)]
pub publish: bool,
#[serde(default)]
pub updated: i64,
}
#[derive(new, Debug, Clone, Serialize, Deserialize, Default)]
pub struct RuleListOptions {
pub limit: i64,
pub skip: i64,
pub docs: bool,
}
impl Rule {
fn set_status(state: String) -> Self {
Self {
status: state,
..Default::default()
}
}
}
impl<T> RuleService<T>
where
T: Service,
{
pub fn list(&self) -> Result<Vec<Rule>, String> {
let url = format!(
"{}/api/v1/{}/{}/{}",
self.context.host(),
NAMESPACE_ENDPOINT,
self.context.namespace(),
RULES_ENDPOINT,
);
let auth = self.context.auth();
let user = auth.0;
let pass = auth.1;
let request = self
.client
.new_request(HttpMethods::GET, url.as_str(), Some((user, pass)), None)
.unwrap();
match self.client.invoke_request(request) {
Ok(x) => match serde_json::from_value(x) {
Ok(actions) => Ok(actions),
Err(err) => Err(format!("Failed to deserailize Rules {}", err)),
},
Err(x) => Err(format!("Failed to fetch the list of Rules {}", x)),
}
}
pub fn insert(&self, rule: &Rule, overwrite: bool) -> Result<Rule, String> {
let url = format!(
"{}/api/v1/{}/{}/{}/{}?overwrite={}",
self.context.host(),
NAMESPACE_ENDPOINT,
self.context.namespace(),
RULES_ENDPOINT,
rule.name,
overwrite
);
let auth = self.context.auth();
let user = auth.0;
let pass = auth.1;
let body = match serde_json::to_value(rule) {
Ok(body) => body,
Err(error) => return Err(format!("Failed deserailize body {}", error)),
};
let request = self
.client
.new_request(
HttpMethods::PUT,
url.as_str(),
Some((user, pass)),
Some(body),
)
.unwrap();
match self.client.invoke_request(request) {
Ok(x) => match serde_json::from_value(x) {
Ok(actions) => Ok(actions),
Err(err) => Err(format!("Failed to deserailize rule {}", err)),
},
Err(x) => Err(format!("Failed to create rule {}", x)),
}
}
pub fn get(&self, rule_name: &str) -> Result<Rule, String> {
let url = format!(
"{}/api/v1/{}/{}/{}/{}",
self.context.host(),
NAMESPACE_ENDPOINT,
self.context.namespace(),
RULES_ENDPOINT,
rule_name
);
let auth = self.context.auth();
let user = auth.0;
let pass = auth.1;
let request = self
.client
.new_request(HttpMethods::PUT, url.as_str(), Some((user, pass)), None)
.unwrap();
match self.client.invoke_request(request) {
Ok(x) => match serde_json::from_value(x) {
Ok(actions) => Ok(actions),
Err(err) => Err(format!("Failed to deserailize rule {}", err)),
},
Err(x) => Err(format!("Failed to get rule properties{}", x)),
}
}
pub fn delete(&self, rule_name: &str) -> Result<Rule, String> {
let url = format!(
"{}/api/v1/{}/{}/{}/{}",
self.context.host(),
NAMESPACE_ENDPOINT,
self.context.namespace(),
RULES_ENDPOINT,
rule_name
);
let auth = self.context.auth();
let user = auth.0;
let pass = auth.1;
let request = self
.client
.new_request(HttpMethods::PUT, url.as_str(), Some((user, pass)), None)
.unwrap();
match self.client.invoke_request(request) {
Ok(x) => match serde_json::from_value(x) {
Ok(actions) => Ok(actions),
Err(err) => Err(format!("Failed to deserailize rule {}", err)),
},
Err(x) => Err(format!("Failed to get rule properties{}", x)),
}
}
pub fn setstate(&self, rule_name: &str, state: &str) -> Result<Rule, String> {
let state = state.to_lowercase();
if state != "active" && state != "inactive" {
return Err(format!("Invalid setstate options"));
} else {
let url = format!(
"{}/api/v1/{}/{}/{}/{}",
self.context.host(),
NAMESPACE_ENDPOINT,
self.context.namespace(),
RULES_ENDPOINT,
rule_name
);
let auth = self.context.auth();
let user = auth.0;
let pass = auth.1;
let setstate = Rule::set_status(state);
let body = match serde_json::to_value(setstate) {
Ok(body) => body,
Err(error) => return Err(format!("Failed deserailize body {}", error)),
};
let request = self
.client
.new_request(
HttpMethods::POST,
url.as_str(),
Some((user, pass)),
Some(body),
)
.unwrap();
match self.client.invoke_request(request) {
Ok(x) => match serde_json::from_value(x) {
Ok(actions) => Ok(actions),
Err(err) => Err(format!("Failed to deserailize rule {}", err)),
},
Err(x) => Err(format!("Failed to SetState for Rule {}", x)),
}
}
}
}