From f42df93e8d338bfb2e5ed3c1996946a0c684014e Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Fri, 30 Jun 2017 01:08:49 +0300 Subject: [PATCH] destroy artifact --- artifact.go | 18 +++++++++++++++++- builder.go | 3 ++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/artifact.go b/artifact.go index 53aef4206..8094bf1dc 100644 --- a/artifact.go +++ b/artifact.go @@ -1,9 +1,15 @@ package main +import ( + "github.com/vmware/govmomi/object" + "context" +) + const BuilderId = "jetbrains.vsphere" type Artifact struct { - VMName string `json:"vm_name"` + VMName string + Conn *object.VirtualMachine } func (a *Artifact) BuilderId() string { @@ -27,5 +33,15 @@ func (a *Artifact) State(name string) interface{} { } func (a *Artifact) Destroy() error { + ctx := context.TODO() + task, err := a.Conn.Destroy(ctx) + if err != nil { + return err + } + _, err = task.WaitForResult(ctx, nil) + if err != nil { + return err + } + return nil } diff --git a/builder.go b/builder.go index 64e26c744..97f9b1167 100644 --- a/builder.go +++ b/builder.go @@ -14,6 +14,7 @@ import ( "context" "net/url" "github.com/vmware/govmomi/find" + "github.com/vmware/govmomi/object" ) type Builder struct { @@ -121,9 +122,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return nil, errors.New("Build was halted.") } - // No errors, must've worked artifact := &Artifact{ VMName: b.config.VMName, + Conn: state.Get("vm").(*object.VirtualMachine), } return artifact, nil }