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        Ok(self
126            .service_client
127            .lock()
128            .await
129            .update_retentions(make_request_with_metadata(
130                UpdateRetentionsRequest {
131                    retention_update_elements,
132                },
133                &self.metadata_map,
134            ))
135            .await
136            .map_err(|status| {
137                SdkError::ApiError(SdkApiError {
138                    status,
139                    endpoint: "/com.coralogix.archive.v1.RetentionsService/UpdateRetentions"
140                        .to_string(),
141                    feature_group: ARCHIVE_FEATURE_GROUP_ID.to_string(),
142                })
143            })?
144            .into_inner())
145    }
146
147    /// Activates all archive retentions
148    pub async fn activate(&self) -> Result<ActivateRetentionsResponse> {
149        Ok(self
150            .service_client
151            .lock()
152            .await
153            .activate_retentions(make_request_with_metadata(
154                ActivateRetentionsRequest {},
155                &self.metadata_map,
156            ))
157            .await
158            .map_err(|status| {
159                SdkError::ApiError(SdkApiError {
160                    status,
161                    endpoint: "/com.coralogix.archive.v1.RetentionsService/ActivateRetentions"
162                        .to_string(),
163                    feature_group: ARCHIVE_FEATURE_GROUP_ID.to_string(),
164                })
165            })?
166            .into_inner())
167    }
168}