use cx_api::proto::com::coralogix::metrics::metrics_configurator::{
ConfigureTenantRequest,
GetTenantConfigRequest,
GetTenantConfigResponse,
GetTenantConfigResponseV2,
InternalUpdateRequest,
ListHotStoreConfigsRequest,
ListHotStoreConfigsResponse,
ListTenantConfigsRequest,
ListTenantConfigsResponse,
MigrateTenantRequest,
UpdateRequest,
ValidateBucketRequest,
metrics_configurator_public_service_client::MetricsConfiguratorPublicServiceClient,
metrics_configurator_service_client::MetricsConfiguratorServiceClient,
};
use std::str::FromStr;
use tokio::sync::Mutex;
use tonic::{
metadata::MetadataMap,
transport::{
Channel,
ClientTlsConfig,
Endpoint,
},
};
use crate::{
CoralogixRegion,
auth::AuthContext,
error::{
Result,
SdkApiError,
SdkError,
},
metadata::CallProperties,
util::make_request_with_metadata,
};
pub use cx_api::proto::com::coralogix::metrics::metrics_configurator::{
RetentionPolicyRequest,
S3Config,
configure_tenant_request::StorageConfig,
internal_update_request::StorageConfig as InternalStorageConfigUpdate,
tenant_config::StorageConfig as InternalStorageConfig,
tenant_config_v2::StorageConfig as StorageConfigView,
update_request::StorageConfig as StorageConfigUpdate,
validate_bucket_request::StorageConfig as StorageConfigValidation,
};
const ARCHIVE_METRICS_FEATURE_GROUP_ID: &str = "metrics";
pub struct MetricsArchiveClient {
metadata_map: MetadataMap,
service_client: Mutex<MetricsConfiguratorPublicServiceClient<Channel>>,
}
impl MetricsArchiveClient {
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(MetricsConfiguratorPublicServiceClient::new(channel)),
})
}
pub async fn configure_tenant(
&self,
retention_policy: Option<RetentionPolicyRequest>,
storage_config: StorageConfig,
) -> Result<()> {
let request = make_request_with_metadata(
ConfigureTenantRequest {
retention_policy,
storage_config: Some(storage_config),
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.configure_tenant(request)
.await
.map(|_| ())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorPublicService/ConfigureTenant".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn update_tenant(
&self,
retention_days: u32,
storage_config: StorageConfigUpdate,
) -> Result<()> {
let request = make_request_with_metadata(
UpdateRequest {
retention_days: Some(retention_days),
storage_config: Some(storage_config),
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client.update(request).await.map(|_| ()).map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorPublicService/Update".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn validate_bucket(&self, storage_config: StorageConfigValidation) -> Result<()> {
let request = make_request_with_metadata(
ValidateBucketRequest {
storage_config: Some(storage_config),
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.validate_bucket(request)
.await
.map(|_| ())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorPublicService/ValidateBucket".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn get_tenant_config(&self) -> Result<GetTenantConfigResponseV2> {
let request = make_request_with_metadata((), &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.get_tenant_config(request)
.await
.map(|response| response.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorPublicService/GetTenantConfig".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn enable_archive(&self) -> Result<()> {
let request = make_request_with_metadata((), &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.enable_archive(request)
.await
.map(|_| ())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorPublicService/EnableArchive".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn disable_archive(&self) -> Result<()> {
let request = make_request_with_metadata((), &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.disable_archive(request)
.await
.map(|_| ())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorPublicService/DisableArchive".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
}
pub struct MetricsArchiveInternalClient {
metadata_map: MetadataMap,
service_client: Mutex<MetricsConfiguratorServiceClient<Channel>>,
}
impl MetricsArchiveInternalClient {
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(MetricsConfiguratorServiceClient::new(channel)),
})
}
pub async fn get_tenant_config(&self, tenant_id: u32) -> Result<GetTenantConfigResponse> {
let request =
make_request_with_metadata(GetTenantConfigRequest { tenant_id }, &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.get_tenant_config(request)
.await
.map(|response| response.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorService/GetTenantConfig".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn list_tenant_configs(&self) -> Result<ListTenantConfigsResponse> {
let request = make_request_with_metadata(ListTenantConfigsRequest {}, &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.list_tenant_configs(request)
.await
.map(|response| response.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorService/ListTenantConfigs".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn list_hot_store_configs(&self) -> Result<ListHotStoreConfigsResponse> {
let request = make_request_with_metadata(ListHotStoreConfigsRequest {}, &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.list_host_store_configs(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorService/ListHotStoreConfigs".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn migrate_tenant(&self, tenant_id: u32) -> Result<()> {
let request =
make_request_with_metadata(MigrateTenantRequest { tenant_id }, &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.migrate_tenant(request)
.await
.map(|_| ())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorService/MigrateTenant".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
pub async fn update(
&self,
tenant_id: u32,
retention_days: u32,
storage_config: InternalStorageConfigUpdate,
) -> Result<()> {
let request = make_request_with_metadata(
InternalUpdateRequest {
retention_days: Some(retention_days),
storage_config: Some(storage_config),
tenant_id,
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client.update(request).await.map(|_| ()).map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.metrics.metrics_configurator.MetricsConfiguratorService/Update".into(),
feature_group: ARCHIVE_METRICS_FEATURE_GROUP_ID.into(),
},
))
}
}
}