2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-03-04 05:14:55 -05:00
|
|
|
"fmt"
|
2019-04-08 11:57:27 -04:00
|
|
|
"time"
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2019-04-08 11:57:27 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
2017-11-16 19:36:42 -05:00
|
|
|
const (
|
|
|
|
maxResourcesToDelete = 50
|
|
|
|
)
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
type StepDeleteResourceGroup struct {
|
|
|
|
client *AzureClient
|
2018-04-06 04:12:58 -04:00
|
|
|
delete func(ctx context.Context, state multistep.StateBag, resourceGroupName string) error
|
2016-03-04 05:14:55 -05:00
|
|
|
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 {
|
2017-11-06 00:16:58 -05:00
|
|
|
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)
|
2017-11-06 00:16:58 -05:00
|
|
|
if err != nil {
|
2017-11-30 03:11:17 -05:00
|
|
|
return err
|
2017-11-06 00:16:58 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 03:11:17 -05:00
|
|
|
|
|
|
|
return nil
|
2017-11-06 00:16:58 -05:00
|
|
|
} 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) {
|
2019-07-25 00:49:51 -04:00
|
|
|
// 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
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-11-06 00:16:58 -05:00
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
}
|
|
|
|
return err
|
2017-06-08 20:57:59 -04:00
|
|
|
}
|
2016-03-04 05:14:55 -05: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 {
|
2018-06-06 18:25:19 -04:00
|
|
|
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{
|
2019-04-08 11:57:27 -04:00
|
|
|
Tries: 10,
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 10 * time.Second, MaxBackoff: 600 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
2018-08-30 19:28:15 -04:00
|
|
|
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-08-30 19:28:15 -04:00
|
|
|
})
|
|
|
|
|
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 {
|
2016-03-04 05:14:55 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:19:55 -04:00
|
|
|
state.Put(constants.ArmIsResourceGroupCreated, false)
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
return multistep.ActionContinue
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepDeleteResourceGroup) Cleanup(multistep.StateBag) {
|
|
|
|
}
|