2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-12-12 04:12:53 -05:00
|
|
|
"errors"
|
2016-03-04 05:14:55 -05:00
|
|
|
"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
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepDeleteOSDisk struct {
|
2017-11-06 00:16:58 -05:00
|
|
|
client *AzureClient
|
|
|
|
delete func(string, string) error
|
|
|
|
deleteManaged func(string, string) error
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-11-06 00:16:58 -05:00
|
|
|
step.deleteManaged = step.deleteManagedDisk
|
2016-03-04 05:14:55 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-06 00:16:58 -05:00
|
|
|
func (s *StepDeleteOSDisk) deleteManagedDisk(resourceGroupName string, imageName string) error {
|
|
|
|
xs := strings.Split(imageName, "/")
|
|
|
|
diskName := xs[len(xs)-1]
|
|
|
|
_, errChan := s.client.DisksClient.Delete(resourceGroupName, diskName, nil)
|
|
|
|
err := <-errChan
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepDeleteOSDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-03-04 05:14:55 -05:00
|
|
|
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)
|
2017-11-06 00:16:58 -05:00
|
|
|
var isExistingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool)
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
2017-05-29 00:06:09 -04:00
|
|
|
|
2017-11-06 00:16:58 -05:00
|
|
|
if isManagedDisk && !isExistingResourceGroup {
|
2017-05-29 00:06:09 -04:00
|
|
|
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
|
|
|
|
2017-11-06 00:16:58 -05:00
|
|
|
var err error
|
|
|
|
if isManagedDisk {
|
|
|
|
err = s.deleteManaged(resourceGroupName, osDisk)
|
|
|
|
if err != nil {
|
|
|
|
s.say("Failed to delete the managed OS Disk!")
|
2017-11-09 06:20:09 -05:00
|
|
|
return processStepResult(err, s.error, state)
|
2017-11-06 00:16:58 -05:00
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
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!")
|
2017-11-09 06:20:09 -05:00
|
|
|
return processStepResult(err, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
xs := strings.Split(u.Path, "/")
|
2017-12-12 04:12:53 -05:00
|
|
|
if len(xs) < 3 {
|
|
|
|
err = errors.New("Failed to parse OS Disk's VHD URI!")
|
|
|
|
} else {
|
|
|
|
var storageAccountName = xs[1]
|
|
|
|
var blobName = strings.Join(xs[2:], "/")
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-12-12 04:12:53 -05:00
|
|
|
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) {
|
|
|
|
}
|