github.com/vmware/govmomi@v0.43.0/govc/storage/policy/ls.go (about)

     1  /*
     2  Copyright (c) 2020-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 policy
    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/pbm"
    29  	"github.com/vmware/govmomi/pbm/types"
    30  )
    31  
    32  type ls struct {
    33  	*flags.ClientFlag
    34  	*flags.OutputFlag
    35  
    36  	id bool
    37  }
    38  
    39  func init() {
    40  	cli.Register("storage.policy.ls", &ls{})
    41  }
    42  
    43  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    44  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    45  	cmd.ClientFlag.Register(ctx, f)
    46  
    47  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    48  	cmd.OutputFlag.Register(ctx, f)
    49  
    50  	f.BoolVar(&cmd.id, "i", false, "List policy ID only")
    51  }
    52  
    53  func (cmd *ls) 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  	return nil
    61  }
    62  
    63  func (cmd *ls) Usage() string {
    64  	return "[NAME]"
    65  }
    66  
    67  func (cmd *ls) Description() string {
    68  	return `VM Storage Policy listing.
    69  
    70  Examples:
    71    govc storage.policy.ls
    72    govc storage.policy.ls "vSAN Default Storage Policy"
    73    govc storage.policy.ls -i "vSAN Default Storage Policy"`
    74  }
    75  
    76  func ListProfiles(ctx context.Context, c *pbm.Client, name string) ([]types.BasePbmProfile, error) {
    77  	m, err := c.ProfileMap(ctx)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	if name == "" {
    82  		return m.Profile, nil
    83  	}
    84  	if p, ok := m.Name[name]; ok {
    85  		return []types.BasePbmProfile{p}, nil
    86  	}
    87  	return nil, fmt.Errorf("profile %q not found", name)
    88  }
    89  
    90  type lsResult struct {
    91  	Profile []types.BasePbmProfile `json:"profile"`
    92  	cmd     *ls
    93  }
    94  
    95  func (r *lsResult) Write(w io.Writer) error {
    96  	tw := tabwriter.NewWriter(r.cmd.Out, 2, 0, 2, ' ', 0)
    97  
    98  	for i := range r.Profile {
    99  		p := r.Profile[i].GetPbmProfile()
   100  		_, _ = fmt.Fprintf(tw, "%s", p.ProfileId.UniqueId)
   101  		if !r.cmd.id {
   102  			_, _ = fmt.Fprintf(tw, "\t%s", p.Name)
   103  		}
   104  		_, _ = fmt.Fprintln(tw)
   105  	}
   106  
   107  	return tw.Flush()
   108  }
   109  
   110  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
   111  	c, err := cmd.PbmClient()
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	profiles, err := ListProfiles(ctx, c, f.Arg(0))
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	return cmd.WriteResult(&lsResult{profiles, cmd})
   122  }