github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/subnet/add.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/juju/network" 12 "github.com/juju/names" 13 14 "github.com/juju/juju/cmd/modelcmd" 15 ) 16 17 // NewAddCommand returns a command used to add an existing subnet to Juju. 18 func NewAddCommand() cmd.Command { 19 return modelcmd.Wrap(&addCommand{}) 20 } 21 22 // addCommand calls the API to add an existing subnet to Juju. 23 type addCommand struct { 24 SubnetCommandBase 25 26 CIDR names.SubnetTag 27 RawCIDR string // before normalizing (e.g. 10.10.0.0/8 to 10.0.0.0/8) 28 ProviderId string 29 Space names.SpaceTag 30 Zones []string 31 } 32 33 const addCommandDoc = ` 34 Adds an existing subnet to Juju, making it available for use. Unlike 35 "juju create-subnet", this command does not create a new subnet, so it 36 is supported on a wider variety of clouds (where SDN features are not 37 available, e.g. MAAS). The subnet will be associated with the given 38 existing Juju network space. 39 40 Subnets can be referenced by either their CIDR or ProviderId (if the 41 provider supports it). If CIDR is used an multiple subnets have the 42 same CIDR, an error will be returned, including the list of possible 43 provider IDs uniquely identifying each subnet. 44 45 Any availablility zones associated with the added subnet are automatically 46 discovered using the cloud API (if supported). If this is not possible, 47 since any subnet needs to be part of at least one zone, specifying 48 zone(s) is required. 49 ` 50 51 // Info is defined on the cmd.Command interface. 52 func (c *addCommand) Info() *cmd.Info { 53 return &cmd.Info{ 54 Name: "add-subnet", 55 Args: "<CIDR>|<provider-id> <space> [<zone1> <zone2> ...]", 56 Purpose: "add an existing subnet to Juju", 57 Doc: strings.TrimSpace(addCommandDoc), 58 } 59 } 60 61 // Init is defined on the cmd.Command interface. It checks the 62 // arguments for sanity and sets up the command to run. 63 func (c *addCommand) Init(args []string) (err error) { 64 defer errors.DeferredAnnotatef(&err, "invalid arguments specified") 65 66 // Ensure we have 2 or more arguments. 67 switch len(args) { 68 case 0: 69 return errNoCIDROrID 70 case 1: 71 return errNoSpace 72 } 73 74 // Try to validate first argument as a CIDR first. 75 c.RawCIDR = args[0] 76 c.CIDR, err = c.ValidateCIDR(args[0], false) 77 if err != nil { 78 // If it's not a CIDR it could be a ProviderId, so ignore the 79 // error. 80 c.ProviderId = args[0] 81 c.RawCIDR = "" 82 } 83 84 // Validate the space name. 85 c.Space, err = c.ValidateSpace(args[1]) 86 if err != nil { 87 return err 88 } 89 90 // Add any given zones. 91 for _, zone := range args[2:] { 92 c.Zones = append(c.Zones, zone) 93 } 94 return nil 95 } 96 97 // Run implements Command.Run. 98 func (c *addCommand) Run(ctx *cmd.Context) error { 99 return c.RunWithAPI(ctx, func(api SubnetAPI, ctx *cmd.Context) error { 100 if c.CIDR.Id() != "" && c.RawCIDR != c.CIDR.Id() { 101 ctx.Infof( 102 "WARNING: using CIDR %q instead of the incorrectly specified %q.", 103 c.CIDR.Id(), c.RawCIDR, 104 ) 105 } 106 107 // Add the existing subnet. 108 err := api.AddSubnet(c.CIDR, network.Id(c.ProviderId), c.Space, c.Zones) 109 // TODO(dimitern): Change this once the API returns a concrete error. 110 if err != nil && strings.Contains(err.Error(), "multiple subnets with") { 111 // Special case: multiple subnets with the same CIDR exist 112 ctx.Infof("ERROR: %v.", err) 113 return nil 114 } else if err != nil { 115 return errors.Annotatef(err, "cannot add subnet") 116 } 117 118 if c.ProviderId != "" { 119 ctx.Infof("added subnet with ProviderId %q in space %q", c.ProviderId, c.Space.Id()) 120 } else { 121 ctx.Infof("added subnet with CIDR %q in space %q", c.CIDR.Id(), c.Space.Id()) 122 } 123 return nil 124 }) 125 }