Async delete Resource Group

This commit is contained in:
Hariharan Jayaraman 2018-05-14 20:06:23 -07:00
parent 5f47a220fc
commit c8c9bbb22a
6 changed files with 25 additions and 4 deletions

View File

@ -344,6 +344,7 @@ func (b *Builder) configureStateBag(stateBag multistep.StateBag) {
stateBag.Put(constants.ArmIsManagedImage, b.config.isManagedImage())
stateBag.Put(constants.ArmManagedImageResourceGroupName, b.config.ManagedImageResourceGroupName)
stateBag.Put(constants.ArmManagedImageName, b.config.ManagedImageName)
stateBag.Put(constants.ArmAsyncRGDelete, b.config.AsyncRGDelete)
}
// Parameters that are only known at runtime after querying Azure.

View File

@ -151,6 +151,9 @@ type Config struct {
Comm communicator.Config `mapstructure:",squash"`
ctx *interpolate.Context
//Cleanup
AsyncRGDelete bool `mapstructure:"async_resourcegroup_delete"`
}
type keyVaultCertificate struct {

View File

@ -118,14 +118,20 @@ func (s *StepCreateResourceGroup) Cleanup(state multistep.StateBag) {
ctx := context.TODO()
f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName)
if err == nil {
if state.Get(constants.ArmAsyncRGDelete).(bool) {
s.say(fmt.Sprintf("\n Not waiting for Resource Group delete as requested by user. Resource Group Name is %s", resourceGroupName))
} else {
err = f.WaitForCompletion(ctx, s.client.GroupsClient.Client)
}
}
if err != nil {
ui.Error(fmt.Sprintf("Error deleting resource group. Please delete it manually.\n\n"+
"Name: %s\n"+
"Error: %s", resourceGroupName, err))
return
}
if !state.Get(constants.ArmAsyncRGDelete).(bool) {
ui.Say("Resource group has been deleted.")
}
}
}

View File

@ -53,9 +53,15 @@ func (s *StepDeleteResourceGroup) deleteResourceGroup(ctx context.Context, state
s.say("\nThe resource group was created by Packer, deleting ...")
f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName)
if err == nil {
if state.Get(constants.ArmAsyncRGDelete).(bool) {
// 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)
}
}
if err != nil {
s.say(s.client.LastError.Error())
}

View File

@ -35,4 +35,5 @@ const (
ArmManagedImageResourceGroupName string = "arm.ManagedImageResourceGroupName"
ArmManagedImageLocation string = "arm.ManagedImageLocation"
ArmManagedImageName string = "arm.ManagedImageName"
ArmAsyncRGDelete string = "arm.AsyncRGDelete"
)

View File

@ -221,6 +221,10 @@ Providing `temp_resource_group_name` or `location` in combination with `build_re
CLI example `azure vm sizes -l westus`
- `async_resourcegroup_delete` (boolean) If you want packer to delete the temporary resource group asynchronously. Its a boolean value
and defaults to false. **Important** Setting this true would mean that your builds are faster however this is a very
small chance that the temporary resource group is not deleted by Azure.
## Basic Example
Here is a basic example for Azure.