Remove StepDeleteOSDisk in favor of StepDeployTemplate#Cleanup
This commit is contained in:
parent
aed97e1a8b
commit
ba6d11d518
|
@ -1,99 +0,0 @@
|
||||||
package arm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
||||||
|
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
|
||||||
"github.com/hashicorp/packer/packer"
|
|
||||||
)
|
|
||||||
|
|
||||||
type StepDeleteOSDisk struct {
|
|
||||||
client *AzureClient
|
|
||||||
delete func(string, string) error
|
|
||||||
deleteManaged func(context.Context, 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
|
|
||||||
step.deleteManaged = step.deleteManagedDisk
|
|
||||||
return step
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StepDeleteOSDisk) deleteBlob(storageContainerName string, blobName string) error {
|
|
||||||
blob := s.client.BlobStorageClient.GetContainerReference(storageContainerName).GetBlobReference(blobName)
|
|
||||||
err := blob.Delete(nil)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
s.say(s.client.LastError.Error())
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StepDeleteOSDisk) deleteManagedDisk(ctx context.Context, resourceGroupName string, imageName string) error {
|
|
||||||
xs := strings.Split(imageName, "/")
|
|
||||||
diskName := xs[len(xs)-1]
|
|
||||||
f, err := s.client.DisksClient.Delete(ctx, resourceGroupName, diskName)
|
|
||||||
if err == nil {
|
|
||||||
err = f.WaitForCompletionRef(ctx, s.client.DisksClient.Client)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StepDeleteOSDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
||||||
s.say("Deleting the temporary OS disk ...")
|
|
||||||
|
|
||||||
var osDisk = state.Get(constants.ArmOSDiskVhd).(string)
|
|
||||||
var isManagedDisk = state.Get(constants.ArmIsManagedImage).(bool)
|
|
||||||
var isExistingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool)
|
|
||||||
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
||||||
|
|
||||||
if isManagedDisk && !isExistingResourceGroup {
|
|
||||||
s.say(fmt.Sprintf(" -> OS Disk : skipping, managed disk was used..."))
|
|
||||||
return multistep.ActionContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
s.say(fmt.Sprintf(" -> OS Disk : '%s'", osDisk))
|
|
||||||
|
|
||||||
var err error
|
|
||||||
if isManagedDisk {
|
|
||||||
err = s.deleteManaged(ctx, resourceGroupName, osDisk)
|
|
||||||
if err != nil {
|
|
||||||
s.say("Failed to delete the managed OS Disk!")
|
|
||||||
return processStepResult(err, s.error, state)
|
|
||||||
}
|
|
||||||
return multistep.ActionContinue
|
|
||||||
}
|
|
||||||
u, err := url.Parse(osDisk)
|
|
||||||
if err != nil {
|
|
||||||
s.say("Failed to parse the OS Disk's VHD URI!")
|
|
||||||
return processStepResult(err, s.error, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
xs := strings.Split(u.Path, "/")
|
|
||||||
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:], "/")
|
|
||||||
|
|
||||||
err = s.delete(storageAccountName, blobName)
|
|
||||||
}
|
|
||||||
return processStepResult(err, s.error, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*StepDeleteOSDisk) Cleanup(multistep.StateBag) {
|
|
||||||
}
|
|
|
@ -1,227 +0,0 @@
|
||||||
package arm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldFailIfGetFails(t *testing.T) {
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
|
|
||||||
deleteManaged: func(context.Context, string, string) error { return nil },
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
|
|
||||||
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
if result != multistep.ActionHalt {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := stateBag.GetOk(constants.Error); ok == false {
|
|
||||||
t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldPassIfGetPasses(t *testing.T) {
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(string, string) error { return nil },
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
|
|
||||||
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
if result != multistep.ActionContinue {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := stateBag.GetOk(constants.Error); ok == true {
|
|
||||||
t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldTakeStepArgumentsFromStateBag(t *testing.T) {
|
|
||||||
var actualStorageContainerName string
|
|
||||||
var actualBlobName string
|
|
||||||
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(storageContainerName string, blobName string) error {
|
|
||||||
actualStorageContainerName = storageContainerName
|
|
||||||
actualBlobName = blobName
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
|
|
||||||
if result != multistep.ActionContinue {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if actualStorageContainerName != "images" {
|
|
||||||
t.Fatalf("Expected the storage container name to be 'images', but found '%s'.", actualStorageContainerName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if actualBlobName != "pkrvm_os.vhd" {
|
|
||||||
t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldHandleComplexStorageContainerNames(t *testing.T) {
|
|
||||||
var actualStorageContainerName string
|
|
||||||
var actualBlobName string
|
|
||||||
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(storageContainerName string, blobName string) error {
|
|
||||||
actualStorageContainerName = storageContainerName
|
|
||||||
actualBlobName = blobName
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/abc/def/pkrvm_os.vhd")
|
|
||||||
testSubject.Run(context.Background(), stateBag)
|
|
||||||
|
|
||||||
if actualStorageContainerName != "abc" {
|
|
||||||
t.Fatalf("Expected the storage container name to be 'abc/def', but found '%s'.", actualStorageContainerName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if actualBlobName != "def/pkrvm_os.vhd" {
|
|
||||||
t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldFailIfVHDNameCannotBeURLParsed(t *testing.T) {
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(string, string) error { return nil },
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
deleteManaged: func(context.Context, string, string) error { return nil },
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invalid URL per https://golang.org/src/net/url/url_test.go
|
|
||||||
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://[fe80::1%en0]/")
|
|
||||||
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
if result != multistep.ActionHalt {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionHalt', but got '%v'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := stateBag.GetOk(constants.Error); ok == false {
|
|
||||||
t.Fatalf("Expected the step to not stateBag['%s'], but it was.", constants.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func TestStepDeleteOSDiskShouldFailIfVHDNameIsTooShort(t *testing.T) {
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(string, string) error { return nil },
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
deleteManaged: func(context.Context, string, string) error { return nil },
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := DeleteTestStateBagStepDeleteOSDisk("storage.blob.core.windows.net/abc")
|
|
||||||
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
if result != multistep.ActionHalt {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := stateBag.GetOk(constants.Error); ok == false {
|
|
||||||
t.Fatalf("Expected the step to not stateBag['%s'], but it was.", constants.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldPassIfManagedDiskInTempResourceGroup(t *testing.T) {
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(string, string) error { return nil },
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := new(multistep.BasicStateBag)
|
|
||||||
stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")
|
|
||||||
stateBag.Put(constants.ArmIsManagedImage, true)
|
|
||||||
stateBag.Put(constants.ArmIsExistingResourceGroup, false)
|
|
||||||
stateBag.Put(constants.ArmResourceGroupName, "testgroup")
|
|
||||||
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
if result != multistep.ActionContinue {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := stateBag.GetOk(constants.Error); ok == true {
|
|
||||||
t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldFailIfManagedDiskInExistingResourceGroupFailsToDelete(t *testing.T) {
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(string, string) error { return nil },
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
deleteManaged: func(context.Context, string, string) error { return errors.New("UNIT TEST FAIL!") },
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := new(multistep.BasicStateBag)
|
|
||||||
stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")
|
|
||||||
stateBag.Put(constants.ArmIsManagedImage, true)
|
|
||||||
stateBag.Put(constants.ArmIsExistingResourceGroup, true)
|
|
||||||
stateBag.Put(constants.ArmResourceGroupName, "testgroup")
|
|
||||||
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
if result != multistep.ActionHalt {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := stateBag.GetOk(constants.Error); ok == false {
|
|
||||||
t.Fatalf("Expected the step to not stateBag['%s'], but it was.", constants.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStepDeleteOSDiskShouldFailIfManagedDiskInExistingResourceGroupIsDeleted(t *testing.T) {
|
|
||||||
var testSubject = &StepDeleteOSDisk{
|
|
||||||
delete: func(string, string) error { return nil },
|
|
||||||
say: func(message string) {},
|
|
||||||
error: func(e error) {},
|
|
||||||
deleteManaged: func(context.Context, string, string) error { return nil },
|
|
||||||
}
|
|
||||||
|
|
||||||
stateBag := new(multistep.BasicStateBag)
|
|
||||||
stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")
|
|
||||||
stateBag.Put(constants.ArmIsManagedImage, true)
|
|
||||||
stateBag.Put(constants.ArmIsExistingResourceGroup, true)
|
|
||||||
stateBag.Put(constants.ArmResourceGroupName, "testgroup")
|
|
||||||
|
|
||||||
var result = testSubject.Run(context.Background(), stateBag)
|
|
||||||
if result != multistep.ActionContinue {
|
|
||||||
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := stateBag.GetOk(constants.Error); ok == true {
|
|
||||||
t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteTestStateBagStepDeleteOSDisk(osDiskVhd string) multistep.StateBag {
|
|
||||||
stateBag := new(multistep.BasicStateBag)
|
|
||||||
stateBag.Put(constants.ArmOSDiskVhd, osDiskVhd)
|
|
||||||
stateBag.Put(constants.ArmIsManagedImage, false)
|
|
||||||
stateBag.Put(constants.ArmIsExistingResourceGroup, false)
|
|
||||||
stateBag.Put(constants.ArmResourceGroupName, "testgroup")
|
|
||||||
|
|
||||||
return stateBag
|
|
||||||
}
|
|
Loading…
Reference in New Issue