github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_delete_resource_group.go (about)

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