cx_sdk/client/
archive_logs.rspub use cx_api::proto::com::coralogix::archive::v2::set_target_request::TargetSpec;
use cx_api::proto::com::coralogix::archive::v2::target_service_client::TargetServiceClient;
use cx_api::proto::com::coralogix::archive::v2::{
GetTargetRequest,
GetTargetResponse,
SetTargetRequest,
SetTargetResponse,
ValidateTargetRequest,
};
use std::str::FromStr;
use tokio::sync::Mutex;
use tonic::transport::ClientTlsConfig;
use tonic::{
metadata::MetadataMap,
transport::{
Channel,
Endpoint,
},
};
pub use cx_api::proto::com::coralogix::archive::v2::S3TargetSpec;
pub use cx_api::proto::com::coralogix::archive::v2::validate_target_request::TargetSpec as TargetSpecValidation;
use crate::CoralogixRegion;
use crate::auth::AuthContext;
use crate::error::{
SdkApiError,
SdkError,
};
use crate::{
error::Result,
metadata::CallProperties,
util::make_request_with_metadata,
};
const ARCHIVE_LOGS_FEATURE_GROUP_ID: &str = "logs";
pub struct LogsArchiveClient {
metadata_map: MetadataMap,
service_client: Mutex<TargetServiceClient<Channel>>,
}
impl LogsArchiveClient {
pub fn new(region: CoralogixRegion, auth_context: AuthContext) -> Result<Self> {
let channel: Channel = Endpoint::from_str(®ion.grpc_endpoint())?
.tls_config(ClientTlsConfig::new().with_native_roots())?
.connect_lazy();
let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
Ok(Self {
metadata_map: request_metadata.to_metadata_map(),
service_client: Mutex::new(TargetServiceClient::new(channel)),
})
}
pub async fn get_target(&self) -> Result<GetTargetResponse> {
let request = make_request_with_metadata(GetTargetRequest {}, &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.get_target(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.archive.v2.TargetService/GetTarget".into(),
feature_group: ARCHIVE_LOGS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn set_target(
&self,
is_active: bool,
target_spec: TargetSpec,
) -> Result<SetTargetResponse> {
let request = make_request_with_metadata(
SetTargetRequest {
is_active,
target_spec: Some(target_spec),
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.set_target(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.archive.v2.TargetService/SetTarget".into(),
feature_group: ARCHIVE_LOGS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn validate_target(
&self,
is_active: bool,
target_spec: TargetSpecValidation,
) -> Result<()> {
let request = make_request_with_metadata(
ValidateTargetRequest {
target_spec: Some(target_spec),
is_active,
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.validate_target(request)
.await
.map(|_| ())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.archive.v2.TargetService/ValidateTarget".into(),
feature_group: ARCHIVE_LOGS_FEATURE_GROUP_ID.into(),
})
})
}
}
}