vsphere/resource_vsphere_datastore_cluster_vm_anti_affinity_rule.go
func resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry(
	pod *object.StoragePod,
	key int32,
) (*types.ClusterAntiAffinityRuleSpec, error) {
	props, err := storagepod.Properties(pod)
	if err != nil {
		return nil, fmt.Errorf("error fetching datastore cluster properties: %s", err)
	}

	for _, info := range props.PodStorageDrsEntry.StorageDrsConfig.PodConfig.Rule {
		if info.GetClusterRuleInfo().Key == key {
			if vmAntiAffinityRuleInfo, ok := info.(*types.ClusterAntiAffinityRuleSpec); ok {
				log.Printf("[DEBUG] Found VM anti-affinity rule key %d in datastore cluster %q", key, pod.Name())
				return vmAntiAffinityRuleInfo, nil
			}
			return nil, fmt.Errorf("rule key %d in datastore cluster %q is not a VM anti-affinity rule", key, pod.Name())
		}
	}

	log.Printf("[DEBUG] No VM anti-affinity rule key %d found in datastore cluster %q", key, pod.Name())
	return nil, nil
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry(
	pod *object.StoragePod,
	key int32,
) (*types.ClusterAntiAffinityRuleSpec, error) {
	props, err := storagepod.Properties(pod)
	if err != nil {
		return nil, fmt.Errorf("error fetching datastore cluster properties: %s", err)
	}

	for _, info := range props.PodStorageDrsEntry.StorageDrsConfig.PodConfig.Rule {
		if info.GetClusterRuleInfo().Key == key {
			if vmAntiAffinityRuleInfo, ok := info.(*types.ClusterAntiAffinityRuleSpec); ok {
				log.Printf("[DEBUG] Found VM anti-affinity rule key %d in datastore cluster %q", key, pod.Name())
				return vmAntiAffinityRuleInfo, nil
			}
			return nil, fmt.Errorf("rule key %d in datastore cluster %q is not a VM anti-affinity rule", key, pod.Name())
		}
	}

	log.Printf("[DEBUG] No VM anti-affinity rule key %d found in datastore cluster %q", key, pod.Name())
	return nil, nil
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry attempts to
// locate an existing VM anti-affinity rule in a datastore 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.