2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-03-04 05:14:55 -05:00
|
|
|
"fmt"
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2016-03-04 05:14:55 -05:00
|
|
|
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
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
func (s *StepGetOSDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-03-04 05:14:55 -05:00
|
|
|
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)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put(constants.Error, err)
|
|
|
|
s.error(err)
|
|
|
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-05-29 00:06:09 -04:00
|
|
|
var vhdUri string
|
|
|
|
if vm.StorageProfile.OsDisk.Vhd != nil {
|
|
|
|
vhdUri = *vm.StorageProfile.OsDisk.Vhd.URI
|
2017-05-30 03:33:54 -04:00
|
|
|
s.say(fmt.Sprintf(" -> OS Disk : '%s'", vhdUri))
|
2017-05-29 00:06:09 -04:00
|
|
|
} else {
|
|
|
|
vhdUri = *vm.StorageProfile.OsDisk.ManagedDisk.ID
|
2017-05-30 03:33:54 -04:00
|
|
|
s.say(fmt.Sprintf(" -> Managed OS Disk : '%s'", vhdUri))
|
2017-05-29 00:06:09 -04:00
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-05-29 00:06:09 -04:00
|
|
|
state.Put(constants.ArmOSDiskVhd, vhdUri)
|
2016-03-04 05:14:55 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepGetOSDisk) Cleanup(multistep.StateBag) {
|
|
|
|
}
|