use script options struct rather than passing all the variables around an extra time
This commit is contained in:
parent
a1b3b63cb9
commit
9557f3eea7
|
@ -232,32 +232,15 @@ Hyper-V\Set-VMFloppyDiskDrive -VMName $vmName -Path $null
|
||||||
// Hyper-V\New-VHD -Path $vhdPath -SizeBytes 8192 -BlockSizeBytes 10
|
// Hyper-V\New-VHD -Path $vhdPath -SizeBytes 8192 -BlockSizeBytes 10
|
||||||
// Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch
|
// Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch
|
||||||
//
|
//
|
||||||
func getCreateVMScript(vmName string, path string, harddrivePath string, ram int64,
|
func getCreateVMScript(opts *scriptOptions) (string, error) {
|
||||||
diskSize int64, diskBlockSize int64, switchName string, generation uint,
|
|
||||||
diffDisks bool, fixedVHD bool, version string) (string, error) {
|
|
||||||
|
|
||||||
if fixedVHD && generation == 2 {
|
if opts.FixedVHD && opts.Generation == 2 {
|
||||||
return "", fmt.Errorf("Generation 2 VMs don't support fixed disks.")
|
return "", fmt.Errorf("Generation 2 VMs don't support fixed disks.")
|
||||||
}
|
}
|
||||||
|
|
||||||
vhdx := vmName + ".vhdx"
|
opts.VHDX = opts.VMName + ".vhdx"
|
||||||
if fixedVHD {
|
if opts.FixedVHD {
|
||||||
vhdx = vmName + ".vhd"
|
opts.VHDX = opts.VMName + ".vhd"
|
||||||
}
|
|
||||||
|
|
||||||
opts := scriptOptions{
|
|
||||||
Version: version,
|
|
||||||
VMName: vmName,
|
|
||||||
VHDX: vhdx,
|
|
||||||
Path: path,
|
|
||||||
HardDrivePath: harddrivePath,
|
|
||||||
MemoryStartupBytes: ram,
|
|
||||||
NewVHDSizeBytes: diskSize,
|
|
||||||
VHDBlockSizeBytes: diskBlockSize,
|
|
||||||
SwitchName: switchName,
|
|
||||||
Generation: generation,
|
|
||||||
DiffDisks: diffDisks,
|
|
||||||
FixedVHD: fixedVHD,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var tpl = template.Must(template.New("createVM").Parse(`
|
var tpl = template.Must(template.New("createVM").Parse(`
|
||||||
|
@ -304,9 +287,21 @@ func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram
|
||||||
diskSize int64, diskBlockSize int64, switchName string, generation uint,
|
diskSize int64, diskBlockSize int64, switchName string, generation uint,
|
||||||
diffDisks bool, fixedVHD bool, version string) error {
|
diffDisks bool, fixedVHD bool, version string) error {
|
||||||
|
|
||||||
script, err := getCreateVMScript(vmName, path, harddrivePath, ram,
|
opts := scriptOptions{
|
||||||
diskSize, diskBlockSize, switchName, generation,
|
Version: version,
|
||||||
diffDisks, fixedVHD, version)
|
VMName: vmName,
|
||||||
|
Path: path,
|
||||||
|
HardDrivePath: harddrivePath,
|
||||||
|
MemoryStartupBytes: ram,
|
||||||
|
NewVHDSizeBytes: diskSize,
|
||||||
|
VHDBlockSizeBytes: diskBlockSize,
|
||||||
|
SwitchName: switchName,
|
||||||
|
Generation: generation,
|
||||||
|
DiffDisks: diffDisks,
|
||||||
|
FixedVHD: fixedVHD,
|
||||||
|
}
|
||||||
|
|
||||||
|
script, err := getCreateVMScript(&opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,22 +6,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_getCreateVMScript(t *testing.T) {
|
func Test_getCreateVMScript(t *testing.T) {
|
||||||
vmName := "myvm"
|
opts := scriptOptions{
|
||||||
path := "C://mypath"
|
Version: "5.0",
|
||||||
harddrivepath := "C://harddrivepath"
|
VMName: "myvm",
|
||||||
ram := int64(1024)
|
Path: "C://mypath",
|
||||||
disksize := int64(8192)
|
HardDrivePath: "C://harddrivepath",
|
||||||
diskBlockSize := int64(10)
|
MemoryStartupBytes: int64(1024),
|
||||||
switchName := "hyperv-vmx-switch"
|
NewVHDSizeBytes: int64(8192),
|
||||||
generation := uint(1)
|
VHDBlockSizeBytes: int64(10),
|
||||||
diffdisks := true
|
SwitchName: "hyperv-vmx-switch",
|
||||||
fixedVHD := true
|
Generation: uint(1),
|
||||||
version := "5.0"
|
DiffDisks: true,
|
||||||
|
FixedVHD: true,
|
||||||
|
}
|
||||||
|
|
||||||
// Check Fixed VHD conditional set
|
// Check Fixed VHD conditional set
|
||||||
scriptString, err := getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err := getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -35,19 +35,15 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
|
||||||
|
|
||||||
// We should never get here thanks to good template validation, but it's
|
// We should never get here thanks to good template validation, but it's
|
||||||
// good to fail rather than trying to run the ps script and erroring.
|
// good to fail rather than trying to run the ps script and erroring.
|
||||||
generation = uint(2)
|
opts.Generation = uint(2)
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("Should have Error: %s", err.Error())
|
t.Fatalf("Should have Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check VHDX conditional set
|
// Check VHDX conditional set
|
||||||
fixedVHD = false
|
opts.FixedVHD = false
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -60,11 +56,9 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check generation 1 no fixed VHD
|
// Check generation 1 no fixed VHD
|
||||||
fixedVHD = false
|
opts.FixedVHD = false
|
||||||
generation = uint(1)
|
opts.Generation = uint(1)
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -77,10 +71,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that we use generation one template even if generation is unset
|
// Check that we use generation one template even if generation is unset
|
||||||
generation = uint(0)
|
opts.Generation = uint(0)
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -89,10 +81,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
|
||||||
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
||||||
}
|
}
|
||||||
|
|
||||||
version = ""
|
opts.Version = ""
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -103,10 +93,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
|
||||||
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
||||||
}
|
}
|
||||||
|
|
||||||
diffdisks = false
|
opts.DiffDisks = false
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -117,10 +105,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
|
||||||
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
||||||
}
|
}
|
||||||
|
|
||||||
harddrivepath = ""
|
opts.HardDrivePath = ""
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -131,10 +117,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
|
||||||
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
|
||||||
}
|
}
|
||||||
|
|
||||||
fixedVHD = true
|
opts.FixedVHD = true
|
||||||
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
|
scriptString, err = getCreateVMScript(&opts)
|
||||||
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
|
|
||||||
version)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue