retry error handling
Some checks are pending
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
CI / Lint (push) Waiting to run
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-13 09:50:51 +10:00
parent 424474f4fd
commit 2de8cf593c
2 changed files with 16 additions and 11 deletions

View File

@@ -75,7 +75,7 @@ func (v *Vcenter) Login(vUrl string) error {
v.client = c
v.Logger.Debug("successfully connected to vCenter")
v.Logger.Debug("successfully connected to vCenter", "url", vUrl, "username", username)
return nil
}
@@ -115,6 +115,9 @@ func (v *Vcenter) FindVMByName(vmName string) ([]mo.VirtualMachine, error) {
}
func (v *Vcenter) FindVMByID(vmID string) (*VmProperties, error) {
v.Logger.Debug("searching for vm id", "vm_id", vmID)
finder := find.NewFinder(v.client.Client, true)
// List all datacenters
@@ -138,18 +141,19 @@ func (v *Vcenter) FindVMByID(vmID string) (*VmProperties, error) {
var vm mo.VirtualMachine
err := v.client.RetrieveOne(v.ctx, vmRef, []string{"config", "name"}, &vm)
if err != nil {
if err == nil {
return &VmProperties{
Datacenter: dc.Name(),
Vm: vm,
}, nil
} else if _, ok := err.(*find.NotFoundError); !ok {
// If the error is not a NotFoundError, return it
if err == err.(*find.NotFoundError) {
return nil, fmt.Errorf("failed to retrieve VM with ID %s in datacenter %s: %w", vmID, dc.Name(), err)
}
//return nil, fmt.Errorf("failed to retrieve VM with ID %s in datacenter %s: %w", vmID, dc.Name(), err)
v.Logger.Debug("Couldn't find vm in datacenter", "vm_id", vmID, "datacenter_name", dc.Name())
} else if err != nil {
return nil, fmt.Errorf("failed to retrieve VM: %w", err)
}
return &VmProperties{
Datacenter: dc.Name(),
Vm: vm,
}, nil
}
return nil, fmt.Errorf("VM with ID %s not found in any datacenter", vmID)