From c4a9715e50f8f90727241e31e1d7ef4be723288b Mon Sep 17 00:00:00 2001 From: Ryan Hartje Date: Wed, 22 Jan 2020 16:58:26 -0600 Subject: [PATCH] updated the deleteTemplate invocation to a defer at the start of cleanup --- builder/azure/arm/step_deploy_template.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/builder/azure/arm/step_deploy_template.go b/builder/azure/arm/step_deploy_template.go index ef02fc3f7..a8626bd00 100644 --- a/builder/azure/arm/step_deploy_template.go +++ b/builder/azure/arm/step_deploy_template.go @@ -58,7 +58,12 @@ func (s *StepDeployTemplate) deployTemplate(ctx context.Context, resourceGroupNa return err } -func (s *StepDeployTemplate) deleteTemplate(ctx context.Context, resourceGroupName string, deploymentName string) error { +func (s *StepDeployTemplate) deleteTemplate(ctx context.Context, state multistep.StateBag) error { + var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) + var deploymentName = s.name + ui := state.Get("ui").(packer.Ui) + ui.Say(fmt.Sprintf("Removing the created Deployment object: '%s'", deploymentName)) + f, err := s.client.DeploymentsClient.Delete(ctx, resourceGroupName, deploymentName) if err == nil { err = f.WaitForCompletionRef(ctx, s.client.DeploymentsClient.Client) @@ -172,6 +177,8 @@ func (s *StepDeployTemplate) deleteImage(ctx context.Context, imageType string, } func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) { + defer s.deleteTemplate(context.Background(), state) + //Only clean up if this was an existing resource group and the resource group //is marked as created var existingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool) @@ -235,8 +242,5 @@ func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) { "Name: %s\n"+ "Error: %s", imageName, err)) } - - ui.Say(fmt.Sprintf(" -> Deployment: '%s'", deploymentName)) - s.deleteTemplate(context.Background(), resourceGroupName, deploymentName) } }