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
15pub use cx_api::proto::com::coralogix::archive::v2::set_target_request::TargetSpec;
16use cx_api::proto::com::coralogix::archive::v2::target_service_client::TargetServiceClient;
17use cx_api::proto::com::coralogix::archive::v2::{
18    GetTargetRequest,
19    GetTargetResponse,
20    SetTargetRequest,
21    SetTargetResponse,
22    ValidateTargetRequest,
23};
24use std::str::FromStr;
25use tokio::sync::Mutex;
26use tonic::transport::ClientTlsConfig;
27use tonic::{
28    metadata::MetadataMap,
29    transport::{
30        Channel,
31        Endpoint,
32    },
33};
34
35pub use cx_api::proto::com::coralogix::archive::v2::S3TargetSpec;
36pub use cx_api::proto::com::coralogix::archive::v2::validate_target_request::TargetSpec as TargetSpecValidation;
37
38use crate::CoralogixRegion;
39use crate::auth::AuthContext;
40use crate::error::{
41    SdkApiError,
42    SdkError,
43};
44use crate::{
45    error::Result,
46    metadata::CallProperties,
47    util::make_request_with_metadata,
48};
49
50const ARCHIVE_LOGS_FEATURE_GROUP_ID: &str = "logs";
51
52/// The logs archive API client.
53/// Read more at [https://coralogix.com/docs/archive-s3-bucket-forever/]()
54pub struct LogsArchiveClient {
55    metadata_map: MetadataMap,
56    service_client: Mutex<TargetServiceClient<Channel>>,
57}
58
59impl LogsArchiveClient {
60    /// Creates a new client for the Logs Archive API.
61    ///
62    /// # Arguments
63    /// * `auth_context` - The  to use for authentication.
64    /// * `region` - The region to connect to.
65    pub fn new(region: CoralogixRegion, auth_context: AuthContext) -> Result<Self> {
66        let channel: Channel = Endpoint::from_str(&region.grpc_endpoint())?
67            .tls_config(ClientTlsConfig::new().with_native_roots())?
68            .connect_lazy();
69        let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
70        Ok(Self {
71            metadata_map: request_metadata.to_metadata_map(),
72            service_client: Mutex::new(TargetServiceClient::new(channel)),
73        })
74    }
75
76    /// Retrieves the current target configuration.
77    pub async fn get_target(&self) -> Result<GetTargetResponse> {
78        let request = make_request_with_metadata(GetTargetRequest {}, &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<SetTargetResponse> {
106        let request = make_request_with_metadata(
107            SetTargetRequest {
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
130    /// Validates a target configuration.
131    ///
132    /// # Arguments
133    /// * `is_active` - Whether the target should be active.
134    /// * `target_spec` - The target configuration to validate.
135    pub async fn validate_target(
136        &self,
137        is_active: bool,
138        target_spec: TargetSpecValidation,
139    ) -> Result<()> {
140        let request = make_request_with_metadata(
141            ValidateTargetRequest {
142                target_spec: Some(target_spec),
143                is_active,
144            },
145            &self.metadata_map,
146        );
147        {
148            let mut client = self.service_client.lock().await.clone();
149
150            client
151                .validate_target(request)
152                .await
153                .map(|_| ())
154                .map_err(|status| {
155                    SdkError::ApiError(SdkApiError {
156                        status,
157                        endpoint: "/com.coralogix.archive.v2.TargetService/ValidateTarget".into(),
158                        feature_group: ARCHIVE_LOGS_FEATURE_GROUP_ID.into(),
159                    })
160                })
161        }
162    }
163}