github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/space/add.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package space
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/collections/set"
    12  	"github.com/juju/errors"
    13  
    14  	"github.com/juju/juju/apiserver/params"
    15  	jujucmd "github.com/juju/juju/cmd"
    16  	"github.com/juju/juju/cmd/juju/common"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  )
    19  
    20  // NewAddCommand returns a command used to add a network space.
    21  func NewAddCommand() modelcmd.ModelCommand {
    22  	return modelcmd.Wrap(&AddCommand{})
    23  }
    24  
    25  // AddCommand calls the API to add a new network space.
    26  type AddCommand struct {
    27  	SpaceCommandBase
    28  	Name  string
    29  	CIDRs set.Strings
    30  }
    31  
    32  const addCommandDoc = `
    33  Adds a new space with the given name and associates the given
    34  (optional) list of existing subnet CIDRs with it.`
    35  
    36  // Info is defined on the cmd.Command interface.
    37  func (c *AddCommand) Info() *cmd.Info {
    38  	return jujucmd.Info(&cmd.Info{
    39  		Name:    "add-space",
    40  		Args:    "<name> [<CIDR1> <CIDR2> ...]",
    41  		Purpose: "Add a new network space.",
    42  		Doc:     strings.TrimSpace(addCommandDoc),
    43  	})
    44  }
    45  
    46  // Init is defined on the cmd.Command interface. It checks the
    47  // arguments for sanity and sets up the command to run.
    48  func (c *AddCommand) Init(args []string) error {
    49  	var err error
    50  	c.Name, c.CIDRs, err = ParseNameAndCIDRs(args, true)
    51  	return err
    52  }
    53  
    54  // Run implements Command.Run.
    55  func (c *AddCommand) Run(ctx *cmd.Context) error {
    56  	return c.RunWithAPI(ctx, func(api SpaceAPI, ctx *cmd.Context) error {
    57  		// Prepare a nicer message and proper arguments to use in case
    58  		// there are not CIDRs given.
    59  		var subnetIds []string
    60  		msgSuffix := "no subnets"
    61  		if !c.CIDRs.IsEmpty() {
    62  			subnetIds = c.CIDRs.SortedValues()
    63  			msgSuffix = fmt.Sprintf("subnets %s", strings.Join(subnetIds, ", "))
    64  		}
    65  
    66  		// Add the new space.
    67  		// TODO(dimitern): Accept --public|--private and pass it here.
    68  		err := api.AddSpace(c.Name, subnetIds, true)
    69  		if err != nil {
    70  			if errors.IsNotSupported(err) {
    71  				ctx.Infof("cannot add space %q: %v", c.Name, err)
    72  			}
    73  			if params.IsCodeUnauthorized(err) {
    74  				common.PermissionsMessage(ctx.Stderr, "add a space")
    75  			}
    76  			return errors.Annotatef(err, "cannot add space %q", c.Name)
    77  		}
    78  
    79  		ctx.Infof("added space %q with %s", c.Name, msgSuffix)
    80  		return nil
    81  	})
    82  }