github.com/vmware/govmomi@v0.37.1/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  	rtype := types.PbmProfileResourceType{
    78  		ResourceType: string(types.PbmProfileResourceTypeEnumSTORAGE),
    79  	}
    80  
    81  	category := types.PbmProfileCategoryEnumREQUIREMENT
    82  
    83  	ids, err := c.QueryProfile(ctx, rtype, string(category))
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	profiles, err := c.RetrieveContent(ctx, ids)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	if name == "" {
    94  		return profiles, nil
    95  	}
    96  
    97  	for _, p := range profiles {
    98  		if p.GetPbmProfile().Name == name {
    99  			return []types.BasePbmProfile{p}, nil
   100  		}
   101  	}
   102  
   103  	return c.RetrieveContent(ctx, []types.PbmProfileId{{UniqueId: name}})
   104  }
   105  
   106  type lsResult struct {
   107  	Profile []types.BasePbmProfile `json:"profile"`
   108  	cmd     *ls
   109  }
   110  
   111  func (r *lsResult) Write(w io.Writer) error {
   112  	tw := tabwriter.NewWriter(r.cmd.Out, 2, 0, 2, ' ', 0)
   113  
   114  	for i := range r.Profile {
   115  		p := r.Profile[i].GetPbmProfile()
   116  		_, _ = fmt.Fprintf(tw, "%s", p.ProfileId.UniqueId)
   117  		if !r.cmd.id {
   118  			_, _ = fmt.Fprintf(tw, "\t%s", p.Name)
   119  		}
   120  		_, _ = fmt.Fprintln(tw)
   121  	}
   122  
   123  	return tw.Flush()
   124  }
   125  
   126  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
   127  	c, err := cmd.PbmClient()
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	profiles, err := ListProfiles(ctx, c, f.Arg(0))
   133  	if err != nil {
   134  		return err
   135  	}
   136  
   137  	return cmd.WriteResult(&lsResult{profiles, cmd})
   138  }