本文整理匯總了Golang中github.com/juju/names.IsValidUser函數的典型用法代碼示例。如果您正苦於以下問題:Golang IsValidUser函數的具體用法?Golang IsValidUser怎麽用?Golang IsValidUser使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了IsValidUser函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: AddUser
// AddUser creates a new local user in the juju server.
func (c *Client) AddUser(username, displayName, password string) (names.UserTag, error) {
if !names.IsValidUser(username) {
return names.UserTag{}, fmt.Errorf("invalid user name %q", username)
}
userArgs := params.AddUsers{
Users: []params.AddUser{{Username: username, DisplayName: displayName, Password: password}},
}
var results params.AddUserResults
err := c.facade.FacadeCall("AddUser", userArgs, &results)
if err != nil {
return names.UserTag{}, errors.Trace(err)
}
if count := len(results.Results); count != 1 {
logger.Errorf("expected 1 result, got %#v", results)
return names.UserTag{}, errors.Errorf("expected 1 result, got %d", count)
}
result := results.Results[0]
if result.Error != nil {
return names.UserTag{}, errors.Trace(result.Error)
}
tag, err := names.ParseUserTag(result.Tag)
if err != nil {
return names.UserTag{}, errors.Trace(err)
}
logger.Infof("created user %s", result.Tag)
return tag, nil
}
示例2: ListModels
// ListModels returns the models that the specified user
// has access to in the current server. Only that controller owner
// can list models for any user (at this stage). Other users
// can only ask about their own models.
func (c *Client) ListModels(user string) ([]base.UserModel, error) {
var models params.UserModelList
if !names.IsValidUser(user) {
return nil, errors.Errorf("invalid user name %q", user)
}
entity := params.Entity{names.NewUserTag(user).String()}
err := c.facade.FacadeCall("ListModels", entity, &models)
if err != nil {
return nil, errors.Trace(err)
}
result := make([]base.UserModel, len(models.UserModels))
for i, model := range models.UserModels {
owner, err := names.ParseUserTag(model.OwnerTag)
if err != nil {
return nil, errors.Annotatef(err, "OwnerTag %q at position %d", model.OwnerTag, i)
}
result[i] = base.UserModel{
Name: model.Name,
UUID: model.UUID,
Owner: owner.Canonical(),
LastConnection: model.LastConnection,
}
}
return result, nil
}
示例3: ListEnvironments
// ListEnvironments returns the environments that the specified user
// has access to in the current server. Only that state server owner
// can list environments for any user (at this stage). Other users
// can only ask about their own environments.
func (c *Client) ListEnvironments(user string) ([]base.UserEnvironment, error) {
var environments params.UserEnvironmentList
if !names.IsValidUser(user) {
return nil, errors.Errorf("invalid user name %q", user)
}
entity := params.Entity{names.NewUserTag(user).String()}
err := c.facade.FacadeCall("ListEnvironments", entity, &environments)
if err != nil {
return nil, errors.Trace(err)
}
result := make([]base.UserEnvironment, len(environments.UserEnvironments))
for i, env := range environments.UserEnvironments {
owner, err := names.ParseUserTag(env.OwnerTag)
if err != nil {
return nil, errors.Annotatef(err, "OwnerTag %q at position %d", env.OwnerTag, i)
}
result[i] = base.UserEnvironment{
Name: env.Name,
UUID: env.UUID,
Owner: owner.Username(),
LastConnection: env.LastConnection,
}
}
return result, nil
}
示例4: writeServerFile
func writeServerFile(endpointProvider EndpointProvider, ctx *cmd.Context, username, password, outPath string) error {
outPath = ctx.AbsPath(outPath)
endpoint, err := endpointProvider.ConnectionEndpoint()
if err != nil {
return errors.Trace(err)
}
if !names.IsValidUser(username) {
return errors.Errorf("%q is not a valid username", username)
}
outputInfo := modelcmd.ServerFile{
Username: username,
Password: password,
Addresses: endpoint.Addresses,
CACert: endpoint.CACert,
}
yaml, err := cmd.FormatYaml(outputInfo)
if err != nil {
return errors.Trace(err)
}
if err := ioutil.WriteFile(outPath, yaml, 0644); err != nil {
return errors.Trace(err)
}
serverFileNotify(outPath)
ctx.Infof("server file written to %s", outPath)
return nil
}
示例5: Init
// SetFlags implements Command.Init.
func (c *useEnvironmentCommand) Init(args []string) error {
if len(args) == 0 || strings.TrimSpace(args[0]) == "" {
return errors.New("no environment supplied")
}
name, args := args[0], args[1:]
// First check to see if an owner has been specified.
bits := strings.SplitN(name, "/", 2)
switch len(bits) {
case 1:
// No user specified
c.EnvName = bits[0]
case 2:
owner := bits[0]
if names.IsValidUser(owner) {
c.Owner = owner
} else {
return errors.Errorf("%q is not a valid user", owner)
}
c.EnvName = bits[1]
}
// Environment names can generally be anything, but we take a good
// stab at trying to determine if the user has specified a UUID
// instead of a name. For now, we only accept a properly formatted UUID,
// which means one with dashes in the right place.
if names.IsValidEnvironment(c.EnvName) {
c.EnvUUID, c.EnvName = c.EnvName, ""
}
return cmd.CheckEmpty(args)
}
示例6: Init
func (c *JenvCommand) Init(args []string) error {
if len(args) == 0 {
return errors.New("no jenv file provided")
}
// Store the path to the jenv file.
if err := c.jenvFile.Set(args[0]); err != nil {
return errors.Annotate(err, "invalid jenv path")
}
args = args[1:]
if len(args) > 0 {
// Store and validate the provided environment name.
c.envName, args = args[0], args[1:]
if !names.IsValidUser(c.envName) {
return errors.Errorf("invalid model name %q", c.envName)
}
} else {
// Retrieve the environment name from the jenv file name.
base := filepath.Base(c.jenvFile.Path)
c.envName = base[:len(base)-len(filepath.Ext(base))]
}
// No other arguments are expected.
return cmd.CheckEmpty(args)
}
示例7: Init
func (c *createModelCommand) Init(args []string) error {
if len(args) == 0 {
return errors.New("model name is required")
}
c.Name, args = args[0], args[1:]
if c.Owner != "" && !names.IsValidUser(c.Owner) {
return errors.Errorf("%q is not a valid user", c.Owner)
}
if c.CredentialSpec != "" {
parts := strings.Split(c.CredentialSpec, ":")
if len(parts) < 2 {
return errors.Errorf("invalid cloud credential %s, expected <cloud>:<credential-name>", c.CredentialSpec)
}
c.CloudName = parts[0]
if cloud, err := common.CloudOrProvider(c.CloudName, cloud.CloudByName); err != nil {
return errors.Trace(err)
} else {
c.CloudType = cloud.Type
}
c.CredentialName = parts[1]
}
return nil
}
示例8: AddUser
// AddUser adds a user to the state.
func (st *State) AddUser(username, displayName, password, creator string) (*User, error) {
if !names.IsValidUser(username) {
return nil, errors.Errorf("invalid user name %q", username)
}
salt, err := utils.RandomSalt()
if err != nil {
return nil, err
}
timestamp := time.Now().Round(time.Second).UTC()
u := &User{
st: st,
doc: userDoc{
Name: username,
DisplayName: displayName,
PasswordHash: utils.UserPasswordHash(password, salt),
PasswordSalt: salt,
CreatedBy: creator,
DateCreated: timestamp,
},
}
ops := []txn.Op{{
C: usersC,
Id: username,
Assert: txn.DocMissing,
Insert: &u.doc,
}}
err = st.runTransaction(ops)
if err == txn.ErrAborted {
err = errors.New("user already exists")
}
if err != nil {
return nil, errors.Trace(err)
}
return u, nil
}
示例9: validateUser
func validateUser(name string) error {
if !names.IsValidUser(name) {
return errors.NotValidf("account name %q", name)
}
if tag := names.NewUserTag(name); tag.Id() != tag.Canonical() {
return errors.NotValidf("unqualified account name %q", name)
}
return nil
}
示例10: ListEnvironments
// ListEnvironments returns the environments that the specified user
// has access to in the current server. Only that state server owner
// can list environments for any user (at this stage). Other users
// can only ask about their own environments.
func (c *Client) ListEnvironments(user string) ([]params.Environment, error) {
var result params.EnvironmentList
if !names.IsValidUser(user) {
return nil, fmt.Errorf("invalid user name %q", user)
}
entity := params.Entity{names.NewUserTag(user).String()}
err := c.facade.FacadeCall("ListEnvironments", entity, &result)
if err != nil {
return nil, errors.Trace(err)
}
return result.Environments, nil
}
示例11: Init
// Init initializes the command for running.
func (c *BootstrapCommand) Init(args []string) error {
if len(c.EnvConfig) == 0 {
return cmdutil.RequiredError("model-config")
}
if c.InstanceId == "" {
return cmdutil.RequiredError("instance-id")
}
if !names.IsValidUser(c.AdminUsername) {
return errors.Errorf("%q is not a valid username", c.AdminUsername)
}
return c.AgentConf.CheckArgs(args)
}
示例12: Init
func (c *unshareCommand) Init(args []string) (err error) {
if len(args) == 0 {
return errors.New("no users specified")
}
for _, arg := range args {
if !names.IsValidUser(arg) {
return errors.Errorf("invalid username: %q", arg)
}
c.Users = append(c.Users, names.NewUserTag(arg))
}
return nil
}
示例13: AddUser
func (c *Client) AddUser(username, displayName, password string) error {
if !names.IsValidUser(username) {
return fmt.Errorf("invalid user name %q", username)
}
userArgs := usermanager.ModifyUsers{
Changes: []usermanager.ModifyUser{{Username: username, DisplayName: displayName, Password: password}},
}
results := new(params.ErrorResults)
err := c.facade.FacadeCall("AddUser", userArgs, results)
if err != nil {
return errors.Trace(err)
}
return results.OneError()
}
示例14: userCall
func (c *Client) userCall(username string, methodCall string) error {
if !names.IsValidUser(username) {
return errors.Errorf("%q is not a valid username", username)
}
tag := names.NewUserTag(username)
var results params.ErrorResults
args := params.Entities{
[]params.Entity{{tag.String()}},
}
err := c.facade.FacadeCall(methodCall, args, &results)
if err != nil {
return errors.Trace(err)
}
return results.OneError()
}
示例15: CreateModel
// CreateModel creates a new model using the account and
// model config specified in the args.
func (c *Client) CreateModel(owner string, account, config map[string]interface{}) (params.Model, error) {
var result params.Model
if !names.IsValidUser(owner) {
return result, errors.Errorf("invalid owner name %q", owner)
}
createArgs := params.ModelCreateArgs{
OwnerTag: names.NewUserTag(owner).String(),
Account: account,
Config: config,
}
err := c.facade.FacadeCall("CreateModel", createArgs, &result)
if err != nil {
return result, errors.Trace(err)
}
return result, nil
}