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