github.com/vmware/govmomi@v0.43.0/govc/host/portgroup/info.go (about)

     1  /*
     2  Copyright (c) 2016-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 portgroup
    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/govc/host/vswitch"
    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.portgroup.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 networkInfoPortgroup(ctx context.Context, c *flags.ClientFlag, h *flags.HostSystemFlag) ([]types.HostPortGroup, error) {
    67  	client, err := c.Client()
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	ns, err := h.HostNetworkSystem()
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	var mns mo.HostNetworkSystem
    78  
    79  	pc := property.DefaultCollector(client)
    80  	err = pc.RetrieveOne(ctx, ns.Reference(), []string{"networkInfo.portgroup"}, &mns)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return mns.NetworkInfo.Portgroup, nil
    86  }
    87  
    88  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    89  	pg, err := networkInfoPortgroup(ctx, cmd.ClientFlag, cmd.HostSystemFlag)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	r := &infoResult{pg}
    95  
    96  	return cmd.WriteResult(r)
    97  }
    98  
    99  type infoResult struct {
   100  	Portgroup []types.HostPortGroup `json:"portgroup"`
   101  }
   102  
   103  func (r *infoResult) Write(w io.Writer) error {
   104  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   105  
   106  	for i, s := range r.Portgroup {
   107  		if i > 0 {
   108  			fmt.Fprintln(tw)
   109  		}
   110  		fmt.Fprintf(tw, "Name:\t%s\n", s.Spec.Name)
   111  		fmt.Fprintf(tw, "Virtual switch:\t%s\n", s.Spec.VswitchName)
   112  		fmt.Fprintf(tw, "VLAN ID:\t%d\n", s.Spec.VlanId)
   113  		fmt.Fprintf(tw, "Active ports:\t%d\n", len(s.Port))
   114  		vswitch.HostNetworkPolicy(tw, &s.ComputedPolicy)
   115  	}
   116  
   117  	return tw.Flush()
   118  }