packer-cn/builder/azure/arm/step_delete_resource_group.go

148 lines
4.1 KiB
Go
Raw Normal View History

package arm
import (
"context"
"fmt"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/azure/common/constants"
retry "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
2017-11-16 19:36:42 -05:00
const (
maxResourcesToDelete = 50
)
type StepDeleteResourceGroup struct {
client *AzureClient
2018-04-06 04:12:58 -04:00
delete func(ctx context.Context, state multistep.StateBag, resourceGroupName string) error
say func(message string)
error func(e error)
}
func NewStepDeleteResourceGroup(client *AzureClient, ui packer.Ui) *StepDeleteResourceGroup {
var step = &StepDeleteResourceGroup{
client: client,
say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) },
}
step.delete = step.deleteResourceGroup
return step
}
2018-04-06 04:12:58 -04:00
func (s *StepDeleteResourceGroup) deleteResourceGroup(ctx context.Context, state multistep.StateBag, resourceGroupName string) error {
var err error
if state.Get(constants.ArmIsExistingResourceGroup).(bool) {
s.say("\nThe resource group was not created by Packer, only deleting individual resources ...")
var deploymentName = state.Get(constants.ArmDeploymentName).(string)
2018-04-06 04:12:58 -04:00
err = s.deleteDeploymentResources(ctx, deploymentName, resourceGroupName)
2017-11-30 03:11:17 -05:00
if err != nil {
return err
}
if keyVaultDeploymentName, ok := state.GetOk(constants.ArmKeyVaultDeploymentName); ok {
2018-04-06 04:12:58 -04:00
err = s.deleteDeploymentResources(ctx, keyVaultDeploymentName.(string), resourceGroupName)
if err != nil {
2017-11-30 03:11:17 -05:00
return err
}
}
2017-11-30 03:11:17 -05:00
return nil
} else {
2017-11-16 20:32:35 -05:00
s.say("\nThe resource group was created by Packer, deleting ...")
2018-04-06 04:12:58 -04:00
f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName)
if err == nil {
2018-05-15 14:41:26 -04:00
if state.Get(constants.ArmAsyncResourceGroupDelete).(bool) {
2018-05-14 23:06:23 -04:00
// No need to wait for the complition for delete if request is Accepted
s.say(fmt.Sprintf("\nResource Group is being deleted, not waiting for deletion due to config. Resource Group Name '%s'", resourceGroupName))
} else {
f.WaitForCompletion(ctx, s.client.GroupsClient.Client)
}
2018-04-06 04:12:58 -04:00
}
if err != nil {
s.say(s.client.LastError.Error())
}
return err
2017-06-08 20:57:59 -04:00
}
}
2018-04-06 04:12:58 -04:00
func (s *StepDeleteResourceGroup) deleteDeploymentResources(ctx context.Context, deploymentName, resourceGroupName string) error {
2017-11-30 03:11:17 -05:00
maxResources := int32(maxResourcesToDelete)
2018-04-06 04:12:58 -04:00
deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(ctx, resourceGroupName, deploymentName, &maxResources)
2017-11-30 03:11:17 -05:00
if err != nil {
s.reportIfError(err, resourceGroupName)
return err
}
2018-04-06 04:12:58 -04:00
for deploymentOperations.NotDone() {
deploymentOperation := deploymentOperations.Value()
2017-11-30 03:11:17 -05:00
// Sometimes an empty operation is added to the list by Azure
if deploymentOperation.Properties.TargetResource == nil {
deploymentOperations.Next()
2017-11-30 03:11:17 -05:00
continue
}
resourceName := *deploymentOperation.Properties.TargetResource.ResourceName
2018-04-06 04:12:58 -04:00
resourceType := *deploymentOperation.Properties.TargetResource.ResourceType
2017-11-30 03:11:17 -05:00
2018-04-06 04:12:58 -04:00
s.say(fmt.Sprintf(" -> %s : '%s'",
resourceType,
resourceName))
err := retry.Retry(10, 600, 10, func(attempt uint) (bool, error) {
err := deleteResource(ctx, s.client,
resourceType,
resourceName,
resourceGroupName)
if err != nil {
s.reportIfError(err, resourceName)
return false, nil
}
return true, nil
})
2018-04-06 04:12:58 -04:00
if err = deploymentOperations.Next(); err != nil {
return err
2017-11-30 03:11:17 -05:00
}
}
return nil
}
func (s *StepDeleteResourceGroup) reportIfError(err error, resourceName string) {
if err != nil {
s.say(fmt.Sprintf("Error deleting resource. Please delete manually.\n\n"+
"Name: %s\n"+
"Error: %s", resourceName, err.Error()))
s.error(err)
}
}
2018-04-06 04:12:58 -04:00
func (s *StepDeleteResourceGroup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
s.say("Deleting resource group ...")
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
2018-04-06 04:12:58 -04:00
err := s.delete(ctx, state, resourceGroupName)
if err != nil {
state.Put(constants.Error, err)
s.error(err)
return multistep.ActionHalt
}
state.Put(constants.ArmIsResourceGroupCreated, false)
2018-04-06 04:12:58 -04:00
return multistep.ActionContinue
}
func (*StepDeleteResourceGroup) Cleanup(multistep.StateBag) {
}