github.com/vmware/govmomi@v0.43.0/govc/vm/target/info.go (about)

     1  /*
     2  Copyright (c) 2023-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 target
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"text/tabwriter"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/units"
    29  	"github.com/vmware/govmomi/vim25/types"
    30  )
    31  
    32  type info struct {
    33  	flags.EnvBrowser
    34  
    35  	datastore bool
    36  	network   bool
    37  	disk      bool
    38  	device    bool
    39  }
    40  
    41  func init() {
    42  	cli.Register("vm.target.info", &info{})
    43  }
    44  
    45  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    46  	cmd.EnvBrowser.Register(ctx, f)
    47  
    48  	f.BoolVar(&cmd.datastore, "datastore", true, "Include Datastores")
    49  	f.BoolVar(&cmd.network, "network", true, "Include Networks")
    50  	f.BoolVar(&cmd.disk, "disk", false, "Include Disks")
    51  	f.BoolVar(&cmd.device, "device", true, "Include Devices")
    52  }
    53  
    54  func (cmd *info) Description() string {
    55  	return `VM config target info.
    56  
    57  The config target data contains information about the execution environment for a VM
    58  in the given CLUSTER, and optionally for a specific HOST.
    59  
    60  Examples:
    61    govc vm.target.info -cluster C0
    62    govc vm.target.info -host my_hostname
    63    govc vm.target.info -vm my_vm`
    64  }
    65  
    66  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    67  	b, err := cmd.Browser(ctx)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	host, err := cmd.HostSystemIfSpecified()
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	target, err := b.QueryConfigTarget(ctx, host)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	if cmd.network == false {
    83  		target.Network = nil
    84  		target.DistributedVirtualPortgroup = nil
    85  		target.DistributedVirtualSwitch = nil
    86  		target.OpaqueNetwork = nil
    87  		target.LegacyNetworkInfo = nil
    88  	}
    89  
    90  	if cmd.datastore == false {
    91  		target.Datastore = nil
    92  	}
    93  
    94  	if cmd.disk == false {
    95  		target.ScsiDisk = nil
    96  		target.IdeDisk = nil
    97  	}
    98  
    99  	return cmd.VirtualMachineFlag.WriteResult(&infoResult{target})
   100  }
   101  
   102  type infoResult struct {
   103  	*types.ConfigTarget
   104  }
   105  
   106  func (r *infoResult) Write(w io.Writer) error {
   107  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   108  
   109  	fmt.Fprintf(tw, "CPUs:\t%d\n", r.ConfigTarget.NumCpus)
   110  	fmt.Fprintf(tw, "CPU cores:\t%d\n", r.ConfigTarget.NumCpuCores)
   111  
   112  	for _, ds := range r.ConfigTarget.Datastore {
   113  		fmt.Fprintf(tw, "Datastore:\t%s\n", ds.Name)
   114  		fmt.Fprintf(tw, "  Capacity:\t%s\n", units.ByteSize(ds.Datastore.Capacity))
   115  		fmt.Fprintf(tw, "  Free:\t%s\n", units.ByteSize(ds.Datastore.FreeSpace))
   116  		fmt.Fprintf(tw, "  Uncommitted:\t%s\n", units.ByteSize(ds.Datastore.Uncommitted))
   117  	}
   118  
   119  	for _, net := range r.ConfigTarget.Network {
   120  		fmt.Fprintf(tw, "Network:\t%s\n", net.Name)
   121  	}
   122  
   123  	for _, net := range r.ConfigTarget.DistributedVirtualPortgroup {
   124  		if net.UplinkPortgroup {
   125  			continue
   126  		}
   127  		fmt.Fprintf(tw, "Network:\t%s\n", net.PortgroupName)
   128  		fmt.Fprintf(tw, "  Switch:\t%s\n", net.SwitchName)
   129  	}
   130  
   131  	for _, disk := range r.ConfigTarget.ScsiPassthrough {
   132  		fmt.Fprintf(tw, "SCSI passthrough:\t%s\n", disk.Name)
   133  		fmt.Fprintf(tw, "  Unit:\t%d\n", disk.PhysicalUnitNumber)
   134  	}
   135  
   136  	for _, clock := range r.ConfigTarget.PrecisionClockInfo {
   137  		fmt.Fprintf(tw, "PrecisionClock:\t%s\n", clock.SystemClockProtocol)
   138  	}
   139  
   140  	return tw.Flush()
   141  }
   142  
   143  func (r *infoResult) Dump() interface{} {
   144  	return r.ConfigTarget
   145  }