github.com/vmware/govmomi@v0.37.2/govc/cluster/rule/info.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/cli"
    25  )
    26  
    27  type info struct {
    28  	*InfoFlag
    29  }
    30  
    31  func init() {
    32  	cli.Register("cluster.rule.info", &info{})
    33  }
    34  
    35  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    36  	cmd.InfoFlag, ctx = NewInfoFlag(ctx)
    37  	cmd.InfoFlag.Register(ctx, f)
    38  }
    39  
    40  func (cmd *info) Process(ctx context.Context) error {
    41  	return cmd.InfoFlag.Process(ctx)
    42  }
    43  
    44  func (cmd *info) Description() string {
    45  	return `Provides detailed infos about cluster rules, their types and rule members.
    46  
    47  Examples:
    48    govc cluster.rule.info -cluster my_cluster
    49    govc cluster.rule.info -cluster my_cluster -name my_rule`
    50  }
    51  
    52  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    53  	var res ruleResult
    54  
    55  	rules, err := cmd.Rules(ctx)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	for _, rule := range rules {
    61  		ruleName := rule.GetClusterRuleInfo().Name
    62  		ruleInfo := GetExtendedClusterRuleInfo(rule)
    63  		if cmd.name == "" || cmd.name == ruleName {
    64  			res = append(res, fmt.Sprintf("Rule: %s", ruleName))
    65  			res = append(res, fmt.Sprintf("  Type: %s", ruleInfo.ruleType))
    66  			switch ruleInfo.ruleType {
    67  			case "ClusterAffinityRuleSpec", "ClusterAntiAffinityRuleSpec":
    68  				names, err := cmd.Names(ctx, *ruleInfo.refs)
    69  				if err != nil {
    70  					cmd.WriteResult(res)
    71  					return err
    72  				}
    73  
    74  				for _, ref := range *ruleInfo.refs {
    75  					res = append(res, fmt.Sprintf("  VM: %s", names[ref]))
    76  				}
    77  			case "ClusterVmHostRuleInfo":
    78  				res = append(res, fmt.Sprintf("  vmGroupName: %s", ruleInfo.vmGroupName))
    79  				res = append(res, fmt.Sprintf("  affineHostGroupName %s", ruleInfo.affineHostGroupName))
    80  				res = append(res, fmt.Sprintf("  antiAffineHostGroupName %s", ruleInfo.antiAffineHostGroupName))
    81  			case "ClusterDependencyRuleInfo":
    82  				res = append(res, fmt.Sprintf("  VmGroup %s", ruleInfo.VmGroup))
    83  				res = append(res, fmt.Sprintf("  DependsOnVmGroup %s", ruleInfo.DependsOnVmGroup))
    84  			default:
    85  				res = append(res, "unknown rule type, no further rule details known")
    86  			}
    87  		}
    88  
    89  	}
    90  
    91  	return cmd.WriteResult(res)
    92  }