Skip to content

Commit 593260e

Browse files
committed
powershell compilation
1 parent aba7bfd commit 593260e

File tree

6 files changed

+411
-2
lines changed

6 files changed

+411
-2
lines changed

src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<Compile Include="DataContract\OdataError.cs" />
136136
<Compile Include="DataContract\OdataErrorMessage.cs" />
137137
<Compile Include="Model\AutomationAccount.cs" />
138+
<Compile Include="Model\DscCompilationJob.cs" />
138139
<Compile Include="Properties\AssemblyInfo.cs" />
139140
<Compile Include="Properties\Resources.Designer.cs">
140141
<AutoGen>True</AutoGen>

src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs

Lines changed: 248 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
using System.Net;
2222
using System.Security;
2323
using System.Security.Cryptography.X509Certificates;
24-
using Microsoft.Azure.Commands.Automation.Properties;
2524
using Microsoft.Azure.Commands.Automation.Model;
25+
using Microsoft.Azure.Commands.Automation.Properties;
2626
using Microsoft.Azure.Management.Automation;
2727
using Microsoft.Azure.Management.Automation.Models;
2828
using Microsoft.WindowsAzure.Commands.Common;
@@ -190,5 +190,252 @@ public void DeleteAutomationAccount(string resourceGroupName, string automationA
190190
}
191191

