Skip to content

Commit

Permalink
Fixed bug created from latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondshtogu committed Aug 3, 2023
1 parent a9c7ed0 commit 7fadd8e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 36 deletions.
14 changes: 13 additions & 1 deletion examples/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ func getConfig(t *testing.T) map[string]string {
t.Skipf("Skipping test due failure on reading .env file! Err: %s", err)
return nil
}
defer file.Close()
defer func(file *os.File) {
e := file.Close()
if e != nil {
log.Fatal(e)
}
}(file)

// Read the file line by line
scanner := bufio.NewScanner(file)
Expand Down Expand Up @@ -74,10 +79,17 @@ func getCwd(t *testing.T) string {

func getBaseOptions(t *testing.T) integration.ProgramTestOptions {
config := getConfig(t)
localProviders := make([]integration.LocalDependency, 1)
localProviders[0] = integration.LocalDependency{
Package: "github.com/pulumiverse/pulumi-esxi-native/provider",
Path: filepath.Join(getCwd(t), "../provider"),
}

return integration.ProgramTestOptions{
Config: config,
ExpectRefreshChanges: true,
SkipRefresh: true,
Quick: true,
LocalProviders: localProviders,
}
}
3 changes: 2 additions & 1 deletion provider/cmd/pulumi-resource-esxi-native/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"password": {
"type": "string",
"description": "ESXi Password config"
"description": "ESXi Password config",
"secret": true
}
}
},
Expand Down
11 changes: 5 additions & 6 deletions provider/pkg/esxi/connectionInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
)

type ConnectionInfo struct {
Host string
SSHPort string
SslPort string
UserName string
Password string
OvfLocation string
Host string
SSHPort string
SslPort string
UserName string
Password string
}

