github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/user/add.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package user
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/names"
    12  	"github.com/juju/utils"
    13  	"launchpad.net/gnuflag"
    14  
    15  	"github.com/juju/juju/cmd/juju/block"
    16  )
    17  
    18  const userAddCommandDoc = `
    19  Add users to an existing environment.
    20  
    21  The user information is stored within an existing environment, and will be
    22  lost when the environent is destroyed.  A server file will be written out in
    23  the current directory.  You can control the name and location of this file
    24  using the --output option.
    25  
    26  Examples:
    27      # Add user "foobar" with a strong random password is generated.
    28      juju user add foobar
    29  
    30  
    31  See Also:
    32      juju help user change-password
    33  `
    34  
    35  // AddUserAPI defines the usermanager API methods that the add command uses.
    36  type AddUserAPI interface {
    37  	AddUser(username, displayName, password string) (names.UserTag, error)
    38  	Close() error
    39  }
    40  
    41  // AddCommand adds new users into a Juju Server.
    42  type AddCommand struct {
    43  	UserCommandBase
    44  	api         AddUserAPI
    45  	User        string
    46  	DisplayName string
    47  	OutPath     string
    48  }
    49  
    50  // Info implements Command.Info.
    51  func (c *AddCommand) Info() *cmd.Info {
    52  	return &cmd.Info{
    53  		Name:    "add",
    54  		Args:    "<username> [<display name>]",
    55  		Purpose: "adds a user",
    56  		Doc:     userAddCommandDoc,
    57  	}
    58  }
    59  
    60  // SetFlags implements Command.SetFlags.
    61  func (c *AddCommand) SetFlags(f *gnuflag.FlagSet) {
    62  	f.StringVar(&c.OutPath, "o", "", "specify the environment file for new user")
    63  	f.StringVar(&c.OutPath, "output", "", "")
    64  }
    65  
    66  // Init implements Command.Init.
    67  func (c *AddCommand) Init(args []string) error {
    68  	if len(args) == 0 {
    69  		return fmt.Errorf("no username supplied")
    70  	}
    71  	c.User, args = args[0], args[1:]
    72  	if len(args) > 0 {
    73  		c.DisplayName, args = args[0], args[1:]
    74  	}
    75  	if c.OutPath == "" {
    76  		c.OutPath = c.User + ".server"
    77  	}
    78  	return cmd.CheckEmpty(args)
    79  }
    80  
    81  // Run implements Command.Run.
    82  func (c *AddCommand) Run(ctx *cmd.Context) error {
    83  	if c.api == nil {
    84  		api, err := c.NewUserManagerAPIClient()
    85  		if err != nil {
    86  			return errors.Trace(err)
    87  		}
    88  		c.api = api
    89  		defer c.api.Close()
    90  	}
    91  
    92  	password, err := utils.RandomPassword()
    93  	if err != nil {
    94  		return errors.Annotate(err, "failed to generate random password")
    95  	}
    96  	randomPasswordNotify(password)
    97  
    98  	if _, err := c.api.AddUser(c.User, c.DisplayName, password); err != nil {
    99  		return block.ProcessBlockedError(err, block.BlockChange)
   100  	}
   101  
   102  	displayName := c.User
   103  	if c.DisplayName != "" {
   104  		displayName = fmt.Sprintf("%s (%s)", c.DisplayName, c.User)
   105  	}
   106  
   107  	ctx.Infof("user %q added", displayName)
   108  
   109  	return writeServerFile(c, ctx, c.User, password, c.OutPath)
   110  }