Skip to content

Commit

Permalink
govc: add '-spec' flag to vm clone, migrate and create commands
Browse files Browse the repository at this point in the history
This flag is only enabled when GOVC_SHOW_UNRELEASED=true
  • Loading branch information
dougm committed Jul 15, 2024
1 parent d92015c commit 976bca3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
24 changes: 24 additions & 0 deletions govc/flags/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/dougm/pretty"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/task"
"github.com/vmware/govmomi/vim25/progress"
"github.com/vmware/govmomi/vim25/soap"
Expand All @@ -49,6 +50,7 @@ type OutputFlag struct {
TTY bool
Dump bool
Out io.Writer
Spec bool

formatError bool
formatIndent bool
Expand All @@ -71,6 +73,9 @@ func (flag *OutputFlag) Register(ctx context.Context, f *flag.FlagSet) {
f.BoolVar(&flag.JSON, "json", false, "Enable JSON output")
f.BoolVar(&flag.XML, "xml", false, "Enable XML output")
f.BoolVar(&flag.Dump, "dump", false, "Enable Go output")
if cli.ShowUnreleased() {
f.BoolVar(&flag.Spec, "spec", false, "Output spec without sending request")
}
// Avoid adding more flags for now..
flag.formatIndent = os.Getenv("GOVC_INDENT") != "false" // Default to indented output
flag.formatError = os.Getenv("GOVC_FORMAT_ERROR") != "false" // Default to formatted errors
Expand Down Expand Up @@ -158,6 +163,25 @@ func dumpValue(val interface{}) interface{} {
return val
}

type outputAny struct {
Value any
}

func (*outputAny) Write(io.Writer) error {
return nil
}

func (a *outputAny) Dump() interface{} {
return a.Value
}

func (flag *OutputFlag) WriteAny(val any) error {
if !flag.All() {
flag.XML = true
}
return flag.WriteResult(&outputAny{val})
}

func (flag *OutputFlag) WriteResult(result OutputWriter) error {
var err error

Expand Down
11 changes: 9 additions & 2 deletions govc/vm/clone.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2014-2016 VMware, Inc. All Rights Reserved.
Copyright (c) 2016-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -243,6 +243,9 @@ func (cmd *clone) Run(ctx context.Context, f *flag.FlagSet) error {
if err != nil {
return err
}
if cmd.Spec {
return nil
}

if cmd.cpus > 0 || cmd.memory > 0 || cmd.annotation != "" {
vmConfigSpec := types.VirtualMachineConfigSpec{}
Expand Down Expand Up @@ -471,6 +474,10 @@ func (cmd *clone) cloneVM(ctx context.Context) (*object.VirtualMachine, error) {
cloneSpec.Customization = &customSpec
}

if cmd.Spec {
return nil, cmd.WriteAny(cloneSpec)
}

task, err := cmd.VirtualMachine.Clone(ctx, cmd.Folder, cmd.name, *cloneSpec)
if err != nil {
return nil, err
Expand Down
8 changes: 6 additions & 2 deletions govc/vm/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
if err != nil {
return err
}
if cmd.place {
if cmd.place || cmd.Spec {
return nil
}
info, err := task.WaitForResult(ctx, nil)
Expand Down Expand Up @@ -490,7 +490,7 @@ func (cmd *create) createVM(ctx context.Context) (*object.Task, error) {
return nil, fmt.Errorf("please provide either a cluster, datastore or datastore-cluster")
}

if !cmd.force {
if !cmd.force && !cmd.Spec {
vmxPath := fmt.Sprintf("%s/%s.vmx", cmd.name, cmd.name)

_, err := datastore.Stat(ctx, vmxPath)
Expand All @@ -506,6 +506,10 @@ func (cmd *create) createVM(ctx context.Context) (*object.Task, error) {
VmPathName: fmt.Sprintf("[%s]", datastore.Name()),
}

if cmd.Spec {
return nil, cmd.WriteAny(spec)
}

return folder.CreateVM(ctx, *spec, cmd.ResourcePool, cmd.HostSystem)
}

Expand Down
8 changes: 6 additions & 2 deletions govc/vm/migrate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Copyright (c) 2016-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -127,6 +127,10 @@ func (cmd *migrate) relocate(ctx context.Context, vm *object.VirtualMachine) err
})
}

if cmd.VirtualMachineFlag.Spec {
return cmd.VirtualMachineFlag.WriteAny(spec)
}

task, err := vm.Relocate(ctx, spec, cmd.priority)
if err != nil {
return err
Expand Down

0 comments on commit 976bca3

Please sign in to comment.