use serde_json::Value;
use url::form_urlencoded;
fn trim_base(base: &str) -> &str {
base.trim_end_matches('/')
}
pub fn id_from_json(val: &Value) -> Option<String> {
let id = val.get("id")?;
if let Some(s) = id.as_str() {
return Some(s.to_string());
}
if let Some(n) = id.as_i64() {
return Some(n.to_string());
}
if let Some(n) = id.as_u64() {
return Some(n.to_string());
}
None
}
pub fn dashboard_url(base: &str, id: &str) -> String {
format!("{}/dashboards/{id}", trim_base(base))
}
pub fn dashboards_url(base: &str) -> String {
format!("{}/dashboards", trim_base(base))
}
pub fn alert_url(base: &str, id: &str) -> String {
format!("{}/alerts/{id}", trim_base(base))
}
pub fn alerts_url(base: &str) -> String {
format!("{}/alerts", trim_base(base))
}
pub fn suppression_rule_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!(
"{}/suppression-rules?edit={encoded}&meta=edit",
trim_base(base)
)
}
pub fn suppression_rules_url(base: &str) -> String {
format!("{}/suppression-rules", trim_base(base))
}
pub fn case_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!("{}/cases?id={encoded}", trim_base(base))
}
pub fn view_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!("{}/explore?viewId={encoded}", trim_base(base))
}
pub fn views_url(base: &str) -> String {
format!("{}/explore", trim_base(base))
}
pub fn e2m_url(base: &str, id: &str) -> String {
format!("{}/tco/metrics/{id}", trim_base(base))
}
pub fn e2m_definitions_url(base: &str) -> String {
format!("{}/tco/metrics", trim_base(base))
}
pub fn slo_url(base: &str, id: &str) -> String {
format!("{}/slo/{id}/overview", trim_base(base))
}
pub fn slos_url(base: &str) -> String {
format!("{}/slo", trim_base(base))
}
pub fn notification_connectors_url(base: &str) -> String {
format!("{}/notification-center/connectors", trim_base(base))
}
pub fn notification_connector_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!(
"{}/notification-center/connectors?id={encoded}",
trim_base(base)
)
}
pub fn notification_router_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!(
"{}/notification-center/routers?id={encoded}",
trim_base(base)
)
}
pub fn notification_routers_url(base: &str) -> String {
format!("{}/notification-center/routers", trim_base(base))
}
pub fn iam_role_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!(
"{}/settings/roles?selectedRoleId={encoded}",
trim_base(base)
)
}
pub fn iam_scope_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!(
"{}/settings/scopes?selectedScopeId={encoded}",
trim_base(base)
)
}
pub fn iam_scopes_url(base: &str) -> String {
format!("{}/settings/scopes", trim_base(base))
}
pub fn iam_group_url(base: &str, id: &str) -> String {
let encoded: String = form_urlencoded::byte_serialize(id.as_bytes()).collect();
format!(
"{}/settings/account/groups?selectedGroupId={encoded}",
trim_base(base)
)
}
pub fn iam_groups_url(base: &str) -> String {
format!("{}/settings/account/groups", trim_base(base))
}
pub fn usage_url(base: &str) -> String {
format!("{}/settings/datausage", trim_base(base))
}
pub fn tco_url(base: &str) -> String {
format!("{}/tco-policies", trim_base(base))
}
pub fn enrichments_url(base: &str) -> String {
format!("{}/enrichments", trim_base(base))
}
pub fn integrations_url(base: &str) -> String {
format!("{}/extensions/integrations", trim_base(base))
}
pub fn webhooks_url(base: &str) -> String {
format!("{}/extensions/all-outbound-webhooks", trim_base(base))
}
pub fn iam_api_keys_url(base: &str) -> String {
format!("{}/settings/api-keys", trim_base(base))
}
pub fn iam_users_url(base: &str) -> String {
format!("{}/settings/team/members", trim_base(base))
}
pub fn iam_ip_access_url(base: &str) -> String {
format!("{}/settings/login-access-policies", trim_base(base))
}
pub fn ai_center_applications_url(base: &str) -> String {
format!("{}/ai-center/ai-apps/application-catalog", trim_base(base))
}
pub fn ai_center_application_url(base: &str, application: &str, subsystem: &str) -> String {
format!(
"{}/ai-center/ai-apps/application-catalog/application-drilldown?{}",
trim_base(base),
ai_center_application_query(application, subsystem)
)
}
pub fn ai_center_evaluations_url(base: &str) -> String {
format!("{}/ai-center/ai-apps/policy-catalog", trim_base(base))
}
pub fn ai_center_application_evaluations_url(
base: &str,
application: &str,
subsystem: &str,
) -> String {
format!(
"{}/ai-center/ai-apps/application-catalog/policy-configuration?{}",
trim_base(base),
ai_center_application_query(application, subsystem)
)
}
fn ai_center_application_query(application: &str, subsystem: &str) -> String {
let app: String = form_urlencoded::byte_serialize(application.as_bytes()).collect();
let sub: String = form_urlencoded::byte_serialize(subsystem.as_bytes()).collect();
format!("application={app}&subsystem={sub}")
}
pub fn olly_url(base: &str) -> String {
format!("{}/olly", trim_base(base))
}
pub fn olly_chat_url(base: &str, chat_id: &str) -> String {
format!("{}/olly/chat/{}", trim_base(base), chat_id)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dashboard_url_joins_base_and_id() {
assert_eq!(
dashboard_url("https://c4c.app.eu2.coralogix.com", "dash-abc123"),
"https://c4c.app.eu2.coralogix.com/dashboards/dash-abc123"
);
}
#[test]
fn dashboard_url_trims_trailing_slash_on_base() {
assert_eq!(
dashboard_url("https://c4c.app.eu2.coralogix.com/", "dash-abc123"),
"https://c4c.app.eu2.coralogix.com/dashboards/dash-abc123"
);
}
#[test]
fn dashboards_url_is_static() {
assert_eq!(
dashboards_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/dashboards"
);
}
#[test]
fn dashboards_url_trims_trailing_slash_on_base() {
assert_eq!(
dashboards_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/dashboards"
);
}
#[test]
fn alert_url_joins_base_and_id() {
assert_eq!(
alert_url("https://c4c.app.eu2.coralogix.com", "alert-xyz789"),
"https://c4c.app.eu2.coralogix.com/alerts/alert-xyz789"
);
}
#[test]
fn alert_url_trims_trailing_slash_on_base() {
assert_eq!(
alert_url("https://c4c.app.eu2.coralogix.com/", "alert-xyz789"),
"https://c4c.app.eu2.coralogix.com/alerts/alert-xyz789"
);
}
#[test]
fn case_url_uses_query_param_shape() {
assert_eq!(
case_url("https://c4c.app.eu2.coralogix.com", "case-777"),
"https://c4c.app.eu2.coralogix.com/cases?id=case-777"
);
}
#[test]
fn case_url_percent_encodes_id() {
assert_eq!(
case_url("https://c4c.app.eu2.coralogix.com", "case #1 & 2"),
"https://c4c.app.eu2.coralogix.com/cases?id=case+%231+%26+2"
);
}
#[test]
fn case_url_trims_trailing_slash_on_base() {
assert_eq!(
case_url("https://c4c.app.eu2.coralogix.com/", "case-777"),
"https://c4c.app.eu2.coralogix.com/cases?id=case-777"
);
}
#[test]
fn view_url_joins_base_and_id() {
assert_eq!(
view_url("https://c4c.app.eu2.coralogix.com", "view-123"),
"https://c4c.app.eu2.coralogix.com/explore?viewId=view-123"
);
}
#[test]
fn view_url_percent_encodes_id() {
assert_eq!(
view_url("https://c4c.app.eu2.coralogix.com", "view #1"),
"https://c4c.app.eu2.coralogix.com/explore?viewId=view+%231"
);
}
#[test]
fn view_url_trims_trailing_slash_on_base() {
assert_eq!(
view_url("https://c4c.app.eu2.coralogix.com/", "view-123"),
"https://c4c.app.eu2.coralogix.com/explore?viewId=view-123"
);
}
#[test]
fn views_url_is_static() {
assert_eq!(
views_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/explore"
);
}
#[test]
fn views_url_trims_trailing_slash_on_base() {
assert_eq!(
views_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/explore"
);
}
#[test]
fn no_double_slash_when_base_has_trailing_slash() {
let url = dashboard_url("https://c4c.app.eu2.coralogix.com/", "abc");
assert!(!url.contains("//dashboards"));
}
#[test]
fn e2m_url_joins_base_and_id() {
assert_eq!(
e2m_url("https://c4c.app.eu2.coralogix.com", "e2m-123"),
"https://c4c.app.eu2.coralogix.com/tco/metrics/e2m-123"
);
}
#[test]
fn e2m_url_trims_trailing_slash_on_base() {
assert_eq!(
e2m_url("https://c4c.app.eu2.coralogix.com/", "e2m-123"),
"https://c4c.app.eu2.coralogix.com/tco/metrics/e2m-123"
);
}
#[test]
fn e2m_definitions_url_is_static() {
assert_eq!(
e2m_definitions_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/tco/metrics"
);
}
#[test]
fn e2m_definitions_url_trims_trailing_slash_on_base() {
assert_eq!(
e2m_definitions_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/tco/metrics"
);
}
#[test]
fn slo_url_joins_base_and_id() {
assert_eq!(
slo_url("https://c4c.app.eu2.coralogix.com", "slo-abc"),
"https://c4c.app.eu2.coralogix.com/slo/slo-abc/overview"
);
}
#[test]
fn slo_url_trims_trailing_slash_on_base() {
assert_eq!(
slo_url("https://c4c.app.eu2.coralogix.com/", "slo-abc"),
"https://c4c.app.eu2.coralogix.com/slo/slo-abc/overview"
);
}
#[test]
fn slos_url_is_static() {
assert_eq!(
slos_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/slo"
);
}
#[test]
fn slos_url_trims_trailing_slash_on_base() {
assert_eq!(
slos_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/slo"
);
}
#[test]
fn notification_connectors_url_is_static() {
assert_eq!(
notification_connectors_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/notification-center/connectors"
);
}
#[test]
fn notification_connectors_url_trims_trailing_slash_on_base() {
assert_eq!(
notification_connectors_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/notification-center/connectors"
);
}
#[test]
fn notification_connector_url_uses_query_param_shape() {
assert_eq!(
notification_connector_url("https://c4c.app.eu2.coralogix.com", "conn-1"),
"https://c4c.app.eu2.coralogix.com/notification-center/connectors?id=conn-1"
);
}
#[test]
fn notification_connector_url_trims_trailing_slash_on_base() {
assert_eq!(
notification_connector_url("https://c4c.app.eu2.coralogix.com/", "conn-1"),
"https://c4c.app.eu2.coralogix.com/notification-center/connectors?id=conn-1"
);
}
#[test]
fn notification_router_url_uses_query_param_shape() {
assert_eq!(
notification_router_url("https://c4c.app.eu2.coralogix.com", "router-1"),
"https://c4c.app.eu2.coralogix.com/notification-center/routers?id=router-1"
);
}
#[test]
fn notification_router_url_trims_trailing_slash_on_base() {
assert_eq!(
notification_router_url("https://c4c.app.eu2.coralogix.com/", "router-1"),
"https://c4c.app.eu2.coralogix.com/notification-center/routers?id=router-1"
);
}
#[test]
fn notification_routers_url_is_static() {
assert_eq!(
notification_routers_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/notification-center/routers"
);
}
#[test]
fn notification_routers_url_trims_trailing_slash_on_base() {
assert_eq!(
notification_routers_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/notification-center/routers"
);
}
#[test]
fn iam_role_url_uses_query_param_shape() {
assert_eq!(
iam_role_url("https://c4c.app.eu2.coralogix.com", "42"),
"https://c4c.app.eu2.coralogix.com/settings/roles?selectedRoleId=42"
);
}
#[test]
fn iam_role_url_trims_trailing_slash_on_base() {
assert_eq!(
iam_role_url("https://c4c.app.eu2.coralogix.com/", "42"),
"https://c4c.app.eu2.coralogix.com/settings/roles?selectedRoleId=42"
);
}
#[test]
fn iam_scope_url_uses_query_param_shape() {
assert_eq!(
iam_scope_url("https://c4c.app.eu2.coralogix.com", "scope-1"),
"https://c4c.app.eu2.coralogix.com/settings/scopes?selectedScopeId=scope-1"
);
}
#[test]
fn iam_scope_url_trims_trailing_slash_on_base() {
assert_eq!(
iam_scope_url("https://c4c.app.eu2.coralogix.com/", "scope-1"),
"https://c4c.app.eu2.coralogix.com/settings/scopes?selectedScopeId=scope-1"
);
}
#[test]
fn iam_scopes_url_is_static() {
assert_eq!(
iam_scopes_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/settings/scopes"
);
}
#[test]
fn iam_scopes_url_trims_trailing_slash_on_base() {
assert_eq!(
iam_scopes_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/settings/scopes"
);
}
#[test]
fn iam_group_url_uses_query_param_shape() {
assert_eq!(
iam_group_url("https://c4c.app.eu2.coralogix.com", "7"),
"https://c4c.app.eu2.coralogix.com/settings/account/groups?selectedGroupId=7"
);
}
#[test]
fn iam_group_url_trims_trailing_slash_on_base() {
assert_eq!(
iam_group_url("https://c4c.app.eu2.coralogix.com/", "7"),
"https://c4c.app.eu2.coralogix.com/settings/account/groups?selectedGroupId=7"
);
}
#[test]
fn iam_groups_url_is_static() {
assert_eq!(
iam_groups_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/settings/account/groups"
);
}
#[test]
fn iam_groups_url_trims_trailing_slash_on_base() {
assert_eq!(
iam_groups_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/settings/account/groups"
);
}
#[test]
fn id_from_json_reads_string_id() {
let val = serde_json::json!({"id": "abc-123"});
assert_eq!(id_from_json(&val), Some("abc-123".to_string()));
}
#[test]
fn id_from_json_reads_numeric_id() {
let val = serde_json::json!({"id": 42});
assert_eq!(id_from_json(&val), Some("42".to_string()));
}
#[test]
fn id_from_json_returns_none_when_missing() {
let val = serde_json::json!({"name": "no id here"});
assert_eq!(id_from_json(&val), None);
}
#[test]
fn usage_url_is_static() {
assert_eq!(
usage_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/settings/datausage"
);
}
#[test]
fn usage_url_trims_trailing_slash_on_base() {
assert_eq!(
usage_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/settings/datausage"
);
}
#[test]
fn tco_url_is_static() {
assert_eq!(
tco_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/tco-policies"
);
}
#[test]
fn enrichments_url_is_static() {
assert_eq!(
enrichments_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/enrichments"
);
}
#[test]
fn integrations_url_is_static() {
assert_eq!(
integrations_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/extensions/integrations"
);
}
#[test]
fn webhooks_url_is_static() {
assert_eq!(
webhooks_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/extensions/all-outbound-webhooks"
);
}
#[test]
fn iam_api_keys_url_is_static() {
assert_eq!(
iam_api_keys_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/settings/api-keys"
);
}
#[test]
fn iam_users_url_is_static() {
assert_eq!(
iam_users_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/settings/team/members"
);
}
#[test]
fn iam_ip_access_url_is_static() {
assert_eq!(
iam_ip_access_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/settings/login-access-policies"
);
}
#[test]
fn ai_center_applications_url_is_static() {
assert_eq!(
ai_center_applications_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/ai-center/ai-apps/application-catalog"
);
}
#[test]
fn ai_center_application_url_uses_query_params() {
assert_eq!(
ai_center_application_url("https://c4c.app.eu2.coralogix.com", "checkout", "payments"),
"https://c4c.app.eu2.coralogix.com/ai-center/ai-apps/application-catalog/application-drilldown?application=checkout&subsystem=payments"
);
}
#[test]
fn ai_center_application_url_percent_encodes_params() {
assert_eq!(
ai_center_application_url("https://c4c.app.eu2.coralogix.com", "my app", "sub & sys"),
"https://c4c.app.eu2.coralogix.com/ai-center/ai-apps/application-catalog/application-drilldown?application=my+app&subsystem=sub+%26+sys"
);
}
#[test]
fn ai_center_application_url_trims_trailing_slash_on_base() {
assert_eq!(
ai_center_application_url("https://c4c.app.eu2.coralogix.com/", "checkout", "payments"),
"https://c4c.app.eu2.coralogix.com/ai-center/ai-apps/application-catalog/application-drilldown?application=checkout&subsystem=payments"
);
}
#[test]
fn ai_center_evaluations_url_is_static() {
assert_eq!(
ai_center_evaluations_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/ai-center/ai-apps/policy-catalog"
);
}
#[test]
fn ai_center_application_evaluations_url_uses_query_params() {
assert_eq!(
ai_center_application_evaluations_url(
"https://c4c.app.eu2.coralogix.com/",
"my app",
"sub & sys"
),
"https://c4c.app.eu2.coralogix.com/ai-center/ai-apps/application-catalog/policy-configuration?application=my+app&subsystem=sub+%26+sys"
);
}
#[test]
fn olly_url_is_static() {
assert_eq!(
olly_url("https://c4c.app.eu2.coralogix.com"),
"https://c4c.app.eu2.coralogix.com/olly"
);
}
#[test]
fn olly_chat_url_includes_chat_id() {
assert_eq!(
olly_chat_url(
"https://c4c.app.eu2.coralogix.com",
"1a58088c-aff2-46aa-b5b5-f6e109d7bc3f"
),
"https://c4c.app.eu2.coralogix.com/olly/chat/1a58088c-aff2-46aa-b5b5-f6e109d7bc3f"
);
}
#[test]
fn static_urls_trim_trailing_slash_on_base() {
assert_eq!(
tco_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/tco-policies"
);
assert_eq!(
olly_url("https://c4c.app.eu2.coralogix.com/"),
"https://c4c.app.eu2.coralogix.com/olly"
);
}
}