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 pub fn builder() -> PolicyConfigBuilder {
25 PolicyConfigBuilder::new()
26 }
27}
28
29#[derive(Debug, Clone)]
31pub struct PolicyConfigBuilder {
32 config: PolicyConfig,
33}
34
35impl PolicyConfigBuilder {
36 fn new() -> Self {
37 Self {
38 config: PolicyConfig::new(),
39 }
40 }
41
42 pub fn name<T: Into<String>>(self, name: T) -> Self {
44 Self {
45 config: PolicyConfig {
46 name: name.into(),
47 ..self.config
48 },
49 }
50 }
51
52 pub fn configuration<T: Serialize>(self, config: T) -> Self {
54 Self {
55 config: PolicyConfig {
56 config: serde_json::to_string(&config).unwrap_or_else(|_| "{}".to_string()),
57 ..self.config
58 },
59 }
60 }
61
62 pub fn build(self) -> PolicyConfig {
64 self.config
65 }
66}
67
68impl ToGcl for PolicyConfig {
69 fn to_gcl(&self) -> String {
72 let name = self.name.as_str();
73 let config = self.config.as_str();
74 format!(
75 r#"
76 - policyRef:
77 name: {name}
78 namespace: default
79 config: {config}"#
80 )
81 }
82
83 fn name(&self) -> &str {
84 &self.name
85 }
86}