Skip to main content

pdk_test/services/flex/
policy.rs

1// Copyright (c) 2026, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5use crate::services::flex::gcl::ToGcl;
6use serde::Serialize;
7
8/// Configuration for a local API service.
9#[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    /// Creates a builder for initialize a [`PolicyConfig`].
34    pub fn builder() -> PolicyConfigBuilder {
35        PolicyConfigBuilder::new()
36    }
37}
38
39/// A builder for initilizing a [`PolicyConfig`].
40#[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    /// Set the policy name.
53    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    /// Set the policy configuration.
63    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    /// Builds a new [`PolicyConfig`].
73    pub fn build(self) -> PolicyConfig {
74        self.config
75    }
76}
77
78impl ToGcl for PolicyConfig {
79    // NOTE: If in the future we need to support non-compact policy GCLs (to apply policies to
80    // services) we'll need change the api GCL to the non-compact one.
81    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}