use std::str::FromStr;
use cx_api::proto::com::coralogixapis::notification_center::{
connectors::v1::{
BatchGetConnectorsRequest,
BatchGetConnectorsResponse,
CreateConnectorRequest,
CreateConnectorResponse,
DeleteConnectorRequest,
DeleteConnectorResponse,
GetConnectorRequest,
GetConnectorResponse,
GetConnectorTypeSummariesRequest,
GetConnectorTypeSummariesResponse,
ListConnectorsRequest,
ListConnectorsResponse,
ReplaceConnectorRequest,
ReplaceConnectorResponse,
connectors_service_client::ConnectorsServiceClient,
},
notifications::v1::{
TestConnectorConfigRequest,
TestConnectorConfigResponse,
TestExistingConnectorRequest,
TestExistingConnectorResponse,
TestPresetConfigRequest,
TestPresetConfigResponse,
TestTemplateRenderRequest,
TestTemplateRenderResponse,
testing_service_client::TestingServiceClient,
},
presets::v1::{
BatchGetPresetsRequest,
BatchGetPresetsResponse,
CreateCustomPresetRequest,
CreateCustomPresetResponse,
DeleteCustomPresetRequest,
DeleteCustomPresetResponse,
GetDefaultPresetSummaryRequest,
GetDefaultPresetSummaryResponse,
GetPresetRequest,
GetPresetResponse,
GetSystemDefaultPresetSummaryRequest,
GetSystemDefaultPresetSummaryResponse,
ListPresetSummariesRequest,
ListPresetSummariesResponse,
ReplaceCustomPresetRequest,
ReplaceCustomPresetResponse,
SetCustomPresetAsDefaultRequest,
SetCustomPresetAsDefaultResponse,
presets_service_client::PresetsServiceClient,
},
};
pub use cx_api::proto::com::coralogixapis::notification_center::{
ConditionType,
ConfigOverrides,
ConnectorConfigField,
ConnectorType,
MatchEntityTypeCondition,
MessageConfig,
MessageConfigField,
OrderBy,
condition_type,
connectors::v1::{
Connector,
ConnectorConfig,
},
presets::v1::Preset,
};
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,
};
const NOTIFICATIONS_FEATURE_GROUP_ID: &str = "notifications";
pub struct TestPresetConfigParams {
pub entity_type: String,
pub entity_sub_type: Option<String>,
pub connector_id: String,
pub output_schema_id: String,
pub message_config_fields: Vec<MessageConfigField>,
pub preset_id: String,
pub config_overrides: Vec<ConfigOverrides>,
}
pub struct NotificationsClient {
metadata_map: MetadataMap,
connectors_client: Mutex<ConnectorsServiceClient<Channel>>,
presets_client: Mutex<PresetsServiceClient<Channel>>,
testing_client: Mutex<TestingServiceClient<Channel>>,
}
impl NotificationsClient {
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 request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
Ok(Self {
metadata_map: request_metadata.to_metadata_map(),
connectors_client: Mutex::new(ConnectorsServiceClient::new(channel.clone())),
presets_client: Mutex::new(PresetsServiceClient::new(channel.clone())),
testing_client: Mutex::new(TestingServiceClient::new(channel)),
})
}
pub async fn get_connector(&self, connector_id: String) -> Result<GetConnectorResponse> {
let request = make_request_with_metadata(
GetConnectorRequest { id: connector_id },
&self.metadata_map,
);
{
let mut client = self.connectors_client.lock().await.clone();
client
.get_connector(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.connectors.v1.ConnectorsService/GetConnector".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn list_connectors(
&self,
connector_type: ConnectorType,
order_bys: Vec<OrderBy>,
entity_type: String,
) -> Result<ListConnectorsResponse> {
let request = make_request_with_metadata(
ListConnectorsRequest {
connector_type: connector_type as i32,
order_bys,
entity_type: Some(entity_type),
},
&self.metadata_map,
);
{
let mut client = self.connectors_client.lock().await.clone();
client
.list_connectors(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.connectors.v1.ConnectorsService/ListConnectors".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn create_connector(&self, connector: Connector) -> Result<CreateConnectorResponse> {
let request = make_request_with_metadata(
CreateConnectorRequest {
connector: Some(connector),
},
&self.metadata_map,
);
{
let mut client = self.connectors_client.lock().await.clone();
client
.create_connector(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.connectors.v1.ConnectorsService/CreateConnector".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn replace_connector(
&self,
connector: Connector,
) -> Result<ReplaceConnectorResponse> {
let request = make_request_with_metadata(
ReplaceConnectorRequest {
connector: Some(connector),
},
&self.metadata_map,
);
{
let mut client = self.connectors_client.lock().await.clone();
client
.replace_connector(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.connectors.v1.ConnectorsService/ReplaceConnector".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn delete_connector(&self, connector_id: String) -> Result<DeleteConnectorResponse> {
let request = make_request_with_metadata(
DeleteConnectorRequest { id: connector_id },
&self.metadata_map,
);
{
let mut client = self.connectors_client.lock().await.clone();
client
.delete_connector(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.connectors.v1.ConnectorsService/DeleteConnector".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn batch_get_connectors(
&self,
connector_ids: Vec<String>,
) -> Result<BatchGetConnectorsResponse> {
let request = make_request_with_metadata(
BatchGetConnectorsRequest { ids: connector_ids },
&self.metadata_map,
);
{
let mut client = self.connectors_client.lock().await.clone();
client
.batch_get_connectors(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.connectors.v1.ConnectorsService/BatchGetConnectors".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn get_connector_type_summaries(&self) -> Result<GetConnectorTypeSummariesResponse> {
let request =
make_request_with_metadata(GetConnectorTypeSummariesRequest {}, &self.metadata_map);
{
let mut client = self.connectors_client.lock().await.clone();
client
.get_connector_type_summaries(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.connectors.v1.ConnectorsService/GetConnectorTypeSummaries".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn create_custom_preset(&self, preset: Preset) -> Result<CreateCustomPresetResponse> {
let request = make_request_with_metadata(
CreateCustomPresetRequest {
preset: Some(preset),
},
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.create_custom_preset(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/CreateCustomPreset".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn replace_custom_preset(
&self,
preset: Preset,
) -> Result<ReplaceCustomPresetResponse> {
let request = make_request_with_metadata(
ReplaceCustomPresetRequest {
preset: Some(preset),
},
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.replace_custom_preset(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/ReplaceCustomPreset".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn delete_custom_preset(
&self,
preset_id: String,
) -> Result<DeleteCustomPresetResponse> {
let request = make_request_with_metadata(
DeleteCustomPresetRequest { id: preset_id },
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.delete_custom_preset(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/DeleteCustomPreset".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn set_custom_preset_as_default(
&self,
preset_id: String,
) -> Result<SetCustomPresetAsDefaultResponse> {
let request = make_request_with_metadata(
SetCustomPresetAsDefaultRequest { id: preset_id },
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.set_custom_preset_as_default(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/SetCustomPresetAsDefault".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn get_preset(&self, preset_id: String) -> Result<GetPresetResponse> {
let request =
make_request_with_metadata(GetPresetRequest { id: preset_id }, &self.metadata_map);
{
let mut client = self.presets_client.lock().await.clone();
client
.get_preset(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/GetPreset".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn list_preset_summaries(
&self,
connector_type: ConnectorType,
entity_type: String,
order_bys: Vec<OrderBy>,
) -> Result<ListPresetSummariesResponse> {
let request = make_request_with_metadata(
ListPresetSummariesRequest {
connector_type: connector_type as i32,
entity_type,
order_bys,
},
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.list_preset_summaries(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/ListPresetSummaries".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn batch_get_presets(
&self,
preset_ids: Vec<String>,
) -> Result<BatchGetPresetsResponse> {
let request = make_request_with_metadata(
BatchGetPresetsRequest { ids: preset_ids },
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.batch_get_presets(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/BatchGetPresets".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn get_default_preset_summary(
&self,
connector_type: ConnectorType,
entity_type: String,
) -> Result<GetDefaultPresetSummaryResponse> {
let request = make_request_with_metadata(
GetDefaultPresetSummaryRequest {
connector_type: connector_type as i32,
entity_type,
},
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.get_default_preset_summary(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/GetDefaultPresetSummary".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn get_system_default_preset_summary(
&self,
connector_type: ConnectorType,
entity_type: String,
) -> Result<GetSystemDefaultPresetSummaryResponse> {
let request = make_request_with_metadata(
GetSystemDefaultPresetSummaryRequest {
connector_type: connector_type as i32,
entity_type,
},
&self.metadata_map,
);
{
let mut client = self.presets_client.lock().await.clone();
client
.get_system_default_preset_summary(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.presets.v1.PresetsService/GetSystemDefaultPresetSummary".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn test_connector_config(
&self,
connector_type: ConnectorType,
output_schema_id: String,
connector_config_fields: Vec<ConnectorConfigField>,
entity_type: String,
) -> Result<TestConnectorConfigResponse> {
let request = make_request_with_metadata(
TestConnectorConfigRequest {
r#type: connector_type as i32,
output_schema_id,
fields: connector_config_fields,
entity_type,
},
&self.metadata_map,
);
{
let mut client = self.testing_client.lock().await.clone();
client
.test_connector_config(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.notifications.v1.TestingService/TestConnectorConfig".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn test_existing_connector(
&self,
connector_id: String,
output_schema_id: String,
) -> Result<TestExistingConnectorResponse> {
let request = make_request_with_metadata(
TestExistingConnectorRequest {
connector_id,
output_schema_id,
},
&self.metadata_map,
);
{
let mut client = self.testing_client.lock().await.clone();
client
.test_existing_connector(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.notifications.v1.TestingService/TestExistingConnector".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn test_preset_config(
&self,
params: TestPresetConfigParams,
) -> Result<TestPresetConfigResponse> {
let request = make_request_with_metadata(
TestPresetConfigRequest {
entity_type: params.entity_type,
entity_sub_type: params.entity_sub_type,
connector_id: params.connector_id,
preset_id: params.preset_id,
config_overrides: params.config_overrides,
},
&self.metadata_map,
);
{
let mut client = self.testing_client.lock().await.clone();
client
.test_preset_config(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.notifications.v1.TestingService/TestPresetConfig".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
pub async fn test_template_render(
&self,
entity_type: String,
entity_sub_type: Option<String>,
template: String,
) -> Result<TestTemplateRenderResponse> {
let request = make_request_with_metadata(
TestTemplateRenderRequest {
entity_type,
entity_sub_type,
template,
},
&self.metadata_map,
);
{
let mut client = self.testing_client.lock().await.clone();
client
.test_template_render(request)
.await
.map(|r| r.into_inner())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.notifications.v1.TestingService/TestTemplateRender".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into()
},
))
}
}
}