Skip to content

Commit

Permalink
Ensure VM is always deleted first
Browse files Browse the repository at this point in the history
  • Loading branch information
JenGoldstrich committed Apr 17, 2024
1 parent 94d542c commit 6d62d9c
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions builder/azure/arm/step_deploy_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ func (s *StepDeployTemplate) deleteDeploymentResources(ctx context.Context, subs
}

resources := map[string]string{}

var vmID *virtualmachines.VirtualMachineId
var nicID *commonids.NetworkInterfaceId
for _, deploymentOperation := range deploymentOperations.Items {
// Sometimes an empty operation is added to the list by Azure
if deploymentOperation.Properties.TargetResource == nil {
Expand All @@ -295,30 +296,38 @@ func (s *StepDeployTemplate) deleteDeploymentResources(ctx context.Context, subs
resourceName := *deploymentOperation.Properties.TargetResource.ResourceName
resourceType := *deploymentOperation.Properties.TargetResource.ResourceType

// Delete the Virtual Machine and NIC before deleting other network resources to avoid still in use errors on other resources
if resourceType == "Microsoft.Compute/virtualMachines" {
s.say(fmt.Sprintf("Attempting deletion -> %s : '%s'", resourceType, resourceName))
vmID := virtualmachines.NewVirtualMachineID(subscriptionId, resourceGroupName, resourceName)
if err := s.client.VirtualMachinesClient.DeleteThenPoll(pollingContext, vmID, virtualmachines.DefaultDeleteOperationOptions()); err != nil {
s.reportIfError(err, resourceName)
return err
}
s.say(fmt.Sprintf("Finished deleting -> %s : '%s'", resourceType, resourceName))
} else if resourceType == "Microsoft.Network/networkInterfaces" {
s.say(fmt.Sprintf("Attempting deletion -> %s : '%s'", resourceType, resourceName))
interfaceID := commonids.NewNetworkInterfaceID(subscriptionId, resourceGroupName, resourceName)
err := s.client.NetworkMetaClient.NetworkInterfaces.DeleteThenPoll(pollingContext, interfaceID)
if err != nil {
s.reportIfError(err, resourceName)
return err
}
s.say(fmt.Sprintf("Finished deleting -> %s : '%s'", resourceType, resourceName))
} else {
// Create vm and NIC ID to use them to delete later
switch resourceType {
case "Microsoft.Compute/virtualMachines":
vmIDDeref := virtualmachines.NewVirtualMachineID(subscriptionId, resourceGroupName, resourceName)
vmID = &vmIDDeref
case "Microsoft.Network/networkInterfaces":
nicIDDeref := commonids.NewNetworkInterfaceID(subscriptionId, resourceGroupName, resourceName)
nicID = &nicIDDeref
default:
s.say(fmt.Sprintf("Adding to deletion queue -> %s : '%s'", resourceType, resourceName))
resources[resourceType] = resourceName
}
}

// Always delete VM first, then delete NIC, this way we avoid conflicts on deletion
if vmID != nil {
s.say(fmt.Sprintf("Attempting deletion -> %s : '%s'", "Microsoft.Compute/virtualMachines", vmID.VirtualMachineName))
if err := s.client.VirtualMachinesClient.DeleteThenPoll(pollingContext, *vmID, virtualmachines.DefaultDeleteOperationOptions()); err != nil {
s.reportIfError(err, vmID.VirtualMachineName)
return err
}
s.say(fmt.Sprintf("Finished deleting -> %s : '%s'", "Microsoft.Compute/virtualMachines", vmID.VirtualMachineName))
}
if nicID != nil {
s.say(fmt.Sprintf("Attempting deletion -> %s : '%s'", "Microsoft.Network/networkInterfaces", nicID.NetworkInterfaceName))
err := s.client.NetworkMetaClient.NetworkInterfaces.DeleteThenPoll(pollingContext, *nicID)
if err != nil {
s.reportIfError(err, nicID.NetworkInterfaceName)
return err
}
s.say(fmt.Sprintf("Finished deleting -> %s : '%s'", "Microsoft.Network/networkInterfaces", nicID.NetworkInterfaceName))
}
var wg sync.WaitGroup
wg.Add(len(resources))

Expand Down

0 comments on commit 6d62d9c

Please sign in to comment.