github.com/vmware/govmomi@v0.43.0/govc/host/vswitch/info.go (about) 1 /* 2 Copyright (c) 2014-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 vswitch 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "strings" 25 "text/tabwriter" 26 27 "github.com/vmware/govmomi/govc/cli" 28 "github.com/vmware/govmomi/govc/flags" 29 "github.com/vmware/govmomi/property" 30 "github.com/vmware/govmomi/vim25/mo" 31 "github.com/vmware/govmomi/vim25/types" 32 ) 33 34 type info struct { 35 *flags.ClientFlag 36 *flags.OutputFlag 37 *flags.HostSystemFlag 38 } 39 40 func init() { 41 cli.Register("host.vswitch.info", &info{}) 42 } 43 44 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 45 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 46 cmd.ClientFlag.Register(ctx, f) 47 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 48 cmd.OutputFlag.Register(ctx, f) 49 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 50 cmd.HostSystemFlag.Register(ctx, f) 51 } 52 53 func (cmd *info) Process(ctx context.Context) error { 54 if err := cmd.ClientFlag.Process(ctx); err != nil { 55 return err 56 } 57 if err := cmd.OutputFlag.Process(ctx); err != nil { 58 return err 59 } 60 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 61 return err 62 } 63 return nil 64 } 65 66 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 67 client, err := cmd.Client() 68 if err != nil { 69 return err 70 } 71 72 ns, err := cmd.HostNetworkSystem() 73 if err != nil { 74 return err 75 } 76 77 var mns mo.HostNetworkSystem 78 79 pc := property.DefaultCollector(client) 80 err = pc.RetrieveOne(ctx, ns.Reference(), []string{"networkInfo.vswitch"}, &mns) 81 if err != nil { 82 return err 83 } 84 85 r := &infoResult{mns.NetworkInfo.Vswitch} 86 87 return cmd.WriteResult(r) 88 } 89 90 type infoResult struct { 91 Vswitch []types.HostVirtualSwitch `json:"vswitch"` 92 } 93 94 func (r *infoResult) Write(w io.Writer) error { 95 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 96 97 for i, s := range r.Vswitch { 98 if i > 0 { 99 fmt.Fprintln(tw) 100 } 101 fmt.Fprintf(tw, "Name:\t%s\n", s.Name) 102 fmt.Fprintf(tw, "Portgroup:\t%s\n", keys("key-vim.host.PortGroup-", s.Portgroup)) 103 fmt.Fprintf(tw, "Pnic:\t%s\n", keys("key-vim.host.PhysicalNic-", s.Pnic)) 104 fmt.Fprintf(tw, "MTU:\t%d\n", s.Mtu) 105 fmt.Fprintf(tw, "Ports:\t%d\n", s.NumPorts) 106 fmt.Fprintf(tw, "Ports Available:\t%d\n", s.NumPortsAvailable) 107 HostNetworkPolicy(tw, s.Spec.Policy) 108 } 109 110 return tw.Flush() 111 } 112 113 func keys(key string, vals []string) string { 114 for i, val := range vals { 115 vals[i] = strings.TrimPrefix(val, key) 116 } 117 return strings.Join(vals, ", ") 118 } 119 120 func enabled(b *bool) string { 121 if b != nil && *b { 122 return "Yes" 123 } 124 return "No" 125 } 126 127 func HostNetworkPolicy(w io.Writer, p *types.HostNetworkPolicy) { 128 if p == nil || p.Security == nil { 129 return // e.g. Workstation 130 } 131 fmt.Fprintf(w, "Allow promiscuous mode:\t%s\n", enabled(p.Security.AllowPromiscuous)) 132 fmt.Fprintf(w, "Allow forged transmits:\t%s\n", enabled(p.Security.ForgedTransmits)) 133 fmt.Fprintf(w, "Allow MAC changes:\t%s\n", enabled(p.Security.MacChanges)) 134 }