github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/subnet/remove.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package subnet
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/names"
    12  )
    13  
    14  // RemoveCommand calls the API to remove an existing, unused subnet
    15  // from Juju.
    16  type RemoveCommand struct {
    17  	SubnetCommandBase
    18  
    19  	CIDR names.SubnetTag
    20  }
    21  
    22  const removeCommandDoc = `
    23  Marks an existing subnet for removal. Depending on what features the
    24  cloud infrastructure supports, this command will either delete the
    25  subnet using the cloud API (if supported, e.g. in Amazon VPC) or just
    26  remove the subnet entity from Juju's database (with non-SDN substrates,
    27  e.g. MAAS). In other words "remove" acts like the opposite of "create"
    28  (if supported) or "add" (if "create" is not supported).
    29  
    30  If any machines are still using the subnet, it cannot be removed and
    31  an error is returned instead. If the subnet is not in use, it will be
    32  marked for removal, but it will not be removed from the Juju database
    33  until all related entites are cleaned up (e.g. allocated addresses).
    34  `
    35  
    36  // Info is defined on the cmd.Command interface.
    37  func (c *RemoveCommand) Info() *cmd.Info {
    38  	return &cmd.Info{
    39  		Name:    "remove",
    40  		Args:    "<CIDR>",
    41  		Purpose: "remove an existing subnet from Juju",
    42  		Doc:     strings.TrimSpace(removeCommandDoc),
    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 *RemoveCommand) Init(args []string) error {
    49  	// Ensure we have exactly 1 argument.
    50  	err := c.CheckNumArgs(args, []error{errNoCIDR})
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	// Validate given CIDR.
    56  	c.CIDR, err = c.ValidateCIDR(args[0], true)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	return cmd.CheckEmpty(args[1:])
    62  }
    63  
    64  // Run implements Command.Run.
    65  func (c *RemoveCommand) Run(ctx *cmd.Context) error {
    66  	return c.RunWithAPI(ctx, func(api SubnetAPI, ctx *cmd.Context) error {
    67  		// Try removing the subnet.
    68  		if err := api.RemoveSubnet(c.CIDR); err != nil {
    69  			return errors.Annotatef(err, "cannot remove subnet %q", c.CIDR.Id())
    70  		}
    71  
    72  		ctx.Infof("marked subnet %q for removal", c.CIDR.Id())
    73  		return nil
    74  	})
    75  }