github.com/vmware/govmomi@v0.51.0/cli/host/remove.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package host 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 12 "github.com/vmware/govmomi/cli" 13 "github.com/vmware/govmomi/cli/flags" 14 "github.com/vmware/govmomi/object" 15 "github.com/vmware/govmomi/vim25/mo" 16 ) 17 18 type remove struct { 19 *flags.HostSystemFlag 20 } 21 22 func init() { 23 cli.Register("host.remove", &remove{}) 24 } 25 26 func (cmd *remove) Register(ctx context.Context, f *flag.FlagSet) { 27 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 28 cmd.HostSystemFlag.Register(ctx, f) 29 } 30 31 func (cmd *remove) Process(ctx context.Context) error { 32 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 33 return err 34 } 35 return nil 36 } 37 38 func (cmd *remove) Usage() string { 39 return "HOST..." 40 } 41 42 func (cmd *remove) Description() string { 43 return `Remove HOST from vCenter.` 44 } 45 46 func (cmd *remove) Remove(ctx context.Context, host *object.HostSystem) error { 47 var h mo.HostSystem 48 err := host.Properties(ctx, host.Reference(), []string{"parent"}, &h) 49 if err != nil { 50 return err 51 } 52 53 remove := host.Destroy 54 55 if h.Parent.Type == "ComputeResource" { 56 // Standalone host. From the docs: 57 // "Invoking remove on a HostSystem of standalone type throws a NotSupported fault. 58 // A standalone HostSystem can be removeed only by invoking remove on its parent ComputeResource." 59 remove = object.NewComputeResource(host.Client(), *h.Parent).Destroy 60 } 61 62 task, err := remove(ctx) 63 if err != nil { 64 return err 65 } 66 67 logger := cmd.ProgressLogger(fmt.Sprintf("%s removing... ", host.InventoryPath)) 68 defer logger.Wait() 69 70 _, err = task.WaitForResult(ctx, logger) 71 return err 72 } 73 74 func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error { 75 hosts, err := cmd.HostSystems(f.Args()) 76 if err != nil { 77 return err 78 } 79 80 for _, host := range hosts { 81 err = cmd.Remove(ctx, host) 82 if err != nil { 83 return err 84 } 85 } 86 87 return nil 88 }