github.com/vmware/govmomi@v0.43.0/govc/cluster/mv.go (about) 1 /* 2 Copyright (c) 2023-2023 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 cluster 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 mv struct { 30 *flags.ClusterFlag 31 } 32 33 func init() { 34 cli.Register("cluster.mv", &mv{}) 35 } 36 37 func (cmd *mv) Register(ctx context.Context, f *flag.FlagSet) { 38 cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx) 39 cmd.ClusterFlag.Register(ctx, f) 40 } 41 42 func (cmd *mv) Process(ctx context.Context) error { 43 if err := cmd.ClusterFlag.Process(ctx); err != nil { 44 return err 45 } 46 47 return nil 48 } 49 50 func (cmd *mv) Description() string { 51 return `Move HOST to CLUSTER. 52 53 The hosts are moved to the cluster specified by the 'cluster' flag. 54 55 Examples: 56 govc cluster.mv -cluster ClusterA host1 host2` 57 } 58 59 func (cmd *mv) Move(ctx context.Context, cluster *object.ClusterComputeResource, hosts []*object.HostSystem) error { 60 task, err := cluster.MoveInto(ctx, hosts...) 61 if err != nil { 62 return err 63 } 64 65 logger := cmd.ProgressLogger(fmt.Sprintf("moving %d hosts to cluster %s... ", len(hosts), cluster.InventoryPath)) 66 defer logger.Wait() 67 68 _, err = task.WaitForResult(ctx, logger) 69 return err 70 } 71 72 func (cmd *mv) Run(ctx context.Context, f *flag.FlagSet) error { 73 if f.NArg() == 0 { 74 return flag.ErrHelp 75 } 76 77 cluster, err := cmd.Cluster() 78 if err != nil { 79 return err 80 } 81 82 finder, err := cmd.Finder() 83 if err != nil { 84 return err 85 } 86 87 var hosts []*object.HostSystem 88 89 for _, path := range f.Args() { 90 list, err := finder.HostSystemList(ctx, path) 91 if err != nil { 92 return err 93 } 94 hosts = append(hosts, list...) 95 } 96 97 return cmd.Move(ctx, cluster, hosts) 98 }