pdk_test/services/flex/
policy.rs

1// Copyright (c) 2025, 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    /// Creates a builder for initialize a [`PolicyConfig`].
24    pub fn builder() -> PolicyConfigBuilder {
25        PolicyConfigBuilder::new()
26    }
27}
28
29/// A builder for initilizing a [`PolicyConfig`].
30#[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    /// Set the policy name.
43    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    /// Set the policy configuration.
53    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    /// Builds a new [`PolicyConfig`].
63    pub fn build(self) -> PolicyConfig {
64        self.config
65    }
66}
67
68impl ToGcl for PolicyConfig {
69    // NOTE: If in the future we need to support non-compact policy GCLs (to apply policies to
70    // services) we'll need change the api GCL to the non-compact one.
71    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}