github.com/vmware/govmomi@v0.51.0/cli/flags/storage_profile.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package flags 6 7 import ( 8 "context" 9 "errors" 10 "flag" 11 "fmt" 12 13 "github.com/vmware/govmomi/vim25/types" 14 ) 15 16 type StorageProfileFlag struct { 17 *ClientFlag 18 19 Name []string 20 21 option string 22 } 23 24 func NewStorageProfileFlag(ctx context.Context, option ...string) (*StorageProfileFlag, context.Context) { 25 v := &StorageProfileFlag{} 26 if len(option) == 1 { 27 v.option = option[0] 28 } else { 29 v.option = "profile" 30 } 31 v.ClientFlag, ctx = NewClientFlag(ctx) 32 return v, ctx 33 } 34 35 func (e *StorageProfileFlag) String() string { 36 return fmt.Sprint(e.Name) 37 } 38 39 func (e *StorageProfileFlag) Set(value string) error { 40 e.Name = append(e.Name, value) 41 return nil 42 } 43 44 func (flag *StorageProfileFlag) Register(ctx context.Context, f *flag.FlagSet) { 45 flag.ClientFlag.Register(ctx, f) 46 47 f.Var(flag, flag.option, "Storage profile name or ID") 48 } 49 50 func (flag *StorageProfileFlag) StorageProfileList(ctx context.Context) ([]string, error) { 51 if len(flag.Name) == 0 { 52 return nil, nil 53 } 54 55 c, err := flag.PbmClient() 56 if err != nil { 57 return nil, err 58 } 59 m, err := c.ProfileMap(ctx) 60 if err != nil { 61 return nil, err 62 } 63 64 list := make([]string, len(flag.Name)) 65 66 for i, name := range flag.Name { 67 p, ok := m.Name[name] 68 if !ok { 69 return nil, fmt.Errorf("storage profile %q not found", name) 70 } 71 72 list[i] = p.GetPbmProfile().ProfileId.UniqueId 73 } 74 75 return list, nil 76 } 77 78 func (flag *StorageProfileFlag) StorageProfile(ctx context.Context) (string, error) { 79 switch len(flag.Name) { 80 case 0: 81 return "", nil 82 case 1: 83 default: 84 return "", errors.New("only 1 '-profile' can be specified") 85 } 86 87 list, err := flag.StorageProfileList(ctx) 88 if err != nil { 89 return "", err 90 } 91 92 return list[0], nil 93 } 94 95 func (flag *StorageProfileFlag) StorageProfileSpec(ctx context.Context) ([]types.BaseVirtualMachineProfileSpec, error) { 96 if len(flag.Name) == 0 { 97 return nil, nil 98 } 99 100 list, err := flag.StorageProfileList(ctx) 101 if err != nil { 102 return nil, err 103 } 104 105 spec := make([]types.BaseVirtualMachineProfileSpec, len(list)) 106 for i, name := range list { 107 spec[i] = &types.VirtualMachineDefinedProfileSpec{ 108 ProfileId: name, 109 } 110 } 111 return spec, nil 112 }