Browse Source

New util file for vmware provider and some initial work towards better errors.

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
master
Erik Hollensbe 6 years ago
parent
commit
1c2c56d7fc
  1. 10
      providers/vmware/disk.go
  2. 4
      providers/vmware/disk_test.go
  3. 11
      providers/vmware/ethernet.go
  4. 7
      providers/vmware/util.go
  5. 20
      providers/vmware/vmx.go

10
providers/vmware/disk.go

@ -47,7 +47,7 @@ func (vm *Provider) NewDisk(filename string, typ types.DiskType, size int64) *Di
// NewDiskFromParams constructs a *Disk for use from a VMXParams provided by
// the user.
func (vm *Provider) NewDiskFromParams(vp VMXParams) *Disk {
func (vm *Provider) NewDiskFromParams(vp VMXParams) (*Disk, error) {
vmd := &Disk{provider: vm}
for key, value := range vp {
@ -66,13 +66,17 @@ func (vm *Provider) NewDiskFromParams(vp VMXParams) *Disk {
switch parts[1] {
case "present":
vmd.Present = value == "TRUE"
ok, err := vmwareBool(value)
if err != nil {
return nil, err
}
vmd.Present = ok
case "filename":
vmd.Filename = value
}
}
return vmd
return vmd, nil
}
// Attached returns whether or not the VM is using or will use this disk on

4
providers/vmware/disk_test.go

@ -30,7 +30,9 @@ func (vs *vmwareSuite) TestNewDisk(c *C) {
func (vs *vmwareSuite) TestNewDiskFromParams(c *C) {
vm := &Provider{}
c.Assert(vm.NewDiskFromParams(dummyParams), DeepEquals, &Disk{provider: vm})
disk, err := vm.NewDiskFromParams(dummyParams)
c.Assert(err, IsNil)
c.Assert(disk, DeepEquals, &Disk{provider: vm})
}
func (vs *vmwareSuite) TestDiskInterface(c *C) {

11
providers/vmware/ethernet.go

@ -22,7 +22,7 @@ type Ethernet struct {
}
// NewEthernetFromParams creates a new Ethernet device
func NewEthernetFromParams(vp VMXParams) *Ethernet {
func NewEthernetFromParams(vp VMXParams) (*Ethernet, error) {
vme := &Ethernet{}
for key, value := range vp {
parts := strings.SplitN(key, ".", 2)
@ -36,7 +36,12 @@ func NewEthernetFromParams(vp VMXParams) *Ethernet {
case "connectiontype":
vme.ConnectionType = value
case "present":
vme.Present = value == "TRUE"
ok, err := vmwareBool(value)
if err != nil {
return nil, err
}
vme.Present = ok
case "vnet":
vme.VNet = value
case "virtualdev":
@ -49,7 +54,7 @@ func NewEthernetFromParams(vp VMXParams) *Ethernet {
vme.GeneratedHWAddrOffset = value
}
}
return vme
return vme, nil
}
// Attach attaches an Ethernet Device to the VM.

7
providers/vmware/util.go

@ -0,0 +1,7 @@
package vmware
import "strconv"
func vmwareBool(b string) (bool, error) {
return strconv.ParseBool(b)
}

20
providers/vmware/vmx.go

@ -1,6 +1,7 @@
package vmware
import (
"fmt"
"regexp"
"sort"
"strings"
@ -130,15 +131,20 @@ func sortKeys(deviceMap map[string]VMXParams) []string {
return keys
}
func (v *VMX) loadEthernets(vp VMXParams) {
func (v *VMX) loadEthernets(vp VMXParams) error {
deviceMap := vp.loadDevices(vmxEthernetRegexp.MatchString)
ethernets := []types.Ethernet{}
for _, key := range sortKeys(deviceMap) {
ethernets = append(ethernets, NewEthernetFromParams(deviceMap[key]))
eth, err := NewEthernetFromParams(deviceMap[key])
if err != nil {
return fmt.Errorf("Loading Ethernet Devices: %v", err)
}
ethernets = append(ethernets, eth)
}
v.VMEthernets = ethernets
return nil
}
func diskMatcher(diskMap map[*regexp.Regexp]types.DiskType) func(string) bool {
@ -153,15 +159,21 @@ func diskMatcher(diskMap map[*regexp.Regexp]types.DiskType) func(string) bool {
}
}
func (v *VMX) loadDisks(vp VMXParams) {
func (v *VMX) loadDisks(vp VMXParams) error {
deviceMap := vp.loadDevices(diskMatcher(vmxDiskNameMap))
disks := []types.Disk{}
for _, key := range sortKeys(deviceMap) {
disks = append(disks, v.provider.NewDiskFromParams(deviceMap[key]))
disk, err := v.provider.NewDiskFromParams(deviceMap[key])
if err != nil {
return fmt.Errorf("Loading disks: %v", err)
}
disks = append(disks, disk)
}
v.VMDisks = disks
return nil
}
func (v *VMX) loadDiskControllers(vp VMXParams) {

Loading…
Cancel
Save