github.com/vmware/govmomi@v0.37.2/govc/task/set.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  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/object"
    26  	"github.com/vmware/govmomi/vim25/types"
    27  )
    28  
    29  type set struct {
    30  	*flags.ClientFlag
    31  
    32  	desc     types.LocalizableMessage
    33  	state    string
    34  	err      string
    35  	progress int
    36  }
    37  
    38  func init() {
    39  	cli.Register("task.set", &set{}, true)
    40  }
    41  
    42  func (cmd *set) 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.desc.Key, "d", "", "Task description key")
    47  	f.StringVar(&cmd.desc.Message, "m", "", "Task description message")
    48  	f.StringVar(&cmd.state, "s", "", "Task state")
    49  	f.StringVar(&cmd.err, "e", "", "Task error")
    50  	f.IntVar(&cmd.progress, "p", 0, "Task progress")
    51  }
    52  
    53  func (cmd *set) Description() string {
    54  	return `Set task state.
    55  
    56  Examples:
    57    id=$(govc task.create com.vmware.govmomi.simulator.test)
    58    govc task.set $id -s error`
    59  }
    60  
    61  func (cmd *set) Usage() string {
    62  	return "ID"
    63  }
    64  
    65  func (cmd *set) Run(ctx context.Context, f *flag.FlagSet) error {
    66  	if f.NArg() != 1 {
    67  		return flag.ErrHelp
    68  	}
    69  
    70  	c, err := cmd.Client()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	ref := types.ManagedObjectReference{Type: "Task"}
    76  	if !ref.FromString(f.Arg(0)) {
    77  		ref.Value = f.Arg(0)
    78  	}
    79  
    80  	task := object.NewTask(c, ref)
    81  
    82  	var fault *types.LocalizedMethodFault
    83  
    84  	if cmd.err != "" {
    85  		fault = &types.LocalizedMethodFault{
    86  			Fault:            &types.SystemError{Reason: cmd.err},
    87  			LocalizedMessage: cmd.err,
    88  		}
    89  		cmd.state = string(types.TaskInfoStateError)
    90  	}
    91  
    92  	if cmd.state != "" {
    93  		err := task.SetState(ctx, types.TaskInfoState(cmd.state), nil, fault)
    94  		if err != nil {
    95  			return err
    96  		}
    97  	}
    98  
    99  	if cmd.progress != 0 {
   100  		err := task.UpdateProgress(ctx, cmd.progress)
   101  		if err != nil {
   102  			return err
   103  		}
   104  	}
   105  
   106  	if cmd.desc.Key != "" {
   107  		err := task.SetDescription(ctx, cmd.desc)
   108  		if err != nil {
   109  			return err
   110  		}
   111  	}
   112  
   113  	return nil
   114  }