Skip to content

Commit

Permalink
Host is optional for MarkAsVirtualMachine (#675)
Browse files Browse the repository at this point in the history
The vm.markasvm command had required the host flag,
but it is optional in the API.
Add the pool flag to avoid specifying a host.
  • Loading branch information
dougm authored Mar 1, 2017
1 parent f4a3ffe commit b4ef3b7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
18 changes: 11 additions & 7 deletions govc/vm/markastemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

type markastemplate struct {
*flags.ClientFlag
*flags.SearchFlag
}

Expand All @@ -34,23 +33,28 @@ func init() {
}

func (cmd *markastemplate) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

cmd.SearchFlag, ctx = flags.NewSearchFlag(ctx, flags.SearchVirtualMachines)
cmd.SearchFlag.Register(ctx, f)
}

func (cmd *markastemplate) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
return err
}
if err := cmd.SearchFlag.Process(ctx); err != nil {
return err
}
return nil
}

func (cmd *markastemplate) Usage() string {
return "VM..."
}

func (cmd *markastemplate) Description() string {
return `Mark VM as a virtual machine template.
Examples:
govc vm.markastemplate $name`
}

func (cmd *markastemplate) Run(ctx context.Context, f *flag.FlagSet) error {
vms, err := cmd.VirtualMachines(f.Args())
if err != nil {
Expand Down
47 changes: 35 additions & 12 deletions govc/vm/markasvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,32 @@ import (

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/object"
)

type markasvm struct {
*flags.ClientFlag
*flags.SearchFlag
*flags.HostSystemFlag
*flags.ResourcePoolFlag
HostSystem *object.HostSystem
ResourcePool *object.ResourcePool
*flags.HostSystemFlag
}

func init() {
cli.Register("vm.markasvm", &markasvm{})
}

func (cmd *markasvm) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
cmd.SearchFlag, ctx = flags.NewSearchFlag(ctx, flags.SearchVirtualMachines)
cmd.SearchFlag.Register(ctx, f)
cmd.ResourcePoolFlag, ctx = flags.NewResourcePoolFlag(ctx)
cmd.ResourcePoolFlag.Register(ctx, f)
cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
cmd.HostSystemFlag.Register(ctx, f)
}

func (cmd *markasvm) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
if err := cmd.SearchFlag.Process(ctx); err != nil {
return err
}
if err := cmd.SearchFlag.Process(ctx); err != nil {
if err := cmd.ResourcePoolFlag.Process(ctx); err != nil {
return err
}
if err := cmd.HostSystemFlag.Process(ctx); err != nil {
Expand All @@ -60,24 +56,51 @@ func (cmd *markasvm) Process(ctx context.Context) error {
return nil
}

func (cmd *markasvm) Usage() string {
return "VM..."
}

func (cmd *markasvm) Description() string {
return `Mark VM template as a virtual machine.
Examples:
govc vm.markasvm $name -host host1
govc vm.markasvm $name -pool cluster1/Resources`
}

func (cmd *markasvm) Run(ctx context.Context, f *flag.FlagSet) error {
vms, err := cmd.VirtualMachines(f.Args())
if err != nil {
return err
}
cmd.HostSystem, err = cmd.HostSystemFlag.HostSystem()

pool, err := cmd.ResourcePoolIfSpecified()
if err != nil {
return err
}
cmd.ResourcePool, err = cmd.HostSystem.ResourcePool(ctx)

host, err := cmd.HostSystemFlag.HostSystemIfSpecified()
if err != nil {
return err
}

if pool == nil {
if host == nil {
return flag.ErrHelp
}

pool, err = host.ResourcePool(ctx)
if err != nil {
return err
}
}

for _, vm := range vms {
err := vm.MarkAsVirtualMachine(ctx, *cmd.ResourcePool, cmd.HostSystem)
err := vm.MarkAsVirtualMachine(ctx, *pool, host)
if err != nil {
return err
}
}

return nil
}

0 comments on commit b4ef3b7

Please sign in to comment.