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
use super::*;
pub trait PolicyRuleExt: Sized {
fn new<T: Resource>() -> Self;
fn api_group(self, group: impl ToString) -> Self;
fn api_groups(self, groups: impl IntoIterator<Item = impl ToString>) -> Self;
fn resource(self, resource: impl ToString) -> Self;
fn resources(self, resources: impl IntoIterator<Item = impl ToString>) -> Self;
fn with_status(self) -> Self;
fn verb(self, verb: impl ToString) -> Self;
fn verbs(self, verbs: impl IntoIterator<Item = impl ToString>) -> Self;
fn all_resources(self) -> Self {
self.resource("*")
}
fn all_verbs(self) -> Self {
self.verb("*")
}
}
impl PolicyRuleExt for rbacv1::PolicyRule {
fn new<T: Resource>() -> Self {
default::<Self>()
.api_group(T::GROUP)
.resource(T::URL_PATH_SEGMENT)
}
fn api_group(self, group: impl ToString) -> Self {
let api_groups = Some(vec![group.to_string()]);
Self { api_groups, ..self }
}
fn api_groups(self, groups: impl IntoIterator<Item = impl ToString>) -> Self {
let api_groups = Some(groups.into_iter().map(|group| group.to_string()).collect());
Self { api_groups, ..self }
}
fn resource(self, resource: impl ToString) -> Self {
let resources = Some(vec![resource.to_string()]);
Self { resources, ..self }
}
fn resources(self, resources: impl IntoIterator<Item = impl ToString>) -> Self {
let resources = Some(
resources
.into_iter()
.map(|resource| resource.to_string())
.collect(),
);
Self { resources, ..self }
}
fn with_status(mut self) -> Self {
let add_status = |resource: String| {
if resource.ends_with("/status") {
vec![resource]
} else {
let status = format!("{resource}/status");
vec![resource, status]
}
};
self.resources = self
.resources
.map(|resources| resources.into_iter().flat_map(add_status).collect());
self
}
fn verb(self, verb: impl ToString) -> Self {
let verbs = vec![verb.to_string()];
Self { verbs, ..self }
}
fn verbs(self, verbs: impl IntoIterator<Item = impl ToString>) -> Self {
let verbs = verbs.into_iter().map(|verb| verb.to_string()).collect();
Self { verbs, ..self }
}
}