github.com/vmware/govmomi@v0.43.0/govc/host/autostart/info.go (about) 1 /* 2 Copyright (c) 2015 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 autostart 18 19 import ( 20 "context" 21 "encoding/json" 22 "flag" 23 "fmt" 24 "io" 25 "os" 26 "text/tabwriter" 27 28 "github.com/vmware/govmomi/govc/cli" 29 "github.com/vmware/govmomi/govc/flags" 30 "github.com/vmware/govmomi/vim25" 31 "github.com/vmware/govmomi/vim25/mo" 32 ) 33 34 type info struct { 35 cli.Command 36 37 *AutostartFlag 38 *flags.OutputFlag 39 } 40 41 func init() { 42 cli.Register("host.autostart.info", &info{}) 43 } 44 45 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 46 cmd.AutostartFlag, ctx = newAutostartFlag(ctx) 47 cmd.AutostartFlag.Register(ctx, f) 48 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 49 cmd.OutputFlag.Register(ctx, f) 50 } 51 52 func (cmd *info) Process(ctx context.Context) error { 53 if err := cmd.AutostartFlag.Process(ctx); err != nil { 54 return err 55 } 56 if err := cmd.OutputFlag.Process(ctx); err != nil { 57 return err 58 } 59 return nil 60 } 61 62 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 63 client, err := cmd.Client() 64 if err != nil { 65 return err 66 } 67 68 mhas, err := cmd.HostAutoStartManager() 69 if err != nil { 70 return err 71 } 72 73 return cmd.WriteResult(&infoResult{client, mhas}) 74 } 75 76 type infoResult struct { 77 client *vim25.Client 78 mhas *mo.HostAutoStartManager 79 } 80 81 func (r *infoResult) MarshalJSON() ([]byte, error) { 82 return json.Marshal(r.mhas) 83 } 84 85 // vmPaths resolves the paths for the VMs in the result. 86 func (r *infoResult) vmPaths() (map[string]string, error) { 87 ctx := context.TODO() 88 paths := make(map[string]string) 89 for _, info := range r.mhas.Config.PowerInfo { 90 mes, err := mo.Ancestors(ctx, r.client, r.client.ServiceContent.PropertyCollector, info.Key) 91 if err != nil { 92 return nil, err 93 } 94 95 path := "" 96 for _, me := range mes { 97 // Skip root entity in building inventory path. 98 if me.Parent == nil { 99 continue 100 } 101 path += "/" + me.Name 102 } 103 104 paths[info.Key.Value] = path 105 } 106 107 return paths, nil 108 } 109 110 func (r *infoResult) Write(w io.Writer) error { 111 paths, err := r.vmPaths() 112 if err != nil { 113 return err 114 } 115 116 tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) 117 118 fmt.Fprintf(tw, "VM") 119 fmt.Fprintf(tw, "\tStartAction") 120 fmt.Fprintf(tw, "\tStartDelay") 121 fmt.Fprintf(tw, "\tStartOrder") 122 fmt.Fprintf(tw, "\tStopAction") 123 fmt.Fprintf(tw, "\tStopDelay") 124 fmt.Fprintf(tw, "\tWaitForHeartbeat") 125 fmt.Fprintf(tw, "\n") 126 127 for _, info := range r.mhas.Config.PowerInfo { 128 fmt.Fprintf(tw, "%s", paths[info.Key.Value]) 129 fmt.Fprintf(tw, "\t%s", info.StartAction) 130 fmt.Fprintf(tw, "\t%d", info.StartDelay) 131 fmt.Fprintf(tw, "\t%d", info.StartOrder) 132 fmt.Fprintf(tw, "\t%s", info.StopAction) 133 fmt.Fprintf(tw, "\t%d", info.StopDelay) 134 fmt.Fprintf(tw, "\t%s", info.WaitForHeartbeat) 135 fmt.Fprintf(tw, "\n") 136 } 137 138 _ = tw.Flush() 139 return nil 140 }