Skip to content

Commit

Permalink
Split GetGroup facade into ByName and ByUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulik0 committed Sep 27, 2024
1 parent a346023 commit c0bcc53
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
35 changes: 22 additions & 13 deletions internal/jujuapi/access_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,29 @@ func (r *controllerRoot) AddGroup(ctx context.Context, req apiparams.AddGroupReq
return resp, nil
}

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

var groupEntry *dbmodel.GroupEntry
var err error
switch {
case req.UUID != "":
groupEntry, err = r.jimm.GetGroupByUUID(ctx, r.user, req.UUID)
case req.Name != "":
groupEntry, err = r.jimm.GetGroupByName(ctx, r.user, req.Name)
default:
return apiparams.Group{}, errors.E(op, errors.CodeBadRequest, "invalid GetGroup request")
// GetGroupByUUID returns group information based on a UUID.
func (r *controllerRoot) GetGroupByUUID(ctx context.Context, req apiparams.GetGroupByUUIDRequest) (apiparams.Group, error) {
const op = errors.Op("jujuapi.GetGroupByUUID")

groupEntry, err := r.jimm.GetGroupByUUID(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
}

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

groupEntry, err := r.jimm.GetGroupByName(ctx, r.user, req.Name)
if err != nil {
zapctx.Error(ctx, "failed to get group", zaputil.Error(err))
return apiparams.Group{}, errors.E(op, err)
Expand Down
9 changes: 6 additions & 3 deletions internal/jujuapi/access_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ func (s *accessControlSuite) TestGetGroup(c *gc.C) {
created, err := client.AddGroup(&apiparams.AddGroupRequest{Name: "test-group"})
c.Assert(err, jc.ErrorIsNil)

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

retrievedName, err := client.GetGroup(&apiparams.GetGroupRequest{Name: created.Name})
retrievedName, err := client.GetGroupByName(&apiparams.GetGroupByNameRequest{Name: created.Name})
c.Assert(err, jc.ErrorIsNil)
c.Assert(retrievedName.Group, gc.DeepEquals, created.Group)

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

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

Expand Down
6 changes: 4 additions & 2 deletions internal/jujuapi/jimm.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func init() {
addCloudToControllerMethod := rpc.Method(r.AddCloudToController)
removeCloudFromControllerMethod := rpc.Method(r.RemoveCloudFromController)
addGroupMethod := rpc.Method(r.AddGroup)
getGroupMethod := rpc.Method(r.GetGroup)
getGroupByUUIDMethod := rpc.Method(r.GetGroupByUUID)
getGroupByNameMethod := rpc.Method(r.GetGroupByName)
renameGroupMethod := rpc.Method(r.RenameGroup)
removeGroupMethod := rpc.Method(r.RemoveGroup)
listGroupsMethod := rpc.Method(r.ListGroups)
Expand Down Expand Up @@ -77,7 +78,8 @@ 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, "GetGroupByUUID", getGroupByUUIDMethod)
r.AddMethod("JIMM", 4, "GetGroupByName", getGroupByNameMethod)
r.AddMethod("JIMM", 4, "RenameGroup", renameGroupMethod)
r.AddMethod("JIMM", 4, "RemoveGroup", removeGroupMethod)
r.AddMethod("JIMM", 4, "ListGroups", listGroupsMethod)
Expand Down
13 changes: 10 additions & 3 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ 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) {
// GetGroupByUUID returns the group with the given UUID.
func (c *Client) GetGroupByUUID(req *params.GetGroupByUUIDRequest) (params.GetGroupResponse, error) {
var resp params.GetGroupResponse
err := c.caller.APICall("JIMM", 4, "", "GetGroup", req, &resp)
err := c.caller.APICall("JIMM", 4, "", "GetGroupByUUID", req, &resp)
return resp, err
}

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

Expand Down
8 changes: 6 additions & 2 deletions pkg/api/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,14 @@ type AddGroupResponse struct {
Group
}

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

// GetGroupByNameRequest holds a request to get a group by name.
type GetGroupByNameRequest struct {
// Name holds the name of the group to be retrieved.
Name string `json:"name"`
}
Expand Down

0 comments on commit c0bcc53

Please sign in to comment.