vector_vrl_functions/
set_secret.rs

1use vrl::prelude::*;
2
3fn set_secret(
4    ctx: &mut Context,
5    key: Value,
6    secret: Value,
7) -> std::result::Result<Value, ExpressionError> {
8    let key_str = key.as_str().expect("key must be a string");
9    let secret_str = secret.as_str().expect("secret must be a string");
10
11    ctx.target_mut()
12        .insert_secret(key_str.as_ref(), secret_str.as_ref());
13    Ok(Value::Null)
14}
15
16#[derive(Clone, Copy, Debug)]
17pub struct SetSecret;
18
19impl Function for SetSecret {
20    fn identifier(&self) -> &'static str {
21        "set_secret"
22    }
23
24    fn usage(&self) -> &'static str {
25        "Sets the given secret in the event."
26    }
27
28    fn parameters(&self) -> &'static [Parameter] {
29        &[
30            Parameter {
31                keyword: "key",
32                kind: kind::BYTES,
33                required: true,
34            },
35            Parameter {
36                keyword: "secret",
37                kind: kind::BYTES,
38                required: true,
39            },
40        ]
41    }
42
43    fn examples(&self) -> &'static [Example] {
44        &[example!(
45            title: "Set the datadog api key",
46            source: r#"set_secret("datadog_api_key", "secret-value")"#,
47            result: Ok("null"),
48        )]
49    }
50
51    fn compile(
52        &self,
53        _state: &TypeState,
54        _ctx: &mut FunctionCompileContext,
55        arguments: ArgumentList,
56    ) -> Compiled {
57        let key = arguments.required("key");
58        let secret = arguments.required("secret");
59        Ok(SetSecretFn { key, secret }.as_expr())
60    }
61}
62
63#[derive(Debug, Clone)]
64struct SetSecretFn {
65    key: Box<dyn Expression>,
66    secret: Box<dyn Expression>,
67}
68
69impl FunctionExpression for SetSecretFn {
70    fn resolve(&self, ctx: &mut Context) -> Resolved {
71        let key = self.key.resolve(ctx)?;
72        let secret = self.secret.resolve(ctx)?;
73        set_secret(ctx, key, secret)
74    }
75
76    fn type_def(&self, _: &TypeState) -> TypeDef {
77        TypeDef::null().infallible().impure()
78    }
79}