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