192192
#endregion
193+
194+
#region compilationjob
195+
196+
public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, Guid id)
197+
{
198+
throw new NotImplementedException();
199+
}
200+
201+
public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid Id)
202+
{
203+
var job = this.automationManagementClient.CompilationJobs.Get(resourceGroupName, automationAccountName, Id).Job;
204+
if (job == null)
205+
{
206+
throw new ResourceNotFoundException(typeof(Job),
207+
string.Format(CultureInfo.CurrentCulture, Resources.CompilationJobNotFound, Id));
208+
}
209+
210+
return new Model.DscCompilationJob(automationAccountName, job);
211+
}
212+
213+
public IEnumerable<Model.DscCompilationJob> ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus)
214+
{
215+
IEnumerable<AutomationManagement.Models.DscCompilationJob> jobModels;
216+
217+
if (startTime.HasValue && endTime.HasValue)
218+
{
219+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
220+
skipToken =>
221+
{
222+
var response =
223+
this.automationManagementClient.CompilationJobs.List(
224+
resourceGroupName,
225+
automationAccountName,
226+
new AutomationManagement.Models.DscCompilationJobListParameters
227+
{
228+
StartTime = FormatDateTime(startTime.Value),
229+
EndTime = FormatDateTime(endTime.Value),
230+
ConfigurationName = configurationName,
231+
Status = jobStatus,
232+
});
233+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
234+
});
235+
}
236+
else if (startTime.HasValue)
237+
{
238+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
239+
skipToken =>
240+
{
241+
var response =
242+
this.automationManagementClient.CompilationJobs.List(
243+
resourceGroupName,
244+
automationAccountName,
245+
new AutomationManagement.Models.DscCompilationJobListParameters
246+
{
247+
StartTime = FormatDateTime(startTime.Value),
248+
ConfigurationName = configurationName,
249+
Status = jobStatus
250+
});
251+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
252+
});
253+
}
254+
else if (endTime.HasValue)
255+
{
256+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
257+
skipToken =>
258+
{
259+
var response =
260+
this.automationManagementClient.CompilationJobs.List(
261+
resourceGroupName,
262+
automationAccountName,
263+
new AutomationManagement.Models.DscCompilationJobListParameters
264+
{
265+
EndTime = FormatDateTime(endTime.Value),
266+
ConfigurationName = configurationName,
267+
Status = jobStatus,
268+
});
269+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
270+
});
271+
}
272+
else
273+
{
274+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
275+
skipToken =>
276+
{
277+
var response = this.automationManagementClient.CompilationJobs.List(
278+
resourceGroupName,
279+
automationAccountName,
280+
new AutomationManagement.Models.DscCompilationJobListParameters
281+
{
282+
Status = jobStatus,
283+
ConfigurationName = configurationName
284+
});
285+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
286+
});
287+
}
288+
289+
return jobModels.Select(jobModel => new Commands.Automation.Model.DscCompilationJob(automationAccountName, jobModel));
290+
}
291+
292+
public IEnumerable<Model.DscCompilationJob> ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus)
293+
{
294+
IEnumerable<AutomationManagement.Models.DscCompilationJob> jobModels;
295+
296+
if (startTime.HasValue && endTime.HasValue)
297+
{
298+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
299+
skipToken =>
300+
{
301+
var response =
302+
this.automationManagementClient.CompilationJobs.List(
303+
resourceGroupName,
304+
automationAccountName,
305+
new AutomationManagement.Models.DscCompilationJobListParameters
306+
{
307+
StartTime = FormatDateTime(startTime.Value),
308+
EndTime = FormatDateTime(endTime.Value),
309+
Status = jobStatus,
310+
});
311+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
312+
});
313+
}
314+
else if (startTime.HasValue)
315+
{
316+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
317+
skipToken =>
318+
{
319+
var response =
320+
this.automationManagementClient.CompilationJobs.List(
321+
resourceGroupName,
322+
automationAccountName,
323+
new AutomationManagement.Models.DscCompilationJobListParameters
324+
{
325+
StartTime = FormatDateTime(startTime.Value),
326+
Status = jobStatus,
327+
});
328+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
329+
});
330+
}
331+
else if (endTime.HasValue)
332+
{
333+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
334+
skipToken =>
335+
{
336+
var response =
337+
this.automationManagementClient.CompilationJobs.List(
338+
resourceGroupName,
339+
automationAccountName,
340+
new AutomationManagement.Models.DscCompilationJobListParameters
341+
{
342+
EndTime = FormatDateTime(endTime.Value),
343+
Status = jobStatus,
344+
});
345+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
346+
});
347+
}
348+
else
349+
{
350+
jobModels = AutomationManagementClient.ContinuationTokenHandler(
351+
skipToken =>
352+
{
353+
var response = this.automationManagementClient.CompilationJobs.List(
354+
resourceGroupName,
355+
automationAccountName,
356+
new AutomationManagement.Models.DscCompilationJobListParameters { Status = jobStatus });
357+
return new ResponseWithSkipToken<AutomationManagement.Models.DscCompilationJob>(response, response.Jobs);
358+
});
359+
}
360+
361+
return jobModels.Select(jobModel => new Model.DscCompilationJob(automationAccountName, jobModel));
362+
}
363+
364+
DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters)
365+
{
366+
var createJobProperties = new DscCompilationJobCreateProperties();
367+
createJobProperties.Configuration = new DscConfigurationAssociationProperty() { Name = configurationName };
368+
createJobProperties.Parameters = parameters;
369+
this.automationManagementClient.CompilationJobs.Compile(resourceGroupName, automationAccountName, createJobProperties);
370+
}
371+
372+
#endregion
373+
374+
#region privatemethods
375+
376+
private string FormatDateTime(DateTimeOffset dateTime)
377+
{
378+
return string.Format(CultureInfo.InvariantCulture, "{0:O}", dateTime.ToUniversalTime());
379+
}
380+
381+
private IDictionary<string, string> ProcessConfigurationParameters(string automationAccountName, string configurationName, IDictionary parameters)
382+
{
383+
parameters = parameters ?? new Dictionary<string, string>();
384+
IEnumerable<KeyValuePair<string, RunbookParameter>> runbookParameters = this.ListConfigurationParameters(automationAccountName, configurationName);
385+
var filteredParameters = new Dictionary<string, string>();
386+
387+
foreach (var runbookParameter in runbookParameters)
388+
{
389+
if (parameters.Contains(runbookParameter.Key))
390+
{
391+
object paramValue = parameters[runbookParameter.Key];
392+
try
393+
{
394+
filteredParameters.Add(runbookParameter.Key, PowerShellJsonConverter.Serialize(paramValue));
395+
}
396+
catch (JsonSerializationException)
397+
{
398+
throw new ArgumentException(
399+
string.Format(
400+
CultureInfo.CurrentCulture, Resources.RunbookParameterCannotBeSerializedToJson, runbookParameter.Key));
401+
}
402+
}
403+
else if (runbookParameter.Value.IsMandatory)
404+
{
405+
throw new ArgumentException(
406+
string.Format(
407+
CultureInfo.CurrentCulture, Resources.RunbookParameterValueRequired, runbookParameter.Key));
408+
}
409+
}
410+
411+
if (filteredParameters.Count != parameters.Count)
412+
{
413+
throw new ArgumentException(
414+
string.Format(CultureInfo.CurrentCulture, Resources.InvalidRunbookParameters));
415+
}
416+
417+
var hasJobStartedBy = filteredParameters.Any(filteredParameter => filteredParameter.Key == Constants.JobStartedByParameterName);
418+
419+
if (!hasJobStartedBy)
420+
{
421+
filteredParameters.Add(Constants.JobStartedByParameterName, PowerShellJsonConverter.Serialize(Constants.ClientIdentity));
422+
}
423+
424+
return filteredParameters;
425+
}
426+
427+
private IEnumerable<KeyValuePair<string, RunbookParameter>> ListConfigurationParameters(string automationAccountName, string runbookName)
428+
{
429+
Runbook runbook = this.GetRunbook(automationAccountName, runbookName);
430+
if (0 == String.Compare(runbook.State, RunbookState.New, CultureInfo.InvariantCulture,
431+
CompareOptions.IgnoreCase))
432+
{
433+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoPublishedVersion, runbookName));
434+
}
435+
return runbook.Parameters.Cast<DictionaryEntry>().ToDictionary(k => k.Key.ToString(), k => (RunbookParameter)k.Value);
436+
}
437+
438+
#endregion
439+
193440
}
194441
}

src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,18 @@ public interface IAutomationClient
3838
void DeleteAutomationAccount(string resourceGroupName, string automationAccountName);
3939

4040
#endregion
41+
42+
#region dsccompilationjob
43+
44+
DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid id);
45+
46+
IEnumerable<DscCompilationJob> ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus);
47+
48+
IEnumerable<DscCompilationJob> ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus);
49+
50+
DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, IDictionary parameters);
51+
52+
#endregion
53+
4154
}
4255
}

0 commit comments

Comments
 (0)