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