github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_get_additional_disks.go (about) 1 package arm 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute" 8 9 "github.com/hashicorp/packer/builder/azure/common/constants" 10 11 "github.com/hashicorp/packer/helper/multistep" 12 "github.com/hashicorp/packer/packer" 13 ) 14 15 type StepGetDataDisk struct { 16 client *AzureClient 17 query func(ctx context.Context, resourceGroupName string, computeName string) (compute.VirtualMachine, error) 18 say func(message string) 19 error func(e error) 20 } 21 22 func NewStepGetAdditionalDisks(client *AzureClient, ui packer.Ui) *StepGetDataDisk { 23 var step = &StepGetDataDisk{ 24 client: client, 25 say: func(message string) { ui.Say(message) }, 26 error: func(e error) { ui.Error(e.Error()) }, 27 } 28 29 step.query = step.queryCompute 30 return step 31 } 32 33 func (s *StepGetDataDisk) queryCompute(ctx context.Context, resourceGroupName string, computeName string) (compute.VirtualMachine, error) { 34 vm, err := s.client.VirtualMachinesClient.Get(ctx, resourceGroupName, computeName, "") 35 if err != nil { 36 s.say(s.client.LastError.Error()) 37 } 38 return vm, err 39 } 40 41 func (s *StepGetDataDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { 42 s.say("Querying the machine's additional disks properties ...") 43 44 var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) 45 var computeName = state.Get(constants.ArmComputeName).(string) 46 47 s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) 48 s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName)) 49 50 vm, err := s.query(ctx, resourceGroupName, computeName) 51 if err != nil { 52 state.Put(constants.Error, err) 53 s.error(err) 54 55 return multistep.ActionHalt 56 } 57 58 if vm.StorageProfile.DataDisks != nil { 59 var vhdUri string 60 additional_disks := make([]string, len(*vm.StorageProfile.DataDisks)) 61 for i, additionaldisk := range *vm.StorageProfile.DataDisks { 62 if additionaldisk.Vhd != nil { 63 vhdUri = *additionaldisk.Vhd.URI 64 s.say(fmt.Sprintf(" -> Additional Disk %d : '%s'", i+1, vhdUri)) 65 } else { 66 vhdUri = *additionaldisk.ManagedDisk.ID 67 s.say(fmt.Sprintf(" -> Managed Additional Disk %d : '%s'", i+1, vhdUri)) 68 } 69 additional_disks[i] = vhdUri 70 } 71 state.Put(constants.ArmAdditionalDiskVhds, additional_disks) 72 } 73 74 return multistep.ActionContinue 75 } 76 77 func (*StepGetDataDisk) Cleanup(multistep.StateBag) { 78 }