Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Use sentinel not found error in GetVirtualMachine() #894

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions pkg/providers/vsphere/vcenter/getvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import (
pkgctx "github.com/vmware-tanzu/vm-operator/pkg/context"
)

type getVMNotFoundError struct{}

func (n getVMNotFoundError) Error() string {
return "vm not found"
}
var errVMNotFound = errors.New("vm not found")

// GetVirtualMachine gets the VM from VC, either by the Instance UUID, BIOS UUID, or MoID.
func GetVirtualMachine(
Expand All @@ -34,7 +30,7 @@ func GetVirtualMachine(
if id := vmCtx.VM.UID; id != "" {
if vm, err := findVMByUUID(vmCtx, vimClient, datacenter, string(id), true); err == nil {
return vm, nil
} else if !errors.Is(err, getVMNotFoundError{}) {
} else if !errors.Is(err, errVMNotFound) {
return nil, err
}
}
Expand All @@ -43,7 +39,7 @@ func GetVirtualMachine(
if id := vmCtx.VM.Spec.BiosUUID; id != "" {
if vm, err := findVMByUUID(vmCtx, vimClient, datacenter, id, false); err == nil {
return vm, nil
} else if !errors.Is(err, getVMNotFoundError{}) {
} else if !errors.Is(err, errVMNotFound) {
return nil, err
}
}
Expand All @@ -52,7 +48,7 @@ func GetVirtualMachine(
if id := vmCtx.VM.Status.UniqueID; id != "" {
if vm, err := findVMByMoID(vmCtx, vimClient, id); err == nil {
return vm, nil
} else if !errors.Is(err, getVMNotFoundError{}) {
} else if !errors.Is(err, errVMNotFound) {
return nil, err
}
}
Expand All @@ -74,7 +70,7 @@ func findVMByMoID(
if err := property.DefaultCollector(vimClient).RetrieveOne(vmCtx, moRef, []string{"name"}, &vm); err != nil {
var f *vimtypes.ManagedObjectNotFound
if _, ok := fault.As(err, &f); ok {
return nil, getVMNotFoundError{}
return nil, errVMNotFound
}
return nil, fmt.Errorf("error retreiving VM via MoID: %w", err)
}
Expand All @@ -94,7 +90,7 @@ func findVMByUUID(
if err != nil {
return nil, fmt.Errorf("error finding VM by UUID %q: %w", uuid, err)
} else if ref == nil {
return nil, getVMNotFoundError{}
return nil, errVMNotFound
}

vm, ok := ref.(*object.VirtualMachine)
Expand Down
Loading