Update destroy vifs implementation
This commit is contained in:
parent
feb8791dec
commit
374a26c895
@ -107,6 +107,7 @@ type FlatConfig struct {
|
||||
PlatformArgs map[string]string `mapstructure:"platform_args" cty:"platform_args" hcl:"platform_args"`
|
||||
RawInstallTimeout *string `mapstructure:"install_timeout" cty:"install_timeout" hcl:"install_timeout"`
|
||||
SourcePath *string `mapstructure:"source_path" cty:"source_path" hcl:"source_path"`
|
||||
DestroyVIFs *bool `mapstructure:"destroy_vifs" cty:"destroy_vifs" hcl:"destroy_vifs"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatConfig.
|
||||
@ -218,6 +219,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
||||
"platform_args": &hcldec.AttrSpec{Name: "platform_args", Type: cty.Map(cty.String), Required: false},
|
||||
"install_timeout": &hcldec.AttrSpec{Name: "install_timeout", Type: cty.String, Required: false},
|
||||
"source_path": &hcldec.AttrSpec{Name: "source_path", Type: cty.String, Required: false},
|
||||
"destroy_vifs": &hcldec.AttrSpec{Name: "destroy_vifs", Type: cty.Bool, Required: false},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
@ -1,40 +1,43 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
xsclient "github.com/xenserver/go-xenserver-client"
|
||||
"log"
|
||||
|
||||
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
||||
"github.com/hashicorp/packer-plugin-sdk/packer"
|
||||
)
|
||||
|
||||
type StepDestroyVIFs struct{}
|
||||
|
||||
func (self *StepDestroyVIFs) Run(state multistep.StateBag) multistep.StepAction {
|
||||
config := state.Get("commonconfig").(CommonConfig)
|
||||
func (self *StepDestroyVIFs) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
c := state.Get("client").(*Connection)
|
||||
config := state.Get("config").(Config)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
|
||||
if !config.DestroyVIFs {
|
||||
log.Printf("Not destroying VIFs")
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
client := state.Get("client").(xsclient.XenAPIClient)
|
||||
|
||||
ui.Say("Step: Destroy VIFs")
|
||||
|
||||
uuid := state.Get("instance_uuid").(string)
|
||||
instance, err := client.GetVMByUuid(uuid)
|
||||
instance, err := c.client.VM.GetByUUID(c.session, uuid)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
vifs, err := instance.GetVIFs()
|
||||
vifs, err := c.client.VM.GetVIFs(c.session, instance)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Error getting VIFs: %s", err.Error()))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
for _, vif := range vifs {
|
||||
err = vif.Destroy()
|
||||
err = c.client.VIF.Destroy(c.session, vif)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Error destroying VIF: %s", err.Error()))
|
||||
return multistep.ActionHalt
|
||||
|
Loading…
Reference in New Issue
Block a user