func (c *ConnectionInfo) getSSHConnection() string {
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/esxi/virtualMachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func parseVirtualMachine(id string, inputs resource.PropertyMap, connection *Con

vm.Name = inputs["name"].StringValue()
vm.SourcePath = parseSourcePath(inputs, connection)
vm.BootFirmware = parseStringProperty(inputs, "bootFirmware", "")
vm.BootFirmware = parseStringProperty(inputs, "bootFirmware", "bios")
vm.DiskStore = inputs["diskStore"].StringValue()
vm.ResourcePoolName = parseStringProperty(inputs, "resourcePoolName", "/")
if vm.ResourcePoolName == rootPool {
Expand Down
31 changes: 17 additions & 14 deletions provider/pkg/esxi/virtualMacineUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ ide1:0.clientDevice = "TRUE"`
return VirtualMachine{}, fmt.Errorf("failed to use Resource Pool ID:%s", poolID)
}
command = fmt.Sprintf("vim-cmd solo/registervm \"%s\" %s %s", dstVmxFile, vm.Name, poolID)
_, err = esxi.Execute(command, "solo/registervm")
id, err := esxi.Execute(command, "solo/registervm")
if err != nil {
command = fmt.Sprintf("rm -fr \"%s\"", fullPATH)
_, _ = esxi.Execute(command, "cleanup guest path because of failed events")
return VirtualMachine{}, fmt.Errorf("failed to register guest err:%w", err)
}

vm.Id = id

return vm, nil
}

Expand All @@ -131,19 +133,19 @@ func (esxi *Host) createVirtualMachine(vm VirtualMachine) (VirtualMachine, error
}

// Step 2: Check if guest already exists
id, err := esxi.getOrCreateVirtualMachine(vm)
vm, err = esxi.getOrCreateVirtualMachine(vm)
if err != nil {
return VirtualMachine{}, err
}

// Step 3: Handle OVF properties, if present
err = esxi.handleOvfProperties(id, vm)
err = esxi.handleOvfProperties(vm)
if err != nil {
return VirtualMachine{}, err
}

// Step 4: Grow boot disk to boot_disk_size
err = esxi.growBootDisk(id, vm.BootDiskSize)
err = esxi.growBootDisk(vm.Id, vm.BootDiskSize)
if err != nil {
return VirtualMachine{}, err
}
Expand All @@ -158,10 +160,10 @@ func (esxi *Host) createVirtualMachine(vm VirtualMachine) (VirtualMachine, error
}

// getOrCreateVirtualMachine checks if the virtual machine already exists or creates it if not.
func (esxi *Host) getOrCreateVirtualMachine(vm VirtualMachine) (string, error) {
func (esxi *Host) getOrCreateVirtualMachine(vm VirtualMachine) (VirtualMachine, error) {
id, err := esxi.getVirtualMachineId(vm.Name)
if err != nil {
return "", fmt.Errorf("failed to get VM ID: %w", err)
return VirtualMachine{}, fmt.Errorf("failed to get VM ID: %w", err)
}

switch {
Expand All @@ -175,37 +177,38 @@ func (esxi *Host) getOrCreateVirtualMachine(vm VirtualMachine) (string, error) {
// Create a plain virtual machine
vm, err = esxi.createPlainVirtualMachine(vm)
if err != nil {
return "", err
return VirtualMachine{}, err
}
default:
// Build VM with ovftool or copy from local source
err = esxi.buildVirtualMachineFromSource(vm)
if err != nil {
return "", err
return VirtualMachine{}, err
}

// Retrieve the VM ID after building the virtual machine
id, err = esxi.getVirtualMachineId(vm.Name)
if err != nil {
return "", fmt.Errorf("failed to get VM ID: %w", err)
return VirtualMachine{}, fmt.Errorf("failed to get VM ID: %w", err)
}
vm.Id = id
}

return id, nil
return vm, nil
}

// handleOvfProperties handles OVF properties injection and power off if necessary.
func (esxi *Host) handleOvfProperties(id string, vm VirtualMachine) error {
func (esxi *Host) handleOvfProperties(vm VirtualMachine) error {
if len(vm.OvfProperties) > 0 {
currentPowerState := esxi.getVirtualMachinePowerState(id)
currentPowerState := esxi.getVirtualMachinePowerState(vm.Id)
if currentPowerState != vmTurnedOn {
return fmt.Errorf("failed to power on after ovfProperties injection")
}

// Allow cloud-init to process.
duration := time.Duration(vm.OvfPropertiesTimer) * time.Second
time.Sleep(duration)
esxi.powerOffVirtualMachine(id, vm.ShutdownTimeout)
esxi.powerOffVirtualMachine(vm.Id, vm.ShutdownTimeout)
}
return nil
}
Expand Down Expand Up @@ -465,7 +468,7 @@ func (esxi *Host) updateVmxContents(isNew bool, vm VirtualMachine) error {
return fmt.Errorf("failed to get destination vmx file: %w", err)
}

_, err = esxi.CopyFile(strings.ReplaceAll(vmxContents, "\\\"", "\""), dstVmxFile, "write vmx file")
_, err = esxi.WriteFile(strings.ReplaceAll(vmxContents, "\\\"", "\""), dstVmxFile, "write vmx file")
if err != nil {
return fmt.Errorf("failed to write vmx file: %w", err)
}
Expand Down
24 changes: 12 additions & 12 deletions provider/pkg/schema/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ValidatePortGroup(resourceToken string, inputs resource.PropertyMap) []*pul
}
}

validatePropertyValueInBetween("vlan", 0, maxVlanId, inputs, &failures)
validatePropertyValueInBetween0Max("vlan", maxVlanId, inputs, &failures)

return validateResource(resourceToken, failures)
}
Expand All @@ -63,8 +63,8 @@ func ValidateResourcePool(resourceToken string, inputs resource.PropertyMap) []*
}

// Validate "cpuShares" and "memShares".
validateCPUShares("cpuShares", inputs, &failures)
validateCPUShares("memShares", inputs, &failures)
validateShares("cpuShares", inputs, &failures)
validateShares("memShares", inputs, &failures)

// Validate boolean properties.
booleanProps := []string{"cpuMinExpandable", "memMinExpandable"}
Expand Down Expand Up @@ -104,10 +104,10 @@ func ValidateVirtualMachine(resourceToken string, inputs resource.PropertyMap) [
}

validateDiskType("bootDiskType", inputs, &failures)
validatePropertyValueInBetween("bootDiskSize", 0, maxDiskSize, inputs, &failures)
validatePropertyValueInBetween("shutdownTimeout", 0, maxShutdownTimeout, inputs, &failures)
validatePropertyValueInBetween("startupTimeout", 0, maxStartupTimeout, inputs, &failures)
validatePropertyValueInBetween("ovfPropertiesTimer", 0, maxOvfProperties, inputs, &failures)
validatePropertyValueInBetween0Max("bootDiskSize", maxDiskSize, inputs, &failures)
validatePropertyValueInBetween0Max("shutdownTimeout", maxShutdownTimeout, inputs, &failures)
validatePropertyValueInBetween0Max("startupTimeout", maxStartupTimeout, inputs, &failures)
validatePropertyValueInBetween0Max("ovfPropertiesTimer", maxOvfProperties, inputs, &failures)
validateKeyValuePairsProperty("ovfProperties", inputs, &failures)
validateKeyValuePairsProperty("info", inputs, &failures)
validateVirtualMachineOs(inputs, &failures)
Expand Down Expand Up @@ -139,16 +139,16 @@ func checkRequiredProperty(property string, inputs resource.PropertyMap, failure
}
}

func validatePropertyValueInBetween(key string, min, max float64, inputs resource.PropertyMap, failures *map[string]string) {
if val, has := inputs[resource.PropertyKey(key)]; has && val.NumberValue() < min || val.NumberValue() > max {
(*failures)[key] = fmt.Sprintf(invalidFormat, key, fmt.Sprintf("expected to be in the range (%f - %f), got %f", min, max, val.NumberValue()))
func validatePropertyValueInBetween0Max(key string, max float64, inputs resource.PropertyMap, failures *map[string]string) {
if val, has := inputs[resource.PropertyKey(key)]; has && val.NumberValue() < 0 || val.NumberValue() > max {
(*failures)[key] = fmt.Sprintf(invalidFormat, key, fmt.Sprintf("expected to be in the range (0 - %f), got %f", max, val.NumberValue()))
}
}

func validateCPUShares(key string, inputs resource.PropertyMap, failures *map[string]string) {
func validateShares(key string, inputs resource.PropertyMap, failures *map[string]string) {
if value, has := inputs[resource.PropertyKey(key)]; has {
strVal := value.StringValue()
if _, err := strconv.Atoi(strVal); !contains([]string{"low", "normal", "high"}, strVal) || err != nil {
if _, err := strconv.Atoi(strVal); !contains([]string{"low", "normal", "high"}, strVal) && err != nil {
(*failures)[key] = fmt.Sprintf(invalidFormat, key, fmt.Sprintf("must be low/normal/high/<custom> (%s)", err))
}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/dotnet/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.2-alpha.1691066271+5712c9bf.dirty
0.0.2-alpha.1691081005+a9c7ed06.dirty

0 comments on commit 7fadd8e

Please sign in to comment.