Skip to content

Commit

Permalink
Fixed result parsing...
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondshtogu committed Jun 30, 2023
1 parent 53c046d commit 82d6150
Show file tree
Hide file tree
Showing 16 changed files with 996 additions and 37 deletions.
106 changes: 106 additions & 0 deletions provider/cmd/pulumi-resource-esxi-native/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,112 @@
}
}
}
},
"esxi-native:index:getVirtualMachineById": {
"inputs": {
"properties": {
"id": {
"type": "string",
"description": "Virtual Machine Id to get details of"
}
},
"required": [
"id"
]
},
"outputs": {
"properties": {
"id": {
"type": "string",
"description": "esxi vm id."
},
"name": {
"type": "string",
"description": "esxi vm name."
},
"bootFirmware": {
"type": "string",
"$ref": "#/types/esxi-native:index:BootFirmwareType",
"description": "Boot type('efi' is boot uefi mode)"
},
"diskStore": {
"type": "string",
"description": "esxi diskstore for boot disk."
},
"resourcePoolName": {
"type": "string",
"description": "Resource pool name to place vm."
},
"bootDiskSize": {
"type": "string",
"description": "VM boot disk size. Will expand boot disk to this size."
},
"bootDiskType": {
"type": "string",
"$ref": "#/types/esxi-native:index:DiskType",
"description": "VM boot disk type. thin, zeroedthick, eagerzeroedthick"
},
"memSize": {
"type": "string",
"description": "VM memory size."
},
"numVCpus": {
"type": "string",
"description": "VM number of virtual cpus."
},
"virtualHWVer": {
"type": "string",
"description": "VM Virtual HW version."
},
"os": {
"type": "string",
"description": "VM OS type."
},
"networkInterfaces": {
"type": "array",
"description": "VM network interfaces.",
"items": {
"$ref": "#/types/esxi-native:index:NetworkInterface"
}
},
"power": {
"type": "string",
"description": "VM power state."
},
"ipAddress": {
"type": "string",
"description": "The IP address reported by VMWare tools."
},
"startupTimeout": {
"type": "integer",
"description": "The amount of vm uptime, in seconds, to wait for an available IP address on this virtual machine.",
"default": 600
},
"shutdownTimeout": {
"type": "integer",
"description": "The amount of vm uptime, in seconds, to wait for an available IP address on this virtual machine.",
"default": 600
},
"virtualDisks": {
"type": "array",
"description": "VM virtual disks.",
"items": {
"$ref": "#/types/esxi-native:index:VMVirtualDisk"
}
},
"notes": {
"type": "string",
"description": "VM memory size."
},
"info": {
"type": "array",
"description": "pass data to VM",
"items": {
"$ref": "#/types/esxi-native:index:KeyValuePair"
}
}
}
}
}
},
"language": {
Expand Down
6 changes: 3 additions & 3 deletions provider/pkg/esxi/mapUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (vm *VirtualMachine) toMap(keepId ...bool) map[string]interface{} {
delete(outputs, "ovfProperties")
delete(outputs, "ovfPropertiesTimer")

if vm.BootDiskType == "Unknown" || vm.BootDiskType == "" {
if vm.BootDiskType == "Unknown" || len(vm.BootDiskType) == 0 {
delete(outputs, "bootDiskType")
}

Expand All @@ -24,12 +24,12 @@ func (vm *VirtualMachine) toMap(keepId ...bool) map[string]interface{} {
}

// Do network interfaces
if len(vm.NetworkInterfaces) == 0 || vm.NetworkInterfaces[0].VirtualNetwork == "" {
if len(vm.NetworkInterfaces) == 0 || len(vm.NetworkInterfaces[0].VirtualNetwork) == 0 {
delete(outputs, "networkInterfaces")
}

// Do virtual disks
if len(vm.VirtualDisks) == 0 || vm.VirtualDisks[0].VirtualDiskId == "" {
if len(vm.VirtualDisks) == 0 || len(vm.VirtualDisks[0].VirtualDiskId) == 0 {
delete(outputs, "virtualDisks")
}

Expand Down
43 changes: 26 additions & 17 deletions provider/pkg/esxi/resourceService.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewResourceService() *ResourceService {
"esxi-native:index:VirtualMachine:Delete": functionMapper{VirtualMachineDeleteParser, VirtualMachineDelete},
"esxi-native:index:VirtualMachine:Read": functionMapper{VirtualMachineReadParser, VirtualMachineRead},
"esxi-native:index:getVirtualMachine": functionMapper{VirtualMachineGetParser, VirtualMachineGet},
"esxi-native:index:getVirtualMachineById": functionMapper{VirtualMachineGetByIdParser, VirtualMachineGetById},
"esxi-native:index:VirtualSwitch:Create": functionMapper{VirtualSwitchCreateParser, VirtualSwitchCreate},
"esxi-native:index:VirtualSwitch:Update": functionMapper{VirtualSwitchUpdateParser, VirtualSwitchUpdate},
"esxi-native:index:VirtualSwitch:Delete": functionMapper{VirtualSwitchDeleteParser, VirtualSwitchDelete},
Expand All @@ -44,45 +45,53 @@ func NewResourceService() *ResourceService {
}
}

func (receiver *ResourceService) Create(token string, inputs resource.PropertyMap, esxi *Host) (rId string, result resource.PropertyMap, err error) {
func (receiver *ResourceService) Create(token string, inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) {
token = fmt.Sprintf("%s:Create", token)
return receiver.call(token, "", inputs, esxi)
}

func (receiver *ResourceService) Update(token string, id string, inputs resource.PropertyMap, esxi *Host) (rId string, result resource.PropertyMap, err error) {
func (receiver *ResourceService) Update(token string, id string, inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) {
token = fmt.Sprintf("%s:Update", token)
return receiver.call(token, id, inputs, esxi)
}

func (receiver *ResourceService) Delete(token string, id string, inputs resource.PropertyMap, esxi *Host) (rId string, result resource.PropertyMap, err error) {
func (receiver *ResourceService) Delete(token string, id string, inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) {
token = fmt.Sprintf("%s:Delete", token)
return receiver.call(token, id, inputs, esxi)
}

func (receiver *ResourceService) Read(token string, id string, inputs resource.PropertyMap, esxi *Host) (rId string, result resource.PropertyMap, err error) {
func (receiver *ResourceService) Read(token string, id string, inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) {
token = fmt.Sprintf("%s:Read", token)
return receiver.call(token, id, inputs, esxi)
}

func (receiver *ResourceService) Invoke(token string, inputs resource.PropertyMap, esxi *Host) (result resource.PropertyMap, err error) {
mapper := receiver.functions[token]
func (receiver *ResourceService) Invoke(token string, inputs resource.PropertyMap, esxi *Host) (resource.PropertyMap, error) {
mapper, ok := receiver.functions[token]
if !ok {
return nil, fmt.Errorf("unknown function '%s'", token)
}
params := mapper.getParams("", inputs, esxi)
functionHandler := reflect.ValueOf(mapper.handler)
var res []reflect.Value
res = functionHandler.Call(params)
result = res[0].Interface().(resource.PropertyMap)
return
var functionResult []reflect.Value
functionResult = functionHandler.Call(params)
result := functionResult[0].Interface().(resource.PropertyMap)
err := functionResult[1].Interface().(error)
return result, err
}

func (receiver *ResourceService) call(token string, id string, inputs resource.PropertyMap, esxi *Host) (rId string, result resource.PropertyMap, err error) {
mapper := receiver.functions[token]
func (receiver *ResourceService) call(token string, id string, inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) {
mapper, ok := receiver.functions[token]
if !ok {
return "", nil, fmt.Errorf("unknown operation '%s'", token)
}
params := mapper.getParams(id, inputs, esxi)
functionHandler := reflect.ValueOf(mapper.handler)
var res []reflect.Value
res = functionHandler.Call(params)
rId = res[0].Interface().(string)
result = res[1].Interface().(resource.PropertyMap)
return
var functionResult []reflect.Value
functionResult = functionHandler.Call(params)
resourceId := functionResult[0].Interface().(string)
resourceData := functionResult[1].Interface().(resource.PropertyMap)
err := functionResult[2].Interface().(error)
return resourceId, resourceData, err
}

func (m *functionMapper) getParams(id string, inputs resource.PropertyMap, esxi *Host) []reflect.Value {
Expand Down
22 changes: 17 additions & 5 deletions provider/pkg/esxi/virtualMachineGet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package esxi

import (
"fmt"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
)

Expand All @@ -9,17 +10,28 @@ func VirtualMachineGetParser(inputs resource.PropertyMap) string {
}

func VirtualMachineGet(name string, esxi *Host) (resource.PropertyMap, error) {
id, _ := esxi.getVirtualMachineId(name)
id, err := esxi.getVirtualMachineId(name)
if err != nil {
return nil, fmt.Errorf("unable to find a virtual machine corresponding to the name '%s'", name)
}

return VirtualMachineGetById(id, esxi)
}

func VirtualMachineGetByIdParser(inputs resource.PropertyMap) string {
return inputs["id"].StringValue()
}

vm, err := esxi.readVirtualMachine(VirtualMachine{
func VirtualMachineGetById(id string, esxi *Host) (resource.PropertyMap, error) {
vm := esxi.readVirtualMachine(VirtualMachine{
Id: id,
StartupTimeout: 1,
})

if err != nil || vm.Name == "" {
return nil, err
if len(vm.Name) == 0 {
return nil, fmt.Errorf("unable to find a virtual machine corresponding to the id '%s'", vm.Id)
}

result := vm.toMap(true)
return resource.NewPropertyMapFromMap(result), err
return resource.NewPropertyMapFromMap(result), nil
}
14 changes: 7 additions & 7 deletions provider/pkg/esxi/virtualMachineRead.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func VirtualMachineReadParser(id string, inputs resource.PropertyMap) VirtualMac

func VirtualMachineRead(vm VirtualMachine, esxi *Host) (string, resource.PropertyMap, error) {
// read vm
vm, err := esxi.readVirtualMachine(vm)
vm = esxi.readVirtualMachine(vm)

if err != nil || vm.Name == "" {
return "", nil, err
if len(vm.Name) == 0 {
return "", nil, fmt.Errorf("unable to find a virtual machine corresponding to the id '%s'", vm.Id)
}

result := vm.toMap()
return vm.Id, resource.NewPropertyMapFromMap(result), err
return vm.Id, resource.NewPropertyMapFromMap(result), nil
}

func (esxi *Host) readVirtualMachine(vm VirtualMachine) (VirtualMachine, error) {
func (esxi *Host) readVirtualMachine(vm VirtualMachine) VirtualMachine {
command := fmt.Sprintf("vim-cmd vmsvc/get.summary %s", vm.Id)
stdout, err := esxi.Execute(command, "Get Guest summary")

Expand All @@ -54,7 +54,7 @@ func (esxi *Host) readVirtualMachine(vm VirtualMachine) (VirtualMachine, error)
vm.Notes = ""
vm.Info = nil

return vm, nil
return vm
}

r, _ := regexp.Compile("")
Expand Down Expand Up @@ -261,5 +261,5 @@ func (esxi *Host) readVirtualMachine(vm VirtualMachine) (VirtualMachine, error)
i++
}

return vm, nil
return vm
}
4 changes: 2 additions & 2 deletions provider/pkg/esxi/virtualMacineUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (esxi *Host) updateVmxContents(isNew bool, vm VirtualMachine) error {
for i, ni := range vm.NetworkInterfaces {
logging.V(9).Infof("updateVmxContents: ethernet%d", i)

if ni.VirtualNetwork == "" && strings.Contains(vmxContents, "ethernet"+strconv.Itoa(i)) == true {
if len(ni.VirtualNetwork) == 0 && strings.Contains(vmxContents, "ethernet"+strconv.Itoa(i)) == true {
// This is Modify (Delete existing network configuration)
logging.V(9).Infof("updateVmxContents: Modify ethernet%d - Delete existing.", i)
regexReplacement = fmt.Sprintf("")
Expand Down Expand Up @@ -293,7 +293,7 @@ func (esxi *Host) updateVmxContents(isNew bool, vm VirtualMachine) error {
}

// Set network type
if ni.NicType == "" {
if len(ni.NicType) == 0 {
networkType = defaultNetworkType
} else {
networkType = ni.NicType
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/esxi/virtualSwitchCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func VirtualSwitchCreate(inputs resource.PropertyMap, esxi *Host) (resource.Prop
// mtu = 1500
//}
//
//if linkDiscoveryMode == "" {
//if len(linkDiscoveryMode) == 0 {
// linkDiscoveryMode = "listen"
//}
//
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (p *esxiProvider) Create(ctx context.Context, req *pulumirpc.CreateRequest)
// Process Create call.
var result resource.PropertyMap
id, result, err := p.resourceService.Create(token, inputs, p.esxi)
if err != nil || len(id) == 0 || result == nil {
if err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 82d6150

Please sign in to comment.