github.com/mitchellh/packer@v1.3.2/builder/azure/arm/step_get_os_disk.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 StepGetOSDisk 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 NewStepGetOSDisk(client *AzureClient, ui packer.Ui) *StepGetOSDisk {
    23  	var step = &StepGetOSDisk{
    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 *StepGetOSDisk) 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 *StepGetOSDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
    42  	s.say("Querying the machine's 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  	var vhdUri string
    59  	if vm.StorageProfile.OsDisk.Vhd != nil {
    60  		vhdUri = *vm.StorageProfile.OsDisk.Vhd.URI
    61  		s.say(fmt.Sprintf(" -> OS Disk           : '%s'", vhdUri))
    62  	} else {
    63  		vhdUri = *vm.StorageProfile.OsDisk.ManagedDisk.ID
    64  		s.say(fmt.Sprintf(" -> Managed OS Disk   : '%s'", vhdUri))
    65  	}
    66  
    67  	state.Put(constants.ArmOSDiskVhd, vhdUri)
    68  	return multistep.ActionContinue
    69  }
    70  
    71  func (*StepGetOSDisk) Cleanup(multistep.StateBag) {
    72  }