Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace VirtualClient.Common.Contracts
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using VirtualClient.Common.Extensions;

/// <summary>
/// Provides a JSON converter that can handle the serialization/deserialization of
/// <see cref="List{T}"/> objects where T is <see cref="IDictionary{TKey, TValue}"/> with string keys and <see cref="IConvertible"/> values.
/// </summary>
public class ParameterDictionaryCollectionJsonConverter : JsonConverter
{
private static readonly Type ParameterDictionaryListType = typeof(List<IDictionary<string, IConvertible>>);
private static readonly ParameterDictionaryJsonConverter DictionaryConverter = new ParameterDictionaryJsonConverter();

/// <summary>
/// Returns true/false whether the object type is supported for JSON serialization/deserialization.
/// </summary>
/// <param name="objectType">The type of object to serialize/deserialize.</param>
/// <returns>
/// True if the object is supported, false if not.
/// </returns>
public override bool CanConvert(Type objectType)
{
return objectType == ParameterDictionaryListType;
}

/// <summary>
/// Reads the JSON text from the reader and converts it into a <see cref="List{T}"/> of <see cref="IDictionary{TKey, TValue}"/>
/// object instance.
/// </summary>
/// <param name="reader">Contains the JSON text defining the list of dictionaries object.</param>
/// <param name="objectType">The type of object (in practice this will only be a list of dictionaries type).</param>
/// <param name="existingValue">Unused.</param>
/// <param name="serializer">Unused.</param>
/// <returns>
/// A deserialized list of dictionaries object converted from JSON text.
/// </returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader == null)
{
throw new ArgumentException("The reader parameter is required.", nameof(reader));
}

List<IDictionary<string, IConvertible>> list = new List<IDictionary<string, IConvertible>>();
if (reader.TokenType == JsonToken.StartArray)
{
JArray array = JArray.Load(reader);
foreach (JToken item in array)
{
if (item.Type == JTokenType.Object)
{
IDictionary<string, IConvertible> dictionary = new Dictionary<string, IConvertible>();
ReadDictionaryEntries(item, dictionary);
list.Add(dictionary);
}
}
}

return list;
}

/// <summary>
/// Writes a list of dictionaries object to JSON text.
/// </summary>
/// <param name="writer">Handles the writing of the JSON text.</param>
/// <param name="value">The list of dictionaries object to serialize to JSON text.</param>
/// <param name="serializer">The JSON serializer handling the serialization to JSON text.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.ThrowIfNull(nameof(writer));
serializer.ThrowIfNull(nameof(serializer));

List<IDictionary<string, IConvertible>> list = value as List<IDictionary<string, IConvertible>>;
if (list != null)
{
writer.WriteStartArray();
foreach (var dictionary in list)
{
WriteDictionaryEntries(writer, dictionary, serializer);
}

writer.WriteEndArray();
}
}

private static void ReadDictionaryEntries(JToken jsonObject, IDictionary<string, IConvertible> dictionary)
{
IEnumerable<JToken> children = jsonObject.Children();
if (children.Any())
{
foreach (JToken child in children)
{
if (child.Type == JTokenType.Property)
{
if (child.First != null)
{
JValue propertyValue = child.First as JValue;
IConvertible settingValue = propertyValue?.Value as IConvertible;

// JSON properties that have periods (.) in them will have a path representation
// like this: ['this.is.a.path']. We have to account for that when adding the key
// to the dictionary. The key we want to add is 'this.is.a.path'
string key = child.Path;
int lastDotIndex = key.LastIndexOf('.');
if (lastDotIndex >= 0)
{
key = key.Substring(lastDotIndex + 1);
}

dictionary.Add(key, settingValue);
}
}
}
}
}

private static void WriteDictionaryEntries(JsonWriter writer, IDictionary<string, IConvertible> dictionary, JsonSerializer serializer)
{
writer.WriteStartObject();
if (dictionary.Count > 0)
{
foreach (KeyValuePair<string, IConvertible> entry in dictionary)
{
writer.WritePropertyName(entry.Key);
serializer.Serialize(writer, entry.Value);
}
}

writer.WriteEndObject();
}
}
}
20 changes: 17 additions & 3 deletions src/VirtualClient/VirtualClient.Contracts/ExecutionProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public ExecutionProfile(
IEnumerable<ExecutionProfileElement> dependencies,
IEnumerable<ExecutionProfileElement> monitors,
IDictionary<string, IConvertible> metadata,
IDictionary<string, IConvertible> parameters)
IDictionary<string, IConvertible> parameters,
List<IDictionary<string, IConvertible>> parametersOn = null)
{
description.ThrowIfNullOrWhiteSpace(nameof(description));

Expand All @@ -62,6 +63,10 @@ public ExecutionProfile(
? new Dictionary<string, IConvertible>(parameters, StringComparer.OrdinalIgnoreCase)
: new Dictionary<string, IConvertible>(StringComparer.OrdinalIgnoreCase);

this.ParametersOn = parametersOn != null
? parametersOn
: new List<IDictionary<string, IConvertible>>();

if (this.Actions?.Any() == true)
{
this.Actions.ForEach(action => action.ComponentType = ComponentType.Action);
Expand Down Expand Up @@ -90,7 +95,8 @@ public ExecutionProfile(ExecutionProfile other)
other?.Dependencies,
other?.Monitors,
other?.Metadata,
other?.Parameters)
other?.Parameters,
other?.ParametersOn)
{
}

Expand All @@ -106,7 +112,8 @@ public ExecutionProfile(ExecutionProfileYamlShim other)
other?.Dependencies?.Select(d => new ExecutionProfileElement(d)),
other?.Monitors?.Select(m => new ExecutionProfileElement(m)),
other?.Metadata,
other?.Parameters)
other?.Parameters,
other?.ParametersOn)
{
}

