Skip to content

Commit

Permalink
api: use CheckTestType in compat and provisioning checkers
Browse files Browse the repository at this point in the history
Use the CheckTestType enum as the testType arg type instead of a string
in the VmCompatibilityChecker and VmProvisioningChecker functions.  The
req task structs end up getting auto generated using the string type so
we need to do a little unfortunate conversion.

Add check test type param to the govc commands
  • Loading branch information
bryanv committed Jul 19, 2024
1 parent 4ce273c commit 5c0af37
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions govc/vm/check/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (cmd *compat) Description() string {
return `Check if VM can be placed on the given HOST in the given resource POOL.
Examples:
govc vm.check.compat -vm my-vm -pool $pool`
govc vm.check.compat -vm my-vm -host $host -pool $pool`
}

func (cmd *compat) Run(ctx context.Context, f *flag.FlagSet) error {
Expand All @@ -53,7 +53,7 @@ func (cmd *compat) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

res, err := checker.CheckCompatibility(ctx, vm.Reference(), cmd.Host, cmd.Pool, cmd.Test...)
res, err := checker.CheckCompatibility(ctx, vm.Reference(), cmd.Host, cmd.Pool, cmd.testTypes...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion govc/vm/check/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (cmd *config) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

res, err := checker.CheckVmConfig(ctx, spec, cmd.Machine, cmd.Host, cmd.Pool, cmd.Test...)
res, err := checker.CheckVmConfig(ctx, spec, cmd.Machine, cmd.Host, cmd.Pool, cmd.testTypes...)
if err != nil {
return err
}
Expand Down
26 changes: 25 additions & 1 deletion govc/vm/check/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,36 @@ import (
"github.com/vmware/govmomi/vim25/xml"
)

var checkTestTypesList = []string{
string(types.CheckTestTypeDatastoreTests),
string(types.CheckTestTypeHostTests),
string(types.CheckTestTypeNetworkTests),
string(types.CheckTestTypeResourcePoolTests),
string(types.CheckTestTypeSourceTests),
}

type checkTestTypes []types.CheckTestType

func (c *checkTestTypes) String() string {
return fmt.Sprint(*c)
}

func (c *checkTestTypes) Set(value string) error {
if !slices.Contains(checkTestTypesList, value) {
return fmt.Errorf("invalid CheckTestType value %q", value)
}
*c = append(*c, types.CheckTestType(value))
return nil
}

type checkFlag struct {
*flags.VirtualMachineFlag
*flags.HostSystemFlag
*flags.ResourcePoolFlag

Machine, Host, Pool *types.ManagedObjectReference

Test []string
testTypes checkTestTypes
}

func (cmd *checkFlag) Register(ctx context.Context, f *flag.FlagSet) {
Expand All @@ -50,6 +72,8 @@ func (cmd *checkFlag) Register(ctx context.Context, f *flag.FlagSet) {
cmd.HostSystemFlag.Register(ctx, f)
cmd.ResourcePoolFlag, ctx = flags.NewResourcePoolFlag(ctx)
cmd.ResourcePoolFlag.Register(ctx, f)

f.Var(&cmd.testTypes, "test", fmt.Sprintf("The set of tests to run (%s)", strings.Join(checkTestTypesList, ",")))
}

func (cmd *checkFlag) Process(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion govc/vm/check/relocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (cmd *relocate) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

res, err := checker.CheckRelocate(ctx, vm.Reference(), spec, cmd.Test...)
res, err := checker.CheckRelocate(ctx, vm.Reference(), spec, cmd.testTypes...)
if err != nil {
return err
}
Expand Down
20 changes: 16 additions & 4 deletions object/vm_compatability_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func (c VmCompatibilityChecker) CheckCompatibility(
vm types.ManagedObjectReference,
host *types.ManagedObjectReference,
pool *types.ManagedObjectReference,
testTypes ...string) ([]types.CheckResult, error) {
testTypes ...types.CheckTestType) ([]types.CheckResult, error) {

req := types.CheckCompatibility_Task{
This: c.Reference(),
Vm: vm,
Host: host,
Pool: pool,
TestType: testTypes,
TestType: checkTestTypesToStrings(testTypes),
}

res, err := methods.CheckCompatibility_Task(ctx, c.c, &req)
Expand All @@ -74,15 +74,15 @@ func (c VmCompatibilityChecker) CheckVmConfig(
vm *types.ManagedObjectReference,
host *types.ManagedObjectReference,
pool *types.ManagedObjectReference,
testTypes ...string) ([]types.CheckResult, error) {
testTypes ...types.CheckTestType) ([]types.CheckResult, error) {

req := types.CheckVmConfig_Task{
This: c.Reference(),
Spec: spec,
Vm: vm,
Host: host,
Pool: pool,
TestType: testTypes,
TestType: checkTestTypesToStrings(testTypes),
}

res, err := methods.CheckVmConfig_Task(ctx, c.c, &req)
Expand All @@ -97,3 +97,15 @@ func (c VmCompatibilityChecker) CheckVmConfig(

return ti.Result.(types.ArrayOfCheckResult).CheckResult, nil
}

func checkTestTypesToStrings(testTypes []types.CheckTestType) []string {
if len(testTypes) == 0 {
return nil
}

s := make([]string, len(testTypes))
for i := range testTypes {
s[i] = string(testTypes[i])
}
return s
}
4 changes: 2 additions & 2 deletions object/vm_provisioning_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func (c VmProvisioningChecker) CheckRelocate(
ctx context.Context,
vm types.ManagedObjectReference,
spec types.VirtualMachineRelocateSpec,
testTypes ...string) ([]types.CheckResult, error) {
testTypes ...types.CheckTestType) ([]types.CheckResult, error) {

req := types.CheckRelocate_Task{
This: c.Reference(),
Vm: vm,
Spec: spec,
TestType: testTypes,
TestType: checkTestTypesToStrings(testTypes),
}

res, err := methods.CheckRelocate_Task(ctx, c.c, &req)
Expand Down

0 comments on commit 5c0af37

Please sign in to comment.