github.com/vmware/govmomi@v0.37.2/govc/cluster/group/change.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 change struct {
    28  	*InfoFlag
    29  }
    30  
    31  func init() {
    32  	cli.Register("cluster.group.change", &change{})
    33  }
    34  
    35  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    36  	cmd.InfoFlag, ctx = NewInfoFlag(ctx)
    37  	cmd.InfoFlag.Register(ctx, f)
    38  }
    39  
    40  func (cmd *change) Process(ctx context.Context) error {
    41  	if cmd.name == "" {
    42  		return flag.ErrHelp
    43  	}
    44  	return cmd.InfoFlag.Process(ctx)
    45  }
    46  
    47  func (cmd *change) Usage() string {
    48  	return `NAME...`
    49  }
    50  
    51  func (cmd *change) Description() string {
    52  	return `Set cluster group members.
    53  
    54  Examples:
    55    govc cluster.group.change -name my_group vm_a vm_b vm_c # set
    56    govc cluster.group.change -name my_group vm_a vm_b vm_c $(govc cluster.group.ls -name my_group) vm_d # add
    57    govc cluster.group.ls -name my_group | grep -v vm_b | xargs govc cluster.group.change -name my_group vm_a vm_b vm_c # remove`
    58  }
    59  
    60  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    61  	update := types.ArrayUpdateSpec{Operation: types.ArrayUpdateOperationEdit}
    62  	group, err := cmd.Group(ctx)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	refs, err := cmd.ObjectList(ctx, group.kind, f.Args())
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	*group.refs = refs
    73  
    74  	return cmd.Apply(ctx, update, group.info)
    75  }