Expand Down Expand Up @@ -148,6 +155,13 @@ public ExecutionProfile(ExecutionProfileYamlShim other)
[JsonConverter(typeof(ParameterDictionaryJsonConverter))]
public IDictionary<string, IConvertible> Parameters { get; }

/// <summary>
/// List of parameter dictionaries that are associated with the profile.
/// </summary>
[JsonProperty(PropertyName = "ParametersOn", Required = Required.Default, Order = 75)]
[JsonConverter(typeof(ParameterDictionaryCollectionJsonConverter))]
public List<IDictionary<string, IConvertible>> ParametersOn { get; }

/// <summary>
/// Workload actions to run as part of the profile execution.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ namespace VirtualClient.Contracts
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using VirtualClient.Common.Extensions;
using VirtualClient.Common.Telemetry;

/// <summary>
/// Extension methods for <see cref="ExecutionProfile"/>
Expand All @@ -28,6 +32,54 @@ public static void Inline(this ExecutionProfile profile)
ExecutionProfileExtensions.InlineElements(elements, profile.Parameters);
}

/// <summary>
/// Processes conditional parameter sets in the profile's ParametersOn sections.
/// </summary>
/// <param name="profile">The execution profile to process.</param>
/// <param name="dependencies">The service dependencies.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static async Task EvaluateConditionalParametersAsync(this ExecutionProfile profile, IServiceCollection dependencies)
{
if (profile.ParametersOn?.Any() == true)
{
const string conditionKey = "Condition";
var evaluator = dependencies.GetService<IExpressionEvaluator>();

IDictionary<string, IConvertible> profileParameters = new Dictionary<string, IConvertible>(profile.Parameters, StringComparer.OrdinalIgnoreCase);
await evaluator.EvaluateAsync(dependencies, profileParameters);

foreach (var parametersSection in profile.ParametersOn)
{
if (!parametersSection.TryGetValue(conditionKey, out IConvertible condition))
{
throw new SchemaException(
$"Invalid '{nameof(profile.ParametersOn)}' configuration. A '{conditionKey}' must be defined in each '{nameof(profile.ParametersOn)}' section.");
}

// Parameters in ParametersOn sections take priority over the profile's default parameters.
IDictionary<string, IConvertible> conditionalParameters = new Dictionary<string, IConvertible>(parametersSection, StringComparer.OrdinalIgnoreCase);
conditionalParameters.AddRange(profileParameters);

await evaluator.EvaluateAsync(dependencies, conditionalParameters);

if (!bool.TryParse(conditionalParameters[conditionKey].ToString(), out bool conditionMatches))
{
throw new SchemaException(
$"Invalid '{nameof(profile.ParametersOn)}' configuration. A '{conditionKey}' must always evaluate to true or false.");
}

if (conditionMatches)
{
profile.Parameters.Clear();
profile.Parameters.AddRange(conditionalParameters);
break;
}
}

profile.ParametersOn.Clear();
}
}

/// <summary>
/// Returns true/false whether the current executor scenario matches any defined in the
/// excluded scenarios supplied.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public ExecutionProfileYamlShim()
this.Monitors = new List<ExecutionProfileElementYamlShim>();
this.Metadata = new Dictionary<string, IConvertible>(StringComparer.OrdinalIgnoreCase);
this.Parameters = new Dictionary<string, IConvertible>(StringComparer.OrdinalIgnoreCase);
this.ParametersOn = new List<IDictionary<string, IConvertible>>();
}

/// <summary>
Expand Down Expand Up @@ -68,6 +69,11 @@ public ExecutionProfileYamlShim(ExecutionProfile other)
this.Parameters.AddRange(other.Parameters);
}

if (other?.ParametersOn?.Any() == true)
{
this.ParametersOn.AddRange(other.ParametersOn);
}

if (this.Actions?.Any() == true)
{
this.Actions.ForEach(action => action.ComponentType = ComponentType.Action);
Expand Down Expand Up @@ -108,6 +114,12 @@ public ExecutionProfileYamlShim(ExecutionProfile other)
[YamlMember(Alias = "parameters", Order = 20, ScalarStyle = ScalarStyle.Plain)]
public IDictionary<string, IConvertible> Parameters { get; set; }

/// <summary>
/// Collection of parameters that are associated with the profile.
/// </summary>
[YamlMember(Alias = "parameters_on", Order = 25, ScalarStyle = ScalarStyle.Plain)]
public List<IDictionary<string, IConvertible>> ParametersOn { get; set; }

/// <summary>
/// Workload actions to run as part of the profile execution.
/// </summary>
Expand Down
Loading
Loading