File size: 6,119 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;
using Microsoft.PowerShell.Cim;

using Dbg = System.Management.Automation.Diagnostics;

namespace Microsoft.PowerShell.Cmdletization.Cim
{
    internal sealed class CimCustomOptionsDictionary
    {
        private readonly IDictionary<string, object> _dict;
        private readonly object _dictModificationLock = new();

        private CimCustomOptionsDictionary(IEnumerable<KeyValuePair<string, object>> wrappedDictionary)
        {
            // no need to lock _dictModificationLock inside the constructor
            _dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            foreach (var kvp in wrappedDictionary)
            {
                _dict[kvp.Key] = kvp.Value;
            }
        }

        private IEnumerable<KeyValuePair<string, object>> GetSnapshot()
        {
            lock (_dictModificationLock)
            {
                return _dict.ToList();
            }
        }

        internal static CimCustomOptionsDictionary Create(IEnumerable<KeyValuePair<string, object>> wrappedDictionary)
        {
            return new CimCustomOptionsDictionary(wrappedDictionary);
        }

        private static readonly ConditionalWeakTable<CimInstance, CimCustomOptionsDictionary> s_cimInstanceToCustomOptions = new();

        internal static void AssociateCimInstanceWithCustomOptions(CimInstance cimInstance, CimCustomOptionsDictionary newCustomOptions)
        {
            if (newCustomOptions == null)
            {
                return;
            }

            lock (newCustomOptions._dictModificationLock)
            {
                if (newCustomOptions._dict.Count == 0)
                {
                    return;
                }
            }

            bool foundAssociatedOptions = true;
            CimCustomOptionsDictionary oldCustomOptions = s_cimInstanceToCustomOptions.GetValue(
                cimInstance,
                delegate
                    {
                        foundAssociatedOptions = false;
                        return newCustomOptions;
                    });

            if (foundAssociatedOptions)
            {
                lock (oldCustomOptions._dictModificationLock)
                {
                    foreach (KeyValuePair<string, object> newCustomOption in newCustomOptions.GetSnapshot())
                    {
                        oldCustomOptions._dict[newCustomOption.Key] = newCustomOption.Value;
                    }
                }
            }
        }

        internal static CimCustomOptionsDictionary MergeOptions(CimCustomOptionsDictionary optionsFromCommandLine, CimInstance instanceRelatedToThisOperation)
        {
            CimCustomOptionsDictionary instanceRelatedOptions;
            if (s_cimInstanceToCustomOptions.TryGetValue(instanceRelatedToThisOperation, out instanceRelatedOptions) && instanceRelatedOptions != null)
            {
                IEnumerable<KeyValuePair<string, object>> instanceRelatedOptionsSnapshot = instanceRelatedOptions.GetSnapshot();
                IEnumerable<KeyValuePair<string, object>> optionsFromCommandLineSnapshot = optionsFromCommandLine.GetSnapshot();
                var mergedOptions = instanceRelatedOptionsSnapshot.Concat(optionsFromCommandLineSnapshot); // note - order matters here
                return new CimCustomOptionsDictionary(mergedOptions);
            }
            else
            {
                return optionsFromCommandLine;
            }
        }

        internal static CimCustomOptionsDictionary MergeOptions(CimCustomOptionsDictionary optionsFromCommandLine, IEnumerable<CimInstance> instancesRelatedToThisOperation)
        {
            CimCustomOptionsDictionary result = optionsFromCommandLine;
            if (instancesRelatedToThisOperation != null)
            {
                foreach (CimInstance instanceRelatedToThisOperation in instancesRelatedToThisOperation)
                {
                    result = MergeOptions(result, instanceRelatedToThisOperation);
                }
            }

            return result;
        }

        internal void Apply(CimOperationOptions cimOperationOptions, CimSensitiveValueConverter cimSensitiveValueConverter)
        {
            CimOperationOptionsHelper.SetCustomOptions(cimOperationOptions, this.GetSnapshot(), cimSensitiveValueConverter);
        }
    }

    /// <summary>
    /// CimQuery supports building of queries against CIM object model.
    /// </summary>
    internal static class CimOperationOptionsHelper
    {
        internal static void SetCustomOptions(
            CimOperationOptions operationOptions,
            IEnumerable<KeyValuePair<string, object>> customOptions,
            CimSensitiveValueConverter cimSensitiveValueConverter)
        {
            if (customOptions != null)
            {
                foreach (KeyValuePair<string, object> queryOption in customOptions)
                {
                    SetCustomOption(operationOptions, queryOption.Key, queryOption.Value, cimSensitiveValueConverter);
                }
            }
        }

        internal static void SetCustomOption(
            CimOperationOptions operationOptions,
            string optionName,
            object optionValue,
            CimSensitiveValueConverter cimSensitiveValueConverter)
        {
            Dbg.Assert(!string.IsNullOrWhiteSpace(optionName), "Caller should verify optionName != null");

            if (optionValue == null)
            {
                return;
            }

            object cimValue = cimSensitiveValueConverter.ConvertFromDotNetToCim(optionValue);
            CimType cimType = CimConverter.GetCimType(CimSensitiveValueConverter.GetCimType(optionValue.GetType()));

            operationOptions.SetCustomOption(optionName, cimValue, cimType, mustComply: false);
        }
    }
}