Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Dec 9, 2021
1 parent 155ab78 commit 3df0ede
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 155 deletions.
22 changes: 18 additions & 4 deletions azure/services/availabilitysets/availabilitysets.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (s *Service) Reconcile(ctx context.Context) error {
if err != nil {
s.Scope.UpdatePutStatus(infrav1.AvailabilitySetReadyCondition, serviceName, err)
return err
} else if setSpec == nil {
s.Scope.UpdatePutStatus(infrav1.AvailabilitySetReadyCondition, serviceName, nil)
return nil
}

_, err = s.CreateResource(ctx, setSpec, serviceName)
Expand All @@ -88,28 +91,39 @@ func (s *Service) Delete(ctx context.Context) error {
if err != nil {
s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, err)
return err
} else if setSpec == nil {
s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, nil)
return nil
}

existingSet, err := s.Client.Get(ctx, setSpec)
if err != nil {
if azure.ResourceNotFound(err) {
// already deleted
s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, nil)
return nil
}
return errors.Wrapf(err, "failed to get availability set %s in resource group %s", setSpec.ResourceName(), setSpec.ResourceGroupName())
resultingErr := errors.Wrapf(err, "failed to get availability set %s in resource group %s", setSpec.ResourceName(), setSpec.ResourceGroupName())

s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, resultingErr)
return resultingErr
}
availabilitySet, ok := existingSet.(compute.AvailabilitySet)
if !ok {
return errors.Errorf("%T is not a compute.AvailabilitySet", existingSet)
err = errors.Errorf("%T is not a compute.AvailabilitySet", existingSet)

s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, err)
return err
}

// only delete when the availability set does not have any vms
if availabilitySet.AvailabilitySetProperties != nil && availabilitySet.VirtualMachines != nil && len(*availabilitySet.VirtualMachines) > 0 {
log.V(2).Info("skip deleting availability set", "availability set", setSpec.ResourceName(), "when it has VMs")
log.V(2).Info("skip deleting availability set with VMs", "availability set", setSpec.ResourceName())
s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, nil)
return nil
}

err = s.DeleteResource(ctx, setSpec, serviceName)
s.Scope.UpdateDeleteStatus(infrav1.VMRunningCondition, serviceName, err)
s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, err)
return err
}
Loading

0 comments on commit 3df0ede

Please sign in to comment.