github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/collections/set"
    11  	"github.com/juju/errors"
    12  
    13  	jujucmd "github.com/juju/juju/cmd"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  )
    16  
    17  // NewUpdateCommand returns a command used to update subnets in a space.
    18  func NewUpdateCommand() modelcmd.ModelCommand {
    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  // Info is defined on the cmd.Command interface.
    36  func (c *UpdateCommand) Info() *cmd.Info {
    37  	return jujucmd.Info(&cmd.Info{
    38  		Name:    "update-space",
    39  		Args:    "<name> <CIDR1> [ <CIDR2> ...]",
    40  		Purpose: "Update a network space's CIDRs",
    41  		Doc:     strings.TrimSpace(updateCommandDoc),
    42  	})
    43  }
    44  
    45  // Init is defined on the cmd.Command interface. It checks the
    46  // arguments for sanity and sets up the command to run.
    47  func (c *UpdateCommand) Init(args []string) error {
    48  	var err error
    49  	c.Name, c.CIDRs, err = ParseNameAndCIDRs(args, false)
    50  	return errors.Trace(err)
    51  }
    52  
    53  // Run implements Command.Run.
    54  func (c *UpdateCommand) Run(ctx *cmd.Context) error {
    55  	return c.RunWithAPI(ctx, func(api SpaceAPI, ctx *cmd.Context) error {
    56  		// Update the space.
    57  		err := api.UpdateSpace(c.Name, c.CIDRs.SortedValues())
    58  		if err != nil {
    59  			return errors.Annotatef(err, "cannot update space %q", c.Name)
    60  		}
    61  
    62  		ctx.Infof("updated space %q: changed subnets to %s", c.Name, strings.Join(c.CIDRs.SortedValues(), ", "))
    63  		return nil
    64  	})
    65  }