use crate::error::{
SdkApiError,
SdkError,
};
use crate::{
CoralogixRegion,
auth::AuthContext,
error::Result,
metadata::CallProperties,
util::make_request_with_metadata,
};
use cx_api::proto::com::coralogixapis::alerts::v3::alert_defs_service_client::AlertDefsServiceClient;
use cx_api::proto::com::coralogixapis::alerts::v3::{
CreateAlertDefRequest,
CreateAlertDefResponse,
DeleteAlertDefRequest,
GetAlertDefRequest,
GetAlertDefResponse,
ListAlertDefsRequest,
ListAlertDefsResponse,
ReplaceAlertDefRequest,
ReplaceAlertDefResponse,
SetActiveRequest,
};
use std::str::FromStr;
use tokio::sync::Mutex;
use tonic::{
metadata::MetadataMap,
transport::{
Channel,
ClientTlsConfig,
Endpoint,
},
};
pub use cx_api::proto::com::coralogixapis::alerts::v3::{
ActivitySchedule,
ActivitySchedule as AlertDefActivitySchedule,
AlertDef,
AlertDefIncidentSettings,
AlertDefNotificationGroup,
AlertDefOverride,
AlertDefPriority,
AlertDefProperties,
AlertDefType,
AlertDefWebhooksSettings,
AlertsOp,
AutoRetireTimeframe,
DayOfWeek as AlertDayOfWeek,
DayOfWeek,
FlowStages,
FlowStagesGroup,
FlowStagesGroups,
FlowStagesGroupsAlertDefs,
FlowType,
IntegrationType,
LabelFilterType,
LabelFilters,
LogFilterOperationType,
LogSeverity,
LogsAnomalyCondition,
LogsAnomalyConditionType,
LogsAnomalyRule,
LogsAnomalyType,
LogsFilter,
LogsImmediateType,
LogsNewValueCondition,
LogsNewValueRule,
LogsNewValueTimeWindow,
LogsNewValueTimeWindowValue,
LogsNewValueType,
LogsRatioCondition,
LogsRatioRules,
LogsRatioThresholdType,
LogsRatioTimeWindow,
LogsSimpleFilter,
LogsThresholdCondition,
LogsThresholdConditionType,
LogsThresholdRule,
LogsThresholdType,
LogsTimeRelativeCondition,
LogsTimeRelativeConditionType,
LogsTimeRelativeRule,
LogsTimeRelativeThresholdType,
LogsTimeWindow,
LogsTimeWindowValue,
LogsUniqueCountCondition,
LogsUniqueCountRule,
LogsUniqueCountType,
MetricAnomalyCondition,
MetricAnomalyRule,
MetricAnomalyType,
MetricMissingValues,
MetricThresholdType,
MetricTimeWindow,
MetricTimeWindowValue,
NextOp,
NotifyOn,
Recipients,
TimeOfDay,
TimeframeType,
TracingFilter,
TracingFilterOperationType,
TracingImmediateType,
TracingSimpleFilter,
TracingThresholdCondition,
TracingThresholdRule,
TracingThresholdType,
TracingTimeWindow,
TracingTimeWindowValue,
UndetectedValuesManagement,
alert_def_properties::{
Schedule,
TypeDefinition,
},
alert_def_webhooks_settings::*,
integration_type,
logs_filter::FilterType,
logs_time_window::Type as LogsTimeWindowType,
metric_missing_values::MissingValues,
};
const ALERTS_FEATURE_GROUP_ID: &str = "alerts";
pub struct AlertsClient {
metadata_map: MetadataMap,
service_client: Mutex<AlertDefsServiceClient<Channel>>,
}
impl AlertsClient {
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(AlertDefsServiceClient::new(channel)),
})
}
pub async fn get(&self, alert_id: String) -> Result<GetAlertDefResponse> {
let request = make_request_with_metadata(
GetAlertDefRequest { id: Some(alert_id) },
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.get_alert_def(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerts.v3.AlertDefsService/GetAlertDef"
.into(),
feature_group: ALERTS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn list(&self) -> Result<ListAlertDefsResponse> {
let request = make_request_with_metadata(ListAlertDefsRequest {}, &self.metadata_map);
{
let mut client = self.service_client.lock().await.clone();
client
.list_alert_defs(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerts.v3.AlertDefsService/ListAlertDefs"
.into(),
feature_group: ALERTS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn create(&self, alert: AlertDef) -> Result<CreateAlertDefResponse> {
let request = make_request_with_metadata(
CreateAlertDefRequest {
alert_def_properties: alert.alert_def_properties,
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.create_alert_def(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerts.v3.AlertDefsService/CreateAlertDef"
.into(),
feature_group: ALERTS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn replace(&self, alert: AlertDef) -> Result<ReplaceAlertDefResponse> {
let request = make_request_with_metadata(
ReplaceAlertDefRequest {
alert_def_properties: alert.alert_def_properties,
id: alert.id,
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.replace_alert_def(request)
.await
.map(|r| r.into_inner())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerts.v3.AlertDefsService/ReplaceAlertDef"
.into(),
feature_group: ALERTS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn delete(&self, alert_id: String) -> Result<()> {
let request = make_request_with_metadata(
DeleteAlertDefRequest { id: Some(alert_id) },
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.delete_alert_def(request)
.await
.map(|_| ())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerts.v3.AlertDefsService/DeleteAlertDef"
.into(),
feature_group: ALERTS_FEATURE_GROUP_ID.into(),
})
})
}
}
pub async fn set(&self, id: String, active: bool) -> Result<()> {
let request = make_request_with_metadata(
SetActiveRequest {
id: Some(id),
active: Some(active),
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await.clone();
client
.set_active(request)
.await
.map(|_| ())
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerts.v3.AlertDefsService/SetActive".into(),
feature_group: ALERTS_FEATURE_GROUP_ID.into(),
})
})
}
}
}