Skip to content

Commit

Permalink
Enable GlobalRouter test
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 committed Feb 3, 2025
1 parent 96b4184 commit 753eaaa
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 44 deletions.
35 changes: 18 additions & 17 deletions go/examples/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func TestPresets(t *testing.T) {
}

func TestGlobalRouter(t *testing.T) {
t.Skip("Global Router API is still unstable")
region, err := cxsdk.CoralogixRegionFromEnv()
assert.Nil(t, err)
authContext, err := cxsdk.AuthContextFromEnv()
Expand All @@ -158,39 +157,41 @@ func TestGlobalRouter(t *testing.T) {

c := cxsdk.NewNotificationsClient(creator)

createRes, err := c.CreateGlobalRouter(context.Background(), &cxsdk.CreateGlobalRouterRequest{
Router: &cxsdk.GlobalRouter{
Name: "TestGlobalRouter",
EntityType: "alerts",
Description: "This is a test Global Router.",
},
alertsEntityType := "alerts"
listRes, err := c.ListGlobalRouters(context.Background(), &cxsdk.ListGlobalRoutersRequest{
EntityType: &alertsEntityType,
})
if err != nil {
t.Fatal(err)
}

routerId := createRes.Router.Id
router, err := c.GetGlobalRouter(context.Background(), &cxsdk.GetGlobalRouterRequest{
Identifier: &cxsdk.GlobalRouterIdentifier{
Value: &cxsdk.GlobalRouterIdentifierIDValue{
Id: *routerId,
},
var routerId *string
if len(listRes.Routers) > 0 {
routerId = listRes.Routers[0].Id
}

createOrReplaceRes, err := c.CreateOrReplaceGlobalRouter(context.Background(), &cxsdk.CreateOrReplaceGlobalRouterRequest{
Router: &cxsdk.GlobalRouter{
Id: routerId,
Name: "TestGlobalRouter",
EntityType: alertsEntityType,
Description: "This is a test Global Router.",
},
})
if err != nil {
t.Fatal(err)
}

assert.Equal(t, router.Router.Name, "TestGlobalRouter")

_, err = c.DeleteGlobalRouter(context.Background(), &cxsdk.DeleteGlobalRouterRequest{
getRes, err := c.GetGlobalRouter(context.Background(), &cxsdk.GetGlobalRouterRequest{
Identifier: &cxsdk.GlobalRouterIdentifier{
Value: &cxsdk.GlobalRouterIdentifierIDValue{
Id: *routerId,
Id: *createOrReplaceRes.Router.Id,
},
},
})
if err != nil {
t.Fatal(err)
}

assert.Equal(t, getRes.Router.Name, "TestGlobalRouter")
}
23 changes: 23 additions & 0 deletions go/notifications-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ type ReplaceGlobalRouterRequest = routers.ReplaceGlobalRouterRequest
// ReplaceGlobalRouterResponse is a response to replace a global router.
type ReplaceGlobalRouterResponse = routers.ReplaceGlobalRouterResponse

// CreateOrReplaceGlobalRouterRequest is a request to create or replace a global router.
type CreateOrReplaceGlobalRouterRequest = routers.CreateOrReplaceGlobalRouterRequest

// CreateOrReplaceGlobalRouterResponse is a response to create or replace a global router.
type CreateOrReplaceGlobalRouterResponse = routers.CreateOrReplaceGlobalRouterResponse

// DeleteGlobalRouterRequest is a request to delete a global router.
type DeleteGlobalRouterRequest = routers.DeleteGlobalRouterRequest

Expand Down Expand Up @@ -616,6 +622,23 @@ func (c NotificationsClient) ReplaceGlobalRouter(ctx context.Context, req *Repla
return response, nil
}

// CreateOrReplaceGlobalRouter creates or replaces a global router.
func (c NotificationsClient) CreateOrReplaceGlobalRouter(ctx context.Context, req *CreateOrReplaceGlobalRouterRequest) (*CreateOrReplaceGlobalRouterResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
if err != nil {
return nil, err
}
conn := callProperties.Connection
defer conn.Close()
client := routers.NewGlobalRoutersServiceClient(conn)

response, err := client.CreateOrReplaceGlobalRouter(callProperties.Ctx, req, callProperties.CallOptions...)
if err != nil {
return nil, NewSdkAPIError(err, GlobalRoutersReplaceRPC, notificationsFeatureGroupID)
}
return response, nil
}

// DeleteGlobalRouter deletes a global router.
func (c NotificationsClient) DeleteGlobalRouter(ctx context.Context, req *DeleteGlobalRouterRequest) (*DeleteGlobalRouterResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
Expand Down
28 changes: 28 additions & 0 deletions rust/cx-sdk/src/client/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ use cx_api::proto::com::coralogixapis::notification_center::{
BatchGetGlobalRoutersResponse,
CreateGlobalRouterRequest,
CreateGlobalRouterResponse,
CreateOrReplaceGlobalRouterRequest,
CreateOrReplaceGlobalRouterResponse,
DeleteGlobalRouterRequest,
DeleteGlobalRouterResponse,
GetGlobalRouterRequest,
Expand Down Expand Up @@ -680,6 +682,32 @@ impl NotificationsClient {
}))
}

/// Create or replace a global router.
/// # Arguments
/// * `global_router` - The [`GlobalRouter`] to create or replace.
pub async fn create_or_replace_global_router(
&self,
global_router: GlobalRouter,
) -> Result<CreateOrReplaceGlobalRouterResponse> {
let request = make_request_with_metadata(
CreateOrReplaceGlobalRouterRequest {
router: Some(global_router),
},
&self.metadata_map,
);
let mut client = self.global_routers_client.lock().await.clone();

client
.create_or_replace_global_router(request)
.await
.map(|r| r.into_inner())
.map_err(|status| SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.notification_center.routers.v1.GlobalRoutersService/CreateOrReplaceGlobalRouter".into(),
feature_group: NOTIFICATIONS_FEATURE_GROUP_ID.into(),
}))
}

/// Replace an existing global router.
/// # Arguments
/// * `global_router` - The [`GlobalRouter`] to replace.
Expand Down
50 changes: 23 additions & 27 deletions rust/examples/notifications/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,6 @@ mod tests {
}
}

fn create_test_global_router() -> GlobalRouter {
GlobalRouter {
name: "TestGlobalRouter".to_string(),
entity_type: "alerts".to_string(),
description: "Global Router for Notification Center testing.".to_string(),
id: None,
user_facing_id: None,
create_time: None,
update_time: None,
rules: vec![],
fallback: vec![],
entity_labels: std::collections::HashMap::new(),
}
}

#[tokio::test]
async fn test_connectors() {
let notifications_client = NotificationsClient::new(
Expand Down Expand Up @@ -180,21 +165,39 @@ mod tests {
}

#[tokio::test]
#[ignore = "Global Router API is still unstable"]
async fn test_global_router() {
let notifications_client = NotificationsClient::new(
AuthContext::from_env(),
CoralogixRegion::from_env().unwrap(),
)
.unwrap();

let global_router = create_test_global_router();
let list_response = notifications_client
.list_global_routers("alerts".to_string())
.await
.unwrap();

let create_response = notifications_client
.create_global_router(global_router.clone())
let existing_router_id = list_response.routers.first().and_then(|r| r.id.clone());

let global_router = GlobalRouter {
id: existing_router_id.clone(),
name: "TestGlobalRouter".to_string(),
entity_type: "alerts".to_string(),
description: "Global Router for Notification Center testing.".to_string(),
user_facing_id: None,
create_time: None,
update_time: None,
rules: vec![],
fallback: vec![],
entity_labels: std::collections::HashMap::new(),
};

let create_or_replace_response = notifications_client
.create_or_replace_global_router(global_router.clone())
.await
.unwrap();
let router_id = create_response.router.unwrap().id.unwrap();

let router_id = create_or_replace_response.router.unwrap().id.unwrap();

let retrieved_router = notifications_client
.get_global_router(GlobalRouterIdentifier {
Expand All @@ -205,12 +208,5 @@ mod tests {
.router;

assert_eq!(retrieved_router.unwrap().name, global_router.name);

notifications_client
.delete_global_router(GlobalRouterIdentifier {
value: Some(global_router_identifier::Value::Id(router_id)),
})
.await
.unwrap();
}
}

0 comments on commit 753eaaa

Please sign in to comment.