github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_create_switch.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  const (
    12  	SwitchTypeInternal = "Internal"
    13  	SwitchTypePrivate  = "Private"
    14  	DefaultSwitchType  = SwitchTypeInternal
    15  )
    16  
    17  // This step creates switch for VM.
    18  //
    19  // Produces:
    20  //   SwitchName string - The name of the Switch
    21  type StepCreateSwitch struct {
    22  	// Specifies the name of the switch to be created.
    23  	SwitchName string
    24  	// Specifies the type of the switch to be created. Allowed values are Internal and Private. To create an External
    25  	// virtual switch, specify either the NetAdapterInterfaceDescription or the NetAdapterName parameter, which
    26  	// implicitly set the type of the virtual switch to External.
    27  	SwitchType string
    28  	// Specifies the name of the network adapter to be bound to the switch to be created.
    29  	NetAdapterName string
    30  	// Specifies the interface description of the network adapter to be bound to the switch to be created.
    31  	NetAdapterInterfaceDescription string
    32  
    33  	createdSwitch bool
    34  }
    35  
    36  func (s *StepCreateSwitch) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    37  	driver := state.Get("driver").(Driver)
    38  	ui := state.Get("ui").(packer.Ui)
    39  
    40  	if len(s.SwitchType) == 0 {
    41  		s.SwitchType = DefaultSwitchType
    42  	}
    43  
    44  	ui.Say(fmt.Sprintf("Creating switch '%v' if required...", s.SwitchName))
    45  
    46  	createdSwitch, err := driver.CreateVirtualSwitch(s.SwitchName, s.SwitchType)
    47  	if err != nil {
    48  		err := fmt.Errorf("Error creating switch: %s", err)
    49  		state.Put("error", err)
    50  		ui.Error(err.Error())
    51  		s.SwitchName = ""
    52  		return multistep.ActionHalt
    53  	}
    54  
    55  	s.createdSwitch = createdSwitch
    56  
    57  	if !s.createdSwitch {
    58  		ui.Say(fmt.Sprintf("    switch '%v' already exists. Will not delete on cleanup...", s.SwitchName))
    59  	}
    60  
    61  	// Set the final name in the state bag so others can use it
    62  	state.Put("SwitchName", s.SwitchName)
    63  
    64  	return multistep.ActionContinue
    65  }
    66  
    67  func (s *StepCreateSwitch) Cleanup(state multistep.StateBag) {
    68  	if len(s.SwitchName) == 0 || !s.createdSwitch {
    69  		return
    70  	}
    71  
    72  	driver := state.Get("driver").(Driver)
    73  	ui := state.Get("ui").(packer.Ui)
    74  	ui.Say("Unregistering and deleting switch...")
    75  
    76  	err := driver.DeleteVirtualSwitch(s.SwitchName)
    77  	if err != nil {
    78  		ui.Error(fmt.Sprintf("Error deleting switch: %s", err))
    79  	}
    80  }