Skip to content

Commit

Permalink
v1.1.1, fix using ova source
Browse files Browse the repository at this point in the history
Former-commit-id: faadce6
  • Loading branch information
josenk committed Oct 9, 2018
1 parent a92d3c8 commit 4baf909
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Known issues with vmware_esxi

Version History
---------------
* 1.1.1 Fix, unable to provision ova sources. go fmt.
* 1.1.0 Add Import support.
* 1.0.2 Switch authentication method to Keyboard Interactive. Read disk_type (thin, thick, etc)
* 1.0.1 Validate DiskStores and refresh
Expand Down
22 changes: 19 additions & 3 deletions esxi/guest-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
)

func guestCREATE(c *Config, guest_name string, disk_store string,
Expand Down Expand Up @@ -171,13 +173,27 @@ func guestCREATE(c *Config, guest_name string, disk_store string,

} else {
// Build VM by ovftool

// Check if source file exist.
if !strings.HasPrefix(src_path, "vi://") {
if _, err := os.Stat(src_path); os.IsNotExist(err) {
return "", fmt.Errorf("File not found: %s\n", src_path)
}
}

// Set params for ovftool
if boot_disk_type == "zeroedthick" {
boot_disk_type = "thick"
}
dst_path := fmt.Sprintf("vi://%s:%s@%s/%s", c.esxiUserName, c.esxiPassword, c.esxiHostName, resource_pool_name)

net_param := ""
if (strings.HasSuffix(src_path, ".ova") || strings.HasSuffix(src_path, ".ovf")) && virtual_networks[0][0] != "" {
net_param = " --network=\"" + virtual_networks[0][0] + "\""
}

ovf_cmd := fmt.Sprintf("ovftool --acceptAllEulas --noSSLVerify --X:useMacNaming=false "+
"-dm=%s --name='%s' --overwrite -ds='%s' '%s' '%s'", boot_disk_type, guest_name, disk_store, src_path, dst_path)
"-dm=%s --name='%s' --overwrite -ds='%s' %s '%s' '%s'", boot_disk_type, guest_name, disk_store, net_param, src_path, dst_path)
cmd := exec.Command("/bin/bash", "-c", ovf_cmd)

log.Println("[guestCREATE] ovf_cmd: " + ovf_cmd)
Expand All @@ -186,8 +202,8 @@ func guestCREATE(c *Config, guest_name string, disk_store string,
err = cmd.Run()
log.Printf("[guestCREATE] ovftool output: %q\n", out.String())
if err != nil {
log.Printf("Failed, There was an ovftool Error:%s\n", err.Error())
return "", fmt.Errorf("There was an ovftool Error:%s\n", err.Error())
log.Printf("Failed, There was an ovftool Error: %s\n%s\n", out.String(), err.Error())
return "", fmt.Errorf("There was an ovftool Error: %s\n%s\n", out.String(), err.Error())
}

}
Expand Down
9 changes: 8 additions & 1 deletion esxi/guest-read.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceGUESTRead(d *schema.ResourceData, m interface{}) error {
d.Set("guest_name", guest_name)
d.Set("disk_store", disk_store)
d.Set("disk_size", disk_size)
if boot_disk_type != "Unknown" {
if boot_disk_type != "Unknown" && boot_disk_type != "" {
d.Set("boot_disk_type", boot_disk_type)
}
d.Set("resource_pool_name", resource_pool_name)
Expand Down Expand Up @@ -160,6 +160,13 @@ func guestREAD(c *Config, vmid string, guest_startup_timeout int) (string, strin
numvcpus = nr.Replace(stdout)
log.Printf("[guestREAD] numvcpus found: %s\n", numvcpus)

case strings.Contains(scanner.Text(), "numa.autosize.vcpu."):
r, _ = regexp.Compile(`\".*\"`)
stdout = r.FindString(scanner.Text())
nr = strings.NewReplacer(`"`, "", `"`, "")
numvcpus = nr.Replace(stdout)
log.Printf("[guestREAD] numa.vcpu (numvcpus) found: %s\n", numvcpus)

case strings.Contains(scanner.Text(), "virtualHW.version = "):
r, _ = regexp.Compile(`\".*\"`)
stdout = r.FindString(scanner.Text())
Expand Down
15 changes: 13 additions & 2 deletions esxi/resource_guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func resourceGUESTCreate(d *schema.ResourceData, m interface{}) error {
guest_shutdown_timeout := d.Get("guest_shutdown_timeout").(int)
guest_startup_timeout := d.Get("guest_startup_timeout").(int)

saved_boot_disk_type := boot_disk_type
saved_numvcpus := numvcpus

// Validations
if resource_pool_name == "ha-root-pool" {
resource_pool_name = "/"
Expand Down Expand Up @@ -318,12 +321,20 @@ func resourceGUESTCreate(d *schema.ResourceData, m interface{}) error {
d.Set("guest_name", guest_name)
d.Set("disk_store", disk_store)
d.Set("disk_size", disk_size)
if boot_disk_type != "Unknown" {
if boot_disk_type == "Unknown" || boot_disk_type == "" {
if saved_boot_disk_type != "" {
d.Set("boot_disk_type", saved_boot_disk_type)
}
} else {
d.Set("boot_disk_type", boot_disk_type)
}
d.Set("resource_pool_name", resource_pool_name)
d.Set("memsize", memsize)
d.Set("numvcpus", numvcpus)
if numvcpus != "" {
d.Set("numvcpus", numvcpus)
} else {
d.Set("numvcpus", saved_numvcpus)
}
d.Set("virthwver", virthwver)
d.Set("guestos", guestos)
d.Set("ip_address", ip_address)
Expand Down
2 changes: 1 addition & 1 deletion esxi/virtual-disk_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func resourceVIRTUALDISKCreate(d *schema.ResourceData, m interface{}) error {
} else {
log.Println("[resourceVIRTUALDISKCreate] Error: " + err.Error())
d.SetId("")
return fmt.Errorf("Failed to create virtual Disk :%s\nError:%s", virtual_disk_name, err.Error())
return fmt.Errorf("Failed to create virtual Disk: %s\nError: %s", virtual_disk_name, err.Error())
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions esxi/virtual-disk_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func diskStoreValidate(c *Config, disk_store string) error {
remote_cmd = fmt.Sprintf("esxcli storage filesystem list | grep '/vmfs/volumes/.*[VMFS|NFS]' | awk '{print $2}'")
stdout, err = runRemoteSshCommand(esxiSSHinfo, remote_cmd, "Get list of disk stores")
if err != nil {
return fmt.Errorf("Unable to get list of disk stores :%s\n", err)
return fmt.Errorf("Unable to get list of disk stores: %s\n", err)
}
log.Printf("1: Available Disk Stores :%s\n", strings.Replace(stdout, "\n", " ", -1))
log.Printf("1: Available Disk Stores: %s\n", strings.Replace(stdout, "\n", " ", -1))

if strings.Contains(stdout, disk_store) == false {
remote_cmd = fmt.Sprintf("esxcli storage filesystem rescan")
Expand All @@ -35,12 +35,12 @@ func diskStoreValidate(c *Config, disk_store string) error {
remote_cmd = fmt.Sprintf("esxcli storage filesystem list | grep '/vmfs/volumes/.*[VMFS|NFS]' | awk '{print $2}'")
stdout, err = runRemoteSshCommand(esxiSSHinfo, remote_cmd, "Get list of disk stores")
if err != nil {
return fmt.Errorf("Unable to get list of disk stores :%s\n", err)
return fmt.Errorf("Unable to get list of disk stores: %s\n", err)
}
log.Printf("2: Available Disk Stores :%s\n", strings.Replace(stdout, "\n", " ", -1))
log.Printf("2: Available Disk Stores: %s\n", strings.Replace(stdout, "\n", " ", -1))

if strings.Contains(stdout, disk_store) == false {
return fmt.Errorf("Disk Store %s does not exist.\nAvailable Disk Stores :%s\n", disk_store, stdout)
return fmt.Errorf("Disk Store %s does not exist.\nAvailable Disk Stores: %s\n", disk_store, stdout)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/terraform-providers/terraform-provider-esxi/esxi"
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-esxi/esxi"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.1.0
v1.1.1

0 comments on commit 4baf909

Please sign in to comment.