github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"gopkg.in/juju/names.v2"
    12  
    13  	jujucmd "github.com/juju/juju/cmd"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  )
    16  
    17  // NewRemoveCommand returns a command used to remove an unused subnet from Juju.
    18  func NewRemoveCommand() modelcmd.ModelCommand {
    19  	return modelcmd.Wrap(&RemoveCommand{})
    20  }
    21  
    22  // RemoveCommand calls the API to remove an existing, unused subnet
    23  // from Juju.
    24  type RemoveCommand struct {
    25  	SubnetCommandBase
    26  
    27  	CIDR names.SubnetTag
    28  }
    29  
    30  const removeCommandDoc = `
    31  Marks an existing subnet for removal. Depending on what features the
    32  cloud infrastructure supports, this command will either delete the
    33  subnet using the cloud API (if supported, e.g. in Amazon VPC) or just
    34  remove the subnet entity from Juju's database (with non-SDN substrates,
    35  e.g. MAAS). In other words "remove" acts like the opposite of "create"
    36  (if supported) or "add" (if "create" is not supported).
    37  
    38  If any machines are still using the subnet, it cannot be removed and
    39  an error is returned instead. If the subnet is not in use, it will be
    40  marked for removal, but it will not be removed from the Juju database
    41  until all related entites are cleaned up (e.g. allocated addresses).
    42  `
    43  
    44  // Info is defined on the cmd.Command interface.
    45  func (c *RemoveCommand) Info() *cmd.Info {
    46  	return jujucmd.Info(&cmd.Info{
    47  		Name:    "remove-subnet",
    48  		Args:    "<CIDR>",
    49  		Purpose: "remove an existing subnet from Juju",
    50  		Doc:     strings.TrimSpace(removeCommandDoc),
    51  	})
    52  }
    53  
    54  // Init is defined on the cmd.Command interface. It checks the
    55  // arguments for sanity and sets up the command to run.
    56  func (c *RemoveCommand) Init(args []string) error {
    57  	// Ensure we have exactly 1 argument.
    58  	err := c.CheckNumArgs(args, []error{errNoCIDR})
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	// Validate given CIDR.
    64  	c.CIDR, err = c.ValidateCIDR(args[0], true)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	return cmd.CheckEmpty(args[1:])
    70  }
    71  
    72  // Run implements Command.Run.
    73  func (c *RemoveCommand) Run(ctx *cmd.Context) error {
    74  	return c.RunWithAPI(ctx, func(api SubnetAPI, ctx *cmd.Context) error {
    75  		// Try removing the subnet.
    76  		if err := api.RemoveSubnet(c.CIDR); err != nil {
    77  			return errors.Annotatef(err, "cannot remove subnet %q", c.CIDR.Id())
    78  		}
    79  
    80  		ctx.Infof("marked subnet %q for removal", c.CIDR.Id())
    81  		return nil
    82  	})
    83  }