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

153 lines
4.4 KiB
Go
Raw Normal View History

package arm
import (
"context"
"fmt"
"time"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/azure/common/constants"
"github.com/hashicorp/packer/common/retry"
"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 {
// Only delete if custom keyvault was not provided.
if exists := state.Get(constants.ArmIsExistingKeyVault).(bool); exists {
err = s.deleteDeploymentResources(ctx, keyVaultDeploymentName.(string), resourceGroupName)
if err != nil {
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) {
// No need to wait for the completion for delete if request is Accepted
2018-05-14 23:06:23 -04:00
s.say(fmt.Sprintf("\nResource Group is being deleted, not waiting for deletion due to config. Resource Group Name '%s'", resourceGroupName))
} else {
2019-05-30 17:25:43 -04:00
f.WaitForCompletionRef(ctx, s.client.GroupsClient.Client)
2018-05-14 23:06:23 -04:00
}
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))
2019-10-10 14:48:26 -04:00
retry.Config{
Tries: 10,
RetryDelay: (&retry.Backoff{InitialBackoff: 10 * time.Second, MaxBackoff: 600 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error {
err := deleteResource(ctx, s.client,
resourceType,
resourceName,
resourceGroupName)
if err != nil {
s.reportIfError(err, resourceName)
}
2019-10-10 14:48:26 -04:00
return 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) {
}