Skip to content

Commit

Permalink
Merge pull request #142 from madhanrm/csvwmi
Browse files Browse the repository at this point in the history
CSV wmi
  • Loading branch information
madhanrm authored Nov 12, 2024
2 parents 6741b93 + 977fea4 commit fd76bb7
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 10 deletions.
32 changes: 32 additions & 0 deletions pkg/cluster/compute/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,35 @@ func TestGetResources(t *testing.T) {
t.Logf("Resource Owners %v \n", owners)
}
}

func TestGetVirtualMachineResouces(t *testing.T) {
vmr, err := GetVirtualMachineResources(whost)
if err != nil {
t.Fatal("Failed " + err.Error())
return
}
defer vmr.Close()

for _, resource := range vmr {
grpSetName, err := resource.GetPropertyName()
if err != nil {
t.Fatal("Failed " + err.Error())
return
}
t.Logf("Resource Name : %s\n", grpSetName)

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

owners, err := resource.GetPossibleOwnersEx1()
if err != nil {
t.Fatal("Failed " + err.Error())
return
}
t.Logf("Resource Owners %v \n", owners)
}
}
Binary file removed pkg/cluster/compute/resource/test.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions pkg/cluster/compute/resourcegroup/resourcegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package resourcegroup
import (
"reflect"

"github.com/go-ole/go-ole"
"github.com/microsoft/wmi/pkg/base/host"
"github.com/microsoft/wmi/pkg/base/instance"
"github.com/microsoft/wmi/pkg/base/query"
Expand Down Expand Up @@ -90,3 +91,23 @@ func (c *ResourceGroup) IsPendingState() (pendingState bool, err error) {
pendingState = (retState >= CLUSTER_RESOURCE_GROUP_STATE_PARTIAL_ONLINE)
return
}

// GetPreferredOwnersEx1 gets the possible owners of the resource group
func (c *ResourceGroup) GetPreferredOwnersEx1() (preferredOwners []string, err error) {
nodeListVariant := ole.NewVariant(ole.VT_ARRAY|ole.VT_BSTR, 0)
_, err = c.InvokeMethod("GetPreferredOwners", &nodeListVariant)
if err != nil {
return
}

nodeNameValues, err := wmi.GetVariantValues(&nodeListVariant)
if err != nil {
return
}

for _, ownerNode := range nodeNameValues {
preferredOwners = append(preferredOwners, ownerNode.(string))
}

return
}
16 changes: 7 additions & 9 deletions pkg/cluster/compute/resourcegroup/resourcegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,18 @@ func TestGetVirtualMachineResourceGroups(t *testing.T) {
t.Logf("Resource Group : %v\n", cn)
t.Logf("Resource Group VMId : %s\n", cn.VmId)
defer cn.Close()

pw, err := resourceGroup.GetPreferredOwnersEx1()
if err != nil {
t.Fatal("Failed " + err.Error())
return
}
t.Logf("Preferred Owners %v \n", pw)
}
}

