github.com/vmware/govmomi@v0.51.0/cli/library/vmtx_item_info.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 library 6 7 import ( 8 "context" 9 "encoding/json" 10 "flag" 11 "fmt" 12 "io" 13 "text/tabwriter" 14 15 "github.com/vmware/govmomi/cli" 16 "github.com/vmware/govmomi/cli/flags" 17 "github.com/vmware/govmomi/vapi/library" 18 "github.com/vmware/govmomi/vapi/vcenter" 19 ) 20 21 type vmtxItemInfo struct { 22 *flags.ClientFlag 23 *flags.OutputFlag 24 } 25 26 type vmtxItemInfoResultsWriter struct { 27 Result *vcenter.TemplateInfo `json:"result"` 28 m *vcenter.Manager 29 cmd *vmtxItemInfo 30 } 31 32 func (r vmtxItemInfoResultsWriter) MarshalJSON() ([]byte, error) { 33 return json.Marshal(r.Result) 34 } 35 36 func (r vmtxItemInfoResultsWriter) Write(w io.Writer) error { 37 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 38 defer tw.Flush() 39 if err := r.writeLibraryTemplateDetails(tw, *r.Result); err != nil { 40 return err 41 } 42 tw.Flush() 43 return nil 44 } 45 46 func (r vmtxItemInfoResultsWriter) writeLibraryTemplateDetails( 47 w io.Writer, v vcenter.TemplateInfo) error { 48 49 fmt.Fprintf(w, " VM Template:\t%s\n", v.VmTemplate) 50 fmt.Fprintf(w, " Guest OS:\t%s\n", v.GuestOS) 51 fmt.Fprintf(w, " CPU:\t\n") 52 fmt.Fprintf(w, " Count:\t%d\n", v.CPU.Count) 53 fmt.Fprintf(w, " Cores Per Socket:\t%d\n", v.CPU.CoresPerSocket) 54 fmt.Fprintf(w, " Memory:\t\n") 55 fmt.Fprintf(w, " Size in MB:\t%d\n", v.Memory.SizeMB) 56 57 fmt.Fprintf(w, " Disks:\t\n") 58 for _, d := range v.Disks { 59 fmt.Fprintf(w, " Key:\t%s\n", d.Key) 60 fmt.Fprintf(w, " Capacity:\t%d\n", d.Value.Capacity) 61 fmt.Fprintf(w, " Datastore:\t%s\n\n", d.Value.DiskStorage.Datastore) 62 } 63 64 fmt.Fprintf(w, " Nics:\t\n") 65 for _, d := range v.Nics { 66 fmt.Fprintf(w, " Key:\t%s\n", d.Key) 67 fmt.Fprintf(w, " Backing Type:\t%s\n", d.Value.BackingType) 68 fmt.Fprintf(w, " Mac Type:\t%s\n", d.Value.MacType) 69 fmt.Fprintf(w, " Network:\t%s\n\n", d.Value.Network) 70 } 71 return nil 72 } 73 74 func init() { 75 cli.Register("library.vmtx.info", &vmtxItemInfo{}) 76 } 77 78 func (cmd *vmtxItemInfo) Register(ctx context.Context, f *flag.FlagSet) { 79 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 80 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 81 cmd.ClientFlag.Register(ctx, f) 82 cmd.OutputFlag.Register(ctx, f) 83 } 84 85 func (cmd *vmtxItemInfo) Process(ctx context.Context) error { 86 if err := cmd.ClientFlag.Process(ctx); err != nil { 87 return err 88 } 89 return nil 90 } 91 92 func (cmd *vmtxItemInfo) Description() string { 93 return `Display VMTX template details 94 95 Examples: 96 govc library.vmtx.info /library_name/vmtx_template_name` 97 } 98 99 func (cmd *vmtxItemInfo) Run(ctx context.Context, f *flag.FlagSet) error { 100 if f.NArg() != 1 { 101 return flag.ErrHelp 102 } 103 path := f.Arg(0) 104 105 c, err := cmd.RestClient() 106 if err != nil { 107 return err 108 } 109 110 m := vcenter.NewManager(c) 111 112 // Fetch library item 113 item, err := flags.ContentLibraryItem(ctx, c, path) 114 if err != nil { 115 return err 116 } 117 118 var res *vcenter.TemplateInfo 119 if item.Type != library.ItemTypeVMTX { 120 return fmt.Errorf("library item type should be 'vm-template' instead of '%s'", item.Type) 121 } 122 123 res, err = m.GetLibraryTemplateInfo(ctx, item.ID) 124 if err != nil { 125 return fmt.Errorf("error fetching library item details: %s", err.Error()) 126 } 127 128 return cmd.WriteResult(&vmtxItemInfoResultsWriter{res, m, cmd}) 129 }