// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AWS.Deploy.Common.Recipes.Validation; using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace AWS.Deploy.Common.Recipes { /// /// Defines a contract for for getters and setters. /// public interface IOptionSettingItem { /// /// Retrieve the value of an as a specified type. /// T? GetValue(IDictionary replacementTokens, IDictionary? displayableOptionSettings = null); /// /// Retrieve the value of an as an object. /// object GetValue(IDictionary replacementTokens, IDictionary? displayableOptionSettings = null); /// /// Retrieve the default value of an as a specified type. /// T? GetDefaultValue(IDictionary replacementTokens); /// /// Retrieve the default value of an as an object. /// object? GetDefaultValue(IDictionary replacementTokens); /// /// Set the value of an while validating the provided input. /// /// Handler use to set any child option settings /// Value to set /// Validators for this item /// Selected recommendation /// Enables or disables running validators specified in Task SetValue(IOptionSettingHandler optionSettingHandler, object value, IOptionSettingItemValidator[] validators, Recommendation recommendation, bool skipValidation); } /// /// Container for the setting values /// public partial class OptionSettingItem : IOptionSettingItem { /// /// The unique id of setting. This value will be persisted in other config files so its value should never change once a recipe is released. /// public string Id { get; set; } /// /// The fully qualified id of the setting that includes the Id and all of the parent's Ids. /// This helps easily reference the Option Setting without context of the parent setting. /// public string FullyQualifiedId { get; set; } /// /// The parent Option Setting Item that allows an option setting item to reference it's parent. /// public string? ParentId { get; set; } /// /// The id of the parent option setting. This is used for tooling that wants to look up the existing resources for a setting based on the TypeHint but needs /// to know the parent AWS resource. For example if listing the available Beanstalk environments the listing should be for the environments of the Beanstalk application. /// public string? ParentSettingId { get; set; } /// /// The display friendly name of the setting. /// public string Name { get; set; } /// /// The category for the setting. This value must match an id field in the list of categories. /// public string? Category { get; set; } /// /// The description of what the setting is used for. /// public string Description { get; set; } /// /// The type of primitive value expected for this setting. /// For example String, Int /// [JsonConverter(typeof(StringEnumConverter))] public OptionSettingValueType Type { get; set; } /// /// Hint the the UI what type of setting this is optionally add additional UI features to select a value. /// For example a value of BeanstalkApplication tells the UI it can display the list of available Beanstalk applications for the user to pick from. /// [JsonConverter(typeof(StringEnumConverter))] public OptionSettingTypeHint? TypeHint { get; set; } /// /// The default value used for the recipe if the user doesn't override the value. /// public object? DefaultValue { get; set; } /// /// The value used for the recipe if it is set by the user. /// private object? _value { get; set; } /// /// UI can use this to reduce the amount of settings to show to the user when confirming the recommendation. This can make it so the user sees only the most important settings /// they need to know about before deploying. /// public bool AdvancedSetting { get; set; } /// /// If true the setting can be changed during a redeployment. /// public bool Updatable { get; set; } /// /// List of all validators that should be run when configuring this OptionSettingItem. /// public List Validators { get; set; } = new (); /// /// The allowed values for the setting. /// public IList AllowedValues { get; set; } = new List(); /// /// The value mapping for allowed values. The key of the dictionary is what is sent to services /// and the value is the display value shown to users. /// public IDictionary ValueMapping { get; set; } = new Dictionary(StringComparer.OrdinalIgnoreCase); /// /// Property will be displayed if specified dependencies are true /// public IList DependsOn { get; set; } = new List(); /// /// Child option settings for value types /// value depends on the values of /// public List ChildOptionSettings { get; set; } = new (); /// /// Type hint additional data required to facilitate handling of the option setting. /// public Dictionary TypeHintData { get; set; } = new (); /// /// Indicates which option setting items need to be validated if a value update occurs. /// public List Dependents { get; set; } = new List (); /// /// The validation state of the setting that contains the validation status and message. /// [JsonIgnore] public OptionSettingValidation Validation { get; set; } public OptionSettingItem(string id, string fullyQualifiedId, string name, string description) { Id = id; FullyQualifiedId = fullyQualifiedId; Name = name; Description = description; Validation = new OptionSettingValidation(); } /// /// Helper method to get strongly type . /// /// Type of the type hint data /// Returns strongly type type hint data. Returns default value if is empty. public T? GetTypeHintData() { if (!TypeHintData.Any()) { return default; } var serialized = JsonConvert.SerializeObject(TypeHintData); return JsonConvert.DeserializeObject(serialized); } } }