-
Notifications
You must be signed in to change notification settings - Fork 37
feat: migrate Admin RPCs to connectRPC server #1119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package v1beta1connect | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/raystack/frontier/core/group" | ||
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func (h *ConnectHandler) ListGroups(ctx context.Context, request *connect.Request[frontierv1beta1.ListGroupsRequest]) (*connect.Response[frontierv1beta1.ListGroupsResponse], error) { | ||
var groups []*frontierv1beta1.Group | ||
groupList, err := h.groupService.List(ctx, group.Filter{ | ||
SU: true, | ||
OrganizationID: request.Msg.GetOrgId(), | ||
State: group.State(request.Msg.GetState()), | ||
}) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError) | ||
} | ||
|
||
for _, v := range groupList { | ||
groupPB, err := transformGroupToPB(v) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError) | ||
} | ||
|
||
groups = append(groups, &groupPB) | ||
} | ||
|
||
return connect.NewResponse(&frontierv1beta1.ListGroupsResponse{Groups: groups}), nil | ||
} | ||
|
||
func transformGroupToPB(grp group.Group) (frontierv1beta1.Group, error) { | ||
metaData, err := grp.Metadata.ToStructPB() | ||
if err != nil { | ||
return frontierv1beta1.Group{}, err | ||
} | ||
|
||
return frontierv1beta1.Group{ | ||
Id: grp.ID, | ||
Name: grp.Name, | ||
Title: grp.Title, | ||
OrgId: grp.OrganizationID, | ||
Metadata: metaData, | ||
CreatedAt: timestamppb.New(grp.CreatedAt), | ||
UpdatedAt: timestamppb.New(grp.UpdatedAt), | ||
MembersCount: int32(grp.MemberCount), | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package v1beta1connect | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/raystack/frontier/core/group" | ||
"github.com/raystack/frontier/internal/api/v1beta1/mocks" | ||
"github.com/raystack/frontier/pkg/errors" | ||
"github.com/raystack/frontier/pkg/metadata" | ||
"github.com/raystack/frontier/pkg/utils" | ||
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
var ( | ||
testGroupID = "9f256f86-31a3-11ec-8d3d-0242ac130003" | ||
testGroupMap = map[string]group.Group{ | ||
"9f256f86-31a3-11ec-8d3d-0242ac130003": { | ||
ID: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
Name: "group-1", | ||
Metadata: metadata.Metadata{ | ||
"foo": "bar", | ||
}, | ||
OrganizationID: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
CreatedAt: time.Time{}, | ||
UpdatedAt: time.Time{}, | ||
}, | ||
} | ||
) | ||
|
||
func TestHandler_ListGroups(t *testing.T) { | ||
randomID := utils.NewString() | ||
tests := []struct { | ||
name string | ||
setup func(gs *mocks.GroupService) | ||
request *connect.Request[frontierv1beta1.ListGroupsRequest] | ||
want *connect.Response[frontierv1beta1.ListGroupsResponse] | ||
wantErr error | ||
}{ | ||
{ | ||
name: "should return empty groups if query param org_id is not uuid", | ||
setup: func(gs *mocks.GroupService) { | ||
gs.EXPECT().List(mock.Anything, group.Filter{ | ||
SU: true, | ||
OrganizationID: "some-id", | ||
}).Return([]group.Group{}, nil) | ||
}, | ||
request: connect.NewRequest(&frontierv1beta1.ListGroupsRequest{ | ||
OrgId: "some-id", | ||
}), | ||
want: connect.NewResponse(&frontierv1beta1.ListGroupsResponse{ | ||
Groups: nil, | ||
}), | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "should return empty groups if query param org_id is not exist", | ||
setup: func(gs *mocks.GroupService) { | ||
gs.EXPECT().List(mock.Anything, group.Filter{ | ||
SU: true, | ||
OrganizationID: randomID, | ||
}).Return([]group.Group{}, nil) | ||
}, | ||
request: connect.NewRequest(&frontierv1beta1.ListGroupsRequest{ | ||
OrgId: randomID, | ||
}), | ||
want: connect.NewResponse(&frontierv1beta1.ListGroupsResponse{ | ||
Groups: nil, | ||
}), | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "should return all groups if no query param filter exist", | ||
setup: func(gs *mocks.GroupService) { | ||
var testGroupList []group.Group | ||
for _, u := range testGroupMap { | ||
testGroupList = append(testGroupList, u) | ||
} | ||
gs.EXPECT().List(mock.Anything, group.Filter{ | ||
SU: true, | ||
}).Return(testGroupList, nil) | ||
}, | ||
request: connect.NewRequest(&frontierv1beta1.ListGroupsRequest{}), | ||
want: connect.NewResponse(&frontierv1beta1.ListGroupsResponse{ | ||
Groups: []*frontierv1beta1.Group{ | ||
{ | ||
Id: testGroupID, | ||
Name: "group-1", | ||
OrgId: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
Metadata: &structpb.Struct{ | ||
Fields: map[string]*structpb.Value{ | ||
"foo": structpb.NewStringValue("bar"), | ||
}, | ||
}, | ||
CreatedAt: timestamppb.New(time.Time{}), | ||
UpdatedAt: timestamppb.New(time.Time{}), | ||
}, | ||
}, | ||
}), | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "should return filtered groups if query param org_id exist", | ||
setup: func(gs *mocks.GroupService) { | ||
var testGroupList []group.Group | ||
for _, u := range testGroupMap { | ||
testGroupList = append(testGroupList, u) | ||
} | ||
gs.EXPECT().List(mock.Anything, group.Filter{ | ||
SU: true, | ||
OrganizationID: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
}).Return(testGroupList, nil) | ||
}, | ||
request: connect.NewRequest(&frontierv1beta1.ListGroupsRequest{ | ||
OrgId: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
}), | ||
want: connect.NewResponse(&frontierv1beta1.ListGroupsResponse{ | ||
Groups: []*frontierv1beta1.Group{ | ||
{ | ||
Id: testGroupID, | ||
Name: "group-1", | ||
OrgId: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
Metadata: &structpb.Struct{ | ||
Fields: map[string]*structpb.Value{ | ||
"foo": structpb.NewStringValue("bar"), | ||
}, | ||
}, | ||
CreatedAt: timestamppb.New(time.Time{}), | ||
UpdatedAt: timestamppb.New(time.Time{}), | ||
}, | ||
}, | ||
}), | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "should return an error if Group service return some error ", | ||
setup: func(gs *mocks.GroupService) { | ||
gs.EXPECT().List(mock.Anything, group.Filter{ | ||
SU: true, | ||
OrganizationID: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
}).Return(nil, errors.New("test-error")) | ||
}, | ||
request: connect.NewRequest(&frontierv1beta1.ListGroupsRequest{ | ||
OrgId: "9f256f86-31a3-11ec-8d3d-0242ac130003", | ||
}), | ||
want: nil, | ||
wantErr: connect.NewError(connect.CodeInternal, ErrInternalServerError), | ||
}, | ||
{ | ||
name: "should return error while traversing group list if key is integer type", | ||
setup: func(gs *mocks.GroupService) { | ||
gs.EXPECT().List(mock.Anything, group.Filter{ | ||
SU: true, | ||
OrganizationID: "some-id", | ||
}).Return([]group.Group{ | ||
{ | ||
Metadata: metadata.Metadata{ | ||
"key": map[int]any{}, | ||
}, | ||
}, | ||
}, nil) | ||
}, | ||
request: connect.NewRequest(&frontierv1beta1.ListGroupsRequest{ | ||
OrgId: "some-id", | ||
}), | ||
want: nil, | ||
wantErr: connect.NewError(connect.CodeInternal, ErrInternalServerError), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockGroupSvc := new(mocks.GroupService) | ||
if tt.setup != nil { | ||
tt.setup(mockGroupSvc) | ||
} | ||
h := ConnectHandler{ | ||
groupService: mockGroupSvc, | ||
} | ||
got, err := h.ListGroups(context.Background(), tt.request) | ||
assert.EqualValues(t, tt.want, got) | ||
if tt.want == nil { | ||
assert.ErrorContains(t, err, tt.wantErr.Error()) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package v1beta1connect | ||
|
||
var testNSID = "team" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package v1beta1connect | ||
|
||
var ( | ||
testOrgID = "9f256f86-31a3-11ec-8d3d-0242ac130003" | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package v1beta1connect | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/raystack/frontier/core/project" | ||
|
||
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func (h *ConnectHandler) ListProjects(ctx context.Context, request *connect.Request[frontierv1beta1.ListProjectsRequest]) (*connect.Response[frontierv1beta1.ListProjectsResponse], error) { | ||
var projects []*frontierv1beta1.Project | ||
projectList, err := h.projectService.List(ctx, project.Filter{ | ||
State: project.State(request.Msg.GetState()), | ||
OrgID: request.Msg.GetOrgId(), | ||
}) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError) | ||
} | ||
|
||
for _, v := range projectList { | ||
projectPB, err := transformProjectToPB(v) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError) | ||
} | ||
|
||
projects = append(projects, projectPB) | ||
} | ||
|
||
return connect.NewResponse(&frontierv1beta1.ListProjectsResponse{Projects: projects}), nil | ||
} | ||
|
||
func transformProjectToPB(prj project.Project) (*frontierv1beta1.Project, error) { | ||
metaData, err := prj.Metadata.ToStructPB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &frontierv1beta1.Project{ | ||
Id: prj.ID, | ||
Name: prj.Name, | ||
Title: prj.Title, | ||
OrgId: prj.Organization.ID, | ||
Metadata: metaData, | ||
CreatedAt: timestamppb.New(prj.CreatedAt), | ||
UpdatedAt: timestamppb.New(prj.UpdatedAt), | ||
MembersCount: int32(prj.MemberCount), | ||
}, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.