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