github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_get_os_disk.go (about) 1 package arm 2 3 import ( 4 "fmt" 5 6 "github.com/Azure/azure-sdk-for-go/arm/compute" 7 8 "github.com/hashicorp/packer/builder/azure/common/constants" 9 10 "github.com/hashicorp/packer/packer" 11 "github.com/mitchellh/multistep" 12 ) 13 14 type StepGetOSDisk struct { 15 client *AzureClient 16 query func(resourceGroupName string, computeName string) (compute.VirtualMachine, error) 17 say func(message string) 18 error func(e error) 19 } 20 21 func NewStepGetOSDisk(client *AzureClient, ui packer.Ui) *StepGetOSDisk { 22 var step = &StepGetOSDisk{ 23 client: client, 24 say: func(message string) { ui.Say(message) }, 25 error: func(e error) { ui.Error(e.Error()) }, 26 } 27 28 step.query = step.queryCompute 29 return step 30 } 31 32 func (s *StepGetOSDisk) queryCompute(resourceGroupName string, computeName string) (compute.VirtualMachine, error) { 33 vm, err := s.client.VirtualMachinesClient.Get(resourceGroupName, computeName, "") 34 if err != nil { 35 s.say(s.client.LastError.Error()) 36 } 37 return vm, err 38 } 39 40 func (s *StepGetOSDisk) Run(state multistep.StateBag) multistep.StepAction { 41 s.say("Querying the machine's properties ...") 42 43 var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) 44 var computeName = state.Get(constants.ArmComputeName).(string) 45 46 s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) 47 s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName)) 48 49 vm, err := s.query(resourceGroupName, computeName) 50 if err != nil { 51 state.Put(constants.Error, err) 52 s.error(err) 53 54 return multistep.ActionHalt 55 } 56 57 var vhdUri string 58 if vm.StorageProfile.OsDisk.Vhd != nil { 59 vhdUri = *vm.StorageProfile.OsDisk.Vhd.URI 60 s.say(fmt.Sprintf(" -> OS Disk : '%s'", vhdUri)) 61 } else { 62 vhdUri = *vm.StorageProfile.OsDisk.ManagedDisk.ID 63 s.say(fmt.Sprintf(" -> Managed OS Disk : '%s'", vhdUri)) 64 } 65 66 state.Put(constants.ArmOSDiskVhd, vhdUri) 67 return multistep.ActionContinue 68 } 69 70 func (*StepGetOSDisk) Cleanup(multistep.StateBag) { 71 }