Skip to content

Commit

Permalink
govc: add new command cluster.rule.info
Browse files Browse the repository at this point in the history
  • Loading branch information
UBessle committed Jul 27, 2018
1 parent a05cd4b commit 1350eea
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 25 deletions.
92 changes: 92 additions & 0 deletions govc/cluster/rule/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rule

import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
)

type info struct {
*InfoFlag
}

func init() {
cli.Register("cluster.rule.info", &info{})
}

func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
cmd.InfoFlag, ctx = NewInfoFlag(ctx)
cmd.InfoFlag.Register(ctx, f)
}

func (cmd *info) Process(ctx context.Context) error {
return cmd.InfoFlag.Process(ctx)
}

func (cmd *info) Description() string {
return `Provides detailed infos about cluster rules, their types and rule members.
Examples:
govc cluster.rule.info -cluster my_cluster
govc cluster.rule.info -cluster my_cluster -name my_rule`
}

func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
var res ruleResult

rules, err := cmd.Rules(ctx)
if err != nil {
return err
}

for _, rule := range rules {
ruleName := rule.GetClusterRuleInfo().Name
ruleInfo := GetExtendedClusterRuleInfo(rule)
if cmd.name == "" || cmd.name == ruleName {
res = append(res, fmt.Sprintf("Rule: %s", ruleName))
res = append(res, fmt.Sprintf(" Type: %s", ruleInfo.ruleType))
switch ruleInfo.ruleType {
case "ClusterAffinityRuleSpec", "ClusterAntiAffinityRuleSpec":
names, err := cmd.Names(ctx, *ruleInfo.refs)
if err != nil {
cmd.WriteResult(res)
return err
}

for _, ref := range *ruleInfo.refs {
res = append(res, fmt.Sprintf(" VM: %s", names[ref]))
}
case "ClusterVmHostRuleInfo":
res = append(res, fmt.Sprintf(" vmGroupName: %s", ruleInfo.vmGroupName))
res = append(res, fmt.Sprintf(" affineHostGroupName %s", ruleInfo.affineHostGroupName))
res = append(res, fmt.Sprintf(" antiAffineHostGroupName %s", ruleInfo.antiAffineHostGroupName))
case "ClusterDependencyRuleInfo":
res = append(res, fmt.Sprintf(" VmGroup %s", ruleInfo.VmGroup))
res = append(res, fmt.Sprintf(" DependsOnVmGroup %s", ruleInfo.DependsOnVmGroup))
default:
res = append(res, "unknown rule type, no further rule details known")
}
}

}

return cmd.WriteResult(res)
}
51 changes: 33 additions & 18 deletions govc/cluster/rule/info_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type InfoFlag struct {
rules []types.BaseClusterRuleInfo

name string
Long bool
}

