github.com/vmware/govmomi@v0.43.0/govc/cluster/module/vm_rm.go (about) 1 /* 2 Copyright (c) 2022 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 module 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/vapi/cluster" 27 "github.com/vmware/govmomi/vim25/mo" 28 ) 29 30 type rmVMs struct { 31 *flags.SearchFlag 32 moduleID string 33 } 34 35 func init() { 36 cli.Register("cluster.module.vm.rm", &rmVMs{}) 37 } 38 39 func (cmd *rmVMs) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.SearchFlag, ctx = flags.NewSearchFlag(ctx, flags.SearchVirtualMachines) 41 cmd.SearchFlag.Register(ctx, f) 42 43 f.StringVar(&cmd.moduleID, "id", "", "Module ID") 44 } 45 46 func (cmd *rmVMs) Process(ctx context.Context) error { 47 return cmd.SearchFlag.Process(ctx) 48 } 49 50 func (cmd *rmVMs) Usage() string { 51 return `VM...` 52 } 53 54 func (cmd *rmVMs) Description() string { 55 return `Remove VM(s) from a cluster module. 56 57 Examples: 58 govc cluster.module.vm.rm -id module_id $vm...` 59 } 60 61 func (cmd *rmVMs) Run(ctx context.Context, f *flag.FlagSet) error { 62 if f.NArg() < 1 { 63 return flag.ErrHelp 64 } 65 66 c, err := cmd.RestClient() 67 if err != nil { 68 return err 69 } 70 71 vms, err := cmd.VirtualMachines(f.Args()) 72 if err != nil { 73 return err 74 } 75 76 refs := make([]mo.Reference, 0, len(vms)) 77 for _, vm := range vms { 78 refs = append(refs, vm.Reference()) 79 } 80 81 allRemoved, err := cluster.NewManager(c).RemoveModuleMembers(ctx, cmd.moduleID, refs...) 82 if err != nil { 83 return err 84 } 85 86 if !allRemoved { 87 return fmt.Errorf("a VM was not a member of the module") 88 } 89 90 return nil 91 }