github.com/vmware/govmomi@v0.43.0/govc/host/remove.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  	"github.com/vmware/govmomi/vim25/mo"
    28  )
    29  
    30  type remove struct {
    31  	*flags.HostSystemFlag
    32  }
    33  
    34  func init() {
    35  	cli.Register("host.remove", &remove{})
    36  }
    37  
    38  func (cmd *remove) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    40  	cmd.HostSystemFlag.Register(ctx, f)
    41  }
    42  
    43  func (cmd *remove) Process(ctx context.Context) error {
    44  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    45  		return err
    46  	}
    47  	return nil
    48  }
    49  
    50  func (cmd *remove) Usage() string {
    51  	return "HOST..."
    52  }
    53  
    54  func (cmd *remove) Description() string {
    55  	return `Remove HOST from vCenter.`
    56  }
    57  
    58  func (cmd *remove) Remove(ctx context.Context, host *object.HostSystem) error {
    59  	var h mo.HostSystem
    60  	err := host.Properties(ctx, host.Reference(), []string{"parent"}, &h)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	remove := host.Destroy
    66  
    67  	if h.Parent.Type == "ComputeResource" {
    68  		// Standalone host.  From the docs:
    69  		// "Invoking remove on a HostSystem of standalone type throws a NotSupported fault.
    70  		//  A standalone HostSystem can be removeed only by invoking remove on its parent ComputeResource."
    71  		remove = object.NewComputeResource(host.Client(), *h.Parent).Destroy
    72  	}
    73  
    74  	task, err := remove(ctx)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	logger := cmd.ProgressLogger(fmt.Sprintf("%s removing... ", host.InventoryPath))
    80  	defer logger.Wait()
    81  
    82  	_, err = task.WaitForResult(ctx, logger)
    83  	return err
    84  }
    85  
    86  func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error {
    87  	hosts, err := cmd.HostSystems(f.Args())
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	for _, host := range hosts {
    93  		err = cmd.Remove(ctx, host)
    94  		if err != nil {
    95  			return err
    96  		}
    97  	}
    98  
    99  	return nil
   100  }