func NewInfoFlag(ctx context.Context) (*InfoFlag, context.Context) {
Expand All @@ -43,6 +44,7 @@ func (f *InfoFlag) Register(ctx context.Context, fs *flag.FlagSet) {
f.ClusterFlag.Register(ctx, fs)

fs.StringVar(&f.name, "name", "", "Cluster rule name")
fs.BoolVar(&f.Long, "l", false, "Long listing format")
}

func (f *InfoFlag) Process(ctx context.Context) error {
Expand Down Expand Up @@ -83,6 +85,10 @@ type ClusterRuleInfo struct {
vmGroupName string
affineHostGroupName string
antiAffineHostGroupName string

// only ClusterDependencyRuleInfo
VmGroup string
DependsOnVmGroup string
}

func (f *InfoFlag) Rule(ctx context.Context) (*ClusterRuleInfo, error) {
Expand All @@ -96,29 +102,38 @@ func (f *InfoFlag) Rule(ctx context.Context) (*ClusterRuleInfo, error) {
continue
}

r := &ClusterRuleInfo{info: rule}

switch info := rule.(type) {
case *types.ClusterAffinityRuleSpec:
r.ruleType = "ClusterAffinityRuleSpec"
r.refs = &info.Vm
r.kind = "VirtualMachine"
case *types.ClusterAntiAffinityRuleSpec:
r.ruleType = "ClusterAntiAffinityRuleSpec"
r.refs = &info.Vm
r.kind = "VirtualMachine"
case *types.ClusterVmHostRuleInfo:
r.ruleType = "ClusterVmHostRuleInfo"
r.vmGroupName = info.VmGroupName
r.affineHostGroupName = info.AffineHostGroupName
r.antiAffineHostGroupName = info.AntiAffineHostGroupName
}
return r, nil
r := GetExtendedClusterRuleInfo(rule)
return &r, nil
}

return nil, fmt.Errorf("rule %q not found", f.name)
}

func GetExtendedClusterRuleInfo(rule types.BaseClusterRuleInfo) ClusterRuleInfo {
r := ClusterRuleInfo{info: rule}

switch info := rule.(type) {
case *types.ClusterAffinityRuleSpec:
r.ruleType = "ClusterAffinityRuleSpec"
r.refs = &info.Vm
r.kind = "VirtualMachine"
case *types.ClusterAntiAffinityRuleSpec:
r.ruleType = "ClusterAntiAffinityRuleSpec"
r.refs = &info.Vm
r.kind = "VirtualMachine"
case *types.ClusterVmHostRuleInfo:
r.ruleType = "ClusterVmHostRuleInfo"
r.vmGroupName = info.VmGroupName
r.affineHostGroupName = info.AffineHostGroupName
r.antiAffineHostGroupName = info.AntiAffineHostGroupName
case *types.ClusterDependencyRuleInfo:
r.ruleType = "ClusterDependencyRuleInfo"
r.VmGroup = info.VmGroup
r.DependsOnVmGroup = info.DependsOnVmGroup
}
return r
}

func (f *InfoFlag) Apply(ctx context.Context, update types.ArrayUpdateSpec, info types.BaseClusterRuleInfo) error {
spec := &types.ClusterConfigSpecEx{
RulesSpec: []types.ClusterRuleSpec{
Expand Down
35 changes: 28 additions & 7 deletions govc/cluster/rule/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func (cmd *ls) Description() string {
Examples:
govc cluster.rule.ls -cluster my_cluster
govc cluster.rule.ls -cluster my_cluster -name my_rule`
govc cluster.rule.ls -cluster my_cluster -name my_rule
govc cluster.rule.ls -cluster my_cluster -l
govc cluster.rule.ls -cluster my_cluster -name my_rule -l`
}

type ruleResult []string
Expand All @@ -70,15 +72,21 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
}

for _, g := range rules {
res = append(res, g.GetClusterRuleInfo().Name)
ruleName := g.GetClusterRuleInfo().Name
if cmd.Long {
ruleTypeInfo := GetExtendedClusterRuleInfo(g).ruleType
res = append(res, fmt.Sprintf("%s (%s)", ruleName, ruleTypeInfo))
} else {
res = append(res, fmt.Sprintf("%s", ruleName))
}
}
} else {
rule, err := cmd.Rule(ctx)
if err != nil {
return err
}

res = append(res, rule.ruleType+":")
//res = append(res, rule.ruleType+":")
switch rule.ruleType {
case "ClusterAffinityRuleSpec", "ClusterAntiAffinityRuleSpec":
names, err := cmd.Names(ctx, *rule.refs)
Expand All @@ -88,12 +96,15 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
}

for _, ref := range *rule.refs {
res = append(res, names[ref])
res = extendedAppend(res, cmd.Long, names[ref], "VM")
}
case "ClusterVmHostRuleInfo":
res = append(res, "VmGroupName="+rule.vmGroupName)
res = append(res, "AffineHostGroupName="+rule.affineHostGroupName)
res = append(res, "AntiAffineHostGroupName="+rule.antiAffineHostGroupName)
res = extendedAppend(res, cmd.Long, rule.vmGroupName, "vmGroupName")
res = extendedAppend(res, cmd.Long, rule.affineHostGroupName, "affineHostGroupName")
res = extendedAppend(res, cmd.Long, rule.antiAffineHostGroupName, "antiAffineHostGroupName")
case "ClusterDependencyRuleInfo":
res = extendedAppend(res, cmd.Long, rule.VmGroup, "VmGroup")
res = extendedAppend(res, cmd.Long, rule.DependsOnVmGroup, "DependsOnVmGroup")
default:
res = append(res, "unknown rule type, no further rule details known")
}
Expand All @@ -102,3 +113,13 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {

return cmd.WriteResult(res)
}

func extendedAppend(res ruleResult, Long bool, entryValue string, entryType string) ruleResult {
var newres ruleResult
if Long {
newres = append(res, fmt.Sprintf("%s (%s)", entryValue, entryType))
} else {
newres = append(res, entryValue)
}
return newres
}
25 changes: 25 additions & 0 deletions govc/test/cluster.bats
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ load test_helper
run govc cluster.rule.ls -cluster DC0_C0 -name pod1 -l=true
assert_success "$(printf "%s (VM)\n" DC0_C0_RP0_VM{0,1,2,3})"

run govc cluster.rule.info -cluster DC0_C0
assert_success "$(cat <<_EOF_
Name: pod1
Type: ClusterAffinityRuleSpec
VM: DC0_C0_RP0_VM0
VM: DC0_C0_RP0_VM1
VM: DC0_C0_RP0_VM2
VM: DC0_C0_RP0_VM3
_EOF_
)"

run govc cluster.rule.change -cluster DC0_C0 -name pod1 DC0_C0_RP0_VM{2,3,4}
assert_success

Expand Down Expand Up @@ -141,6 +152,20 @@ load test_helper
run govc cluster.rule.ls -cluster DC0_C0 -name my_deps -l
assert_success "$(printf "%s\n" {'my_app (VmGroup)','my_db (DependsOnVmGroup)'})"

run govc cluster.rule.info -cluster DC0_C0
assert_success "$(cat <<_EOF_
Name: pod2
Type: ClusterVmHostRuleInfo
vmGroupName: my_vms
affineHostGroupName even_hosts
antiAffineHostGroupName odd_hosts
Name: my_deps
Type: ClusterDependencyRuleInfo
VmGroup my_app
DependsOnVmGroup my_db
_EOF_
)"

}

@test "cluster.vm" {
Expand Down

0 comments on commit 1350eea

Please sign in to comment.