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 parameters(&self) -> &'static [Parameter] {
25        &[
26            Parameter {
27                keyword: "key",
28                kind: kind::BYTES,
29                required: true,
30            },
31            Parameter {
32                keyword: "secret",
33                kind: kind::BYTES,
34                required: true,
35            },
36        ]
37    }
38
39    fn examples(&self) -> &'static [Example] {
40        &[example!(
41            title: "Set the datadog api key",
42            source: r#"set_secret("datadog_api_key", "secret-value")"#,
43            result: Ok("null"),
44        )]
45    }
46
47    fn compile(
48        &self,
49        _state: &TypeState,
50        _ctx: &mut FunctionCompileContext,
51        arguments: ArgumentList,
52    ) -> Compiled {
53        let key = arguments.required("key");
54        let secret = arguments.required("secret");
55        Ok(SetSecretFn { key, secret }.as_expr())
56    }
57}
58
59#[derive(Debug, Clone)]
60struct SetSecretFn {
61    key: Box<dyn Expression>,
62    secret: Box<dyn Expression>,
63}
64
65impl FunctionExpression for SetSecretFn {
66    fn resolve(&self, ctx: &mut Context) -> Resolved {
67        let key = self.key.resolve(ctx)?;
68        let secret = self.secret.resolve(ctx)?;
69        set_secret(ctx, key, secret)
70    }
71
72    fn type_def(&self, _: &TypeState) -> TypeDef {
73        TypeDef::null().infallible().impure()
74    }
75}