cx_sdk/client/
dashboard_folders.rsuse std::str::FromStr;
use crate::{
auth::AuthContext,
error::{
Result,
SdkApiError,
SdkError,
},
metadata::CallProperties,
util::make_request_with_metadata,
};
pub use crate::com::coralogixapis::actions::v2::Action;
pub use cx_api::proto::com::coralogixapis::dashboards::v1::ast::*;
use cx_api::proto::com::coralogixapis::dashboards::v1::services::{
CreateDashboardFolderRequest,
CreateDashboardFolderResponse,
DeleteDashboardFolderRequest,
DeleteDashboardFolderResponse,
GetDashboardFolderRequest,
GetDashboardFolderResponse,
ListDashboardFoldersRequest,
ListDashboardFoldersResponse,
ReplaceDashboardFolderRequest,
ReplaceDashboardFolderResponse,
dashboard_folders_service_client::DashboardFoldersServiceClient,
};
use tokio::sync::Mutex;
use tonic::{
Request,
metadata::MetadataMap,
transport::{
Channel,
ClientTlsConfig,
Endpoint,
},
};
pub use cx_api::proto::com::coralogixapis::dashboards::v1::common::DashboardFolder;
use crate::CoralogixRegion;
const DASHBOARD_FOLDERS_FEATURE_GROUP_ID: &str = "dashboards";
pub struct DashboardFoldersClient {
teams_level_metadata_map: MetadataMap,
user_level_metadata_map: MetadataMap,
service_client: Mutex<DashboardFoldersServiceClient<Channel>>,
}
impl DashboardFoldersClient {
pub fn new(auth_context: AuthContext, region: CoralogixRegion) -> Result<Self> {
let channel: Channel = Endpoint::from_str(region.grpc_endpoint().as_str())?
.tls_config(ClientTlsConfig::new().with_native_roots())?
.connect_lazy();
let teams_level_auth_data: CallProperties = (&auth_context.team_level_api_key).into();
let user_level_auth_data: CallProperties = (&auth_context.user_level_api_key).into();
Ok(Self {
teams_level_metadata_map: teams_level_auth_data.to_metadata_map(),
user_level_metadata_map: user_level_auth_data.to_metadata_map(),
service_client: Mutex::new(DashboardFoldersServiceClient::new(channel)),
})
}
pub async fn create(&self, folder: DashboardFolder) -> Result<CreateDashboardFolderResponse> {
let request: Request<CreateDashboardFolderRequest> = make_request_with_metadata(
CreateDashboardFolderRequest {
folder: Some(folder),
request_id: None,
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.create_dashboard_folder(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardFoldersService/CreateDashboardFolder".to_string(),
feature_group: DASHBOARD_FOLDERS_FEATURE_GROUP_ID.to_string(),
}),
)
}
}
pub async fn replace(&self, folder: DashboardFolder) -> Result<ReplaceDashboardFolderResponse> {
let request: Request<ReplaceDashboardFolderRequest> = make_request_with_metadata(
ReplaceDashboardFolderRequest {
request_id: None,
folder: Some(folder),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.replace_dashboard_folder(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardFoldersService/ReplaceDashboardFolder".to_string(),
feature_group: DASHBOARD_FOLDERS_FEATURE_GROUP_ID.to_string(),
}),
)
}
}
pub async fn get(&self, folder_id: String) -> Result<GetDashboardFolderResponse> {
let request: Request<GetDashboardFolderRequest> = make_request_with_metadata(
GetDashboardFolderRequest {
request_id: None,
folder_id: Some(folder_id),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.get_dashboard_folder(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardFoldersService/GetDashboardFolder".to_string(),
feature_group: DASHBOARD_FOLDERS_FEATURE_GROUP_ID.to_string(),
}),
)
}
}
pub async fn delete(&self, folder_id: String) -> Result<DeleteDashboardFolderResponse> {
let request: Request<DeleteDashboardFolderRequest> = make_request_with_metadata(
DeleteDashboardFolderRequest {
request_id: None,
folder_id: Some(folder_id),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.delete_dashboard_folder(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardFoldersService/DeleteDashboardFolder".to_string(),
feature_group: DASHBOARD_FOLDERS_FEATURE_GROUP_ID.to_string(),
}),
)
}
}
pub async fn list(&self) -> Result<ListDashboardFoldersResponse> {
let request = make_request_with_metadata(
ListDashboardFoldersRequest {},
&self.user_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.list_dashboard_folders(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardFoldersService/ListDashboardFolders".to_string(),
feature_group: DASHBOARD_FOLDERS_FEATURE_GROUP_ID.to_string(),
}),
)
}
}
}