github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_configure_vlan.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/packer"
     8  )
     9  
    10  type StepConfigureVlan struct {
    11  	VlanId       string
    12  	SwitchVlanId string
    13  }
    14  
    15  func (s *StepConfigureVlan) Run(state multistep.StateBag) multistep.StepAction {
    16  	driver := state.Get("driver").(Driver)
    17  	ui := state.Get("ui").(packer.Ui)
    18  
    19  	errorMsg := "Error configuring vlan: %s"
    20  	vmName := state.Get("vmName").(string)
    21  	switchName := state.Get("SwitchName").(string)
    22  	vlanId := s.VlanId
    23  	switchVlanId := s.SwitchVlanId
    24  
    25  	ui.Say("Configuring vlan...")
    26  
    27  	if switchVlanId != "" {
    28  		err := driver.SetNetworkAdapterVlanId(switchName, vlanId)
    29  		if err != nil {
    30  			err := fmt.Errorf(errorMsg, err)
    31  			state.Put("error", err)
    32  			ui.Error(err.Error())
    33  			return multistep.ActionHalt
    34  		}
    35  	}
    36  
    37  	if vlanId != "" {
    38  		err := driver.SetVirtualMachineVlanId(vmName, vlanId)
    39  		if err != nil {
    40  			err := fmt.Errorf(errorMsg, err)
    41  			state.Put("error", err)
    42  			ui.Error(err.Error())
    43  			return multistep.ActionHalt
    44  		}
    45  	}
    46  
    47  	return multistep.ActionContinue
    48  }
    49  
    50  func (s *StepConfigureVlan) Cleanup(state multistep.StateBag) {
    51  	//do nothing
    52  }