-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b795e92
commit 1a882de
Showing
11 changed files
with
354 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,22 @@ | ||
// Copyright 2024 Canonical. | ||
package jimm | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/canonical/jimm/v3/internal/common/pagination" | ||
"github.com/canonical/jimm/v3/internal/db" | ||
"github.com/canonical/jimm/v3/internal/errors" | ||
"github.com/canonical/jimm/v3/internal/openfga" | ||
) | ||
|
||
// ListResources returns a list of resources known to JIMM with a pagination filter. | ||
func (j *JIMM) ListResources(ctx context.Context, user *openfga.User, filter pagination.LimitOffsetPagination) ([]db.Resource, error) { | ||
const op = errors.Op("jimm.GetResources") | ||
|
||
if !user.JimmAdmin { | ||
return nil, errors.E(op, errors.CodeUnauthorized, "unauthorized") | ||
} | ||
|
||
return j.Database.ListResources(ctx, filter.Limit(), filter.Offset()) | ||
} |
This file contains 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,82 @@ | ||
// Copyright 2024 Canonical. | ||
package jimm_test | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
"testing" | ||
"time" | ||
|
||
qt "github.com/frankban/quicktest" | ||
"github.com/google/uuid" | ||
|
||
"github.com/canonical/jimm/v3/internal/common/pagination" | ||
"github.com/canonical/jimm/v3/internal/db" | ||
"github.com/canonical/jimm/v3/internal/dbmodel" | ||
"github.com/canonical/jimm/v3/internal/jimm" | ||
"github.com/canonical/jimm/v3/internal/jimmtest" | ||
"github.com/canonical/jimm/v3/internal/openfga" | ||
) | ||
|
||
func TestGetResources(t *testing.T) { | ||
c := qt.New(t) | ||
ctx := context.Background() | ||
ofgaClient, _, _, err := jimmtest.SetupTestOFGAClient(c.Name()) | ||
c.Assert(err, qt.IsNil) | ||
|
||
now := time.Now().UTC().Round(time.Millisecond) | ||
j := &jimm.JIMM{ | ||
UUID: uuid.NewString(), | ||
Database: db.Database{ | ||
DB: jimmtest.PostgresDB(c, func() time.Time { return now }), | ||
}, | ||
OpenFGAClient: ofgaClient, | ||
} | ||
|
||
err = j.Database.Migrate(ctx, false) | ||
c.Assert(err, qt.IsNil) | ||
user, _, controller, model, applicationOffer, cloud, _ := createTestControllerEnvironment(ctx, c, j.Database) | ||
|
||
ids := []string{user.Name, controller.UUID, model.UUID.String, applicationOffer.UUID, cloud.Name} | ||
slices.Sort(ids) | ||
|
||
u := openfga.NewUser(&dbmodel.Identity{Name: "[email protected]"}, ofgaClient) | ||
u.JimmAdmin = true | ||
|
||
testCases := []struct { | ||
desc string | ||
limit int | ||
offset int | ||
identities []string | ||
}{ | ||
{ | ||
desc: "test with first resources", | ||
limit: 3, | ||
offset: 0, | ||
identities: []string{ids[0], ids[1], ids[2]}, | ||
}, | ||
{ | ||
desc: "test with remianing ids", | ||
limit: 3, | ||
offset: 3, | ||
identities: []string{ids[3], ids[4]}, | ||
}, | ||
{ | ||
desc: "test out of range", | ||
limit: 3, | ||
offset: 6, | ||
identities: []string{}, | ||
}, | ||
} | ||
for _, t := range testCases { | ||
c.Run(t.desc, func(c *qt.C) { | ||
filter := pagination.NewOffsetFilter(t.limit, t.offset) | ||
resources, err := j.ListResources(ctx, u, filter) | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(resources, qt.HasLen, len(t.identities)) | ||
for i := range len(t.identities) { | ||
c.Assert(resources[i].ID.String, qt.Equals, t.identities[i]) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.