vsphere/resource_vsphere_compute_cluster_vm_affinity_rule.go
func resourceVSphereComputeClusterVMAffinityRuleFindEntry(
	cluster *object.ClusterComputeResource,
	key int32,
) (*types.ClusterAffinityRuleSpec, error) {
	props, err := clustercomputeresource.Properties(cluster)
	if err != nil {
		return nil, fmt.Errorf("error fetching cluster properties: %s", err)
	}

	for _, info := range props.ConfigurationEx.(*types.ClusterConfigInfoEx).Rule {
		if info.GetClusterRuleInfo().Key == key {
			if vmAffinityRuleInfo, ok := info.(*types.ClusterAffinityRuleSpec); ok {
				log.Printf("[DEBUG] Found VM affinity rule key %d in cluster %q", key, cluster.Name())
				return vmAffinityRuleInfo, nil
			}
			return nil, fmt.Errorf("rule key %d in cluster %q is not a VM affinity rule", key, cluster.Name())
		}
	}

	log.Printf("[DEBUG] No VM affinity rule key %d found in cluster %q", key, cluster.Name())
	return nil, nil
}
func resourceVSphereComputeClusterVMAffinityRuleFindEntry(
	cluster *object.ClusterComputeResource,
	key int32,
) (*types.ClusterAffinityRuleSpec, error) {
	props, err := clustercomputeresource.Properties(cluster)
	if err != nil {
		return nil, fmt.Errorf("error fetching cluster properties: %s", err)
	}

	for _, info := range props.ConfigurationEx.(*types.ClusterConfigInfoEx).Rule {
		if info.GetClusterRuleInfo().Key == key {
			if vmAffinityRuleInfo, ok := info.(*types.ClusterAffinityRuleSpec); ok {
				log.Printf("[DEBUG] Found VM affinity rule key %d in cluster %q", key, cluster.Name())
				return vmAffinityRuleInfo, nil
			}
			return nil, fmt.Errorf("rule key %d in cluster %q is not a VM affinity rule", key, cluster.Name())
		}
	}

	log.Printf("[DEBUG] No VM affinity rule key %d found in cluster %q", key, cluster.Name())
	return nil, nil
}
// resourceVSphereComputeClusterVMAffinityRuleFindEntry attempts to locate an
// existing VM affinity rule in a cluster's configuration by key. It's used by the
// resource's read functionality and tests. nil is returned if the entry cannot
// be found.