github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/builder/azure/arm/step_delete_os_disk.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  	"net/url"
     9  	"strings"
    10  
    11  	"github.com/mitchellh/packer/builder/azure/common/constants"
    12  
    13  	"github.com/mitchellh/multistep"
    14  	"github.com/mitchellh/packer/packer"
    15  )
    16  
    17  type StepDeleteOSDisk struct {
    18  	client *AzureClient
    19  	delete func(string, string) error
    20  	say    func(message string)
    21  	error  func(e error)
    22  }
    23  
    24  func NewStepDeleteOSDisk(client *AzureClient, ui packer.Ui) *StepDeleteOSDisk {
    25  	var step = &StepDeleteOSDisk{
    26  		client: client,
    27  		say:    func(message string) { ui.Say(message) },
    28  		error:  func(e error) { ui.Error(e.Error()) },
    29  	}
    30  
    31  	step.delete = step.deleteBlob
    32  	return step
    33  }
    34  
    35  func (s *StepDeleteOSDisk) deleteBlob(storageContainerName string, blobName string) error {
    36  	return s.client.BlobStorageClient.DeleteBlob(storageContainerName, blobName)
    37  }
    38  
    39  func (s *StepDeleteOSDisk) Run(state multistep.StateBag) multistep.StepAction {
    40  	s.say("Deleting the temporary OS disk ...")
    41  
    42  	var osDisk = state.Get(constants.ArmOSDiskVhd).(string)
    43  	s.say(fmt.Sprintf(" -> OS Disk             : '%s'", osDisk))
    44  
    45  	u, err := url.Parse(osDisk)
    46  	if err != nil {
    47  		s.say("Failed to parse the OS Disk's VHD URI!")
    48  		return multistep.ActionHalt
    49  	}
    50  
    51  	xs := strings.Split(u.Path, "/")
    52  
    53  	var storageAccountName = xs[1]
    54  	var blobName = strings.Join(xs[2:], "/")
    55  
    56  	err = s.delete(storageAccountName, blobName)
    57  	if err != nil {
    58  		state.Put(constants.Error, err)
    59  		s.error(err)
    60  
    61  		return multistep.ActionHalt
    62  	}
    63  	return multistep.ActionContinue
    64  }
    65  
    66  func (*StepDeleteOSDisk) Cleanup(multistep.StateBag) {
    67  }