github.com/vmware/govmomi@v0.51.0/vsan/client.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package vsan
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  
    11  	"github.com/vmware/govmomi/object"
    12  	"github.com/vmware/govmomi/vim25"
    13  	"github.com/vmware/govmomi/vim25/soap"
    14  	vimtypes "github.com/vmware/govmomi/vim25/types"
    15  	"github.com/vmware/govmomi/vsan/methods"
    16  	vsantypes "github.com/vmware/govmomi/vsan/types"
    17  )
    18  
    19  // Namespace and Path constants
    20  const (
    21  	Namespace = "vsan"
    22  	Path      = "/vsanHealth"
    23  )
    24  
    25  // Creates the vsan cluster config system instance. This is to be queried from vsan health.
    26  var (
    27  	VsanVcClusterConfigSystemInstance = vimtypes.ManagedObjectReference{
    28  		Type:  "VsanVcClusterConfigSystem",
    29  		Value: "vsan-cluster-config-system",
    30  	}
    31  	VsanPerformanceManagerInstance = vimtypes.ManagedObjectReference{
    32  		Type:  "VsanPerformanceManager",
    33  		Value: "vsan-performance-manager",
    34  	}
    35  	VsanQueryObjectIdentitiesInstance = vimtypes.ManagedObjectReference{
    36  		Type:  "VsanObjectSystem",
    37  		Value: "vsan-cluster-object-system",
    38  	}
    39  	VsanPropertyCollectorInstance = vimtypes.ManagedObjectReference{
    40  		Type:  "PropertyCollector",
    41  		Value: "vsan-property-collector",
    42  	}
    43  	VsanVcStretchedClusterSystem = vimtypes.ManagedObjectReference{
    44  		Type:  "VimClusterVsanVcStretchedClusterSystem",
    45  		Value: "vsan-stretched-cluster-system",
    46  	}
    47  )
    48  
    49  // Client used for accessing vsan health APIs.
    50  type Client struct {
    51  	*soap.Client
    52  
    53  	RoundTripper soap.RoundTripper
    54  
    55  	vim25Client *vim25.Client
    56  }
    57  
    58  // NewClient creates a new VsanHealth client
    59  func NewClient(ctx context.Context, c *vim25.Client) (*Client, error) {
    60  	sc := c.Client.NewServiceClient(Path, Namespace)
    61  	return &Client{sc, sc, c}, nil
    62  }
    63  
    64  // RoundTrip dispatches to the RoundTripper field.
    65  func (c *Client) RoundTrip(ctx context.Context, req, res soap.HasFault) error {
    66  	return c.RoundTripper.RoundTrip(ctx, req, res)
    67  }
    68  
    69  // VsanClusterGetConfig calls the Vsan health's VsanClusterGetConfig API.
    70  func (c *Client) VsanClusterGetConfig(ctx context.Context, cluster vimtypes.ManagedObjectReference) (*vsantypes.VsanConfigInfoEx, error) {
    71  	req := vsantypes.VsanClusterGetConfig{
    72  		This:    VsanVcClusterConfigSystemInstance,
    73  		Cluster: cluster,
    74  	}
    75  
    76  	res, err := methods.VsanClusterGetConfig(ctx, c, &req)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	return &res.Returnval, nil
    81  }
    82  
    83  // VsanClusterReconfig calls the Vsan health's VsanClusterReconfig API.
    84  func (c *Client) VsanClusterReconfig(ctx context.Context, cluster vimtypes.ManagedObjectReference, spec vsantypes.VimVsanReconfigSpec) (*object.Task, error) {
    85  	req := vsantypes.VsanClusterReconfig{
    86  		This:             VsanVcClusterConfigSystemInstance,
    87  		Cluster:          cluster,
    88  		VsanReconfigSpec: spec,
    89  	}
    90  
    91  	res, err := methods.VsanClusterReconfig(ctx, c, &req)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return object.NewTask(c.vim25Client, res.Returnval), nil
    97  }
    98  
    99  // VsanPerfQueryPerf calls the vsan performance manager API
   100  func (c *Client) VsanPerfQueryPerf(ctx context.Context, cluster *vimtypes.ManagedObjectReference, qSpecs []vsantypes.VsanPerfQuerySpec) ([]vsantypes.VsanPerfEntityMetricCSV, error) {
   101  	req := vsantypes.VsanPerfQueryPerf{
   102  		This:       VsanPerformanceManagerInstance,
   103  		Cluster:    cluster,
   104  		QuerySpecs: qSpecs,
   105  	}
   106  
   107  	res, err := methods.VsanPerfQueryPerf(ctx, c, &req)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return res.Returnval, nil
   112  }
   113  
   114  // VsanQueryObjectIdentities return host uuid
   115  func (c *Client) VsanQueryObjectIdentities(ctx context.Context, cluster vimtypes.ManagedObjectReference) (*vsantypes.VsanObjectIdentityAndHealth, error) {
   116  	req := vsantypes.VsanQueryObjectIdentities{
   117  		This:    VsanQueryObjectIdentitiesInstance,
   118  		Cluster: &cluster,
   119  	}
   120  
   121  	res, err := methods.VsanQueryObjectIdentities(ctx, c, &req)
   122  
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	return res.Returnval, nil
   128  }
   129  
   130  // VsanHostGetConfig returns the config of host's vSAN system.
   131  func (c *Client) VsanHostGetConfig(ctx context.Context, vsanSystem vimtypes.ManagedObjectReference) (*vsantypes.VsanHostConfigInfoEx, error) {
   132  	req := vimtypes.RetrievePropertiesEx{
   133  		SpecSet: []vimtypes.PropertyFilterSpec{{
   134  			ObjectSet: []vimtypes.ObjectSpec{{
   135  				Obj: vsanSystem}},
   136  			PropSet: []vimtypes.PropertySpec{{
   137  				Type:    "HostVsanSystem",
   138  				PathSet: []string{"config"}}}}},
   139  		This: VsanPropertyCollectorInstance}
   140  
   141  	res, err := methods.RetrievePropertiesEx(ctx, c, &req)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	var property vimtypes.DynamicProperty
   147  	if res != nil && res.Returnval != nil {
   148  		for _, obj := range res.Returnval.Objects {
   149  			for _, prop := range obj.PropSet {
   150  				if prop.Name == "config" {
   151  					property = prop
   152  					break
   153  				}
   154  			}
   155  		}
   156  	}
   157  
   158  	switch cfg := property.Val.(type) {
   159  	case vimtypes.VsanHostConfigInfo:
   160  		return &vsantypes.VsanHostConfigInfoEx{VsanHostConfigInfo: cfg}, nil
   161  	case vsantypes.VsanHostConfigInfoEx:
   162  		return &cfg, nil
   163  	default:
   164  		return nil, errors.New("host vSAN config not found")
   165  	}
   166  }