1/* 2Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package vm 18 19import ( 20 "context" 21 "errors" 22 "flag" 23 "fmt" 24 25 "github.com/vmware/govmomi/govc/cli" 26 "github.com/vmware/govmomi/govc/flags" 27 "github.com/vmware/govmomi/property" 28 "github.com/vmware/govmomi/vim25/mo" 29 "github.com/vmware/govmomi/vim25/types" 30) 31 32type question struct { 33 *flags.VirtualMachineFlag 34 35 answer string 36} 37 38func init() { 39 cli.Register("vm.question", &question{}) 40} 41 42func (cmd *question) Register(ctx context.Context, f *flag.FlagSet) { 43 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 44 cmd.VirtualMachineFlag.Register(ctx, f) 45 46 f.StringVar(&cmd.answer, "answer", "", "Answer to question") 47} 48 49func (cmd *question) Process(ctx context.Context) error { 50 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 51 return err 52 } 53 return nil 54} 55 56func (cmd *question) Run(ctx context.Context, f *flag.FlagSet) error { 57 c, err := cmd.Client() 58 if err != nil { 59 return err 60 } 61 62 vm, err := cmd.VirtualMachine() 63 if err != nil { 64 return err 65 } 66 67 if vm == nil { 68 return errors.New("No VM specified") 69 } 70 71 var mvm mo.VirtualMachine 72 73 pc := property.DefaultCollector(c) 74 err = pc.RetrieveOne(ctx, vm.Reference(), []string{"runtime.question"}, &mvm) 75 if err != nil { 76 return err 77 } 78 79 q := mvm.Runtime.Question 80 if q == nil { 81 fmt.Printf("No pending question\n") 82 return nil 83 } 84 85 // Print question if no answer is specified 86 if cmd.answer == "" { 87 fmt.Printf("Question:\n%s\n\n", q.Text) 88 fmt.Printf("Possible answers:\n") 89 for _, e := range q.Choice.ChoiceInfo { 90 ed := e.(*types.ElementDescription) 91 fmt.Printf("%s) %s\n", ed.Key, ed.Description.Label) 92 } 93 return nil 94 } 95 96 // Answer question 97 return vm.Answer(ctx, q.Id, cmd.answer) 98} 99