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