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

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/packer/builder/azure/common/constants"
     9  
    10  	"github.com/hashicorp/packer/packer"
    11  	"github.com/mitchellh/multistep"
    12  )
    13  
    14  type StepDeleteOSDisk struct {
    15  	client *AzureClient
    16  	delete func(string, string) error
    17  	say    func(message string)
    18  	error  func(e error)
    19  }
    20  
    21  func NewStepDeleteOSDisk(client *AzureClient, ui packer.Ui) *StepDeleteOSDisk {
    22  	var step = &StepDeleteOSDisk{
    23  		client: client,
    24  		say:    func(message string) { ui.Say(message) },
    25  		error:  func(e error) { ui.Error(e.Error()) },
    26  	}
    27  
    28  	step.delete = step.deleteBlob
    29  	return step
    30  }
    31  
    32  func (s *StepDeleteOSDisk) deleteBlob(storageContainerName string, blobName string) error {
    33  	blob := s.client.BlobStorageClient.GetContainerReference(storageContainerName).GetBlobReference(blobName)
    34  	err := blob.Delete(nil)
    35  
    36  	if err != nil {
    37  		s.say(s.client.LastError.Error())
    38  	}
    39  	return err
    40  }
    41  
    42  func (s *StepDeleteOSDisk) Run(state multistep.StateBag) multistep.StepAction {
    43  	s.say("Deleting the temporary OS disk ...")
    44  
    45  	var osDisk = state.Get(constants.ArmOSDiskVhd).(string)
    46  	var isManagedDisk = state.Get(constants.ArmIsManagedImage).(bool)
    47  
    48  	if isManagedDisk {
    49  		s.say(fmt.Sprintf(" -> OS Disk : skipping, managed disk was used..."))
    50  		return multistep.ActionContinue
    51  	}
    52  
    53  	s.say(fmt.Sprintf(" -> OS Disk : '%s'", osDisk))
    54  
    55  	u, err := url.Parse(osDisk)
    56  	if err != nil {
    57  		s.say("Failed to parse the OS Disk's VHD URI!")
    58  		return multistep.ActionHalt
    59  	}
    60  
    61  	xs := strings.Split(u.Path, "/")
    62  
    63  	var storageAccountName = xs[1]
    64  	var blobName = strings.Join(xs[2:], "/")
    65  
    66  	err = s.delete(storageAccountName, blobName)
    67  	return processStepResult(err, s.error, state)
    68  }
    69  
    70  func (*StepDeleteOSDisk) Cleanup(multistep.StateBag) {
    71  }