File size: 7,767 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management.Automation;

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

using Dbg = System.Management.Automation.Diagnostics;

namespace Microsoft.PowerShell.Cmdletization.Cim
{
    /// <summary>
    /// Job wrapping invocation of an extrinsic CIM method.
    /// </summary>
    internal abstract class MethodInvocationJobBase<T> : CimChildJobBase<T>
    {
        internal MethodInvocationJobBase(CimJobContext jobContext, bool passThru, string methodSubject, MethodInvocationInfo methodInvocationInfo)
                : base(jobContext)
        {
            Dbg.Assert(methodInvocationInfo != null, "Caller should verify methodInvocationInfo != null");
            Dbg.Assert(methodSubject != null, "Caller should verify methodSubject != null");

            _passThru = passThru;
            MethodSubject = methodSubject;
            _methodInvocationInfo = methodInvocationInfo;
        }

        private readonly bool _passThru;
        private readonly MethodInvocationInfo _methodInvocationInfo;

        internal string MethodName
        {
            get { return _methodInvocationInfo.MethodName; }
        }

        private const string CustomOperationOptionPrefix = "cim:operationOption:";

        private IEnumerable<MethodParameter> GetMethodInputParametersCore(Func<MethodParameter, bool> filter)
        {
            IEnumerable<MethodParameter> inputParameters = _methodInvocationInfo.Parameters.Where(filter);

            var result = new List<MethodParameter>();
            foreach (MethodParameter inputParameter in inputParameters)
            {
                object cimValue = CimSensitiveValueConverter.ConvertFromDotNetToCim(inputParameter.Value);
                Type cimType = CimSensitiveValueConverter.GetCimType(inputParameter.ParameterType);
                CimValueConverter.AssertIntrinsicCimType(cimType);
                result.Add(new MethodParameter
                {
                    Name = inputParameter.Name,
                    ParameterType = cimType,
                    Bindings = inputParameter.Bindings,
                    Value = cimValue,
                    IsValuePresent = inputParameter.IsValuePresent
                });
            }

            return result;
        }

        internal IEnumerable<MethodParameter> GetMethodInputParameters()
        {
            var allMethodParameters = this.GetMethodInputParametersCore(static p => !p.Name.StartsWith(CustomOperationOptionPrefix, StringComparison.OrdinalIgnoreCase));
            var methodParametersWithInputValue = allMethodParameters.Where(static p => p.IsValuePresent);
            return methodParametersWithInputValue;
        }

        internal IEnumerable<CimInstance> GetCimInstancesFromArguments()
        {
            return _methodInvocationInfo.GetArgumentsOfType<CimInstance>();
        }

        internal override CimCustomOptionsDictionary CalculateJobSpecificCustomOptions()
        {
            IDictionary<string, object> result = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

            IEnumerable<MethodParameter> customOptions = this
                .GetMethodInputParametersCore(static p => p.Name.StartsWith(CustomOperationOptionPrefix, StringComparison.OrdinalIgnoreCase));
            foreach (MethodParameter customOption in customOptions)
            {
                if (customOption.Value == null)
                {
                    continue;
                }

                result.Add(customOption.Name.Substring(CustomOperationOptionPrefix.Length), customOption.Value);
            }

            return CimCustomOptionsDictionary.Create(result);
        }

        internal IEnumerable<MethodParameter> GetMethodOutputParameters()
        {
            IEnumerable<MethodParameter> allParameters_plus_returnValue = _methodInvocationInfo.Parameters;
            if (_methodInvocationInfo.ReturnValue != null)
            {
                allParameters_plus_returnValue = allParameters_plus_returnValue.Append(_methodInvocationInfo.ReturnValue);
            }

            var outParameters = allParameters_plus_returnValue
                .Where(static p => ((p.Bindings & (MethodParameterBindings.Out | MethodParameterBindings.Error)) != 0));

            return outParameters;
        }

        internal string MethodSubject { get; }

        internal bool ShouldProcess()
        {
            Dbg.Assert(this.MethodSubject != null, "MethodSubject property should be initialized before starting main job processing");
            if (!this.JobContext.CmdletInvocationContext.CmdletDefinitionContext.ClientSideShouldProcess)
            {
                return true;
            }

            bool shouldProcess;
            if (!this.JobContext.SupportsShouldProcess)
            {
                shouldProcess = true;
                this.WriteVerboseStartOfCimOperation();
            }
            else
            {
                string target = this.MethodSubject;
                string action = this.MethodName;
                CimResponseType cimResponseType = this.ShouldProcess(target, action);
                switch (cimResponseType)
                {
                    case CimResponseType.Yes:
                    case CimResponseType.YesToAll:
                        shouldProcess = true;
                        break;
                    default:
                        shouldProcess = false;
                        break;
                }
            }

            if (!shouldProcess)
            {
                this.SetCompletedJobState(JobState.Completed, null);
            }

            return shouldProcess;
        }

        #region PassThru functionality

        internal abstract object PassThruObject { get; }

        internal bool IsPassThruObjectNeeded()
        {
            return (_passThru) && (!this.DidUserSuppressTheOperation) && (!this.JobHadErrors);
        }

        public override void OnCompleted()
        {
            this.ExceptionSafeWrapper(
                    delegate
                    {
                        Dbg.Assert(this.MethodSubject != null, "MethodSubject property should be initialized before starting main job processing");

                        if (this.IsPassThruObjectNeeded())
                        {
                            object passThruObject = this.PassThruObject;
                            if (passThruObject != null)
                            {
                                this.WriteObject(passThruObject);
                            }
                        }
                    });

            base.OnCompleted();
        }

        #endregion

        #region Job descriptions

        internal override string Description
        {
            get
            {
                return string.Format(
                    CultureInfo.InvariantCulture,
                    CmdletizationResources.CimJob_MethodDescription,
                    this.MethodSubject,
                    this.MethodName);
            }
        }

        internal override string FailSafeDescription
        {
            get
            {
                return string.Format(
                    CultureInfo.InvariantCulture,
                    CmdletizationResources.CimJob_SafeMethodDescription,
                    this.JobContext.CmdletizationClassName,
                    this.JobContext.Session.ComputerName,
                    this.MethodName);
            }
        }

        #endregion
    }
}