pdk_test/services/flex/
policy.rs1use crate::services::flex::gcl::ToGcl;
6use serde::Serialize;
7
8#[derive(Debug, Clone)]
10pub struct PolicyConfig {
11 name: String,
12 config: String,
13}
14
15impl PolicyConfig {
16 fn new() -> Self {
17 Self {
18 name: "test-policy".to_string(),
19 config: "{}".to_string(),
20 }
21 }
22
23 #[cfg(feature = "experimental")]
24 pub(crate) fn name(&self) -> &str {
25 &self.name
26 }
27
28 #[cfg(feature = "experimental")]
29 pub(crate) fn config(&self) -> &str {
30 &self.config
31 }
32
33 pub fn builder() -> PolicyConfigBuilder {
35 PolicyConfigBuilder::new()
36 }
37}
38
39#[derive(Debug, Clone)]
41pub struct PolicyConfigBuilder {
42 config: PolicyConfig,
43}
44
45impl PolicyConfigBuilder {
46 fn new() -> Self {
47 Self {
48 config: PolicyConfig::new(),
49 }
50 }
51
52 pub fn name<T: Into<String>>(self, name: T) -> Self {
54 Self {
55 config: PolicyConfig {
56 name: name.into(),
57 ..self.config
58 },
59 }
60 }
61
62 pub fn configuration<T: Serialize>(self, config: T) -> Self {
64 Self {
65 config: PolicyConfig {
66 config: serde_json::to_string(&config).unwrap_or_else(|_| "{}".to_string()),
67 ..self.config
68 },
69 }
70 }
71
72 pub fn build(self) -> PolicyConfig {
74 self.config
75 }
76}
77
78impl ToGcl for PolicyConfig {
79 fn to_gcl(&self) -> String {
82 let name = self.name.as_str();
83 let config = self.config.as_str();
84 format!(
85 r#"
86 - policyRef:
87 name: {name}
88 namespace: default
89 config: {config}"#
90 )
91 }
92
93 fn name(&self) -> &str {
94 &self.name
95 }
96}