github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_delete_additional_disks.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 StepDeleteAdditionalDisk 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 NewStepDeleteAdditionalDisks(client *AzureClient, ui packer.Ui) *StepDeleteAdditionalDisk { 25 var step = &StepDeleteAdditionalDisk{ 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 *StepDeleteAdditionalDisk) 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 *StepDeleteAdditionalDisk) deleteManagedDisk(ctx context.Context, resourceGroupName string, diskName string) error { 47 xs := strings.Split(diskName, "/") 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 *StepDeleteAdditionalDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { 57 s.say("Deleting the temporary Additional disk ...") 58 59 var dataDisks = state.Get(constants.ArmAdditionalDiskVhds).([]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 dataDisks == nil { 65 s.say(fmt.Sprintf(" -> No Additional Disks specified")) 66 return multistep.ActionContinue 67 } 68 69 if isManagedDisk && !isExistingResourceGroup { 70 s.say(fmt.Sprintf(" -> Additional Disk : skipping, managed disk was used...")) 71 return multistep.ActionContinue 72 } 73 74 for i, additionaldisk := range dataDisks { 75 s.say(fmt.Sprintf(" -> Additional Disk %d: '%s'", i+1, additionaldisk)) 76 var err error 77 if isManagedDisk { 78 err = s.deleteManaged(ctx, resourceGroupName, additionaldisk) 79 if err != nil { 80 s.say("Failed to delete the managed Additional Disk!") 81 return processStepResult(err, s.error, state) 82 } 83 } else { 84 u, err := url.Parse(additionaldisk) 85 if err != nil { 86 s.say("Failed to parse the Additional Disk's VHD URI!") 87 return processStepResult(err, s.error, state) 88 } 89 90 xs := strings.Split(u.Path, "/") 91 if len(xs) < 3 { 92 err = errors.New("Failed to parse Additional Disk's VHD URI!") 93 } else { 94 var storageAccountName = xs[1] 95 var blobName = strings.Join(xs[2:], "/") 96 97 err = s.delete(storageAccountName, blobName) 98 } 99 if err != nil { 100 return processStepResult(err, s.error, state) 101 } 102 } 103 } 104 return multistep.ActionContinue 105 } 106 107 func (*StepDeleteAdditionalDisk) Cleanup(multistep.StateBag) { 108 }