github.com/vmware/govmomi@v0.37.2/govc/vsan/info.go (about)

     1  /*
     2  Copyright (c) 2021-2023 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  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"text/tabwriter"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  	"github.com/vmware/govmomi/vsan"
    30  	"github.com/vmware/govmomi/vsan/types"
    31  )
    32  
    33  type info struct {
    34  	*flags.DatacenterFlag
    35  }
    36  
    37  func init() {
    38  	cli.Register("vsan.info", &info{})
    39  }
    40  
    41  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    43  	cmd.DatacenterFlag.Register(ctx, f)
    44  }
    45  
    46  func (cmd *info) Usage() string {
    47  	return "CLUSTER..."
    48  }
    49  
    50  func (cmd *info) Description() string {
    51  	return `Display vSAN configuration.
    52  
    53  Examples:
    54    govc vsan.info
    55    govc vsan.info ClusterA
    56    govc vsan.info -json`
    57  }
    58  
    59  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    60  	vc, err := cmd.Client()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	c, err := vsan.NewClient(ctx, vc)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	finder, err := cmd.Finder()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	args := f.Args()
    76  	if len(args) == 0 {
    77  		args = []string{"*"}
    78  	}
    79  
    80  	var res []Cluster
    81  
    82  	for _, arg := range args {
    83  		clusters, err := finder.ClusterComputeResourceList(ctx, arg)
    84  		if err != nil {
    85  			return err
    86  		}
    87  
    88  		for _, cluster := range clusters {
    89  			info, err := c.VsanClusterGetConfig(ctx, cluster.Reference())
    90  			if err != nil {
    91  				return err
    92  			}
    93  			res = append(res, Cluster{cluster.InventoryPath, info})
    94  		}
    95  	}
    96  
    97  	return cmd.WriteResult(&infoResult{res})
    98  }
    99  
   100  type Cluster struct {
   101  	Path string                  `json:"path"`
   102  	Info *types.VsanConfigInfoEx `json:"info"`
   103  }
   104  
   105  type infoResult struct {
   106  	Clusters []Cluster `json:"clusters"`
   107  }
   108  
   109  func (r *infoResult) Write(w io.Writer) error {
   110  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
   111  
   112  	for _, cluster := range r.Clusters {
   113  		fmt.Fprintf(tw, "Path:\t%s\n", cluster.Path)
   114  		fmt.Fprintf(tw, "  Enabled:\t%t\n", *cluster.Info.Enabled)
   115  		unmapEnabled := false
   116  		if unmap := cluster.Info.UnmapConfig; unmap != nil {
   117  			unmapEnabled = unmap.Enable
   118  		}
   119  
   120  		fmt.Fprintf(tw, "  Unmap Enabled:\t%t\n", unmapEnabled)
   121  	}
   122  
   123  	return tw.Flush()
   124  }