| 
 | 1 | +// Licensed to Elasticsearch B.V under one or more agreements.  | 
 | 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.  | 
 | 3 | +// See the LICENSE file in the project root for more information  | 
 | 4 | + | 
 | 5 | +using YamlDotNet.Core;  | 
 | 6 | +using YamlDotNet.Core.Events;  | 
 | 7 | +using YamlDotNet.Serialization;  | 
 | 8 | + | 
 | 9 | +namespace Elastic.Markdown.Myst.FrontMatter;  | 
 | 10 | + | 
 | 11 | +[YamlSerializable]  | 
 | 12 | +public record Deployment  | 
 | 13 | +{  | 
 | 14 | +	[YamlMember(Alias = "self")]  | 
 | 15 | +	public SelfManagedDeployment? SelfManaged { get; set; }  | 
 | 16 | + | 
 | 17 | +	[YamlMember(Alias = "cloud")]  | 
 | 18 | +	public CloudManagedDeployment? Cloud { get; set; }  | 
 | 19 | + | 
 | 20 | +	public static Deployment All { get; } = new()  | 
 | 21 | +	{  | 
 | 22 | +		Cloud = CloudManagedDeployment.All,  | 
 | 23 | +		SelfManaged = SelfManagedDeployment.All  | 
 | 24 | +	};  | 
 | 25 | +}  | 
 | 26 | + | 
 | 27 | +[YamlSerializable]  | 
 | 28 | +public record SelfManagedDeployment  | 
 | 29 | +{  | 
 | 30 | +	[YamlMember(Alias = "stack")]  | 
 | 31 | +	public ProductAvailability? Stack { get; set; }  | 
 | 32 | + | 
 | 33 | +	[YamlMember(Alias = "ece")]  | 
 | 34 | +	public ProductAvailability? Ece { get; set; }  | 
 | 35 | + | 
 | 36 | +	[YamlMember(Alias = "eck")]  | 
 | 37 | +	public ProductAvailability? Eck { get; set; }  | 
 | 38 | + | 
 | 39 | +	public static SelfManagedDeployment All { get; } = new()  | 
 | 40 | +	{  | 
 | 41 | +		Stack = ProductAvailability.GenerallyAvailable,  | 
 | 42 | +		Ece = ProductAvailability.GenerallyAvailable,  | 
 | 43 | +		Eck = ProductAvailability.GenerallyAvailable  | 
 | 44 | +	};  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +[YamlSerializable]  | 
 | 48 | +public record CloudManagedDeployment  | 
 | 49 | +{  | 
 | 50 | +	[YamlMember(Alias = "hosted")]  | 
 | 51 | +	public ProductAvailability? Hosted { get; set; }  | 
 | 52 | + | 
 | 53 | +	[YamlMember(Alias = "serverless")]  | 
 | 54 | +	public ProductAvailability? Serverless { get; set; }  | 
 | 55 | + | 
 | 56 | +	public static CloudManagedDeployment All { get; } = new()  | 
 | 57 | +	{  | 
 | 58 | +		Hosted = ProductAvailability.GenerallyAvailable,  | 
 | 59 | +		Serverless = ProductAvailability.GenerallyAvailable  | 
 | 60 | +	};  | 
 | 61 | + | 
 | 62 | +}  | 
 | 63 | + | 
 | 64 | +public class DeploymentConverter : IYamlTypeConverter  | 
 | 65 | +{  | 
 | 66 | +	public bool Accepts(Type type) => type == typeof(Deployment);  | 
 | 67 | + | 
 | 68 | +	public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)  | 
 | 69 | +	{  | 
 | 70 | +		if (parser.TryConsume<Scalar>(out var value))  | 
 | 71 | +		{  | 
 | 72 | +			if (string.IsNullOrWhiteSpace(value.Value))  | 
 | 73 | +				return Deployment.All;  | 
 | 74 | +			if (string.Equals(value.Value, "all", StringComparison.InvariantCultureIgnoreCase))  | 
 | 75 | +				return Deployment.All;  | 
 | 76 | +		}  | 
 | 77 | +		var x = rootDeserializer.Invoke(typeof(Dictionary<string, string>));  | 
 | 78 | +		if (x is not Dictionary<string, string> { Count: > 0 } dictionary)  | 
 | 79 | +			return null;  | 
 | 80 | + | 
 | 81 | +		var deployment = new Deployment();  | 
 | 82 | + | 
 | 83 | +		if (TryGetVersion("stack", out var version))  | 
 | 84 | +		{  | 
 | 85 | +			deployment.SelfManaged ??= new SelfManagedDeployment();  | 
 | 86 | +			deployment.SelfManaged.Stack = version;  | 
 | 87 | +		}  | 
 | 88 | +		if (TryGetVersion("ece", out version))  | 
 | 89 | +		{  | 
 | 90 | +			deployment.SelfManaged ??= new SelfManagedDeployment();  | 
 | 91 | +			deployment.SelfManaged.Ece = version;  | 
 | 92 | +		}  | 
 | 93 | +		if (TryGetVersion("eck", out version))  | 
 | 94 | +		{  | 
 | 95 | +			deployment.SelfManaged ??= new SelfManagedDeployment();  | 
 | 96 | +			deployment.SelfManaged.Eck = version;  | 
 | 97 | +		}  | 
 | 98 | +		if (TryGetVersion("hosted", out version))  | 
 | 99 | +		{  | 
 | 100 | +			deployment.Cloud ??= new CloudManagedDeployment();  | 
 | 101 | +			deployment.Cloud.Hosted = version;  | 
 | 102 | +		}  | 
 | 103 | +		if (TryGetVersion("serverless", out version))  | 
 | 104 | +		{  | 
 | 105 | +			deployment.Cloud ??= new CloudManagedDeployment();  | 
 | 106 | +			deployment.Cloud.Serverless = version;  | 
 | 107 | +		}  | 
 | 108 | +		return deployment;  | 
 | 109 | + | 
 | 110 | +		bool TryGetVersion(string key, out ProductAvailability? semVersion)  | 
 | 111 | +		{  | 
 | 112 | +			semVersion = null;  | 
 | 113 | +			return dictionary.TryGetValue(key, out var v) && ProductAvailability.TryParse(v, out semVersion);  | 
 | 114 | +		}  | 
 | 115 | + | 
 | 116 | +	}  | 
 | 117 | + | 
 | 118 | +	public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) =>  | 
 | 119 | +		serializer.Invoke(value, type);  | 
 | 120 | +}  | 
0 commit comments