github.com/vmware/govmomi@v0.43.0/govc/host/autostart/add.go (about)

     1  /*
     2  Copyright (c) 2015-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 autostart
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type add struct {
    31  	*AutostartFlag
    32  	// from types.AutoStartPowerInfo
    33  	StartOrder       int32
    34  	StartDelay       int32
    35  	WaitForHeartbeat string
    36  	StartAction      string
    37  	StopDelay        int32
    38  	StopAction       string
    39  }
    40  
    41  func init() {
    42  	cli.Register("host.autostart.add", &add{})
    43  }
    44  
    45  var waitHeartbeatTypes = types.AutoStartWaitHeartbeatSetting("").Strings()
    46  
    47  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    48  	cmd.AutostartFlag, ctx = newAutostartFlag(ctx)
    49  	cmd.AutostartFlag.Register(ctx, f)
    50  
    51  	cmd.StartOrder = -1
    52  	cmd.StartDelay = -1
    53  	cmd.StopDelay = -1
    54  	f.Var(flags.NewInt32(&cmd.StartOrder), "start-order", "Start Order")
    55  	f.Var(flags.NewInt32(&cmd.StartDelay), "start-delay", "Start Delay")
    56  	f.Var(flags.NewInt32(&cmd.StopDelay), "stop-delay", "Stop Delay")
    57  	f.StringVar(&cmd.StartAction, "start-action", "powerOn", "Start Action")
    58  	f.StringVar(&cmd.StopAction, "stop-action", "systemDefault", "Stop Action")
    59  	f.StringVar(&cmd.WaitForHeartbeat, "wait", string(types.AutoStartWaitHeartbeatSettingSystemDefault),
    60  		fmt.Sprintf("Wait for Hearbeat Setting (%s)", strings.Join(waitHeartbeatTypes, "|")))
    61  }
    62  
    63  func (cmd *add) Process(ctx context.Context) error {
    64  	if err := cmd.AutostartFlag.Process(ctx); err != nil {
    65  		return err
    66  	}
    67  	return nil
    68  }
    69  
    70  func (cmd *add) Usage() string {
    71  	return "VM..."
    72  }
    73  
    74  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
    75  	powerInfo := types.AutoStartPowerInfo{
    76  		StartOrder:       cmd.StartOrder,
    77  		StartDelay:       cmd.StartDelay,
    78  		WaitForHeartbeat: types.AutoStartWaitHeartbeatSetting(cmd.WaitForHeartbeat),
    79  		StartAction:      cmd.StartAction,
    80  		StopDelay:        cmd.StopDelay,
    81  		StopAction:       cmd.StopAction,
    82  	}
    83  	return cmd.ReconfigureVMs(f.Args(), powerInfo)
    84  }