k8s_openapi_ext/ext/
policy_rule.rs1use super::*;
2
3pub trait PolicyRuleExt: Sized {
4 fn new<T: Resource>() -> Self;
5
6 fn api_group(self, group: impl ToString) -> Self;
7
8 fn api_groups(self, groups: impl IntoIterator<Item = impl ToString>) -> Self;
9
10 fn resource(self, resource: impl ToString) -> Self;
11
12 fn resources(self, resources: impl IntoIterator<Item = impl ToString>) -> Self;
13
14 fn resource_name(self, name: impl ToString) -> Self {
15 self.resource_names([name])
16 }
17
18 fn resource_names(self, names: impl IntoIterator<Item = impl ToString>) -> Self;
19
20 fn with_status(self) -> Self;
21
22 fn verb(self, verb: impl ToString) -> Self;
23
24 fn verbs(self, verbs: impl IntoIterator<Item = impl ToString>) -> Self;
25
26 fn all_resources(self) -> Self {
27 self.resource("*")
28 }
29
30 fn all_verbs(self) -> Self {
31 self.verb("*")
32 }
33}
34
35impl PolicyRuleExt for rbacv1::PolicyRule {
36 fn new<T: Resource>() -> Self {
37 default::<Self>()
38 .api_group(T::GROUP)
39 .resource(T::URL_PATH_SEGMENT)
40 }
41
42 fn api_group(self, group: impl ToString) -> Self {
43 let api_groups = Some(vec![group.to_string()]);
44 Self { api_groups, ..self }
45 }
46
47 fn api_groups(self, groups: impl IntoIterator<Item = impl ToString>) -> Self {
48 let api_groups = Some(groups.into_iter().map(|group| group.to_string()).collect());
49 Self { api_groups, ..self }
50 }
51
52 fn resource(self, resource: impl ToString) -> Self {
53 let resources = Some(vec![resource.to_string()]);
54 Self { resources, ..self }
55 }
56
57 fn resources(self, resources: impl IntoIterator<Item = impl ToString>) -> Self {
58 let resources = Some(
59 resources
60 .into_iter()
61 .map(|resource| resource.to_string())
62 .collect(),
63 );
64 Self { resources, ..self }
65 }
66
67 fn resource_names(self, names: impl IntoIterator<Item = impl ToString>) -> Self {
68 let resource_names = Some(names.into_iter().map(|name| name.to_string()).collect());
69 Self {
70 resource_names,
71 ..self
72 }
73 }
74
75 fn with_status(mut self) -> Self {
76 let add_status = |resource: String| {
77 if resource.ends_with("/status") {
78 vec![resource]
79 } else {
80 let status = format!("{resource}/status");
81 vec![resource, status]
82 }
83 };
84
85 self.resources = self
86 .resources
87 .map(|resources| resources.into_iter().flat_map(add_status).collect());
88 self
89 }
90
91 fn verb(self, verb: impl ToString) -> Self {
92 let verbs = vec![verb.to_string()];
93 Self { verbs, ..self }
94 }
95
96 fn verbs(self, verbs: impl IntoIterator<Item = impl ToString>) -> Self {
97 let verbs = verbs.into_iter().map(|verb| verb.to_string()).collect();
98 Self { verbs, ..self }
99 }
100}