forked from josenk/terraform-provider-esxi
/
guest-delete.go
42 lines (34 loc) · 1.05 KB
/
guest-delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package esxi
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"time"
)
func resourceGUESTDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*Config)
esxiSSHinfo := SshConnectionStruct{c.esxiHostName, c.esxiHostPort, c.esxiUserName, c.esxiPassword}
var remote_cmd, stdout string
var err error
vmid := d.Id()
guest_shutdown_timeout := d.Get("guest_shutdown_timeout").(int)
_, err = guestPowerOff(c, vmid, guest_shutdown_timeout)
if err != nil {
return err
}
// remove storage from vmx so it doesn't get deleted by the vim-cmd destroy
err = cleanStorageFromVmx(c, vmid)
if err != nil {
log.Printf("[resourceGUESTDelete] Failed clean storage from vmid: %s (to be deleted)\n", vmid)
}
time.Sleep(5 * time.Second)
remote_cmd = fmt.Sprintf("vim-cmd vmsvc/destroy %s", vmid)
stdout, err = runRemoteSshCommand(esxiSSHinfo, remote_cmd, "vmsvc/destroy")
if err != nil {
// todo more descriptive err message
log.Printf("[resourceGUESTDelete] Failed destroy vmid: %s\n", stdout)
return err
}
d.SetId("")
return nil
}