func (c *BootstrapCommand) Init(args []string) (err error) { if len(c.Series) > 0 && !c.UploadTools { return fmt.Errorf("--upload-series requires --upload-tools") } if len(c.seriesOld) > 0 && !c.UploadTools { return fmt.Errorf("--series requires --upload-tools") } if len(c.Series) > 0 && len(c.seriesOld) > 0 { return fmt.Errorf("--upload-series and --series can't be used together") } if len(c.seriesOld) > 0 { c.Series = c.seriesOld } // Parse the placement directive. Bootstrap currently only // supports provider-specific placement directives. if c.Placement != "" { _, err = instance.ParsePlacement(c.Placement) if err != instance.ErrPlacementScopeMissing { // We only support unscoped placement directives for bootstrap. return fmt.Errorf("unsupported bootstrap placement directive %q", c.Placement) } } return cmd.CheckEmpty(args) }
func (c *AddMachineCommand) Init(args []string) error { if c.Constraints.Container != nil { return fmt.Errorf("container constraint %q not allowed when adding a machine", *c.Constraints.Container) } placement, err := cmd.ZeroOrOneArgs(args) if err != nil { return err } c.Placement, err = instance.ParsePlacement(placement) if err == instance.ErrPlacementScopeMissing { placement = c.EnvName + ":" + placement c.Placement, err = instance.ParsePlacement(placement) } if err != nil { return err } return nil }
func (s *PlacementSuite) TestParsePlacement(c *gc.C) { parsePlacementTests := []struct { arg string expectScope, expectDirective string err string }{{ arg: "", }, { arg: "0", expectScope: instance.MachineScope, expectDirective: "0", }, { arg: "0/lxc/0", expectScope: instance.MachineScope, expectDirective: "0/lxc/0", }, { arg: "#:x", err: `invalid value "x" for "#" scope: expected machine-id`, }, { arg: "lxc:x", err: `invalid value "x" for "lxc" scope: expected machine-id`, }, { arg: "kvm:x", err: `invalid value "x" for "kvm" scope: expected machine-id`, }, { arg: "kvm:123", expectScope: string(instance.KVM), expectDirective: "123", }, { arg: "lxc", expectScope: string(instance.LXC), }, { arg: "non-standard", err: "placement scope missing", }, { arg: ":non-standard", err: "placement scope missing", }, { arg: "non:standard", expectScope: "non", expectDirective: "standard", }} for i, t := range parsePlacementTests { c.Logf("test %d: %s", i, t.arg) p, err := instance.ParsePlacement(t.arg) if t.err != "" { c.Assert(err, gc.ErrorMatches, t.err) } else { c.Assert(err, gc.IsNil) } if t.expectScope == "" && t.expectDirective == "" { c.Assert(p, gc.IsNil) } else { c.Assert(p, gc.DeepEquals, &instance.Placement{ Scope: t.expectScope, Directive: t.expectDirective, }) } } }