github.com/vmware/govmomi@v0.43.0/govc/library/subscriber/info.go (about) 1 /* 2 Copyright (c) 2020 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 subscriber 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "text/tabwriter" 25 26 "github.com/vmware/govmomi/find" 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/vim25/types" 31 ) 32 33 type info struct { 34 *flags.ClientFlag 35 *flags.OutputFlag 36 } 37 38 func init() { 39 cli.Register("library.subscriber.info", &info{}) 40 } 41 42 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 43 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 44 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 45 cmd.ClientFlag.Register(ctx, f) 46 cmd.OutputFlag.Register(ctx, f) 47 } 48 49 func (cmd *info) Process(ctx context.Context) error { 50 if err := cmd.ClientFlag.Process(ctx); err != nil { 51 return err 52 } 53 return cmd.OutputFlag.Process(ctx) 54 } 55 56 func (cmd *info) Usage() string { 57 return "PUBLISHED-LIBRARY SUBSCRIPTION-ID" 58 } 59 60 func (cmd *info) Description() string { 61 return `Library subscriber info. 62 63 Examples: 64 id=$(govc library.subscriber.ls | grep my-library-name | awk '{print $1}') 65 govc library.subscriber.info published-library-name $id` 66 } 67 68 // path returns the inventory path for id, if possible 69 func (cmd *info) path(kind, id string) string { 70 if id == "" { 71 return "" 72 } 73 ref := types.ManagedObjectReference{Type: kind, Value: id} 74 c, err := cmd.Client() 75 if err == nil { 76 ctx := context.Background() 77 e, err := find.NewFinder(c, false).Element(ctx, ref) 78 if err == nil { 79 return e.Path 80 } 81 } 82 return id 83 } 84 85 type infoResultsWriter struct { 86 *library.Subscriber 87 cmd *info 88 } 89 90 func (r infoResultsWriter) Write(w io.Writer) error { 91 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 92 93 _, _ = fmt.Fprintf(tw, "Name:\t%s\n", r.LibraryName) 94 _, _ = fmt.Fprintf(tw, " ID:\t%s\n", r.LibraryID) 95 _, _ = fmt.Fprintf(tw, " Location:\t%s\n", r.LibraryLocation) 96 97 p := r.cmd.path 98 99 if r.Vcenter != nil { 100 p = func(kind, id string) string { return id } 101 port := "" 102 if r.Vcenter.Port != 0 { 103 port = fmt.Sprintf(":%d", r.Vcenter.Port) 104 } 105 _, _ = fmt.Fprintf(tw, " vCenter:\t\n") 106 _, _ = fmt.Fprintf(tw, " URL:\thttps://%s%s\n", r.Vcenter.Hostname, port) 107 _, _ = fmt.Fprintf(tw, " GUID:\t%s\n", r.Vcenter.ServerGUID) 108 } 109 110 _, _ = fmt.Fprintf(tw, " Placement:\t\n") 111 _, _ = fmt.Fprintf(tw, " Folder:\t%s\n", p("Folder", r.Placement.Folder)) 112 _, _ = fmt.Fprintf(tw, " Cluster:\t%s\n", p("ClusterComputeResource", r.Placement.Cluster)) 113 _, _ = fmt.Fprintf(tw, " Pool:\t%s\n", p("ResourcePool", r.Placement.ResourcePool)) 114 _, _ = fmt.Fprintf(tw, " Network:\t%s\n", p("Network", r.Placement.Network)) 115 116 return tw.Flush() 117 } 118 119 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 120 c, err := cmd.RestClient() 121 if err != nil { 122 return err 123 } 124 125 lib, err := flags.ContentLibrary(ctx, c, f.Arg(0)) 126 if err != nil { 127 return err 128 } 129 m := library.NewManager(c) 130 131 s, err := m.GetSubscriber(ctx, lib, f.Arg(1)) 132 if err != nil { 133 return err 134 } 135 136 return cmd.WriteResult(infoResultsWriter{s, cmd}) 137 }