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