use std::str::FromStr;
use cx_api::proto::com::coralogixapis::alerting::alert_scheduler_rule_protobuf::v1::{
AlertSchedulerRuleIds,
AlertSchedulerRuleWithActiveTimeframe,
CreateAlertSchedulerRuleRequest,
CreateBulkAlertSchedulerRuleRequest,
DeleteAlertSchedulerRuleRequest,
FilterByAlertSchedulerRuleIds,
GetAlertSchedulerRuleRequest,
GetBulkAlertSchedulerRuleRequest,
UpdateAlertSchedulerRuleRequest,
UpdateBulkAlertSchedulerRuleRequest,
alert_scheduler_rule_service_client::AlertSchedulerRuleServiceClient,
filter_by_alert_scheduler_rule_ids,
};
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::coralogixapis::alerting::alert_scheduler_rule_protobuf::v1::{
ActiveTimeframe,
AlertSchedulerRule,
AlertUniqueIds,
Filter as AlertSchedulerFilter,
OneTime,
Schedule,
ScheduleOperation,
Timeframe,
filter::WhichAlerts,
schedule::Scheduler,
timeframe::Until,
};
const ALERT_SCHEDULER_FEATURE_GROUP_ID: &str = "alerts";
pub struct AlertSchedulerClient {
metadata_map: MetadataMap,
service_client: Mutex<AlertSchedulerRuleServiceClient<Channel>>,
}
impl AlertSchedulerClient {
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(AlertSchedulerRuleServiceClient::new(channel)),
})
}
pub async fn create(
&self,
alert_scheduler_rule: AlertSchedulerRule,
) -> Result<AlertSchedulerRule> {
let request = make_request_with_metadata(
CreateAlertSchedulerRuleRequest {
alert_scheduler_rule: Some(alert_scheduler_rule),
},
&self.metadata_map,
);
self.service_client
.lock()
.await
.create_alert_scheduler_rule(request)
.await
.map(|r| r.into_inner().alert_scheduler_rule.unwrap()) .map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerting.alert_scheduler_rule_protobuf.v1.AlertSchedulerRuleService/CreateAlertSchedulerRule".into(),
feature_group: ALERT_SCHEDULER_FEATURE_GROUP_ID.into()
}),
)
}
pub async fn create_bulk(
&self,
alert_scheduler_rules: Vec<AlertSchedulerRule>,
) -> Result<Vec<AlertSchedulerRule>> {
let request = make_request_with_metadata(
CreateBulkAlertSchedulerRuleRequest {
create_alert_scheduler_rule_requests: alert_scheduler_rules
.into_iter()
.map(|alert_scheduler_rule| CreateAlertSchedulerRuleRequest {
alert_scheduler_rule: Some(alert_scheduler_rule),
})
.collect(),
},
&self.metadata_map,
);
self.service_client
.lock()
.await
.create_bulk_alert_scheduler_rule(request)
.await
.map(|r| {
r.into_inner()
.create_suppression_responses
.into_iter()
.map(|response| response.alert_scheduler_rule.unwrap()) .collect()
})
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerting.alert_scheduler_rule_protobuf.v1.AlertSchedulerRuleService/CreateBulkAlertSchedulerRule".into(),
feature_group: ALERT_SCHEDULER_FEATURE_GROUP_ID.into()
}),
)
}
pub async fn update(
&self,
alert_scheduler_rule: AlertSchedulerRule,
) -> Result<AlertSchedulerRule> {
let request = make_request_with_metadata(
UpdateAlertSchedulerRuleRequest {
alert_scheduler_rule: Some(alert_scheduler_rule),
},
&self.metadata_map,
);
self.service_client
.lock()
.await
.update_alert_scheduler_rule(request)
.await
.map(|r| r.into_inner().alert_scheduler_rule.unwrap()) .map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerting.alert_scheduler_rule_protobuf.v1.AlertSchedulerRuleService/UpdateAlertSchedulerRule".into(),
feature_group: ALERT_SCHEDULER_FEATURE_GROUP_ID.into()
}),
)
}
pub async fn update_bulk(
&self,
alert_scheduler_rules: Vec<AlertSchedulerRule>,
) -> Result<Vec<AlertSchedulerRule>> {
let request = make_request_with_metadata(
UpdateBulkAlertSchedulerRuleRequest {
update_alert_scheduler_rule_requests: alert_scheduler_rules
.into_iter()
.map(|alert_scheduler_rule| UpdateAlertSchedulerRuleRequest {
alert_scheduler_rule: Some(alert_scheduler_rule),
})
.collect(),
},
&self.metadata_map,
);
self.service_client
.lock()
.await
.update_bulk_alert_scheduler_rule(request)
.await
.map(|r| {
r.into_inner()
.update_suppression_responses
.into_iter()
.map(|response| response.alert_scheduler_rule.unwrap()) .collect()
})
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerting.alert_scheduler_rule_protobuf.v1.AlertSchedulerRuleService/UpdateBulkAlertSchedulerRule".into(),
feature_group: ALERT_SCHEDULER_FEATURE_GROUP_ID.into()
}),
)
}
pub async fn get(&self, id: String) -> Result<AlertSchedulerRule> {
let request = make_request_with_metadata(
GetAlertSchedulerRuleRequest {
alert_scheduler_rule_id: id,
},
&self.metadata_map,
);
self.service_client
.lock()
.await
.get_alert_scheduler_rule(request)
.await
.map(|r| r.into_inner().alert_scheduler_rule.unwrap())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerting.alert_scheduler_rule_protobuf.v1.AlertSchedulerRuleService/GetAlertSchedulerRule".into(),
feature_group: ALERT_SCHEDULER_FEATURE_GROUP_ID.into()
}),
)
}
pub async fn get_bulk(
&self,
active_time_frame: ActiveTimeframe,
alert_scheduler_rule_ids: Vec<String>,
enabled: bool,
) -> Result<Vec<AlertSchedulerRuleWithActiveTimeframe>> {
let mut all_rules = Vec::new();
let mut next_page_token = Some("".to_string());
while let Some(token) = next_page_token {
let request = make_request_with_metadata(
GetBulkAlertSchedulerRuleRequest {
active_timeframe: Some(active_time_frame.clone()),
enabled: Some(enabled),
alert_scheduler_rules_ids: Some(FilterByAlertSchedulerRuleIds {
alert_scheduler_rule_ids: Some(filter_by_alert_scheduler_rule_ids::AlertSchedulerRuleIds::AlertSchedulerIds(
AlertSchedulerRuleIds {
alert_scheduler_rule_ids: alert_scheduler_rule_ids.clone(),
},
)),
}),
next_page_token: Some(token),
},
&self.metadata_map,
);
let (alert_scheduler_rules, new_token) = self
.service_client
.lock()
.await
.get_bulk_alert_scheduler_rule(request)
.await
.map(|r| {
let response = r.into_inner();
(response.alert_scheduler_rules, response.next_page_token)
})
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerting.alert_scheduler_rule_protobuf.v1.AlertSchedulerRuleService/GetBulkAlertSchedulerRule".into(),
feature_group: ALERT_SCHEDULER_FEATURE_GROUP_ID.into()
}),
)?;
all_rules.extend(alert_scheduler_rules);
if new_token.is_empty() {
next_page_token = None;
} else {
next_page_token = Some(new_token);
}
}
Ok(all_rules)
}
pub async fn delete(&self, id: String) -> Result<()> {
let request = make_request_with_metadata(
DeleteAlertSchedulerRuleRequest {
alert_scheduler_rule_id: id,
},
&self.metadata_map,
);
self.service_client
.lock()
.await
.delete_alert_scheduler_rule(request)
.await
.map(|_| ())
.map_err(
|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.alerting.alert_scheduler_rule_protobuf.v1.AlertSchedulerRuleService/DeleteAlertSchedulerRule".into(),
feature_group: ALERT_SCHEDULER_FEATURE_GROUP_ID.into()
}),
)
}
}