1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::convert::TryFrom;

use snafu::{ResultExt, Snafu};

use vector_lib::configurable::configurable_component;

use crate::{
    aws::AwsAuthentication,
    codecs::EncodingConfig,
    config::AcknowledgementsConfig,
    sinks::util::TowerRequestConfig,
    template::{Template, TemplateParseError},
    tls::TlsConfig,
};

#[derive(Debug, Snafu)]
pub(super) enum BuildError {
    #[snafu(display("`message_group_id` should be defined for FIFO queue."))]
    MessageGroupIdMissing,
    #[snafu(display("`message_group_id` is not allowed with non-FIFO queue."))]
    MessageGroupIdNotAllowed,
    #[snafu(display("invalid topic template: {}", source))]
    TopicTemplate { source: TemplateParseError },
}

/// Base Configuration `aws_s_s` for sns and sqs sink.
#[configurable_component]
#[derive(Clone, Debug)]
#[serde(deny_unknown_fields)]
pub(super) struct BaseSSSinkConfig {
    #[configurable(derived)]
    pub(super) encoding: EncodingConfig,

    /// The tag that specifies that a message belongs to a specific message group.
    ///
    /// Can be applied only to FIFO queues.
    #[configurable(metadata(docs::examples = "vector"))]
    #[configurable(metadata(docs::examples = "vector-%Y-%m-%d"))]
    pub(super) message_group_id: Option<String>,

    /// The message deduplication ID value to allow AWS to identify duplicate messages.
    ///
    /// This value is a template which should result in a unique string for each event. See the [AWS
    /// documentation][deduplication_id_docs] for more about how AWS does message deduplication.
    ///
    /// [deduplication_id_docs]: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagededuplicationid-property.html
    #[configurable(metadata(docs::examples = "{{ transaction_id }}"))]
    pub(super) message_deduplication_id: Option<String>,

    #[configurable(derived)]
    #[serde(default)]
    pub(super) request: TowerRequestConfig,

    #[configurable(derived)]
    pub(super) tls: Option<TlsConfig>,

    /// The ARN of an [IAM role][iam_role] to assume at startup.
    ///
    /// [iam_role]: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html
    #[configurable(deprecated)]
    #[configurable(metadata(docs::hidden))]
    pub(super) assume_role: Option<String>,

    #[configurable(derived)]
    #[serde(default)]
    pub(super) auth: AwsAuthentication,

    #[configurable(derived)]
    #[serde(
        default,
        deserialize_with = "crate::serde::bool_or_struct",
        skip_serializing_if = "crate::serde::is_default"
    )]
    pub(super) acknowledgements: AcknowledgementsConfig,
}

pub(super) fn message_group_id(
    message_group_id: Option<String>,
    fifo: bool,
) -> crate::Result<Option<Template>> {
    match (message_group_id.as_ref(), fifo) {
        (Some(value), true) => Ok(Some(
            Template::try_from(value.clone()).context(TopicTemplateSnafu)?,
        )),
        (Some(_), false) => Err(Box::new(BuildError::MessageGroupIdNotAllowed)),
        (None, true) => Err(Box::new(BuildError::MessageGroupIdMissing)),
        (None, false) => Ok(None),
    }
}
pub(super) fn message_deduplication_id(
    message_deduplication_id: Option<String>,
) -> crate::Result<Option<Template>> {
    Ok(message_deduplication_id
        .clone()
        .map(Template::try_from)
        .transpose()?)
}