2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SwitchTypeInternal = "Internal"
|
2015-10-30 04:23:30 -04:00
|
|
|
SwitchTypePrivate = "Private"
|
|
|
|
DefaultSwitchType = SwitchTypeInternal
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates switch for VM.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// SwitchName string - The name of the Switch
|
|
|
|
type StepCreateSwitch struct {
|
|
|
|
// Specifies the name of the switch to be created.
|
2015-10-30 04:23:30 -04:00
|
|
|
SwitchName string
|
2015-06-21 07:36:07 -04:00
|
|
|
// Specifies the type of the switch to be created. Allowed values are Internal and Private. To create an External
|
|
|
|
// virtual switch, specify either the NetAdapterInterfaceDescription or the NetAdapterName parameter, which
|
|
|
|
// implicitly set the type of the virtual switch to External.
|
2015-10-30 04:23:30 -04:00
|
|
|
SwitchType string
|
2015-06-21 07:36:07 -04:00
|
|
|
// Specifies the name of the network adapter to be bound to the switch to be created.
|
|
|
|
NetAdapterName string
|
|
|
|
// Specifies the interface description of the network adapter to be bound to the switch to be created.
|
|
|
|
NetAdapterInterfaceDescription string
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
createdSwitch bool
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateSwitch) Run(state multistep.StateBag) multistep.StepAction {
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if len(s.SwitchType) == 0 {
|
|
|
|
s.SwitchType = DefaultSwitchType
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating switch '%v' if required...", s.SwitchName))
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
createdSwitch, err := driver.CreateVirtualSwitch(s.SwitchName, s.SwitchType)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating switch: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
2015-10-30 04:23:30 -04:00
|
|
|
s.SwitchName = ""
|
2015-06-21 07:36:07 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.createdSwitch = createdSwitch
|
|
|
|
|
|
|
|
if !s.createdSwitch {
|
|
|
|
ui.Say(fmt.Sprintf(" switch '%v' already exists. Will not delete on cleanup...", s.SwitchName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the final name in the state bag so others can use it
|
|
|
|
state.Put("SwitchName", s.SwitchName)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateSwitch) Cleanup(state multistep.StateBag) {
|
|
|
|
if len(s.SwitchName) == 0 || !s.createdSwitch {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("Unregistering and deleting switch...")
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
err := driver.DeleteVirtualSwitch(s.SwitchName)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting switch: %s", err))
|
|
|
|
}
|
|
|
|
}
|