builder/vsphere-clone: Find the vm within the folder (#8938)

This commit is contained in:
Rui Lopes 2020-03-24 15:28:00 +00:00 committed by GitHub
parent b17b211aa9
commit c387dc2c53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -49,17 +49,18 @@ type StepCloneVM struct {
func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver) d := state.Get("driver").(*driver.Driver)
vmPath := fmt.Sprintf("%s/%s", s.Location.Folder, s.Location.VMName)
vm, err := d.FindVM(s.Location.VMName) vm, err := d.FindVM(vmPath)
if s.Force == false && err == nil { if s.Force == false && err == nil {
state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it", s.Location.VMName)) state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it", vmPath))
return multistep.ActionHalt return multistep.ActionHalt
} else if s.Force == true && err == nil { } else if s.Force == true && err == nil {
ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", s.Location.VMName)) ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", vmPath))
err := vm.Destroy() err := vm.Destroy()
if err != nil { if err != nil {
state.Put("error", fmt.Errorf("error destroying %s: %v", s.Location.VMName, err)) state.Put("error", fmt.Errorf("error destroying %s: %v", vmPath, err))
} }
} }

View File

@ -94,17 +94,18 @@ type StepCreateVM struct {
func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver) d := state.Get("driver").(*driver.Driver)
vmPath := fmt.Sprintf("%s/%s", s.Location.Folder, s.Location.VMName)
vm, err := d.FindVM(s.Location.VMName) vm, err := d.FindVM(vmPath)
if s.Force == false && err == nil { if s.Force == false && err == nil {
state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it: %v", s.Location.VMName, err)) state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it: %v", vmPath, err))
return multistep.ActionHalt return multistep.ActionHalt
} else if s.Force == true && err == nil { } else if s.Force == true && err == nil {
ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", s.Location.VMName)) ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", vmPath))
err := vm.Destroy() err := vm.Destroy()
if err != nil { if err != nil {
state.Put("error", fmt.Errorf("error destroying %s: %v", s.Location.VMName, err)) state.Put("error", fmt.Errorf("error destroying %s: %v", vmPath, err))
} }
} }