Skip to content

Commit

Permalink
Merge pull request #147 from madhanrm/vmpr
Browse files Browse the repository at this point in the history
Fix cluster wmi port
  • Loading branch information
madhanrm authored Nov 18, 2024
2 parents 41083d4 + f41550b commit 46d450d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 54 deletions.
3 changes: 1 addition & 2 deletions pkg/cluster/compute/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func init() {
// new-vm -name test -NoVHD -Path C:\ClusterStorage\Volume1\test
// Get-vm -name test | add-vmtoCluster


/*
func TestGetResource(t *testing.T) {
cn, err := GetResource(whost, "*")
Expand Down Expand Up @@ -111,4 +110,4 @@ func TestGetVirtualMachine(t *testing.T) {
return
}
t.Logf("Resource Name : %s\n", ownerGroup)
}
}
5 changes: 3 additions & 2 deletions pkg/cluster/compute/resourcegroup/resourcegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func GetResourceGroup(whost *host.WmiHost, grpSetName string) (crgSet *ResourceG

// State gets the value of for the instance
func (c *ResourceGroup) State() (value int32, err error) {
// The documentation says uint32, but it is not working
retValue, err := c.GetProperty("State")
if err != nil {
return
Expand Down Expand Up @@ -114,11 +115,11 @@ func (c *ResourceGroup) GetPreferredOwnersEx1() (preferredOwners []string, err e

// DestroyGroupEx1 destroys the resource group
func (c *ResourceGroup) DestroyGroupEx1(Options uint32) (err error) {
// Typecasting to int32 is a fix to get this method working
// Typecasting to int32 is a fix to get this method working
// Documentation says uint32 but it is not working
_, err = c.InvokeMethod("DestroyGroup", int32(Options))
if err != nil {
return
}
return
}
}
57 changes: 35 additions & 22 deletions pkg/cluster/compute/resourcegroup/resourcegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGetResourceGroup(t *testing.T) {
func TestGetResourceGroups(t *testing.T) {
nc, err := GetResourceGroups(whost)
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
defer nc.Close()
Expand All @@ -38,21 +38,21 @@ func TestGetResourceGroups(t *testing.T) {
for _, resourceGroup := range nc {
grpSetName, err := resourceGroup.GetPropertyName()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("Resource Group : %s\n", grpSetName)

state, err := resourceGroup.State()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("Resource States %v \n", state)

pendingState, err := resourceGroup.IsPendingState()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("IsPending State %v \n", pendingState)
Expand All @@ -62,7 +62,7 @@ func TestGetResourceGroups(t *testing.T) {
func TestGetVirtualMachineResourceGroups(t *testing.T) {
resourceGroups, err := GetVirtualMachineResourceGroups(whost)
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
defer resourceGroups.Close()
Expand All @@ -71,40 +71,53 @@ func TestGetVirtualMachineResourceGroups(t *testing.T) {
for _, resourceGroup := range resourceGroups {
grpSetName, err := resourceGroup.GetPropertyName()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("Resource Group : %s\n", grpSetName)

vmId, err := GetVirtualMachineID(whost, grpSetName)
if err != nil {
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("VMId : %s\n", vmId)

state, err := resourceGroup.State()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("Resource States %v \n", state)

pendingState, err := resourceGroup.IsPendingState()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("IsPending State %v \n", pendingState)

cn, err := GetVirtualMachineResourceGroupViaAssociators(whost, grpSetName)
pw, err := resourceGroup.GetPreferredOwnersEx1()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("Resource Group : %v\n", cn)
t.Logf("Resource Group VMId : %s\n", cn.VmId)
defer cn.Close()

pw, err := resourceGroup.GetPreferredOwnersEx1()
t.Logf("Preferred Owners %v \n", pw)
err = resourceGroup.SetVirtualMachinePriority(1000)
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
curPriority, err := resourceGroup.GetVirtualMachinePriority()
if err != nil {
t.Fatalf("Failed %+v\n ", err)
return
}
t.Logf("Priority Set : %v \n", curPriority)
if curPriority != 1000 {
t.Fatalf("Failed %+v\n ", "Unable to set priority")
return
}
t.Logf("Preferred Owners %v \n", pw)
}
}

Expand All @@ -113,7 +126,7 @@ func TestGetVirtualMachineResourceGroups(t *testing.T) {
func TestGetVirtualMachineResourceGroupByVmID(t *testing.T) {
cn, err := GetVirtualMachineResourceGroupByVmID(whost, "Virtual Machine")
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
defer cn.Close()
Expand All @@ -122,29 +135,29 @@ func TestGetVirtualMachineResourceGroupByVmID(t *testing.T) {
func TestSetVirtualMachineAutoFailback(t *testing.T) {
cn, err := GetVirtualMachineResourceGroup(whost, "Virtual Machine")
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
defer cn.Close()
err = cn.SetVirtualMachineAutoFailback(true)
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
}
func TestDisableVirtualMachineDefaultOwner(t *testing.T) {
cn, err := GetVirtualMachineResourceGroup(whost, "Virtual Machine")
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
defer cn.Close()
err = cn.DisableVirtualMachineDefaultOwner()
if err != nil {
t.Fatal("Failed " + err.Error())
t.Fatalf("Failed %+v\n ", err)
return
}
}
Expand Down
69 changes: 41 additions & 28 deletions pkg/cluster/compute/resourcegroup/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,23 @@ package resourcegroup

import (
"fmt"
"reflect"

"github.com/microsoft/wmi/pkg/base/host"
"github.com/microsoft/wmi/pkg/base/instance"
"github.com/microsoft/wmi/pkg/base/query"
"github.com/microsoft/wmi/pkg/constant"
wmi "github.com/microsoft/wmi/pkg/wmiinstance"
fc "github.com/microsoft/wmi/server2019/root/mscluster"
"math"

"github.com/microsoft/wmi/pkg/cluster/compute/internal"
"github.com/microsoft/wmi/pkg/errors"
)

type FailoverClusterVirtualMachine struct {
*ResourceGroup
VmId string
}

var (
groupTypeString = fmt.Sprintf("%d", CLUSTER_RESOURCE_GROUP_TYPE_VIRTUAL_MACHINE)
)

func NewFailoverClusterVirtualMachine(instance *wmi.WmiInstance) (newInstance *FailoverClusterVirtualMachine, err error) {
tmp, err := NewResourceGroup(instance)

if err != nil {
return
}
newInstance = &FailoverClusterVirtualMachine{
ResourceGroup: tmp,
}
return
}

// GetVirtualMachineResourceGroups gets list of resource groups that are of type VirtualMachine
// Make sure to call Close once done using this instance
func GetVirtualMachineResourceGroups(whost *host.WmiHost) (crgSetcollection ResourceGroupCollection, err error) {
Expand Down Expand Up @@ -77,7 +61,7 @@ func GetVirtualMachineResourceGroupByVmID(whost *host.WmiHost, vmID string) (crg
return
}

// SetVirtualMachineAutoFailback returns if the resource group is in partial or pending state
// SetVirtualMachineAutoFailback sets the virtual machine auto failback policy
func (c *ResourceGroup) SetVirtualMachineAutoFailback(enable bool) (err error) {
value := 0
if enable {
Expand All @@ -93,7 +77,7 @@ func (c *ResourceGroup) SetVirtualMachineAutoFailback(enable bool) (err error) {
return
}

// DisableVirtualMachineDefaultOwner returns if the resource group is in partial or pending state
// DisableVirtualMachineDefaultOwner
// DefaultOwner is set to node on which cluster group is created.
// With Autofail back enabled, currently VM will be moved to default owner node when default owner comes online.
// Disabling default owner will ensure that VM fails back based on preferred owners only.
Expand All @@ -110,33 +94,62 @@ func (c *ResourceGroup) DisableVirtualMachineDefaultOwner() (err error) {

// GetVirtualMachineResourceGroupViaAssociators gets list of resource groups that are of type VirtualMachine
// Make sure to call Close once done using this instance
func GetVirtualMachineResourceGroupViaAssociators(whost *host.WmiHost, virtualMachineName string) (fcVm *FailoverClusterVirtualMachine, err error) {
func GetVirtualMachineID(whost *host.WmiHost, virtualMachineName string) (vmID string, err error) {
query := fmt.Sprintf("ASSOCIATORS OF {MSCluster_ResourceGroup.Name='%s'} WHERE AssocClass = MSCluster_ResourceGroupToResource", virtualMachineName)
instances, err := instance.GetWmiInstancesFromHostRawQuery(whost, string(constant.FailoverCluster), query)
if err != nil {
return
}
defer instances.Close()

if len(instances) == 0 {
err = errors.Wrapf(errors.NotFound, "No resource group found for virtual machine %s", virtualMachineName)
return
}

fcVm, err = NewFailoverClusterVirtualMachine(instances[0])
xmlString, err := instances[0].EmbeddedXMLInstance()
if err != nil {
return
}
vmID, err = internal.GetVMID(xmlString)
if err != nil {
return
}

if vmID == "" {
err = errors.Wrapf(errors.NotFound, "Missing VMID for VM '%s' in the cluster", virtualMachineName)
}
return
}

// Get the VMId
xmlString, err := fcVm.EmbeddedXMLInstance()
// SetVirtualMachinePriority sets the priority of the virtual machine
func (c *ResourceGroup) SetVirtualMachinePriority(value int32) (err error) {
err = c.SetProperty("Priority", value)
if err != nil {
return nil, err
return err
}
vmID, err := internal.GetVMID(xmlString)
err = c.Commit()

return
}

// GetVirtualMachinePriority sets the priority of the virtual machine
func (c *ResourceGroup) GetVirtualMachinePriority() (value int32, err error) {
// The documentation says uint32, but it is not working
retValue, err := c.GetProperty("Priority")
if err != nil {
return nil, errors.Wrapf(errors.NotFound, "Missing VMID for VM '%s' in the cluster", virtualMachineName)
return
}
if retValue == nil {
// Doesn't have any value. Return empty
return
}

value, ok := retValue.(int32)
if !ok {
err = errors.Wrapf(errors.InvalidType, " int32 is Invalid. Expected %s", reflect.TypeOf(retValue))
return
}
fcVm.VmId = vmID

return
}

0 comments on commit 46d450d

Please sign in to comment.