ds_common_aws_rs_lib/ssm/mod.rs
1//! AWS SSM module.
2//!
3//! This module contains functions for interacting with AWS SSM.
4
5pub mod error;
6
7use aws_sdk_ssm::{types::ParameterType, Client};
8
9use crate::error::{Error, Result};
10use crate::ssm::error::{map_ssm_err, SsmError};
11
12/// SSM service for interacting with AWS Systems Manager
13///
14/// This service provides a high-level interface for SSM operations
15/// with dependency injection for better performance and testability.
16pub struct SsmService {
17 client: Client,
18}
19
20impl SsmService {
21 /// Create a new SSM service with a custom client
22 ///
23 /// # Arguments
24 ///
25 /// * `client` - The SSM client to use
26 ///
27 /// # Returns
28 ///
29 /// Returns a new SsmService instance with the provided client.
30 pub fn with_client(client: Client) -> Self {
31 Self { client }
32 }
33
34 /// Create a new SSM service with a shared configuration
35 ///
36 /// # Arguments
37 ///
38 /// * `config` - The AWS SDK configuration to use
39 ///
40 /// # Returns
41 ///
42 /// Returns a new SsmService instance with the provided configuration.
43 pub fn with_config(config: &aws_config::SdkConfig) -> Self {
44 Self {
45 client: Client::new(config),
46 }
47 }
48
49 /// Get an SSM parameter
50 ///
51 /// # Arguments
52 ///
53 /// * `name` - The name of the parameter to get
54 ///
55 /// # Returns
56 ///
57 /// Returns the parameter value as a String
58 ///
59 /// # Errors
60 ///
61 /// * [`SsmError::Service`] - If the SSM client fails to create.
62 /// * [`SsmError::Timeout`] - If the request times out.
63 /// * [`SsmError::Transport`] - If the request fails to dispatch.
64 /// * [`SsmError::Build`] - If the request fails to build.
65 /// * [`SsmError::InvalidResponse`] - If the parameter is not found.
66 pub async fn get_parameter(&self, name: &str) -> Result<String> {
67 let response = self
68 .client
69 .get_parameter()
70 .name(name)
71 .with_decryption(true)
72 .send()
73 .await
74 .map_err(map_ssm_err)?;
75
76 match response.parameter() {
77 Some(parameter) => match parameter.value() {
78 Some(value) => Ok(value.to_string()),
79 None => Err(Error::from(SsmError::InvalidResponse(name.to_string()))),
80 },
81 None => Err(Error::from(SsmError::InvalidResponse(name.to_string()))),
82 }
83 }
84
85 /// Put an SSM parameter
86 ///
87 /// # Arguments
88 ///
89 /// * `name` - The name of the parameter to put
90 /// * `value` - The value of the parameter to put
91 /// * `parameter_type` - The type of the parameter
92 /// * `overwrite` - Whether to overwrite an existing parameter
93 ///
94 /// # Returns
95 ///
96 /// Returns nothing
97 ///
98 /// # Errors
99 ///
100 /// * [`SsmError::Service`] - If the SSM client fails to create.
101 /// * [`SsmError::Timeout`] - If the request times out.
102 /// * [`SsmError::Transport`] - If the request fails to dispatch.
103 /// * [`SsmError::Build`] - If the request fails to build.
104 pub async fn put_parameter(
105 &self,
106 name: &str,
107 value: &str,
108 parameter_type: ParameterType,
109 overwrite: bool,
110 ) -> Result<()> {
111 self.client
112 .put_parameter()
113 .name(name)
114 .value(value)
115 .r#type(parameter_type)
116 .overwrite(overwrite)
117 .send()
118 .await
119 .map_err(map_ssm_err)?;
120
121 Ok(())
122 }
123}