use 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::{
AssignDashboardFolderRequest,
AssignDashboardFolderResponse,
CreateDashboardRequest,
CreateDashboardResponse,
DeleteDashboardRequest,
DeleteDashboardResponse,
GetDashboardCatalogRequest,
GetDashboardCatalogResponse,
GetDashboardRequest,
GetDashboardResponse,
PinDashboardRequest,
PinDashboardResponse,
ReplaceDashboardRequest,
ReplaceDashboardResponse,
UnpinDashboardRequest,
UnpinDashboardResponse,
dashboard_catalog_service_client::DashboardCatalogServiceClient,
dashboards_service_client::DashboardsServiceClient,
};
use tokio::sync::Mutex;
use tonic::{
Request,
metadata::MetadataMap,
transport::{
Channel,
ClientTlsConfig,
Endpoint,
},
};
const DASHBOARDS_FEATURE_GROUP_ID: &str = "dashboards";
use crate::CoralogixRegion;
pub struct DashboardsClient {
teams_level_metadata_map: MetadataMap,
user_level_metadata_map: MetadataMap,
service_client: Mutex<DashboardsServiceClient<Channel>>,
catalog_client: Mutex<DashboardCatalogServiceClient<Channel>>,
}
impl DashboardsClient {
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(DashboardsServiceClient::new(channel.clone())),
catalog_client: Mutex::new(DashboardCatalogServiceClient::new(channel)),
})
}
pub async fn create(&self, dashboard: Dashboard) -> Result<CreateDashboardResponse> {
let request: Request<CreateDashboardRequest> = make_request_with_metadata(
CreateDashboardRequest {
request_id: None,
dashboard: Some(dashboard),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.create_dashboard(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint:
"/com.coralogixapis.dashboards.v1.DashboardsService/CreateDashboard"
.into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn replace(&self, dashboard: Dashboard) -> Result<ReplaceDashboardResponse> {
let request: Request<ReplaceDashboardRequest> = make_request_with_metadata(
ReplaceDashboardRequest {
request_id: None,
dashboard: Some(dashboard),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.replace_dashboard(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint:
"/com.coralogixapis.dashboards.v1.DashboardsService/ReplaceDashboard"
.into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn get(&self, dashboard_id: String) -> Result<GetDashboardResponse> {
let request: Request<GetDashboardRequest> = make_request_with_metadata(
GetDashboardRequest {
dashboard_id: Some(dashboard_id),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.get_dashboard(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardsService/GetDashboard"
.into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn list(&self) -> Result<GetDashboardCatalogResponse> {
let mut client = self.catalog_client.lock().await.clone();
client
.get_dashboard_catalog(make_request_with_metadata(
GetDashboardCatalogRequest {},
&self.user_level_metadata_map,
))
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardCatalogService/GetDashboardCatalog".into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
},
))
}
pub async fn delete(&self, dashboard_id: String) -> Result<DeleteDashboardResponse> {
let request: Request<DeleteDashboardRequest> = make_request_with_metadata(
DeleteDashboardRequest {
request_id: None,
dashboard_id: Some(dashboard_id),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.delete_dashboard(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint:
"/com.coralogixapis.dashboards.v1.DashboardsService/DeleteDashboard"
.into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn pin(&self, dashboard_id: String) -> Result<PinDashboardResponse> {
let request: Request<PinDashboardRequest> = make_request_with_metadata(
PinDashboardRequest {
request_id: None,
dashboard_id: Some(dashboard_id),
},
&self.user_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.pin_dashboard(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardsService/PinDashboard"
.into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn unpin(&self, dashboard_id: String) -> Result<UnpinDashboardResponse> {
let request: Request<UnpinDashboardRequest> = make_request_with_metadata(
UnpinDashboardRequest {
request_id: None,
dashboard_id: Some(dashboard_id),
},
&self.user_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.unpin_dashboard(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint:
"/com.coralogixapis.dashboards.v1.DashboardsService/UnpinDashboard"
.into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn assign_to_folder(
&self,
dashboard_id: String,
folder_id: String,
) -> Result<AssignDashboardFolderResponse> {
let request: Request<AssignDashboardFolderRequest> = make_request_with_metadata(
AssignDashboardFolderRequest {
request_id: None,
dashboard_id: Some(dashboard_id),
folder_id: Some(folder_id),
},
&self.teams_level_metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.assign_dashboard_folder(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dashboards.v1.DashboardsService/AssignDashboardFolder".into(),
feature_group: DASHBOARDS_FEATURE_GROUP_ID.into(),
},
))
}
}
}