cx_sdk/client/
archive_logs.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::v2::s3_target_service_client::S3TargetServiceClient;
16pub use cx_api::proto::com::coralogix::archive::v2::s3_target_service_set_target_request::TargetSpec;
17use cx_api::proto::com::coralogix::archive::v2::{
18    S3TargetServiceGetTargetRequest,
19    S3TargetServiceGetTargetResponse,
20    S3TargetServiceSetTargetRequest,
21    S3TargetServiceSetTargetResponse,
22};
23use std::str::FromStr;
24use tokio::sync::Mutex;
25use tonic::transport::ClientTlsConfig;
26use tonic::{
27    metadata::MetadataMap,
28    transport::{
29        Channel,
30        Endpoint,
31    },
32};
33
34pub use cx_api::proto::com::coralogix::archive::v2::S3TargetSpec;
35pub use cx_api::proto::com::coralogix::archive::v2::validate_target_request::TargetSpec as TargetSpecValidation;
36
37use crate::CoralogixRegion;
38use crate::auth::AuthContext;
39use crate::error::{
40    SdkApiError,
41    SdkError,
42};
43use crate::{
44    error::Result,
45    metadata::CallProperties,
46    util::make_request_with_metadata,
47};
48
49const ARCHIVE_LOGS_FEATURE_GROUP_ID: &str = "logs";
50
51/// The logs archive API client.
52/// Read more at [https://coralogix.com/docs/archive-s3-bucket-forever/]()
53pub struct LogsArchiveClient {
54    metadata_map: MetadataMap,
55    service_client: Mutex<S3TargetServiceClient<Channel>>,
56}
57
58impl LogsArchiveClient {
59    /// Creates a new client for the Logs Archive API.
60    ///
61    /// # Arguments
62    /// * `auth_context` - The  to use for authentication.
63    /// * `region` - The region to connect to.
64    pub fn new(region: CoralogixRegion, auth_context: AuthContext) -> Result<Self> {
65        let channel: Channel = Endpoint::from_str(&region.grpc_endpoint())?
66            .tls_config(ClientTlsConfig::new().with_native_roots())?
67            .connect_lazy();
68        let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
69        Ok(Self {
70            metadata_map: request_metadata.to_metadata_map(),
71            service_client: Mutex::new(S3TargetServiceClient::new(channel)),
72        })
73    }
74
75    /// Retrieves the current target configuration.
76    pub async fn get_target(&self) -> Result<S3TargetServiceGetTargetResponse> {
77        let request =
78            make_request_with_metadata(S3TargetServiceGetTargetRequest {}, &self.metadata_map);
79        {
80            let mut client = self.service_client.lock().await.clone();
81
82            client
83                .get_target(request)
84                .await
85                .map(|r| r.into_inner())
86                .map_err(|status| {
87                    SdkError::ApiError(SdkApiError {
88                        status,
89                        endpoint: "/com.coralogix.archive.v2.TargetService/GetTarget".into(),
90                        feature_group: ARCHIVE_LOGS_FEATURE_GROUP_ID.into(),
91                    })
92                })
93        }
94    }
95
96    /// Sets the target configuration.
97    ///
98    /// # Arguments
99    /// * `is_active` - Whether the target should be active.
100    /// * `target_spec` - The target configuration.
101    pub async fn set_target(
102        &self,
103        is_active: bool,
104        target_spec: TargetSpec,
105    ) -> Result<S3TargetServiceSetTargetResponse> {
106        let request = make_request_with_metadata(
107            S3TargetServiceSetTargetRequest {
108                is_active,
109                target_spec: Some(target_spec),
110            },
111            &self.metadata_map,
112        );
113        {
114            let mut client = self.service_client.lock().await.clone();
115
116            client
117                .set_target(request)
118                .await
119                .map(|r| r.into_inner())
120                .map_err(|status| {
121                    SdkError::ApiError(SdkApiError {
122                        status,
123                        endpoint: "/com.coralogix.archive.v2.TargetService/SetTarget".into(),
124                        feature_group: ARCHIVE_LOGS_FEATURE_GROUP_ID.into(),
125                    })
126                })
127        }
128    }
129}