Skip to content

Commit

Permalink
CA-384191: Fix missing values for min VM restrictions
Browse files Browse the repository at this point in the history
- Min and max should also include the defaults when looking across all templates
- Ensure exception isn't hit unnecessarily when parsing restriction value for VMs
- Allow use of `GetRestrictionValueFromMatchingTemplate` with template objects
- Parallelize `GetRestrictionValueAcrossTemplates` call

Signed-off-by: Danilo Del Busso <[email protected]>
  • Loading branch information
danilo-delbusso authored and kc284 committed Oct 25, 2023
1 parent afb419a commit 14a3539
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions XenModel/XenAPI-Extensions/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ public int MinVCPUs()
/// <returns>The value if found. If not found, null is returned instead</returns>
private static T? GetRestrictionValueFromMatchingTemplate<T>(VM vm, string field, string attribute) where T : struct
{
if (!vm.is_a_template && !string.IsNullOrEmpty(vm.reference_label))
if(vm.is_a_template)
return GetRestrictionValue<T>(vm, field, attribute);

if (!string.IsNullOrEmpty(vm.reference_label))
{
var matchingTemplate = vm.Connection.Cache.VMs
.FirstOrDefault(v => v.is_a_template && v.reference_label == vm.reference_label);
Expand Down Expand Up @@ -288,6 +291,13 @@ public int MinVCPUs()
var xn = xd?.SelectSingleNode($@"restrictions/restriction[@field='{field}']");
var resultString = xn?.Attributes?[attribute]?.Value;
T? result = null;

// avoid the expensive operation of throwing an exception
if (string.IsNullOrEmpty(resultString))
{
return null;
}

try
{
var converter = TypeDescriptor.GetConverter(typeof(T));
Expand Down Expand Up @@ -320,6 +330,7 @@ public int MinVCPUs()
private static List<T> GetRestrictionValueAcrossTemplates<T>(IXenObject vm, string field, string attribute) where T : struct
{
return vm.Connection.Cache.VMs
.AsParallel()
.Where(v => v.is_a_template)
.Select(v => GetRestrictionValue<T>(v, field, attribute))
.Where(value => value != null)
Expand All @@ -343,7 +354,8 @@ private static T GetMaxRestrictionValue<T>(VM vm, string field, T defaultValue)
return (T) value;

var templateValues = GetRestrictionValueAcrossTemplates<T>(vm, field, "max");
return templateValues.Count == 0 ? defaultValue : templateValues.Max();
templateValues.Add(defaultValue);
return templateValues.Max();
}

/// <summary>
Expand All @@ -362,7 +374,8 @@ private static T GetMinRestrictionValue<T>(VM vm, string field, T defaultValue)
return (T)value;

var templateValues = GetRestrictionValueAcrossTemplates<T>(vm, field, "min");
return templateValues.Count == 0 ? defaultValue : templateValues.Min();
templateValues.Add(defaultValue);
return templateValues.Min();
}

/// <summary>
Expand Down

0 comments on commit 14a3539

Please sign in to comment.