github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/space/update.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  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/utils/set"
    12  
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  )
    15  
    16  // NewUpdateCommand returns a command used to update subnets in a space.
    17  func NewUpdateCommand() cmd.Command {
    18  	return modelcmd.Wrap(&updateCommand{})
    19  }
    20  
    21  // updateCommand calls the API to update an existing network space.
    22  type updateCommand struct {
    23  	SpaceCommandBase
    24  	Name  string
    25  	CIDRs set.Strings
    26  }
    27  
    28  const updateCommandDoc = `
    29  Replaces the list of associated subnets of the space. Since subnets
    30  can only be part of a single space, all specified subnets (using their
    31  CIDRs) "leave" their current space and "enter" the one we're updating.
    32  `
    33  
    34  // Info is defined on the cmd.Command interface.
    35  func (c *updateCommand) Info() *cmd.Info {
    36  	return &cmd.Info{
    37  		Name:    "update-space",
    38  		Args:    "<name> <CIDR1> [ <CIDR2> ...]",
    39  		Purpose: "Update a network space's CIDRs",
    40  		Doc:     strings.TrimSpace(updateCommandDoc),
    41  	}
    42  }
    43  
    44  // Init is defined on the cmd.Command interface. It checks the
    45  // arguments for sanity and sets up the command to run.
    46  func (c *updateCommand) Init(args []string) error {
    47  	var err error
    48  	c.Name, c.CIDRs, err = ParseNameAndCIDRs(args, false)
    49  	return errors.Trace(err)
    50  }
    51  
    52  // Run implements Command.Run.
    53  func (c *updateCommand) Run(ctx *cmd.Context) error {
    54  	return c.RunWithAPI(ctx, func(api SpaceAPI, ctx *cmd.Context) error {
    55  		// Update the space.
    56  		err := api.UpdateSpace(c.Name, c.CIDRs.SortedValues())
    57  		if err != nil {
    58  			return errors.Annotatef(err, "cannot update space %q", c.Name)
    59  		}
    60  
    61  		ctx.Infof("updated space %q: changed subnets to %s", c.Name, strings.Join(c.CIDRs.SortedValues(), ", "))
    62  		return nil
    63  	})
    64  }