github.com/vmware/govmomi@v0.37.2/govc/cluster/rule/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 rule
    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  	*SpecFlag
    29  	*InfoFlag
    30  }
    31  
    32  func init() {
    33  	cli.Register("cluster.rule.change", &change{})
    34  }
    35  
    36  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    37  	cmd.SpecFlag = new(SpecFlag)
    38  	cmd.SpecFlag.Register(ctx, f)
    39  
    40  	cmd.InfoFlag, ctx = NewInfoFlag(ctx)
    41  	cmd.InfoFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *change) Process(ctx context.Context) error {
    45  	if cmd.name == "" {
    46  		return flag.ErrHelp
    47  	}
    48  	return cmd.InfoFlag.Process(ctx)
    49  }
    50  
    51  func (cmd *change) Usage() string {
    52  	return `NAME...`
    53  }
    54  
    55  func (cmd *change) Description() string {
    56  	return `Change cluster rule.
    57  
    58  Examples:
    59    govc cluster.rule.change -cluster my_cluster -name my_rule -enable=false`
    60  }
    61  
    62  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    63  	update := types.ArrayUpdateSpec{Operation: types.ArrayUpdateOperationEdit}
    64  	rule, err := cmd.Rule(ctx)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	var vms *[]types.ManagedObjectReference
    70  
    71  	switch r := rule.info.(type) {
    72  	case *types.ClusterAffinityRuleSpec:
    73  		vms = &r.Vm
    74  	case *types.ClusterAntiAffinityRuleSpec:
    75  		vms = &r.Vm
    76  	}
    77  
    78  	if vms != nil && f.NArg() != 0 {
    79  		refs, err := cmd.ObjectList(ctx, rule.kind, f.Args())
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		*vms = refs
    85  	}
    86  
    87  	info := rule.info.GetClusterRuleInfo()
    88  	info.Name = cmd.name
    89  	info.Enabled = cmd.Enabled
    90  	info.Mandatory = cmd.Mandatory
    91  
    92  	return cmd.Apply(ctx, update, rule.info)
    93  }