/*
func TestGetVirtualMachineResourceGroup(t *testing.T) {
cn, err := GetVirtualMachineResourceGroup(whost, "Virtual Machine")
if err != nil {
t.Fatal("Failed " + err.Error())
return
}
defer cn.Close()
}
func TestGetVirtualMachineResourceGroupByVmID(t *testing.T) {
cn, err := GetVirtualMachineResourceGroupByVmID(whost, "Virtual Machine")
if err != nil {
Expand Down
99 changes: 98 additions & 1 deletion pkg/cluster/storage/clustersharedvolume/clustersharedvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
package clustersharedvolume

import (
"fmt"
"path/filepath"
"strings"

"github.com/microsoft/wmi/pkg/base/host"
"github.com/microsoft/wmi/pkg/base/instance"
"github.com/microsoft/wmi/pkg/base/query"
Expand All @@ -12,6 +16,7 @@ import (
"github.com/microsoft/wmi/pkg/errors"
"reflect"

"github.com/microsoft/wmi/pkg/cluster/compute/resource"
wmi "github.com/microsoft/wmi/pkg/wmiinstance"
fc "github.com/microsoft/wmi/server2019/root/mscluster"
)
Expand Down Expand Up @@ -61,6 +66,49 @@ func GetClusterSharedVolume(whost *host.WmiHost, volumeName string) (cvolume *Cl
return
}

// GetClusterSharedVolume gets an existing virtual machine
// Make sure to call Close once done using this instance
func GetClusterSharedVolumebyVolumeName(whost *host.WmiHost, volumeName string) (cvolume *ClusterSharedVolume, err error) {
creds := whost.GetCredential()
query := query.NewWmiQuery("MSCluster_ClusterSharedVolume", "VolumeName", volumeName)
volinstance, err := fc.NewMSCluster_ClusterSharedVolumeEx6(whost.HostName, string(constant.FailoverCluster), creds.UserName, creds.Password, creds.Domain, query)
if err != nil {
return
}
cvolume = &ClusterSharedVolume{volinstance}
return
}

// GetClusterSharedVolumebyName gets an name of the cluster shared volume
// Make sure to call Close once done using this instance
// Sometimes the volume name is difficult to compare
func GetClusterSharedVolumebyName(whost *host.WmiHost, name string) (cvolume *ClusterSharedVolume, err error) {
csvInstances, err := GetClusterSharedVolumes(whost)
if err != nil {
return
}
defer csvInstances.Close()
inPath := strings.ToLower(filepath.Clean(name))
for _, instance := range csvInstances {
instanceName, err1 := instance.GetPropertyName()
if err1 != nil {
err = err1
return
}
matchingPath := strings.ToLower(filepath.Clean(instanceName))
if strings.Contains(inPath, matchingPath) {
tmpInstance, err1 := instance.Clone()
if err1 != nil {
err = err1
return
}
return NewClusterSharedVolume(tmpInstance)
}
}

return
}

// IsStatusOK get the cluster health status
func (c *ClusterSharedVolume) IsStatusOK() (status bool, err error) {
statusValue, err := c.GetPropertyStatus()
Expand Down Expand Up @@ -105,6 +153,55 @@ func (c *ClusterSharedVolume) IsFaultStateOK() (status bool, err error) {

// ContainsPath checks if the input path is part of this cluster shared volume
func (c *ClusterSharedVolume) ContainsPath(absolutePath string) (status bool, err error) {
// TBD
volName, err := c.GetPropertyName()
if err != nil {
return
}

if strings.Contains(strings.ToLower(absolutePath), strings.ToLower(volName)) {
status = true
return
}

return
}

func (c *ClusterSharedVolume) GetAssociatedClusterResource() (clusterResource *resource.Resource, err error) {
volumeName, err := c.GetPropertyName()
if err != nil {
return
}

volumeAssocQuery := fmt.Sprintf("Associators of {MSCluster_ClusterSharedVolume='%s'} where assocclass=MSCluster_ClusterSharedVolumeToResource", volumeName)
assocInstances, err := instance.GetWmiInstancesFromHostRawQuery(c.GetWmiHost(), string(constant.FailoverCluster), volumeAssocQuery)
if err != nil {
return
}
defer assocInstances.Close()
if len(assocInstances) == 0 {
err = errors.Wrapf(errors.NotFound, "No Cluster Resource found for Cluster Shared Volume %s", volumeName)
return
}

clonedResource, err := assocInstances[0].Clone()
if err != nil {
return
}
clusterResource, err = resource.NewResource(clonedResource)
return
}

// OwnerGroup checks if the input path is part of this cluster shared volume
func (c *ClusterSharedVolume) OwnerGroup() (ownerGroup string, err error) {
cr, err := c.GetAssociatedClusterResource()
if err != nil {
return
}
defer cr.Close()

ownerGroup, err = cr.GetPropertyOwnerGroup()
if err != nil {
return
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ func TestGetClusterSharedVolume(t *testing.T) {
return
}
defer cn.Close()
volumeName, err := cn.GetPropertyName()
if err != nil {
t.Fatal("Failed " + err.Error())
return
}

t.Logf("Volume Name: %s\n", volumeName)
}

func TestGetClusterSharedVolumeByName(t *testing.T) {
cn, err := GetClusterSharedVolumebyName(whost, "C:/ClusterStorage/Volume1")
if err != nil {
t.Fatal("Failed " + err.Error())
return
}
defer cn.Close()
volumeName, err := cn.GetPropertyName()
if err != nil {
t.Fatal("Failed " + err.Error())
return
}

t.Logf("Volume Name: %s\n", volumeName)
}

func TestGetClusterSharedVolumes(t *testing.T) {
Expand Down Expand Up @@ -62,5 +85,12 @@ func TestGetClusterSharedVolumes(t *testing.T) {
return
}
t.Logf("Volume FaultStatusOK %v \n", statusfault)

ownerGroup, err := volume.OwnerGroup()
if err != nil {
t.Fatal("Failed " + err.Error())
return
}
t.Logf("Volume OwnerGroup %v \n", ownerGroup)
}
}

0 comments on commit fd76bb7

Please sign in to comment.