cx_sdk/client/
archive_logs.rs1use 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
51pub struct LogsArchiveClient {
54    metadata_map: MetadataMap,
55    service_client: Mutex<S3TargetServiceClient<Channel>>,
56}
57
58impl LogsArchiveClient {
59    pub fn new(region: CoralogixRegion, auth_context: AuthContext) -> Result<Self> {
65        let channel: Channel = Endpoint::from_str(®ion.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    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    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}