Merge pull request #8496 from ryanhartje/delete_arm_deployment_on_cleanup

attempting to resolve deployment quota issue due to non-cleanup
This commit is contained in:
Megan Marsh 2020-01-23 09:48:20 -08:00 committed by GitHub
commit b0b10f6939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 1 deletions

View File

@ -58,6 +58,23 @@ func (s *StepDeployTemplate) deployTemplate(ctx context.Context, resourceGroupNa
return err return err
} }
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)
}
if err != nil {
s.say(s.client.LastError.Error())
}
return err
}
func (s *StepDeployTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepDeployTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
s.say("Deploying deployment template ...") s.say("Deploying deployment template ...")
@ -160,6 +177,8 @@ func (s *StepDeployTemplate) deleteImage(ctx context.Context, imageType string,
} }
func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) { 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 //Only clean up if this was an existing resource group and the resource group
//is marked as created //is marked as created
var existingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool) var existingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool)
@ -178,7 +197,7 @@ func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) {
ui.Error("Could not retrieve OS Image details") ui.Error("Could not retrieve OS Image details")
} }
ui.Say(" -> Deployment: " + deploymentName) ui.Say(" -> Deployment Resources within: " + deploymentName)
if deploymentName != "" { if deploymentName != "" {
maxResources := int32(50) maxResources := int32(50)
deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(context.TODO(), resourceGroupName, deploymentName, &maxResources) deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(context.TODO(), resourceGroupName, deploymentName, &maxResources)