cx_sdk/client/
archive_retentions.rs

1// Copyright 2024 Coralogix Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use cx_api::proto::com::coralogix::archive::v1::{
16    ActivateRetentionsRequest,
17    ActivateRetentionsResponse,
18    GetRetentionsEnabledRequest,
19    GetRetentionsEnabledResponse,
20    GetRetentionsRequest,
21    GetRetentionsResponse,
22    UpdateRetentionsRequest,
23    UpdateRetentionsResponse,
24    retentions_service_client::RetentionsServiceClient,
25};
26use std::str::FromStr;
27use tokio::sync::Mutex;
28use tonic::{
29    metadata::MetadataMap,
30    transport::{
31        Channel,
32        ClientTlsConfig,
33        Endpoint,
34    },
35};
36
37use crate::{
38    CoralogixRegion,
39    auth::AuthContext,
40    error::{
41        Result,
42        SdkApiError,
43        SdkError,
44    },
45    metadata::CallProperties,
46    util::make_request_with_metadata,
47};
48
49pub use cx_api::proto::com::coralogix::archive::v1::RetentionUpdateElement;
50
51const ARCHIVE_FEATURE_GROUP_ID: &str = "archive";
52
53/// The Archive Retention API client.
54/// Read more at <https://coralogix.com/docs/archive-retention/>
55pub struct ArchiveRetentionClient {
56    metadata_map: MetadataMap,
57    service_client: Mutex<RetentionsServiceClient<Channel>>,
58}
59
60impl ArchiveRetentionClient {
61    /// Creates a new client for the Archive Retention API.
62    ///
63    /// # Arguments
64    /// * `auth_context` - The [`AuthContext`] to use for authentication.
65    /// * `region` - The [`CoralogixRegion`] to connect to.
66    pub fn new(auth_context: AuthContext, region: CoralogixRegion) -> Result<Self> {
67        let channel: Channel = Endpoint::from_str(region.grpc_endpoint().as_str())?
68            .tls_config(ClientTlsConfig::new().with_native_roots())?
69            .connect_lazy();
70        let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
71        Ok(Self {
72            metadata_map: request_metadata.to_metadata_map(),
73            service_client: Mutex::new(RetentionsServiceClient::new(channel)),
74        })
75    }
76
77    /// Retrieves the retention settings for a tenant.
78    pub async fn get(&self) -> Result<GetRetentionsResponse> {
79        Ok(self
80            .service_client
81            .lock()
82            .await
83            .get_retentions(make_request_with_metadata(
84                GetRetentionsRequest {},
85                &self.metadata_map,
86            ))
87            .await
88            .map_err(|status| {
89                SdkError::ApiError(SdkApiError {
90                    status,
91                    endpoint: "/com.coralogix.archive.v1.RetentionsService/GetRetentions"
92                        .to_string(),
93                    feature_group: ARCHIVE_FEATURE_GROUP_ID.to_string(),
94                })
95            })?
96            .into_inner())
97    }
98
99    /// Retrieves the enabled retention settings for a tenant.
100    pub async fn get_enabled(&self) -> Result<GetRetentionsEnabledResponse> {
101        Ok(self
102            .service_client
103            .lock()
104            .await
105            .get_retentions_enabled(make_request_with_metadata(
106                GetRetentionsEnabledRequest {},
107                &self.metadata_map,
108            ))
109            .await
110            .map_err(|status| {
111                SdkError::ApiError(SdkApiError {
112                    status,
113                    endpoint: "/com.coralogix.archive.v1.RetentionsService/GetEnabled".to_string(),
114                    feature_group: ARCHIVE_FEATURE_GROUP_ID.to_string(),
115                })
116            })?
117            .into_inner())
118    }
119
120    /// Updates the retention settings for a tenant.
121    pub async fn update(
122        &self,
123        retention_update_elements: Vec<RetentionUpdateElement>,
124    ) -> Result<UpdateRetentionsResponse> {
125        let response = {
126            let mut client = self.service_client.lock().await;
127            let update_response = client
128                .update_retentions(make_request_with_metadata(
129                    UpdateRetentionsRequest {
130                        retention_update_elements,
131                    },
132                    &self.metadata_map,
133                ))
134                .await;
135            match update_response.map_err(|status| {
136                SdkError::ApiError(SdkApiError {
137                    status,
138                    endpoint: "/com.coralogix.archive.v1.RetentionsService/UpdateRetentions"
139                        .to_string(),
140                    feature_group: ARCHIVE_FEATURE_GROUP_ID.to_string(),
141                })
142            }) {
143                Ok(output) => {
144                    let _ = client
145                        .activate_retentions(ActivateRetentionsRequest {})
146                        .await
147                        .map_err(|status| {
148                            SdkError::ApiError(SdkApiError {
149                                status,
150                                endpoint:
151                                    "/com.coralogix.archive.v1.RetentionsService/ActivateRetentions"
152                                        .to_string(),
153                                feature_group: ARCHIVE_FEATURE_GROUP_ID.to_string(),
154                            })
155                        })?;
156                    output.into_inner()
157                }
158                Err(error) => return Err(error),
159            }
160        };
161        Ok(response)
162    }
163
164    /// Activates all archive retentions    
165    #[deprecated(since = "1.8.0", note = "Update activates automatically")]
166    pub async fn activate(&self) -> Result<ActivateRetentionsResponse> {
167        Ok(self
168            .service_client
169            .lock()
170            .await
171            .activate_retentions(make_request_with_metadata(
172                ActivateRetentionsRequest {},
173                &self.metadata_map,
174            ))
175            .await
176            .map_err(|status| {
177                SdkError::ApiError(SdkApiError {
178                    status,
179                    endpoint: "/com.coralogix.archive.v1.RetentionsService/ActivateRetentions"
180                        .to_string(),
181                    feature_group: ARCHIVE_FEATURE_GROUP_ID.to_string(),
182                })
183            })?
184            .into_inner())
185    }
186}