github.com/vmware/govmomi@v0.43.0/govc/cluster/group/create.go (about) 1 /* 2 Copyright (c) 2017 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 group 18 19 import ( 20 "context" 21 "flag" 22 23 "github.com/vmware/govmomi/govc/cli" 24 "github.com/vmware/govmomi/vim25/types" 25 ) 26 27 type create struct { 28 *InfoFlag 29 30 vm bool 31 host bool 32 } 33 34 func init() { 35 cli.Register("cluster.group.create", &create{}) 36 } 37 38 func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) { 39 cmd.InfoFlag, ctx = NewInfoFlag(ctx) 40 cmd.InfoFlag.Register(ctx, f) 41 42 f.BoolVar(&cmd.vm, "vm", false, "Create cluster VM group") 43 f.BoolVar(&cmd.host, "host", false, "Create cluster Host group") 44 } 45 46 func (cmd *create) Process(ctx context.Context) error { 47 if cmd.name == "" { 48 return flag.ErrHelp 49 } 50 return cmd.InfoFlag.Process(ctx) 51 } 52 53 func (cmd *create) Description() string { 54 return `Create cluster group. 55 56 One of '-vm' or '-host' must be provided to specify the group type. 57 58 Examples: 59 govc cluster.group.create -name my_vm_group -vm vm_a vm_b vm_c 60 govc cluster.group.create -name my_host_group -host host_a host_b host_c` 61 } 62 63 func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error { 64 update := types.ArrayUpdateSpec{Operation: types.ArrayUpdateOperationAdd} 65 var info types.BaseClusterGroupInfo 66 var err error 67 68 switch { 69 case cmd.vm: 70 info = new(types.ClusterVmGroup) 71 case cmd.host: 72 info = new(types.ClusterHostGroup) 73 default: 74 return flag.ErrHelp 75 } 76 77 info.GetClusterGroupInfo().Name = cmd.name 78 79 group := newGroupInfo(info) 80 *group.refs, err = cmd.ObjectList(ctx, group.kind, f.Args()) 81 if err != nil { 82 return err 83 } 84 85 return cmd.Apply(ctx, update, info) 86 }