github.com/vmware/govmomi@v0.37.2/govc/cluster/rule/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 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 create struct {
    28  	*SpecFlag
    29  	*InfoFlag
    30  
    31  	vmhost       bool
    32  	affinity     bool
    33  	antiaffinity bool
    34  	depends      bool
    35  }
    36  
    37  func init() {
    38  	cli.Register("cluster.rule.create", &create{})
    39  }
    40  
    41  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.SpecFlag = new(SpecFlag)
    43  	cmd.SpecFlag.Register(ctx, f)
    44  
    45  	cmd.InfoFlag, ctx = NewInfoFlag(ctx)
    46  	cmd.InfoFlag.Register(ctx, f)
    47  
    48  	f.BoolVar(&cmd.vmhost, "vm-host", false, "Virtual Machines to Hosts")
    49  	f.BoolVar(&cmd.affinity, "affinity", false, "Keep Virtual Machines Together")
    50  	f.BoolVar(&cmd.antiaffinity, "anti-affinity", false, "Separate Virtual Machines")
    51  	f.BoolVar(&cmd.depends, "depends", false, "Virtual Machines to Virtual Machines")
    52  }
    53  
    54  func (cmd *create) Process(ctx context.Context) error {
    55  	if cmd.name == "" {
    56  		return flag.ErrHelp
    57  	}
    58  	return cmd.InfoFlag.Process(ctx)
    59  }
    60  
    61  func (cmd *create) Usage() string {
    62  	return "NAME..."
    63  }
    64  
    65  func (cmd *create) Description() string {
    66  	return `Create cluster rule.
    67  
    68  Rules are not enabled by default, use the 'enable' flag to enable upon creation or cluster.rule.change after creation.
    69  
    70  One of '-affinity', '-anti-affinity', '-depends' or '-vm-host' must be provided to specify the rule type.
    71  
    72  With '-affinity' or '-anti-affinity', at least 2 vm NAME arguments must be specified.
    73  
    74  With '-depends', vm group NAME and vm group dependency NAME arguments must be specified.
    75  
    76  With '-vm-host', use the '-vm-group' flag combined with the '-host-affine-group' and/or '-host-anti-affine-group' flags.
    77  
    78  Examples:
    79    govc cluster.rule.create -name pod1 -enable -affinity vm_a vm_b vm_c
    80    govc cluster.rule.create -name pod2 -enable -anti-affinity vm_d vm_e vm_f
    81    govc cluster.rule.create -name pod3 -enable -mandatory -vm-host -vm-group my_vms -host-affine-group my_hosts
    82    govc cluster.rule.create -name pod4 -depends vm_group_app vm_group_db`
    83  }
    84  
    85  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    86  	args := f.Args()
    87  	update := types.ArrayUpdateSpec{Operation: types.ArrayUpdateOperationAdd}
    88  	var rule types.BaseClusterRuleInfo
    89  	var err error
    90  
    91  	switch {
    92  	case cmd.vmhost:
    93  		rule = &cmd.ClusterVmHostRuleInfo
    94  	case cmd.affinity:
    95  		rule = &cmd.ClusterAffinityRuleSpec
    96  		if len(args) < 2 {
    97  			return flag.ErrHelp // can't create this rule without 2 or more hosts
    98  		}
    99  		cmd.ClusterAffinityRuleSpec.Vm, err = cmd.ObjectList(ctx, "VirtualMachine", args)
   100  		if err != nil {
   101  			return err
   102  		}
   103  	case cmd.antiaffinity:
   104  		rule = &cmd.ClusterAntiAffinityRuleSpec
   105  		if len(args) < 2 {
   106  			return flag.ErrHelp // can't create this rule without 2 or more hosts
   107  		}
   108  		cmd.ClusterAntiAffinityRuleSpec.Vm, err = cmd.ObjectList(ctx, "VirtualMachine", args)
   109  		if err != nil {
   110  			return err
   111  		}
   112  	case cmd.depends:
   113  		if len(args) != 2 {
   114  			return flag.ErrHelp
   115  		}
   116  		rule = &types.ClusterDependencyRuleInfo{
   117  			VmGroup:          args[0],
   118  			DependsOnVmGroup: args[1],
   119  		}
   120  	default:
   121  		return flag.ErrHelp
   122  	}
   123  
   124  	if cmd.Enabled == nil {
   125  		// ClusterDependencyRuleInfo throws InvalidArgument if Enabled == nil
   126  		cmd.Enabled = types.NewBool(false)
   127  	}
   128  
   129  	info := rule.GetClusterRuleInfo()
   130  	info.Name = cmd.name
   131  	info.Enabled = cmd.Enabled
   132  	info.Mandatory = cmd.Mandatory
   133  	info.UserCreated = types.NewBool(true)
   134  
   135  	return cmd.Apply(ctx, update, rule)
   136  }