2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepDeleteOSDisk struct {
|
|
|
|
client *AzureClient
|
|
|
|
delete func(string, string) error
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStepDeleteOSDisk(client *AzureClient, ui packer.Ui) *StepDeleteOSDisk {
|
|
|
|
var step = &StepDeleteOSDisk{
|
|
|
|
client: client,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.delete = step.deleteBlob
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepDeleteOSDisk) deleteBlob(storageContainerName string, blobName string) error {
|
2017-05-28 03:38:45 -04:00
|
|
|
blob := s.client.BlobStorageClient.GetContainerReference(storageContainerName).GetBlobReference(blobName)
|
2017-06-08 20:57:59 -04:00
|
|
|
err := blob.Delete(nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
}
|
|
|
|
return err
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepDeleteOSDisk) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
s.say("Deleting the temporary OS disk ...")
|
|
|
|
|
|
|
|
var osDisk = state.Get(constants.ArmOSDiskVhd).(string)
|
2017-05-29 00:06:09 -04:00
|
|
|
var isManagedDisk = state.Get(constants.ArmIsManagedImage).(bool)
|
|
|
|
|
|
|
|
if isManagedDisk {
|
|
|
|
s.say(fmt.Sprintf(" -> OS Disk : skipping, managed disk was used..."))
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:19:55 -04:00
|
|
|
s.say(fmt.Sprintf(" -> OS Disk : '%s'", osDisk))
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
u, err := url.Parse(osDisk)
|
|
|
|
if err != nil {
|
|
|
|
s.say("Failed to parse the OS Disk's VHD URI!")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
xs := strings.Split(u.Path, "/")
|
|
|
|
|
|
|
|
var storageAccountName = xs[1]
|
|
|
|
var blobName = strings.Join(xs[2:], "/")
|
|
|
|
|
|
|
|
err = s.delete(storageAccountName, blobName)
|
2016-04-21 19:50:03 -04:00
|
|
|
return processStepResult(err, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepDeleteOSDisk) Cleanup(multistep.StateBag) {
|
|
|
|
}
|