-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRunbookParamDialog.xaml.cs
292 lines (276 loc) · 12 KB
/
RunbookParamDialog.xaml.cs
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Azure.Management.Automation.Models;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
namespace AutomationISE
{
/// <summary>
/// Interaction logic for RunbookParamDialog.xaml
/// </summary>
public partial class RunbookParamDialog : Window
{
private IDictionary<string, RunbookParameter> parameterDict;
private IList<HybridRunbookWorkerGroup> hybridGroups;
private IDictionary<string, string> existingParamsDict;
private String existingRunOn;
private String runbookType;
private IDictionary<string, string> _paramValues;
public IDictionary<string, string> paramValues { get { return _paramValues; } }
private string _runOnSelection;
public string runOnSelection { get { return _runOnSelection; } }
public RunbookParamDialog(IDictionary<string, RunbookParameter> parameterDict, IDictionary<string, string> existingParamsDict, String lastRunOn, IList<HybridRunbookWorkerGroup> hybridGroups, String runbookType)
{
InitializeComponent();
this.hybridGroups = hybridGroups;
this.parameterDict = parameterDict;
this.existingParamsDict = existingParamsDict;
this.existingRunOn = lastRunOn;
if (runbookType == "Python2")
{
this.runbookType = runbookType;
AddPthonForms();
}
else
{
if (parameterDict.Count > 0)
AddParamForms();
}
if (hybridGroups.Count > 0)
AddRunOnForms();
}
private void AddPthonForms()
{
/* Update the UI Grid to fit everything */
for (int i = 0; i < 2; i++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.Height = System.Windows.GridLength.Auto;
ParametersGrid.RowDefinitions.Add(rowDef);
}
Grid.SetRow(ButtonsPanel, ParametersGrid.RowDefinitions.Count - 1);
Label argLabel = new Label();
argLabel.Content = "Enter any arguments";
Grid.SetRow(argLabel, ParametersGrid.RowDefinitions.Count - 3);
Grid.SetColumn(argLabel, 0);
Grid.SetColumnSpan(argLabel, 2);
/* Input field */
TextBox parameterValueBox = new TextBox();
// Set previous value for this parameter if available
String argLine = null;
if (existingParamsDict != null)
{
foreach (var param in existingParamsDict.Values)
{
try
{
argLine = argLine + JsonConvert.DeserializeObject(param) + " ";
}
catch
{
argLine = argLine + param + " ";
}
}
parameterValueBox.Text = argLine;
// var paramValue = existingParamsDict.FirstOrDefault(x => x.Key == paramName).Value;
// if (paramValue != null) parameterValueBox.Text = paramValue;
}
parameterValueBox.MinWidth = 200;
parameterValueBox.Margin = new System.Windows.Thickness(0, 5, 5, 5);
Grid.SetColumn(parameterValueBox, 0);
Grid.SetRow(parameterValueBox, ParametersGrid.RowDefinitions.Count - 2);
Grid.SetColumnSpan(parameterValueBox, 2);
/* Add to Grid */
ParametersGrid.Children.Add(argLabel);
ParametersGrid.Children.Add(parameterValueBox);
}
[DllImport("shell32.dll", SetLastError = true)]
static extern IntPtr CommandLineToArgvW(
[MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
public static string[] ReturnArgs(string arguments)
{
int argc;
var argv = CommandLineToArgvW(arguments, out argc);
if (argv == IntPtr.Zero)
throw new System.ComponentModel.Win32Exception();
try
{
var args = new string[argc];
for (var i = 0; i < args.Length; i++)
{
var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
args[i] = Marshal.PtrToStringUni(p);
}
return args;
}
finally
{
Marshal.FreeHGlobal(argv);
}
}
/*
private static MatchCollection ReturnArgs(string arguments)
{
var pattern = new Regex(@"("".*?""|[^ ""]+)+");
return pattern.Matches(arguments);
}
*/
private void AddRunOnForms()
{
/* Update the UI Grid to fit everything */
for (int i = 0; i < 2; i++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.Height = System.Windows.GridLength.Auto;
ParametersGrid.RowDefinitions.Add(rowDef);
}
Grid.SetRow(ButtonsPanel, ParametersGrid.RowDefinitions.Count - 1);
Label runOnLabel = new Label();
runOnLabel.Content = "Choose where to run the test job:";
Grid.SetRow(runOnLabel, ParametersGrid.RowDefinitions.Count - 3);
Grid.SetColumn(runOnLabel, 0);
Grid.SetColumnSpan(runOnLabel, 2);
IList<string> runOnOptions = new List<string>();
runOnOptions.Add("Azure");
foreach (HybridRunbookWorkerGroup group in this.hybridGroups)
{
runOnOptions.Add(group.Name);
}
ComboBox runOnComboBox = new ComboBox();
runOnComboBox.ItemsSource = runOnOptions;
if (existingRunOn != null)
{
runOnComboBox.SelectedItem = existingRunOn;
_runOnSelection = existingRunOn;
}
else runOnComboBox.SelectedIndex = 0;
runOnComboBox.SelectionChanged += changeRunOnSelection;
Grid.SetRow(runOnComboBox, ParametersGrid.RowDefinitions.Count - 2);
Grid.SetColumn(runOnComboBox, 0);
Grid.SetColumnSpan(runOnComboBox, 2);
/* Add to Grid */
ParametersGrid.Children.Add(runOnLabel);
ParametersGrid.Children.Add(runOnComboBox);
}
private void changeRunOnSelection(object sender, RoutedEventArgs e)
{
_runOnSelection = ((sender as ComboBox).SelectedItem as string);
}
private void AddParamForms()
{
/* Update the UI Grid to fit everything */
for (int i = 0; i < parameterDict.Count * 2; i++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.Height = System.Windows.GridLength.Auto;
ParametersGrid.RowDefinitions.Add(rowDef);
}
Grid.SetRow(ButtonsPanel, ParametersGrid.RowDefinitions.Count - 1);
/* Fill the UI with parameter data */
int count = 0;
foreach (string paramName in parameterDict.Keys)
{
/* Parameter Name and Type */
Label parameterNameLabel = new Label();
parameterNameLabel.Content = paramName;
Label parameterTypeLabel = new Label();
parameterTypeLabel.Content = "(" + parameterDict[paramName].Type + ")\t";
if (parameterDict[paramName].IsMandatory)
{
parameterTypeLabel.Content += "[REQUIRED]";
}
else
{
parameterTypeLabel.Content += "[OPTIONAL]";
}
Grid.SetRow(parameterNameLabel, count * 2);
Grid.SetRow(parameterTypeLabel, count * 2);
Grid.SetColumn(parameterNameLabel, 0);
Grid.SetColumn(parameterTypeLabel, 1);
/* Input field */
TextBox parameterValueBox = new TextBox();
parameterValueBox.Name = paramName;
// Set previous value for this parameter if available
if (existingParamsDict != null)
{
var paramValue = existingParamsDict.FirstOrDefault(x => x.Key == paramName).Value;
if (paramValue != null) parameterValueBox.Text = paramValue;
}
parameterValueBox.MinWidth = 200;
parameterValueBox.Margin = new System.Windows.Thickness(0,5,5,5);
Grid.SetColumn(parameterValueBox, 0);
Grid.SetRow(parameterValueBox, count * 2 + 1);
Grid.SetColumnSpan(parameterValueBox, 2);
/* Add to Grid */
ParametersGrid.Children.Add(parameterNameLabel);
ParametersGrid.Children.Add(parameterTypeLabel);
ParametersGrid.Children.Add(parameterValueBox);
count++;
}
// Set focus to first parameter textbox
if(count > 0) ParametersGrid.Children[3].Focus();
}
/*
* This method assumes that:
* 1. The window has already been populated with the parameter fields
* 2. Each input field (text box) has the same name as the parameter it is for
*
*/
private void OkButton_Click(object sender, RoutedEventArgs e)
{
/* Validate parameters and return */
_paramValues = new Dictionary<string, string>();
string validationErrors = null;
int count = 0;
foreach (UIElement element in ParametersGrid.Children)
{
try
{
TextBox inputField = (TextBox)element;
if (this.runbookType == "Python2")
{
var args = ReturnArgs("AddOn.exe " + inputField.Text);
if (args.Count() > 1)
{
// Remove AddOn.exe from the argument list
args = args.Where((val, index) => index != 0).ToArray();
foreach (var arg in args)
{
count++;
if (!String.IsNullOrEmpty(arg.ToString()))
_paramValues.Add("[Parameter " + count.ToString() + "]", JsonConvert.SerializeObject(arg.ToString()));
}
}
}
else
{
if (String.IsNullOrEmpty(inputField.Text) && parameterDict[inputField.Name].IsMandatory == true)
validationErrors += "A value was not provided for the required parameter: " + inputField.Name + "\r\n";
if (!String.IsNullOrEmpty(inputField.Text))
_paramValues.Add(inputField.Name, inputField.Text);
}
}
catch { /* not an input field */ }
}
if (String.IsNullOrEmpty(validationErrors))
{
this.DialogResult = true;
}
else {
System.Windows.Forms.MessageBox.Show("Could not submit test job. The following errors were found:\r\n\r\n" + validationErrors);
}
}
}
}