Skip to content

Commit

Permalink
rename & parallelise controller calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ale8k committed Jun 20, 2024
1 parent 45cbeb2 commit b73b7a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/jimm/jimm.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ type permission struct {
// has some kind of access to any model on that given controller.
//
// Returns a closer to close all connections. Defer the closer.
func (j *JIMM) dialAllControllers(ctx context.Context, user *openfga.User) ([]API, func(), error) {
func (j *JIMM) dialControllersByUserModelAccess(ctx context.Context, user *openfga.User) ([]API, func(), error) {
const op = errors.Op("jimm.dialAllControllers")
apis := []API{}

Expand Down
40 changes: 32 additions & 8 deletions internal/jimm/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"
"sort"
"strings"
"sync"
"time"

jujupermission "github.com/juju/juju/core/permission"
Expand Down Expand Up @@ -766,23 +767,46 @@ func (j *JIMM) GetAllModelSummariesForUser(ctx context.Context, user *openfga.Us

filteredSummaries := jujuparams.ModelSummaryResults{}
flattenedSummaries := jujuparams.ModelSummaryResults{}
summaries := []jujuparams.ModelSummaryResults{}

apis, closer, err := j.dialAllControllers(ctx, user)
apis, closer, err := j.dialControllersByUserModelAccess(ctx, user)
if err != nil {
return filteredSummaries, errors.E(op, err)
}
defer closer()

var wg sync.WaitGroup
summariesCh := make(chan jujuparams.ModelSummaryResults, len(apis))
errorsCh := make(chan error, len(apis))
// Get all summaries from all controllers
for _, api := range apis {
var out jujuparams.ModelSummaryResults
in := jujuparams.ModelSummariesRequest{UserTag: names.NewUserTag("admin").String(), All: true}
if err := api.ListModelSummaries(context.Background(), &in, &out); err != nil {
return filteredSummaries, err
}
wg.Add(1)
go func(a API) {
defer wg.Done()
var out jujuparams.ModelSummaryResults
in := jujuparams.ModelSummariesRequest{UserTag: names.NewUserTag("admin").String(), All: true}

if err := a.ListModelSummaries(context.Background(), &in, &out); err != nil {
errorsCh <- err
}
summariesCh <- out
}(api)
}
wg.Wait()
close(summariesCh)
close(errorsCh)

summaries = append(summaries, out)
summaries := []jujuparams.ModelSummaryResults{}
errs := []error{}

for e := range errorsCh {
errs = append(errs, e)
}
if len(errs) > 0 {
// What do we do with call errors? Just take the first?
return filteredSummaries, errors.E(op, errs[0])
}
for s := range summariesCh {
summaries = append(summaries, s)
}

// Flatten the summaries into a single results
Expand Down
10 changes: 6 additions & 4 deletions internal/jimm/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1358,10 +1358,12 @@ func TestGetAllModelSummariesForUser(t *testing.T) {

res, err := j.GetAllModelSummariesForUser(ctx, user)
c.Assert(err, qt.IsNil)
c.Assert(res, qt.HasLen, 2) // 4 with controller models, but they're filtered
for _, r := range res.Results {
c.Assert(r.Result.UserAccess, qt.Equals, "admin") // Ensure access level set for both models
}
c.Assert(res.Results, qt.HasLen, 2) // 4 with controller models, but they're filtered

r0 := res.Results[0].Result.UserAccess
r1 := res.Results[1].Result.UserAccess
c.Assert(r0, qt.Equals, jujuparams.UserAccessPermission("admin"))
c.Assert(r1, qt.Equals, jujuparams.UserAccessPermission("admin"))
}

func TestModelInfo(t *testing.T) {
Expand Down

0 comments on commit b73b7a1

Please sign in to comment.