本文整理匯總了Golang中github.com/juju/names.NewLocalUserTag函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewLocalUserTag函數的具體用法?Golang NewLocalUserTag怎麽用?Golang NewLocalUserTag使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewLocalUserTag函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestUserAddGrantModel
func (s *UserSuite) TestUserAddGrantModel(c *gc.C) {
sharedModelState := s.Factory.MakeModel(c, &factory.ModelParams{
Name: "amodel",
})
defer sharedModelState.Close()
ctx, err := s.RunUserCommand(c, "", "add-user", "test", "--models", "amodel")
c.Assert(err, jc.ErrorIsNil)
c.Assert(testing.Stdout(ctx), jc.HasPrefix, `User "test" added`)
user, err := s.State.User(names.NewLocalUserTag("test"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.IsDisabled(), jc.IsFalse)
// Check model is shared with expected users.
sharedModel, err := sharedModelState.Model()
c.Assert(err, jc.ErrorIsNil)
users, err := sharedModel.Users()
c.Assert(err, jc.ErrorIsNil)
var modelUserTags = make([]names.UserTag, len(users))
for i, u := range users {
modelUserTags[i] = u.UserTag()
}
c.Assert(modelUserTags, jc.SameContents, []names.UserTag{
user.Tag().(names.UserTag),
names.NewLocalUserTag("admin"),
})
}
示例2: addUserWithSharedModel
func (s *userManagerSuite) addUserWithSharedModel(c *gc.C, access params.ModelAccessPermission) {
sharedModelState := s.Factory.MakeModel(c, nil)
defer sharedModelState.Close()
s.assertAddUser(c, access, []string{sharedModelState.ModelTag().String()})
// Check that the model has been shared.
sharedModel, err := sharedModelState.Model()
c.Assert(err, jc.ErrorIsNil)
users, err := sharedModel.Users()
c.Assert(err, jc.ErrorIsNil)
var modelUserTags = make([]names.UserTag, len(users))
for i, u := range users {
modelUserTags[i] = u.UserTag()
if u.UserName() == "foobar" {
c.Assert(u.ReadOnly(), gc.Equals, access == params.ModelReadAccess)
} else if u.UserName() == "admin" {
c.Assert(u.ReadOnly(), gc.Equals, false)
}
}
c.Assert(modelUserTags, jc.SameContents, []names.UserTag{
names.NewLocalUserTag("foobar"),
names.NewLocalUserTag("admin"),
})
}
示例3: TestRemoveUser
func (s *userManagerSuite) TestRemoveUser(c *gc.C) {
args := usermanager.ModifyUsers{
Changes: []usermanager.ModifyUser{{
Username: "foobar",
DisplayName: "Foo Bar",
Password: "password",
}}}
removeArg := params.Entity{
Tag: "foobar",
}
removeArgs := params.Entities{Entities: []params.Entity{removeArg}}
_, err := s.usermanager.AddUser(args)
c.Assert(err, gc.IsNil)
user, err := s.State.User(names.NewLocalUserTag("foobar"))
c.Assert(user.IsDeactivated(), gc.Equals, false) // The user should be active
result, err := s.usermanager.RemoveUser(removeArgs)
c.Assert(err, gc.IsNil)
c.Assert(result, gc.DeepEquals, params.ErrorResults{Results: []params.ErrorResult{params.ErrorResult{Error: nil}}})
user, err = s.State.User(names.NewLocalUserTag("foobar"))
c.Assert(err, gc.IsNil)
// Removal makes the user in active
c.Assert(user.IsDeactivated(), gc.Equals, true)
c.Assert(user.PasswordValid(args.Changes[0].Password), gc.Equals, false)
}
示例4: TestNewLocalUserTag
func (s *userSuite) TestNewLocalUserTag(c *gc.C) {
user := names.NewLocalUserTag("bob")
c.Assert(user.Canonical(), gc.Equals, "[email protected]")
c.Assert(user.Name(), gc.Equals, "bob")
c.Assert(user.Domain(), gc.Equals, "local")
c.Assert(user.IsLocal(), gc.Equals, true)
c.Assert(user.String(), gc.Equals, "[email protected]")
c.Assert(func() { names.NewLocalUserTag("[email protected]") }, gc.PanicMatches, `invalid user name "[email protected]"`)
c.Assert(func() { names.NewLocalUserTag("") }, gc.PanicMatches, `invalid user name ""`)
c.Assert(func() { names.NewLocalUserTag("[email protected]#") }, gc.PanicMatches, `invalid user name "[email protected]#"`)
}
示例5: TestBlockEnableUser
func (s *userManagerSuite) TestBlockEnableUser(c *gc.C) {
alex := s.Factory.MakeUser(c, &factory.UserParams{Name: "alex"})
barb := s.Factory.MakeUser(c, &factory.UserParams{Name: "barb", Disabled: true})
args := params.Entities{
Entities: []params.Entity{
{alex.Tag().String()},
{barb.Tag().String()},
{names.NewLocalUserTag("ellie").String()},
{names.NewUserTag("[email protected]").String()},
{"not-a-tag"},
}}
s.BlockAllChanges(c, "TestBlockEnableUser")
_, err := s.usermanager.EnableUser(args)
// Check that the call is blocked
s.AssertBlocked(c, err, "TestBlockEnableUser")
err = alex.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(alex.IsDisabled(), jc.IsFalse)
err = barb.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(barb.IsDisabled(), jc.IsTrue)
}
示例6: TestEnvironConfigWithoutAgentVersion
func (s *InitializeSuite) TestEnvironConfigWithoutAgentVersion(c *gc.C) {
// admin-secret blocks Initialize.
good := testing.EnvironConfig(c)
attrs := good.AllAttrs()
delete(attrs, "agent-version")
bad, err := config.New(config.NoDefaults, attrs)
c.Assert(err, jc.ErrorIsNil)
owner := names.NewLocalUserTag("initialize-admin")
_, err = state.Initialize(owner, statetesting.NewMongoInfo(), bad, statetesting.NewDialOpts(), state.Policy(nil))
c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state")
st := statetesting.Initialize(c, owner, good, nil)
// yay side effects
st.Close()
s.openState(c, st.EnvironTag())
err = s.State.UpdateEnvironConfig(map[string]interface{}{}, []string{"agent-version"}, nil)
c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state")
// EnvironConfig remains inviolate.
cfg, err := s.State.EnvironConfig()
c.Assert(err, jc.ErrorIsNil)
c.Assert(cfg.AllAttrs(), gc.DeepEquals, good.AllAttrs())
}
示例7: assertAddUser
func (s *userManagerSuite) assertAddUser(c *gc.C, access params.ModelAccessPermission, sharedModelTags []string) {
sharedModelState := s.Factory.MakeModel(c, nil)
defer sharedModelState.Close()
args := params.AddUsers{
Users: []params.AddUser{{
Username: "foobar",
DisplayName: "Foo Bar",
Password: "password",
SharedModelTags: sharedModelTags,
ModelAccess: access,
}}}
result, err := s.usermanager.AddUser(args)
// Check that the call is successful
c.Assert(err, jc.ErrorIsNil)
c.Assert(result.Results, gc.HasLen, 1)
foobarTag := names.NewLocalUserTag("foobar")
c.Assert(result.Results[0], gc.DeepEquals, params.AddUserResult{
Tag: foobarTag.String()})
// Check that the call results in a new user being created
user, err := s.State.User(foobarTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(user, gc.NotNil)
c.Assert(user.Name(), gc.Equals, "foobar")
c.Assert(user.DisplayName(), gc.Equals, "Foo Bar")
}
示例8: TestAddEnvironmentNoCreatedByUserFails
func (s *EnvUserSuite) TestAddEnvironmentNoCreatedByUserFails(c *gc.C) {
user := s.Factory.MakeUser(c, &factory.UserParams{Name: "validusername"})
_, err := s.State.AddEnvironmentUser(state.EnvUserSpec{
User: user.UserTag(),
CreatedBy: names.NewLocalUserTag("createdby")})
c.Assert(err, gc.ErrorMatches, `createdBy user "createdby" does not exist locally: user "createdby" not found`)
}
示例9: TestAddModelNoUserFails
func (s *ModelUserSuite) TestAddModelNoUserFails(c *gc.C) {
createdBy := s.Factory.MakeUser(c, &factory.UserParams{Name: "createdby"})
_, err := s.State.AddModelUser(state.ModelUserSpec{
User: names.NewLocalUserTag("validusername"),
CreatedBy: createdBy.UserTag()})
c.Assert(err, gc.ErrorMatches, `user "validusername" does not exist locally: user "validusername" not found`)
}
示例10: TestNewMetricsAdderAPIRefusesNonAgent
func (s *metricsAdderSuite) TestNewMetricsAdderAPIRefusesNonAgent(c *gc.C) {
tests := []struct {
tag names.Tag
environManager bool
expectedError string
}{
// TODO(cmars): unit agent should get permission denied when callers are
// moved to machine agent.
{names.NewUnitTag("mysql/0"), false, ""},
{names.NewLocalUserTag("admin"), true, "permission denied"},
{names.NewMachineTag("0"), false, ""},
{names.NewMachineTag("0"), true, ""},
}
for i, test := range tests {
c.Logf("test %d", i)
anAuthoriser := s.authorizer
anAuthoriser.EnvironManager = test.environManager
anAuthoriser.Tag = test.tag
endPoint, err := metricsadder.NewMetricsAdderAPI(s.State, nil, anAuthoriser)
if test.expectedError == "" {
c.Assert(err, jc.ErrorIsNil)
c.Assert(endPoint, gc.NotNil)
} else {
c.Assert(err, gc.ErrorMatches, test.expectedError)
c.Assert(endPoint, gc.IsNil)
}
}
}
示例11: TestAddUserWithSecretKey
func (s *userManagerSuite) TestAddUserWithSecretKey(c *gc.C) {
args := params.AddUsers{
Users: []params.AddUser{{
Username: "foobar",
DisplayName: "Foo Bar",
Password: "", // assign secret key
}}}
result, err := s.usermanager.AddUser(args)
// Check that the call is succesful
c.Assert(err, jc.ErrorIsNil)
c.Assert(result.Results, gc.HasLen, 1)
foobarTag := names.NewLocalUserTag("foobar")
// Check that the call results in a new user being created
user, err := s.State.User(foobarTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(user, gc.NotNil)
c.Assert(user.Name(), gc.Equals, "foobar")
c.Assert(user.DisplayName(), gc.Equals, "Foo Bar")
c.Assert(user.SecretKey(), gc.NotNil)
c.Assert(user.PasswordValid(""), jc.IsFalse)
// Check that the secret key returned by the API matches what
// is in state.
c.Assert(result.Results[0], gc.DeepEquals, params.AddUserResult{
Tag: foobarTag.String(),
SecretKey: user.SecretKey(),
})
}
示例12: TestFinishInstanceConfigNonDefault
func (s *CloudInitSuite) TestFinishInstanceConfigNonDefault(c *gc.C) {
userTag := names.NewLocalUserTag("not-touched")
attrs := dummySampleConfig().Merge(testing.Attrs{
"authorized-keys": "we-are-the-keys",
"ssl-hostname-verification": false,
})
cfg, err := config.New(config.NoDefaults, attrs)
c.Assert(err, jc.ErrorIsNil)
icfg := &instancecfg.InstanceConfig{
MongoInfo: &mongo.MongoInfo{Tag: userTag},
APIInfo: &api.Info{Tag: userTag},
}
err = instancecfg.FinishInstanceConfig(icfg, cfg)
c.Assert(err, jc.ErrorIsNil)
c.Assert(icfg, jc.DeepEquals, &instancecfg.InstanceConfig{
AuthorizedKeys: "we-are-the-keys",
AgentEnvironment: map[string]string{
agent.ProviderType: "dummy",
agent.ContainerType: "",
},
MongoInfo: &mongo.MongoInfo{Tag: userTag},
APIInfo: &api.Info{Tag: userTag},
DisableSSLHostnameVerification: true,
PreferIPv6: true,
EnableOSRefreshUpdate: true,
EnableOSUpgrade: true,
})
}
示例13: TestUserInfoUserExists
func (s *userManagerSuite) TestUserInfoUserExists(c *gc.C) {
foobar := "foobar"
fooTag := names.NewLocalUserTag(foobar)
user := s.Factory.MakeUser(c, &factory.UserParams{Name: foobar, DisplayName: "Foo Bar"})
args := params.Entities{
Entities: []params.Entity{{Tag: fooTag.String()}},
}
results, err := s.usermanager.UserInfo(args)
c.Assert(err, gc.IsNil)
expected := usermanager.UserInfoResults{
Results: []usermanager.UserInfoResult{
{
Result: &usermanager.UserInfo{
Username: "foobar",
DisplayName: "Foo Bar",
CreatedBy: s.adminName,
DateCreated: user.DateCreated(),
LastConnection: user.LastLogin(),
},
},
},
}
c.Assert(results, gc.DeepEquals, expected)
}
示例14: RemoveUser
// RemoveUser removes a user.
func (api *UserManagerAPI) RemoveUser(args params.Entities) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
if len(args.Entities) == 0 {
return result, nil
}
for i, arg := range args.Entities {
if !names.IsValidUserName(arg.Tag) {
result.Results[i].Error = common.ServerError(errors.Errorf("%q is not a valid username", arg.Tag))
continue
}
user, err := api.state.User(names.NewLocalUserTag(arg.Tag))
if err != nil {
result.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}
err = user.Deactivate()
if err != nil {
result.Results[i].Error = common.ServerError(fmt.Errorf("Failed to remove user: %s", err))
continue
}
}
return result, nil
}
示例15: TestUserInfo
func (s *userManagerSuite) TestUserInfo(c *gc.C) {
userFoo := s.Factory.MakeUser(c, &factory.UserParams{Name: "foobar", DisplayName: "Foo Bar"})
userBar := s.Factory.MakeUser(c, &factory.UserParams{Name: "barfoo", DisplayName: "Bar Foo", Disabled: true})
args := params.UserInfoRequest{
Entities: []params.Entity{
{
Tag: userFoo.Tag().String(),
}, {
Tag: userBar.Tag().String(),
}, {
Tag: names.NewLocalUserTag("ellie").String(),
}, {
Tag: names.NewUserTag("[email protected]").String(),
}, {
Tag: "not-a-tag",
},
}}
results, err := s.usermanager.UserInfo(args)
c.Assert(err, jc.ErrorIsNil)
expected := params.UserInfoResults{
Results: []params.UserInfoResult{
{
Result: ¶ms.UserInfo{
Username: "foobar",
DisplayName: "Foo Bar",
CreatedBy: s.adminName,
DateCreated: userFoo.DateCreated(),
LastConnection: userFoo.LastLogin(),
},
}, {
Result: ¶ms.UserInfo{
Username: "barfoo",
DisplayName: "Bar Foo",
CreatedBy: s.adminName,
DateCreated: userBar.DateCreated(),
LastConnection: userBar.LastLogin(),
Disabled: true,
},
}, {
Error: ¶ms.Error{
Message: "permission denied",
Code: params.CodeUnauthorized,
},
}, {
Error: ¶ms.Error{
Message: "permission denied",
Code: params.CodeUnauthorized,
},
}, {
Error: ¶ms.Error{
Message: `"not-a-tag" is not a valid tag`,
},
}},
}
c.Assert(results, jc.DeepEquals, expected)
}