packer-cn/builder/azure/arm/step_get_os_disk.go

73 lines
2.0 KiB
Go
Raw Normal View History

package arm
import (
"context"
"fmt"
2018-04-06 04:12:58 -04:00
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/azure/common/constants"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
type StepGetOSDisk struct {
client *AzureClient
2018-04-06 04:12:58 -04:00
query func(ctx context.Context, resourceGroupName string, computeName string) (compute.VirtualMachine, error)
say func(message string)
error func(e error)
}
func NewStepGetOSDisk(client *AzureClient, ui packer.Ui) *StepGetOSDisk {
var step = &StepGetOSDisk{
client: client,
say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) },
}
step.query = step.queryCompute
return step
}
2018-04-06 04:12:58 -04:00
func (s *StepGetOSDisk) queryCompute(ctx context.Context, resourceGroupName string, computeName string) (compute.VirtualMachine, error) {
vm, err := s.client.VirtualMachinesClient.Get(ctx, resourceGroupName, computeName, "")
2017-06-08 20:57:59 -04:00
if err != nil {
s.say(s.client.LastError.Error())
}
return vm, err
}
2018-04-06 04:12:58 -04:00
func (s *StepGetOSDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
s.say("Querying the machine's properties ...")
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
var computeName = state.Get(constants.ArmComputeName).(string)
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName))
2018-04-06 04:12:58 -04:00
vm, err := s.query(ctx, resourceGroupName, computeName)
if err != nil {
state.Put(constants.Error, err)
s.error(err)
return multistep.ActionHalt
}
var vhdUri string
if vm.StorageProfile.OsDisk.Vhd != nil {
vhdUri = *vm.StorageProfile.OsDisk.Vhd.URI
s.say(fmt.Sprintf(" -> OS Disk : '%s'", vhdUri))
} else {
vhdUri = *vm.StorageProfile.OsDisk.ManagedDisk.ID
s.say(fmt.Sprintf(" -> Managed OS Disk : '%s'", vhdUri))
}
state.Put(constants.ArmOSDiskVhd, vhdUri)
return multistep.ActionContinue
}
func (*StepGetOSDisk) Cleanup(multistep.StateBag) {
}