DSCv3ConfigurationSetProcessorFactory.cs (7269B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="DSCv3ConfigurationSetProcessorFactory.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.Processor 8 { 9 using System; 10 using System.Collections; 11 using System.Collections.Generic; 12 using System.Diagnostics.CodeAnalysis; 13 using Microsoft.Management.Configuration; 14 using Microsoft.Management.Configuration.Processor.DSCv3.Helpers; 15 using Microsoft.Management.Configuration.Processor.DSCv3.Set; 16 using Microsoft.Management.Configuration.Processor.Factory; 17 18 /// <summary> 19 /// IConfigurationSetProcessorFactory implementation using DSC v3. 20 /// </summary> 21 internal sealed partial class DSCv3ConfigurationSetProcessorFactory : ConfigurationSetProcessorFactoryBase, IConfigurationSetProcessorFactory, IDictionary<string, string> 22 { 23 private const string DscExecutablePathPropertyName = "DscExecutablePath"; 24 private const string FoundDscExecutablePathPropertyName = "FoundDscExecutablePath"; 25 private const string DiagnosticTraceEnabledPropertyName = "DiagnosticTraceEnabled"; 26 private const string FindDscStateMachinePropertyName = "FindDscStateMachine"; 27 28 private ProcessorSettings processorSettings = new (); 29 30 /// <summary> 31 /// Initializes a new instance of the <see cref="DSCv3ConfigurationSetProcessorFactory"/> class. 32 /// </summary> 33 public DSCv3ConfigurationSetProcessorFactory() 34 { 35 this.processorSettings.DiagnosticsSink = this; 36 } 37 38 /// <summary> 39 /// Gets or sets the path to the DSC v3 executable. 40 /// </summary> 41 public string? DscExecutablePath 42 { 43 get 44 { 45 return this.processorSettings.DscExecutablePath; 46 } 47 48 set 49 { 50 if (this.IsLimitMode()) 51 { 52 throw new InvalidOperationException("Setting DscExecutablePath in limit mode is invalid."); 53 } 54 55 this.processorSettings.DscExecutablePath = value; 56 } 57 } 58 59 #if !AICLI_DISABLE_TEST_HOOKS 60 /// <summary> 61 /// Gets the processor settings; for tests only. 62 /// </summary> 63 public ProcessorSettings Settings 64 { 65 get 66 { 67 return this.processorSettings; 68 } 69 } 70 #endif 71 72 /// <inheritdoc /> 73 public ICollection<string> Keys => throw new NotImplementedException(); 74 75 /// <inheritdoc /> 76 public ICollection<string> Values => throw new NotImplementedException(); 77 78 /// <inheritdoc /> 79 public int Count => throw new NotImplementedException(); 80 81 /// <inheritdoc /> 82 public bool IsReadOnly => this.IsLimitMode(); 83 84 /// <inheritdoc /> 85 public string this[string key] { get => this.GetValue(key); set => this.SetValue(key, value); } 86 87 /// <inheritdoc /> 88 public void Add(string key, string value) 89 { 90 this.SetValue(key, value); 91 } 92 93 /// <inheritdoc /> 94 public void Add(KeyValuePair<string, string> item) 95 { 96 this.SetValue(item.Key, item.Value); 97 } 98 99 /// <inheritdoc /> 100 public void Clear() 101 { 102 throw new NotImplementedException(); 103 } 104 105 /// <inheritdoc /> 106 public bool Contains(KeyValuePair<string, string> item) 107 { 108 throw new NotImplementedException(); 109 } 110 111 /// <inheritdoc /> 112 public bool ContainsKey(string key) 113 { 114 switch (key) 115 { 116 case DscExecutablePathPropertyName: 117 return this.DscExecutablePath != null; 118 } 119 120 return false; 121 } 122 123 /// <inheritdoc /> 124 public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) 125 { 126 throw new NotImplementedException(); 127 } 128 129 /// <inheritdoc /> 130 public IEnumerator<KeyValuePair<string, string>> GetEnumerator() 131 { 132 throw new NotImplementedException(); 133 } 134 135 /// <inheritdoc /> 136 public bool Remove(string key) 137 { 138 throw new NotImplementedException(); 139 } 140 141 /// <inheritdoc /> 142 public bool Remove(KeyValuePair<string, string> item) 143 { 144 throw new NotImplementedException(); 145 } 146 147 /// <inheritdoc /> 148 public bool TryGetValue(string key, [MaybeNullWhen(false)] out string value) 149 { 150 value = null; 151 152 switch (key) 153 { 154 case DscExecutablePathPropertyName: 155 value = this.DscExecutablePath!; 156 return true; 157 case FoundDscExecutablePathPropertyName: 158 value = this.processorSettings.GetFoundDscExecutablePath() !; 159 return true; 160 case DiagnosticTraceEnabledPropertyName: 161 value = this.processorSettings.DiagnosticTraceEnabled.ToString(); 162 return true; 163 case FindDscStateMachinePropertyName: 164 value = this.processorSettings.PumpFindDscStateMachine().ToString(); 165 return true; 166 } 167 168 return false; 169 } 170 171 /// <inheritdoc /> 172 IEnumerator IEnumerable.GetEnumerator() 173 { 174 return this.GetEnumerator(); 175 } 176 177 /// <inheritdoc /> 178 protected override IConfigurationSetProcessor CreateSetProcessorInternal(ConfigurationSet? set, bool isLimitMode) 179 { 180 ProcessorSettings processorSettingsCopy = this.processorSettings.Clone(); 181 this.OnDiagnostics(DiagnosticLevel.Verbose, "Creating set processor with settings:\n" + processorSettingsCopy.ToString()); 182 return new DSCv3ConfigurationSetProcessor(processorSettingsCopy, set, isLimitMode) { SetProcessorFactory = this }; 183 } 184 185 private string GetValue(string name) 186 { 187 if (this.TryGetValue(name, out string? result)) 188 { 189 return result; 190 } 191 192 throw new ArgumentOutOfRangeException($"Invalid property name: {name}"); 193 } 194 195 private void SetValue(string name, string value) 196 { 197 switch (name) 198 { 199 case DscExecutablePathPropertyName: 200 this.DscExecutablePath = value; 201 break; 202 case DiagnosticTraceEnabledPropertyName: 203 this.processorSettings.DiagnosticTraceEnabled = bool.Parse(value); 204 break; 205 default: 206 throw new ArgumentOutOfRangeException($"Invalid property name: {name}"); 207 } 208 } 209 } 210 }