github.com/vmware/govmomi@v0.43.0/govc/host/vnic/info.go (about) 1 /* 2 Copyright (c) 2015-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 vnic 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "os" 25 "strings" 26 "text/tabwriter" 27 28 "github.com/vmware/govmomi/govc/cli" 29 "github.com/vmware/govmomi/govc/flags" 30 "github.com/vmware/govmomi/object" 31 "github.com/vmware/govmomi/vim25/mo" 32 "github.com/vmware/govmomi/vim25/types" 33 ) 34 35 type info struct { 36 *flags.HostSystemFlag 37 } 38 39 func init() { 40 cli.Register("host.vnic.info", &info{}) 41 } 42 43 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 44 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 45 cmd.HostSystemFlag.Register(ctx, f) 46 } 47 48 func (cmd *info) Process(ctx context.Context) error { 49 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 50 return err 51 } 52 return nil 53 } 54 55 type Info struct { 56 Device string `json:"device"` 57 Network string `json:"network"` 58 Switch string `json:"switch"` 59 Address string `json:"address"` 60 Stack string `json:"stack"` 61 Services []string `json:"services"` 62 } 63 64 type infoResult struct { 65 Info []Info `json:"info"` 66 } 67 68 func (i *infoResult) Write(w io.Writer) error { 69 tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) 70 71 for _, info := range i.Info { 72 fmt.Fprintf(tw, "Device:\t%s\n", info.Device) 73 fmt.Fprintf(tw, "Network label:\t%s\n", info.Network) 74 fmt.Fprintf(tw, "Switch:\t%s\n", info.Switch) 75 fmt.Fprintf(tw, "IP address:\t%s\n", info.Address) 76 fmt.Fprintf(tw, "TCP/IP stack:\t%s\n", info.Stack) 77 fmt.Fprintf(tw, "Enabled services:\t%s\n", strings.Join(info.Services, ", ")) 78 } 79 80 return tw.Flush() 81 } 82 83 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 84 host, err := cmd.HostSystem() 85 if err != nil { 86 return err 87 } 88 89 ns, err := cmd.HostNetworkSystem() 90 if err != nil { 91 return err 92 } 93 94 var mns mo.HostNetworkSystem 95 96 m, err := host.ConfigManager().VirtualNicManager(ctx) 97 if err != nil { 98 return err 99 } 100 101 minfo, err := m.Info(ctx) 102 if err != nil { 103 return err 104 } 105 106 err = ns.Properties(ctx, ns.Reference(), []string{"networkInfo"}, &mns) 107 if err != nil { 108 return err 109 } 110 111 type dnet struct { 112 dvp mo.DistributedVirtualPortgroup 113 dvs mo.VmwareDistributedVirtualSwitch 114 } 115 116 dnets := make(map[string]*dnet) 117 var res infoResult 118 119 for _, nic := range mns.NetworkInfo.Vnic { 120 info := Info{Device: nic.Device} 121 122 if dvp := nic.Spec.DistributedVirtualPort; dvp != nil { 123 dn, ok := dnets[dvp.PortgroupKey] 124 125 if !ok { 126 dn = new(dnet) 127 o := object.NewDistributedVirtualPortgroup(host.Client(), types.ManagedObjectReference{ 128 Type: "DistributedVirtualPortgroup", 129 Value: dvp.PortgroupKey, 130 }) 131 132 err = o.Properties(ctx, o.Reference(), []string{"name", "config.distributedVirtualSwitch"}, &dn.dvp) 133 if err != nil { 134 return err 135 } 136 137 err = o.Properties(ctx, *dn.dvp.Config.DistributedVirtualSwitch, []string{"name"}, &dn.dvs) 138 if err != nil { 139 return err 140 } 141 142 dnets[dvp.PortgroupKey] = dn 143 } 144 145 info.Network = dn.dvp.Name 146 info.Switch = dn.dvs.Name 147 } else { 148 info.Network = nic.Portgroup 149 for _, pg := range mns.NetworkInfo.Portgroup { 150 if pg.Spec.Name == nic.Portgroup { 151 info.Switch = pg.Spec.VswitchName 152 break 153 } 154 } 155 } 156 157 info.Address = nic.Spec.Ip.IpAddress 158 info.Stack = nic.Spec.NetStackInstanceKey 159 160 for _, nc := range minfo.NetConfig { 161 for _, dev := range nc.SelectedVnic { 162 key := nc.NicType + "." + nic.Key 163 if dev == key { 164 info.Services = append(info.Services, nc.NicType) 165 } 166 } 167 168 } 169 170 res.Info = append(res.Info, info) 171 } 172 173 return cmd.WriteResult(&res) 174 }