github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_delete_resource_group.go (about)

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/builder/azure/common"
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/mitchellh/multistep"
    10  )
    11  
    12  type StepDeleteResourceGroup struct {
    13  	client *AzureClient
    14  	delete func(resourceGroupName string, cancelCh <-chan struct{}) error
    15  	say    func(message string)
    16  	error  func(e error)
    17  }
    18  
    19  func NewStepDeleteResourceGroup(client *AzureClient, ui packer.Ui) *StepDeleteResourceGroup {
    20  	var step = &StepDeleteResourceGroup{
    21  		client: client,
    22  		say:    func(message string) { ui.Say(message) },
    23  		error:  func(e error) { ui.Error(e.Error()) },
    24  	}
    25  
    26  	step.delete = step.deleteResourceGroup
    27  	return step
    28  }
    29  
    30  func (s *StepDeleteResourceGroup) deleteResourceGroup(resourceGroupName string, cancelCh <-chan struct{}) error {
    31  	_, errChan := s.client.GroupsClient.Delete(resourceGroupName, cancelCh)
    32  
    33  	err := <-errChan
    34  	if err != nil {
    35  		s.say(s.client.LastError.Error())
    36  	}
    37  	return err
    38  }
    39  
    40  func (s *StepDeleteResourceGroup) Run(state multistep.StateBag) multistep.StepAction {
    41  	s.say("Deleting resource group ...")
    42  
    43  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    44  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    45  
    46  	result := common.StartInterruptibleTask(
    47  		func() bool { return common.IsStateCancelled(state) },
    48  		func(cancelCh <-chan struct{}) error { return s.delete(resourceGroupName, cancelCh) })
    49  
    50  	stepAction := processInterruptibleResult(result, s.error, state)
    51  	state.Put(constants.ArmIsResourceGroupCreated, false)
    52  
    53  	return stepAction
    54  }
    55  
    56  func (*StepDeleteResourceGroup) Cleanup(multistep.StateBag) {
    57  }