2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-13 03:21:07 -05:00
|
|
|
|
2014-12-17 12:45:25 -06:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
type StepFindVdi struct {
|
2014-12-17 12:45:25 -06:00
|
|
|
VdiName string
|
|
|
|
ImagePathFunc func() string
|
|
|
|
VdiUuidKey string
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepFindVdi) Run(state multistep.StateBag) multistep.StepAction {
|
2014-12-17 12:45:25 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-13 03:21:07 -05:00
|
|
|
c := state.Get("client").(*Connection)
|
2014-12-17 12:45:25 -06:00
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
// Ignore if VdiName is not specified
|
|
|
|
if self.VdiName == "" {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2015-02-23 12:30:57 -06:00
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
vdis, err := c.client.VDI.GetByNameLabel(c.session, self.VdiName)
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(vdis) == 0:
|
|
|
|
ui.Error(fmt.Sprintf("Couldn't find a VDI named '%s'", self.VdiName))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
case len(vdis) > 1:
|
|
|
|
ui.Error(fmt.Sprintf("Found more than one VDI with name '%s'. Name must be unique", self.VdiName))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
vdi := vdis[0]
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
vdiUuid, err := c.client.VDI.GetUUID(c.session, vdi)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", self.VdiName, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put(self.VdiUuidKey, vdiUuid)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepFindVdi) Cleanup(state multistep.StateBag) {}
|