github.com/vmware/govmomi@v0.43.0/govc/host/disconnect.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 host
    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/object"
    27  )
    28  
    29  type disconnect struct {
    30  	*flags.HostSystemFlag
    31  }
    32  
    33  func init() {
    34  	cli.Register("host.disconnect", &disconnect{})
    35  }
    36  
    37  func (cmd *disconnect) Register(ctx context.Context, f *flag.FlagSet) {
    38  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    39  	cmd.HostSystemFlag.Register(ctx, f)
    40  }
    41  
    42  func (cmd *disconnect) Process(ctx context.Context) error {
    43  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  func (cmd *disconnect) Description() string {
    50  	return `Disconnect HOST from vCenter.`
    51  }
    52  
    53  func (cmd *disconnect) Disconnect(ctx context.Context, host *object.HostSystem) error {
    54  	task, err := host.Disconnect(ctx)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	logger := cmd.ProgressLogger(fmt.Sprintf("%s disconnecting... ", host.InventoryPath))
    60  	defer logger.Wait()
    61  
    62  	_, err = task.WaitForResult(ctx, logger)
    63  	return err
    64  }
    65  
    66  func (cmd *disconnect) Run(ctx context.Context, f *flag.FlagSet) error {
    67  	hosts, err := cmd.HostSystems(f.Args())
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	for _, host := range hosts {
    73  		err = cmd.Disconnect(ctx, host)
    74  		if err != nil {
    75  			return err
    76  		}
    77  	}
    78  
    79  	return nil
    80  }