cx_sdk/client/
recording_rule_group_sets.rsuse std::str::FromStr;
pub use cx_api::proto::rule_manager::groups::{
CreateRuleGroupSet,
CreateRuleGroupSetResult,
DeleteRuleGroupSet,
FetchRuleGroupResult,
FetchRuleGroupSet,
InRule,
InRuleGroup,
OutRuleGroupSet,
RuleGroupSetListing,
UpdateRuleGroupSet,
};
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 RECORDING_RULES_FEATURE_GROUP_ID: &str = "recording-rules";
use cx_api::proto::rule_manager::groups::rule_group_sets_client::RuleGroupSetsClient;
pub struct RecordingRuleGroupSetsClient {
service_client: Mutex<RuleGroupSetsClient<Channel>>,
metadata_map: MetadataMap,
}
impl RecordingRuleGroupSetsClient {
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(),
service_client: Mutex::new(RuleGroupSetsClient::new(channel)),
})
}
#[allow(clippy::too_many_arguments)]
pub async fn create(
&self,
name: String,
groups: Vec<InRuleGroup>,
) -> Result<CreateRuleGroupSetResult> {
let request = make_request_with_metadata(
CreateRuleGroupSet {
name: Some(name),
groups,
},
&self.metadata_map,
);
Ok(self
.service_client
.lock()
.await
.create(request)
.await
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.rule_manager.groups.RuleGroupSets/Create".to_string(),
feature_group: RECORDING_RULES_FEATURE_GROUP_ID.to_string(),
})
})?
.into_inner())
}
pub async fn get(&self, id: String) -> Result<OutRuleGroupSet> {
let request = make_request_with_metadata(FetchRuleGroupSet { id }, &self.metadata_map);
Ok(self
.service_client
.lock()
.await
.fetch(request)
.await
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.rule_manager.groups.RuleGroupSets/Fetch".to_string(),
feature_group: RECORDING_RULES_FEATURE_GROUP_ID.to_string(),
})
})?
.into_inner())
}
pub async fn list(&self) -> Result<RuleGroupSetListing> {
let request = make_request_with_metadata((), &self.metadata_map);
Ok(self
.service_client
.lock()
.await
.list(request)
.await
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.rule_manager.groups.RuleGroupSets/List".to_string(),
feature_group: RECORDING_RULES_FEATURE_GROUP_ID.to_string(),
})
})?
.into_inner())
}
pub async fn update(&self, id: String, groups: Vec<InRuleGroup>) -> Result<()> {
let request =
make_request_with_metadata(UpdateRuleGroupSet { id, groups }, &self.metadata_map);
self.service_client
.lock()
.await
.update(request)
.await
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.rule_manager.groups.RuleGroupSets/Update".to_string(),
feature_group: RECORDING_RULES_FEATURE_GROUP_ID.to_string(),
})
})?;
Ok(())
}
pub async fn delete(&self, id: String) -> Result<()> {
let request = make_request_with_metadata(DeleteRuleGroupSet { id }, &self.metadata_map);
self.service_client
.lock()
.await
.delete(request)
.await
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogix.rule_manager.groups.RuleGroupSets/Delete".to_string(),
feature_group: RECORDING_RULES_FEATURE_GROUP_ID.to_string(),
})
})?;
Ok(())
}
}