github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/space/add.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 "fmt" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 "github.com/juju/utils/set" 13 14 "github.com/juju/juju/cmd/modelcmd" 15 ) 16 17 // NewAddCommand returns a command used to add a network space. 18 func NewAddCommand() cmd.Command { 19 return modelcmd.Wrap(&addCommand{}) 20 } 21 22 // addCommand calls the API to add a new network space. 23 type addCommand struct { 24 SpaceCommandBase 25 Name string 26 CIDRs set.Strings 27 } 28 29 const addCommandDoc = ` 30 Adds a new space with the given name and associates the given 31 (optional) list of existing subnet CIDRs with it.` 32 33 // Info is defined on the cmd.Command interface. 34 func (c *addCommand) Info() *cmd.Info { 35 return &cmd.Info{ 36 Name: "add-space", 37 Args: "<name> [<CIDR1> <CIDR2> ...]", 38 Purpose: "Add a new network space", 39 Doc: strings.TrimSpace(addCommandDoc), 40 } 41 } 42 43 // Init is defined on the cmd.Command interface. It checks the 44 // arguments for sanity and sets up the command to run. 45 func (c *addCommand) Init(args []string) error { 46 var err error 47 c.Name, c.CIDRs, err = ParseNameAndCIDRs(args, true) 48 return err 49 } 50 51 // Run implements Command.Run. 52 func (c *addCommand) Run(ctx *cmd.Context) error { 53 return c.RunWithAPI(ctx, func(api SpaceAPI, ctx *cmd.Context) error { 54 // Prepare a nicer message and proper arguments to use in case 55 // there are not CIDRs given. 56 var subnetIds []string 57 msgSuffix := "no subnets" 58 if !c.CIDRs.IsEmpty() { 59 subnetIds = c.CIDRs.SortedValues() 60 msgSuffix = fmt.Sprintf("subnets %s", strings.Join(subnetIds, ", ")) 61 } 62 63 // Add the new space. 64 // TODO(dimitern): Accept --public|--private and pass it here. 65 err := api.AddSpace(c.Name, subnetIds, true) 66 if err != nil { 67 if errors.IsNotSupported(err) { 68 ctx.Infof("cannot add space %q: %v", c.Name, err) 69 } 70 return errors.Annotatef(err, "cannot add space %q", c.Name) 71 } 72 73 ctx.Infof("added space %q with %s", c.Name, msgSuffix) 74 return nil 75 }) 76 }