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

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