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 committed Oct 24, 2023
1 parent afb419a commit 0bf2af1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 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,6 +354,7 @@ private static T GetMaxRestrictionValue<T>(VM vm, string field, T defaultValue)
return (T) value;

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

Expand All @@ -360,8 +372,8 @@ private static T GetMinRestrictionValue<T>(VM vm, string field, T defaultValue)
var value = GetRestrictionValueFromMatchingTemplate<T>(vm, field, "min");
if (value != null)
return (T)value;

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

Expand Down

0 comments on commit 0bf2af1

Please sign in to comment.