github.com/vmware/govmomi@v0.37.1/govc/task/create.go (about) 1 /* 2 Copyright (c) 2024-2024 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 task 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 24 "github.com/vmware/govmomi/govc/cli" 25 "github.com/vmware/govmomi/govc/flags" 26 "github.com/vmware/govmomi/session" 27 "github.com/vmware/govmomi/task" 28 "github.com/vmware/govmomi/vim25/methods" 29 "github.com/vmware/govmomi/vim25/types" 30 ) 31 32 type create struct { 33 *flags.ClientFlag 34 35 obj string 36 } 37 38 func init() { 39 cli.Register("task.create", &create{}, true) 40 } 41 42 func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) { 43 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 44 cmd.ClientFlag.Register(ctx, f) 45 46 f.StringVar(&cmd.obj, "o", "", "ManagedObject with which Task will be associated") 47 } 48 49 func (cmd *create) Description() string { 50 return `Create task of type ID. 51 52 ID must be one of: 53 govc extension.info -json | jq -r '.extensions[].taskList | select(. != null) | .[].taskID' 54 55 Examples: 56 govc task.create $ID` 57 } 58 59 func (cmd *create) Usage() string { 60 return "ID" 61 } 62 63 func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error { 64 c, err := cmd.Client() 65 if err != nil { 66 return err 67 } 68 69 if f.NArg() != 1 { 70 return flag.ErrHelp 71 } 72 73 m := task.NewManager(c) 74 75 req := types.CreateTask{ 76 This: m.Reference(), 77 Obj: c.ServiceContent.RootFolder, 78 TaskTypeId: f.Arg(0), 79 Cancelable: true, 80 } 81 82 if cmd.obj != "" { 83 req.Obj.FromString(cmd.obj) 84 } 85 86 s, err := session.NewManager(c).UserSession(ctx) 87 if err != nil { 88 return err 89 } 90 req.InitiatedBy = s.UserName 91 92 res, err := methods.CreateTask(ctx, c, &req) 93 if err != nil { 94 return err 95 } 96 97 fmt.Println(res.Returnval.Task) 98 99 return nil 100 }