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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environment
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/names"
    12  
    13  	"github.com/juju/juju/cmd/envcmd"
    14  	"github.com/juju/juju/cmd/juju/block"
    15  )
    16  
    17  const unshareEnvHelpDoc = `
    18  Deny a user access to an environment that was previously shared with them.
    19  
    20  Examples:
    21   juju environment unshare joe
    22       Deny local user "joe" access to the current environment
    23  
    24   juju environment unshare user1 user2 user3@ubuntuone
    25       Deny two local users and one remote user access to the current environment
    26  
    27   juju environment unshare sam -e/--environment myenv
    28       Deny local user "sam" access to the environment named "myenv"
    29   `
    30  
    31  // UnshareCommand unshares an environment with the given user(s).
    32  type UnshareCommand struct {
    33  	envcmd.EnvCommandBase
    34  	cmd.CommandBase
    35  	envName string
    36  	api     UnshareEnvironmentAPI
    37  
    38  	// Users to unshare the environment with.
    39  	Users []names.UserTag
    40  }
    41  
    42  func (c *UnshareCommand) Info() *cmd.Info {
    43  	return &cmd.Info{
    44  		Name:    "unshare",
    45  		Args:    "<user> ...",
    46  		Purpose: "unshare the current environment with a user",
    47  		Doc:     strings.TrimSpace(unshareEnvHelpDoc),
    48  	}
    49  }
    50  
    51  func (c *UnshareCommand) Init(args []string) (err error) {
    52  	if len(args) == 0 {
    53  		return errors.New("no users specified")
    54  	}
    55  
    56  	for _, arg := range args {
    57  		if !names.IsValidUser(arg) {
    58  			return errors.Errorf("invalid username: %q", arg)
    59  		}
    60  		c.Users = append(c.Users, names.NewUserTag(arg))
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func (c *UnshareCommand) getAPI() (UnshareEnvironmentAPI, error) {
    67  	if c.api != nil {
    68  		return c.api, nil
    69  	}
    70  	return c.NewAPIClient()
    71  }
    72  
    73  // UnshareEnvironmentAPI defines the API functions used by the environment
    74  // unshare command.
    75  type UnshareEnvironmentAPI interface {
    76  	Close() error
    77  	UnshareEnvironment(...names.UserTag) error
    78  }
    79  
    80  func (c *UnshareCommand) Run(ctx *cmd.Context) error {
    81  	client, err := c.getAPI()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	defer client.Close()
    86  
    87  	return block.ProcessBlockedError(client.UnshareEnvironment(c.Users...), block.BlockChange)
    88  }