github.com/vmware/govmomi@v0.43.0/govc/cluster/rule/info_flag.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 "fmt" 23 24 "github.com/vmware/govmomi/govc/flags" 25 "github.com/vmware/govmomi/vim25/types" 26 ) 27 28 type InfoFlag struct { 29 *flags.ClusterFlag 30 31 rules []types.BaseClusterRuleInfo 32 33 name string 34 Long bool 35 } 36 37 func NewInfoFlag(ctx context.Context) (*InfoFlag, context.Context) { 38 f := &InfoFlag{} 39 f.ClusterFlag, ctx = flags.NewClusterFlag(ctx) 40 return f, ctx 41 } 42 43 func (f *InfoFlag) Register(ctx context.Context, fs *flag.FlagSet) { 44 f.ClusterFlag.Register(ctx, fs) 45 46 fs.StringVar(&f.name, "name", "", "Cluster rule name") 47 fs.BoolVar(&f.Long, "l", false, "Long listing format") 48 } 49 50 func (f *InfoFlag) Process(ctx context.Context) error { 51 return f.ClusterFlag.Process(ctx) 52 } 53 54 func (f *InfoFlag) Rules(ctx context.Context) ([]types.BaseClusterRuleInfo, error) { 55 if f.rules != nil { 56 return f.rules, nil 57 } 58 59 cluster, err := f.Cluster() 60 if err != nil { 61 return nil, err 62 } 63 64 config, err := cluster.Configuration(ctx) 65 if err != nil { 66 return nil, err 67 } 68 69 f.rules = config.Rule 70 71 return f.rules, nil 72 } 73 74 type ClusterRuleInfo struct { 75 info types.BaseClusterRuleInfo 76 77 ruleType string 78 79 // only ClusterAffinityRuleSpec and ClusterAntiAffinityRuleSpec 80 refs *[]types.ManagedObjectReference 81 82 kind string 83 84 // only ClusterVmHostRuleInfo 85 vmGroupName string 86 affineHostGroupName string 87 antiAffineHostGroupName string 88 89 // only ClusterDependencyRuleInfo 90 VmGroup string 91 DependsOnVmGroup string 92 } 93 94 func (f *InfoFlag) Rule(ctx context.Context) (*ClusterRuleInfo, error) { 95 rules, err := f.Rules(ctx) 96 if err != nil { 97 return nil, err 98 } 99 100 for _, rule := range rules { 101 if rule.GetClusterRuleInfo().Name != f.name { 102 continue 103 } 104 105 r := GetExtendedClusterRuleInfo(rule) 106 return &r, nil 107 } 108 109 return nil, fmt.Errorf("rule %q not found", f.name) 110 } 111 112 func GetExtendedClusterRuleInfo(rule types.BaseClusterRuleInfo) ClusterRuleInfo { 113 r := ClusterRuleInfo{info: rule} 114 115 switch info := rule.(type) { 116 case *types.ClusterAffinityRuleSpec: 117 r.ruleType = "ClusterAffinityRuleSpec" 118 r.refs = &info.Vm 119 r.kind = "VirtualMachine" 120 case *types.ClusterAntiAffinityRuleSpec: 121 r.ruleType = "ClusterAntiAffinityRuleSpec" 122 r.refs = &info.Vm 123 r.kind = "VirtualMachine" 124 case *types.ClusterVmHostRuleInfo: 125 r.ruleType = "ClusterVmHostRuleInfo" 126 r.vmGroupName = info.VmGroupName 127 r.affineHostGroupName = info.AffineHostGroupName 128 r.antiAffineHostGroupName = info.AntiAffineHostGroupName 129 case *types.ClusterDependencyRuleInfo: 130 r.ruleType = "ClusterDependencyRuleInfo" 131 r.VmGroup = info.VmGroup 132 r.DependsOnVmGroup = info.DependsOnVmGroup 133 } 134 return r 135 } 136 137 func (f *InfoFlag) Apply(ctx context.Context, update types.ArrayUpdateSpec, info types.BaseClusterRuleInfo) error { 138 spec := &types.ClusterConfigSpecEx{ 139 RulesSpec: []types.ClusterRuleSpec{ 140 { 141 ArrayUpdateSpec: update, 142 Info: info, 143 }, 144 }, 145 } 146 147 return f.Reconfigure(ctx, spec) 148 } 149 150 type SpecFlag struct { 151 types.ClusterRuleInfo 152 types.ClusterVmHostRuleInfo 153 types.ClusterAffinityRuleSpec //nolint:govet 154 types.ClusterAntiAffinityRuleSpec //nolint:govet 155 } 156 157 func (s *SpecFlag) Register(ctx context.Context, f *flag.FlagSet) { 158 f.Var(flags.NewOptionalBool(&s.Enabled), "enable", "Enable rule") 159 f.Var(flags.NewOptionalBool(&s.Mandatory), "mandatory", "Enforce rule compliance") 160 161 f.StringVar(&s.VmGroupName, "vm-group", "", "VM group name") 162 f.StringVar(&s.AffineHostGroupName, "host-affine-group", "", "Host affine group name") 163 f.StringVar(&s.AntiAffineHostGroupName, "host-anti-affine-group", "", "Host anti-affine group name") 164 }