github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_delete_os_disk.go (about)

     1  package arm
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/url"
     8  	"strings"
     9  
    10  	"github.com/hashicorp/packer/builder/azure/common/constants"
    11  
    12  	"github.com/hashicorp/packer/helper/multistep"
    13  	"github.com/hashicorp/packer/packer"
    14  )
    15  
    16  type StepDeleteOSDisk struct {
    17  	client        *AzureClient
    18  	delete        func(string, string) error
    19  	deleteManaged func(context.Context, 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  	step.deleteManaged = step.deleteManagedDisk
    33  	return step
    34  }
    35  
    36  func (s *StepDeleteOSDisk) deleteBlob(storageContainerName string, blobName string) error {
    37  	blob := s.client.BlobStorageClient.GetContainerReference(storageContainerName).GetBlobReference(blobName)
    38  	err := blob.Delete(nil)
    39  
    40  	if err != nil {
    41  		s.say(s.client.LastError.Error())
    42  	}
    43  	return err
    44  }
    45  
    46  func (s *StepDeleteOSDisk) deleteManagedDisk(ctx context.Context, resourceGroupName string, imageName string) error {
    47  	xs := strings.Split(imageName, "/")
    48  	diskName := xs[len(xs)-1]
    49  	f, err := s.client.DisksClient.Delete(ctx, resourceGroupName, diskName)
    50  	if err == nil {
    51  		err = f.WaitForCompletion(ctx, s.client.DisksClient.Client)
    52  	}
    53  	return err
    54  }
    55  
    56  func (s *StepDeleteOSDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
    57  	s.say("Deleting the temporary OS disk ...")
    58  
    59  	var osDisk = state.Get(constants.ArmOSDiskVhd).(string)
    60  	var isManagedDisk = state.Get(constants.ArmIsManagedImage).(bool)
    61  	var isExistingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool)
    62  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    63  
    64  	if isManagedDisk && !isExistingResourceGroup {
    65  		s.say(fmt.Sprintf(" -> OS Disk : skipping, managed disk was used..."))
    66  		return multistep.ActionContinue
    67  	}
    68  
    69  	s.say(fmt.Sprintf(" -> OS Disk : '%s'", osDisk))
    70  
    71  	var err error
    72  	if isManagedDisk {
    73  		err = s.deleteManaged(ctx, resourceGroupName, osDisk)
    74  		if err != nil {
    75  			s.say("Failed to delete the managed OS Disk!")
    76  			return processStepResult(err, s.error, state)
    77  		}
    78  		return multistep.ActionContinue
    79  	}
    80  	u, err := url.Parse(osDisk)
    81  	if err != nil {
    82  		s.say("Failed to parse the OS Disk's VHD URI!")
    83  		return processStepResult(err, s.error, state)
    84  	}
    85  
    86  	xs := strings.Split(u.Path, "/")
    87  	if len(xs) < 3 {
    88  		err = errors.New("Failed to parse OS Disk's VHD URI!")
    89  	} else {
    90  		var storageAccountName = xs[1]
    91  		var blobName = strings.Join(xs[2:], "/")
    92  
    93  		err = s.delete(storageAccountName, blobName)
    94  	}
    95  	return processStepResult(err, s.error, state)
    96  }
    97  
    98  func (*StepDeleteOSDisk) Cleanup(multistep.StateBag) {
    99  }