-
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.
feat(modelmanager): listmodels (#1493)
* test(modelmanager): k8s tests fix The api was trying to call /version, but our mock didn't implement it. * feat(listmodels): enable listmodels within the jujuclient pkg * fix(listmodels): enable list models via controller We now list models via contacting controllers directly. * docs(listmodels): update jimm pkg listmodels godoc * refactor(pr): pr comments * chore: pr comments * chore: pr comments * chore: pr comments * chore: pr comment * chore: pr comments * chore: pr comment
- Loading branch information
Showing
13 changed files
with
485 additions
and
120 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
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
qt "github.com/frankban/quicktest" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/juju/juju/api/base" | ||
"github.com/juju/juju/core/life" | ||
"github.com/juju/juju/rpc/params" | ||
jujuparams "github.com/juju/juju/rpc/params" | ||
|
@@ -3804,6 +3805,199 @@ controllers: | |
c.Assert(model.Controller.Name, qt.Equals, "controller-3") | ||
} | ||
|
||
const listModelsTestEnv = `clouds: | ||
- name: test-cloud | ||
type: test-provider | ||
regions: | ||
- name: test-cloud-region | ||
cloud-credentials: | ||
- owner: [email protected] | ||
name: cred-1 | ||
cloud: test-cloud | ||
controllers: | ||
- name: controller-1 | ||
uuid: 00000001-0000-0000-0000-000000000001 | ||
cloud: test-cloud | ||
region: test-cloud-region | ||
- name: controller-2 | ||
uuid: 00000001-0000-0000-0000-000000000001 | ||
cloud: test-cloud | ||
region: test-cloud-region | ||
models: | ||
- name: model-1 | ||
uuid: 00000002-0000-0000-0000-000000000001 | ||
controller: controller-1 | ||
cloud: test-cloud | ||
region: test-cloud-region | ||
cloud-credential: cred-1 | ||
owner: [email protected] | ||
life: alive | ||
users: | ||
- user: [email protected] | ||
access: admin | ||
- user: [email protected] | ||
access: admin | ||
- name: model-2 | ||
uuid: 00000002-0000-0000-0000-000000000002 | ||
controller: controller-1 | ||
cloud: test-cloud | ||
region: test-cloud-region | ||
cloud-credential: cred-1 | ||
owner: [email protected] | ||
life: alive | ||
users: | ||
- user: [email protected] | ||
access: admin | ||
- user: [email protected] | ||
access: write | ||
sla: | ||
level: unsupported | ||
- name: model-3 | ||
uuid: 00000002-0000-0000-0000-000000000003 | ||
controller: controller-2 | ||
cloud: test-cloud | ||
region: test-cloud-region | ||
cloud-credential: cred-1 | ||
owner: [email protected] | ||
life: alive | ||
users: | ||
- user: [email protected] | ||
access: admin | ||
- user: [email protected] | ||
access: read | ||
- name: model-4 | ||
uuid: 00000002-0000-0000-0000-000000000004 | ||
controller: controller-1 | ||
cloud: test-cloud | ||
region: test-cloud-region | ||
cloud-credential: cred-1 | ||
owner: [email protected] | ||
life: alive | ||
users: | ||
- user: [email protected] | ||
access: admin | ||
users: | ||
- username: [email protected] | ||
controller-access: superuser | ||
` | ||
|
||
var modelListTests = []struct { | ||
name string | ||
env string | ||
username string | ||
expectedUserModels []base.UserModel | ||
expectedError string | ||
listModelsMockByControllerName map[string]func(context.Context) ([]base.UserModel, error) | ||
}{ | ||
{ | ||
name: "Bob lists models across controllers 1 and 2", | ||
env: listModelsTestEnv, | ||
username: "[email protected]", | ||
expectedUserModels: []base.UserModel{ | ||
{UUID: "00000002-0000-0000-0000-000000000001", Owner: "[email protected]"}, | ||
{UUID: "00000002-0000-0000-0000-000000000002", Owner: "[email protected]"}, | ||
{UUID: "00000002-0000-0000-0000-000000000003", Owner: "[email protected]"}, | ||
}, | ||
listModelsMockByControllerName: map[string]func(context.Context) ([]base.UserModel, error){ | ||
"controller-1": func(ctx context.Context) ([]base.UserModel, error) { | ||
return []base.UserModel{ | ||
{UUID: "00000002-0000-0000-0000-000000000001"}, | ||
{UUID: "00000002-0000-0000-0000-000000000002"}, | ||
}, nil | ||
}, | ||
"controller-2": func(ctx context.Context) ([]base.UserModel, error) { | ||
return []base.UserModel{ | ||
{UUID: "00000002-0000-0000-0000-000000000003"}, | ||
}, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Alice lists models across controllers 1 and 2", | ||
env: listModelsTestEnv, | ||
username: "[email protected]", | ||
expectedUserModels: []base.UserModel{ | ||
{UUID: "00000002-0000-0000-0000-000000000001", Owner: "[email protected]"}, | ||
{UUID: "00000002-0000-0000-0000-000000000002", Owner: "[email protected]"}, | ||
{UUID: "00000002-0000-0000-0000-000000000003", Owner: "[email protected]"}, | ||
{UUID: "00000002-0000-0000-0000-000000000004", Owner: "[email protected]"}, | ||
}, | ||
listModelsMockByControllerName: map[string]func(context.Context) ([]base.UserModel, error){ | ||
"controller-1": func(ctx context.Context) ([]base.UserModel, error) { | ||
return []base.UserModel{ | ||
{UUID: "00000002-0000-0000-0000-000000000001"}, | ||
{UUID: "00000002-0000-0000-0000-000000000002"}, | ||
{UUID: "00000002-0000-0000-0000-000000000004"}, | ||
}, nil | ||
}, | ||
"controller-2": func(ctx context.Context) ([]base.UserModel, error) { | ||
return []base.UserModel{ | ||
{UUID: "00000002-0000-0000-0000-000000000003"}, | ||
}, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Alice lists models across controllers 1 and 2", | ||
env: listModelsTestEnv, | ||
username: "[email protected]", | ||
expectedUserModels: []base.UserModel{}, | ||
expectedError: "failed to list models", | ||
listModelsMockByControllerName: map[string]func(context.Context) ([]base.UserModel, error){ | ||
"controller-1": func(ctx context.Context) ([]base.UserModel, error) { | ||
return []base.UserModel{}, errors.E("test error") | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func TestListModels(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
for _, test := range modelListTests { | ||
c.Run( | ||
test.name, | ||
func(c *qt.C) { | ||
j := jimmtest.NewJIMM(c, &jimm.Parameters{ | ||
Dialer: jimmtest.DialerMap{ | ||
"controller-1": &jimmtest.Dialer{ | ||
API: &jimmtest.API{ | ||
ListModels_: test.listModelsMockByControllerName["controller-1"], | ||
}, | ||
}, | ||
"controller-2": &jimmtest.Dialer{ | ||
API: &jimmtest.API{ | ||
ListModels_: test.listModelsMockByControllerName["controller-2"], | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
env := jimmtest.ParseEnvironment(c, test.env) | ||
env.PopulateDBAndPermissions(c, j.ResourceTag(), j.Database, j.OpenFGAClient) | ||
|
||
dbUser, err := dbmodel.NewIdentity(test.username) | ||
c.Assert(err, qt.IsNil) | ||
user := openfga.NewUser(dbUser, j.OpenFGAClient) | ||
|
||
models, err := j.ListModels(context.Background(), user) | ||
if test.expectedError != "" { | ||
c.Assert(err, qt.ErrorMatches, test.expectedError) | ||
} else { | ||
c.Assert(models, qt.ContentEquals, test.expectedUserModels) | ||
} | ||
}, | ||
) | ||
} | ||
} | ||
|
||
func newBool(b bool) *bool { | ||
return &b | ||
} | ||
|
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.