Skip to content

Commit

Permalink
add logic to fetch pool info in govc
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Ganesh committed Sep 14, 2023
1 parent 7d97867 commit b32241d
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions pkg/executables/govc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -39,6 +40,8 @@ const (
DeployOptsFile = "deploy-opts.json"
disk1 = "Hard disk 1"
disk2 = "Hard disk 2"
cpuAvailable = "CPU_Available"
memoryAvailable = "Memory_Available"
)

var requiredEnvs = []string{govcUsernameKey, govcPasswordKey, govcURLKey, govcInsecure, govcDatacenterKey}
Expand Down Expand Up @@ -1145,31 +1148,75 @@ func (g *Govc) SetGroupRoleOnObject(ctx context.Context, principal string, role
}

type resourcePoolInfo struct {
resourcePoolIdentifier resourcePool
resourcePoolIdentifier *resourcePool
}

type resourcePool struct {
CPUUsage string
CPULimit string
MemoryUsage string
MemoryLimit string
memoryUsage string
memoryLimit string
}

// ResourcePoolInfo returns the pool info for the provided resource pool.
func (g *Govc) ResourcePoolInfo(ctx context.Context, datacenter, resourcepool string, args ...string) (resourcePool, error) {
params := []string{"pool.info", "-dc", datacenter, "-vm", resourcepool, "-json"}
func (g *Govc) ResourcePoolInfo(ctx context.Context, datacenter, resourcepool string, args ...string) (map[string]int, error) {
params := []string{"pool.info", "-dc", datacenter, resourcepool}
params = append(params, args...)
response, err := g.exec(ctx, params...)
if err != nil {
return resourcePool{}, fmt.Errorf("getting resource pool information: %v", err)
return nil, fmt.Errorf("getting resource pool information: %v", err)
}
var resourcePoolInfoResponse resourcePoolInfo
err = yaml.Unmarshal(response.Bytes(), &resourcePoolInfoResponse)
if err != nil {
return resourcePool{}, fmt.Errorf("unmarshalling devices info: %v", err)
return nil, fmt.Errorf("unmarshalling devices info: %v", err)
}
poolInfo, err := getPoolInfo(resourcePoolInfoResponse.resourcePoolIdentifier)
if err != nil {

}
return poolInfo, nil
}

return resourcePoolInfoResponse.resourcePoolIdentifier, nil
// func validateMemoryAnd
func getPoolInfo(rp *resourcePool) (map[string]int, error) {
CPUUsed, err := getValueFromString(rp.CPUUsage)
if err != nil {
return nil, fmt.Errorf("Unable to obtain CPU usage for resource pool %s: %v", rp.CPUUsage, err)
}
CPULimit, err := getValueFromString(rp.CPULimit)
if err != nil {
return nil, fmt.Errorf("Unable to obtain CPU limit for resource pool %s: %v", rp.CPULimit, err)
}
memoryUsed, err := getValueFromString(rp.memoryUsage)
if err != nil {
return nil, fmt.Errorf("Unable to obtain memory usage for resource pool %s: %v", rp.CPULimit, err)
}
memoryLimit, err := getValueFromString(rp.memoryLimit)
if err != nil {
return nil, fmt.Errorf("Unable to obtain memory limit for resource pool %s: %v", rp.CPULimit, err)
}
poolInfo := make(map[string]int)
if memoryLimit != -1 {
poolInfo[memoryAvailable] = memoryLimit - memoryUsed
} else {
poolInfo[memoryAvailable] = -1
}
if CPULimit != -1 {
poolInfo[cpuAvailable] = CPULimit - CPUUsed
} else {
poolInfo[cpuAvailable] = -1
}
return poolInfo, nil
}

func validateMemoryAnd
func getValueFromString(str string) (int, error) {
splitResponse := strings.Split(str, " ")
nonNumericRegex := regexp.MustCompile(`[^0-9- ]+`)
cleanedString := nonNumericRegex.ReplaceAllString(splitResponse[0], "")
numValue, err := strconv.Atoi(cleanedString)
if err != nil {
return 0, err
}
return numValue, nil
}

0 comments on commit b32241d

Please sign in to comment.