Skip to content

Commit

Permalink
Merge pull request #1363 from pkulik0/get-group-endpoint
Browse files Browse the repository at this point in the history
Implement GetGroup api endpoint
  • Loading branch information
pkulik0 authored Sep 12, 2024
2 parents 8c57ffe + 16883f8 commit 5101bf8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/jujuapi/access_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ func (r *controllerRoot) AddGroup(ctx context.Context, req apiparams.AddGroupReq
return resp, nil
}

// GetGroup returns group information based on a group ID.
func (r *controllerRoot) GetGroup(ctx context.Context, req apiparams.GetGroupRequest) (apiparams.Group, error) {
const op = errors.Op("jujuapi.GetGroup")

groupEntry, err := r.jimm.GetGroupByID(ctx, r.user, req.UUID)
if err != nil {
zapctx.Error(ctx, "failed to get group", zaputil.Error(err))
return apiparams.Group{}, errors.E(op, err)
}
return apiparams.Group{
UUID: groupEntry.UUID,
Name: groupEntry.Name,
CreatedAt: groupEntry.CreatedAt.Format(time.RFC3339),
UpdatedAt: groupEntry.UpdatedAt.Format(time.RFC3339),
}, nil
}

// RenameGroup renames a group within JIMMs DB for reference by OpenFGA.
func (r *controllerRoot) RenameGroup(ctx context.Context, req apiparams.RenameGroupRequest) error {
const op = errors.Op("jujuapi.RenameGroup")
Expand Down
17 changes: 17 additions & 0 deletions internal/jujuapi/access_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ func (s *accessControlSuite) TestAddGroup(c *gc.C) {
c.Assert(err, gc.ErrorMatches, ".*already exists.*")
}

func (s *accessControlSuite) TestGetGroup(c *gc.C) {
conn := s.open(c, nil, "alice")
defer conn.Close()

client := api.NewClient(conn)

created, err := client.AddGroup(&apiparams.AddGroupRequest{Name: "test-group"})
c.Assert(err, jc.ErrorIsNil)

retrieved, err := client.GetGroup(&apiparams.GetGroupRequest{UUID: created.UUID})
c.Assert(err, jc.ErrorIsNil)
c.Assert(retrieved.Group, gc.DeepEquals, created.Group)

_, err = client.GetGroup(&apiparams.GetGroupRequest{UUID: "non-existent"})
c.Assert(err, gc.ErrorMatches, ".*not found.*")
}

func (s *accessControlSuite) TestRemoveGroup(c *gc.C) {
conn := s.open(c, nil, "alice")
defer conn.Close()
Expand Down
2 changes: 2 additions & 0 deletions internal/jujuapi/jimm.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
addCloudToControllerMethod := rpc.Method(r.AddCloudToController)
removeCloudFromControllerMethod := rpc.Method(r.RemoveCloudFromController)
addGroupMethod := rpc.Method(r.AddGroup)
getGroupMethod := rpc.Method(r.GetGroup)
renameGroupMethod := rpc.Method(r.RenameGroup)
removeGroupMethod := rpc.Method(r.RemoveGroup)
listGroupsMethod := rpc.Method(r.ListGroups)
Expand Down Expand Up @@ -76,6 +77,7 @@ func init() {
r.AddMethod("JIMM", 4, "MigrateModel", migrateModel)
// JIMM ReBAC RPC
r.AddMethod("JIMM", 4, "AddGroup", addGroupMethod)
r.AddMethod("JIMM", 4, "GetGroup", getGroupMethod)
r.AddMethod("JIMM", 4, "RenameGroup", renameGroupMethod)
r.AddMethod("JIMM", 4, "RemoveGroup", removeGroupMethod)
r.AddMethod("JIMM", 4, "ListGroups", listGroupsMethod)
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func (c *Client) AddGroup(req *params.AddGroupRequest) (params.AddGroupResponse,
return resp, err
}

// GetGroup returns the group with the given UUID.
func (c *Client) GetGroup(req *params.GetGroupRequest) (params.GetGroupResponse, error) {
var resp params.GetGroupResponse
err := c.caller.APICall("JIMM", 4, "", "GetGroup", req, &resp)
return resp, err
}

// RenameGroup renames a group in JIMM.
func (c *Client) RenameGroup(req *params.RenameGroupRequest) error {
return c.caller.APICall("JIMM", 4, "", "RenameGroup", req, nil)
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ type AddGroupResponse struct {
Group
}

// GetGroupRequest holds a request to get a group.
type GetGroupRequest struct {
// UUID holds the UUID of the group to be retrieved.
UUID string `json:"uuid"`
}

// GetGroupResponse holds the details of the group.
type GetGroupResponse struct {
Group
}

// RenameGroupRequest holds a request to rename a group.
type RenameGroupRequest struct {
// Name holds the name of the group.
Expand Down

0 comments on commit 5101bf8

Please sign in to comment.