From 593260e96cdd049f9e9b0dd450e4a66d729ea179 Mon Sep 17 00:00:00 2001 From: Balu Kambala Date: Thu, 9 Apr 2015 11:51:09 -0700 Subject: [PATCH 1/6] powershell compilation --- ...mands.ResourceManagement.Automation.csproj | 1 + .../Common/AutomationClient.cs | 249 +++++++++++++++++- .../Common/IAutomationClient.cs | 13 + .../Model/DscCompilationJob.cs | 136 ++++++++++ .../Properties/Resources.Designer.cs | 11 +- .../Properties/Resources.resx | 3 + 6 files changed, 411 insertions(+), 2 deletions(-) create mode 100644 src/ResourceManager/Automation/Commands.Automation/Model/DscCompilationJob.cs diff --git a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj index 1b310fd66b05..af49eec26aff 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj +++ b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj @@ -135,6 +135,7 @@ + True diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs index e8c3fdd2e7ae..bb4d73b5a80d 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs @@ -21,8 +21,8 @@ using System.Net; using System.Security; using System.Security.Cryptography.X509Certificates; -using Microsoft.Azure.Commands.Automation.Properties; using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Properties; using Microsoft.Azure.Management.Automation; using Microsoft.Azure.Management.Automation.Models; using Microsoft.WindowsAzure.Commands.Common; @@ -190,5 +190,252 @@ public void DeleteAutomationAccount(string resourceGroupName, string automationA } #endregion + + #region compilationjob + + public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, Guid id) + { + throw new NotImplementedException(); + } + + public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid Id) + { + var job = this.automationManagementClient.CompilationJobs.Get(resourceGroupName, automationAccountName, Id).Job; + if (job == null) + { + throw new ResourceNotFoundException(typeof(Job), + string.Format(CultureInfo.CurrentCulture, Resources.CompilationJobNotFound, Id)); + } + + return new Model.DscCompilationJob(automationAccountName, job); + } + + public IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + { + IEnumerable jobModels; + + if (startTime.HasValue && endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + EndTime = FormatDateTime(endTime.Value), + ConfigurationName = configurationName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else if (startTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + ConfigurationName = configurationName, + Status = jobStatus + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else if (endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + EndTime = FormatDateTime(endTime.Value), + ConfigurationName = configurationName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + Status = jobStatus, + ConfigurationName = configurationName + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + + return jobModels.Select(jobModel => new Commands.Automation.Model.DscCompilationJob(automationAccountName, jobModel)); + } + + public IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + { + IEnumerable jobModels; + + if (startTime.HasValue && endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + EndTime = FormatDateTime(endTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else if (startTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else if (endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + EndTime = FormatDateTime(endTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters { Status = jobStatus }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + + return jobModels.Select(jobModel => new Model.DscCompilationJob(automationAccountName, jobModel)); + } + + DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters) + { + var createJobProperties = new DscCompilationJobCreateProperties(); + createJobProperties.Configuration = new DscConfigurationAssociationProperty() { Name = configurationName }; + createJobProperties.Parameters = parameters; + this.automationManagementClient.CompilationJobs.Compile(resourceGroupName, automationAccountName, createJobProperties); + } + + #endregion + + #region privatemethods + + private string FormatDateTime(DateTimeOffset dateTime) + { + return string.Format(CultureInfo.InvariantCulture, "{0:O}", dateTime.ToUniversalTime()); + } + + private IDictionary ProcessConfigurationParameters(string automationAccountName, string configurationName, IDictionary parameters) + { + parameters = parameters ?? new Dictionary(); + IEnumerable> runbookParameters = this.ListConfigurationParameters(automationAccountName, configurationName); + var filteredParameters = new Dictionary(); + + foreach (var runbookParameter in runbookParameters) + { + if (parameters.Contains(runbookParameter.Key)) + { + object paramValue = parameters[runbookParameter.Key]; + try + { + filteredParameters.Add(runbookParameter.Key, PowerShellJsonConverter.Serialize(paramValue)); + } + catch (JsonSerializationException) + { + throw new ArgumentException( + string.Format( + CultureInfo.CurrentCulture, Resources.RunbookParameterCannotBeSerializedToJson, runbookParameter.Key)); + } + } + else if (runbookParameter.Value.IsMandatory) + { + throw new ArgumentException( + string.Format( + CultureInfo.CurrentCulture, Resources.RunbookParameterValueRequired, runbookParameter.Key)); + } + } + + if (filteredParameters.Count != parameters.Count) + { + throw new ArgumentException( + string.Format(CultureInfo.CurrentCulture, Resources.InvalidRunbookParameters)); + } + + var hasJobStartedBy = filteredParameters.Any(filteredParameter => filteredParameter.Key == Constants.JobStartedByParameterName); + + if (!hasJobStartedBy) + { + filteredParameters.Add(Constants.JobStartedByParameterName, PowerShellJsonConverter.Serialize(Constants.ClientIdentity)); + } + + return filteredParameters; + } + + private IEnumerable> ListConfigurationParameters(string automationAccountName, string runbookName) + { + Runbook runbook = this.GetRunbook(automationAccountName, runbookName); + if (0 == String.Compare(runbook.State, RunbookState.New, CultureInfo.InvariantCulture, + CompareOptions.IgnoreCase)) + { + throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoPublishedVersion, runbookName)); + } + return runbook.Parameters.Cast().ToDictionary(k => k.Key.ToString(), k => (RunbookParameter)k.Value); + } + + #endregion + } } \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs index e55a7a5f85f6..651481b7d1e7 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs @@ -38,5 +38,18 @@ public interface IAutomationClient void DeleteAutomationAccount(string resourceGroupName, string automationAccountName); #endregion + + #region dsccompilationjob + + DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid id); + + IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); + + IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); + + DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, IDictionary parameters); + + #endregion + } } \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation/Model/DscCompilationJob.cs b/src/ResourceManager/Automation/Commands.Automation/Model/DscCompilationJob.cs new file mode 100644 index 000000000000..0830213a551d --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Model/DscCompilationJob.cs @@ -0,0 +1,136 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.Commands.Automation.Model +{ + using System; + using System.Collections; + using System.Globalization; + using System.Linq; + + using Microsoft.Azure.Commands.Automation.Common; + + using AutomationManagement = Microsoft.Azure.Management.Automation; + + /// + /// The Dsc Compilation Job + /// + public class DscCompilationJob + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The account name. + /// + /// + /// The Job. + /// + /// + /// + public DscCompilationJob(string accountName, AutomationManagement.Models.DscCompilationJob job) + { + Requires.Argument("job", job).NotNull(); + Requires.Argument("accountName", accountName).NotNull(); + + this.AutomationAccountName = accountName; + + if (job.Properties == null) return; + + this.Id = job.Properties.JobId; + this.CreationTime = job.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = job.Properties.LastModifiedTime.ToLocalTime(); + this.StartTime = job.Properties.StartTime.HasValue ? job.Properties.StartTime.Value.ToLocalTime() : (DateTimeOffset?)null; + this.Status = job.Properties.Status; + this.StatusDetails = job.Properties.StatusDetails; + this.ConfigurationName = job.Properties.Configuration.Name; + this.Exception = job.Properties.Exception; + this.EndTime = job.Properties.EndTime.HasValue ? job.Properties.EndTime.Value.ToLocalTime() : (DateTimeOffset?)null; + this.LastStatusModifiedTime = job.Properties.LastStatusModifiedTime; + this.JobParameters = new Hashtable(StringComparer.InvariantCultureIgnoreCase); + foreach (var kvp in job.Properties.Parameters.Where(kvp => 0 != String.Compare(kvp.Key, Constants.JobStartedByParameterName, CultureInfo.InvariantCulture, + CompareOptions.IgnoreCase))) + { + this.JobParameters.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); + } + } + + /// + /// Initializes a new instance of the class. + /// + public DscCompilationJob() + { + } + + /// + /// Gets or sets the automaiton account name. + /// + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the job id. + /// + public Guid Id { get; set; } + + /// + /// Gets or sets the tags. + /// + public DateTimeOffset CreationTime { get; set; } + + /// + /// Gets or sets the status of the job. + /// + public string Status { get; set; } + + /// + /// Gets or sets the status details of the job. + /// + public string StatusDetails { get; set; } + + /// + /// Gets or sets the start time of the job. + /// + public DateTimeOffset? StartTime { get; set; } + + /// + /// Gets or sets the end time of the job. + /// + public DateTimeOffset? EndTime { get; set; } + + /// + /// Gets or sets the exception of the job. + /// + public string Exception { get; set; } + + /// + /// Gets or sets the last modified time of the job. + /// + public DateTimeOffset LastModifiedTime { get; set; } + + /// + /// Gets or sets the last status modified time of the job." + /// + public DateTimeOffset LastStatusModifiedTime { get; set; } + + /// + /// Gets or sets the parameters of the job. + /// + public Hashtable JobParameters { get; set; } + + /// + /// Gets or sets the configuration. + /// + public string ConfigurationName { get; set; } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs index 112e6f07708b..3db90ac47b88 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34014 +// Runtime Version:4.0.30319.18449 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -114,6 +114,15 @@ internal static string CertificateNotFound { } } + /// + /// Looks up a localized string similar to The Compilation Job having Id: {0} was not found.. + /// + internal static string CompilationJobNotFound { + get { + return ResourceManager.GetString("CompilationJobNotFound", resourceCulture); + } + } + /// /// Looks up a localized string similar to The Connection already exists. Connection name: {0}.. /// diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx index ac55f018ec24..d6811d83fa86 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx @@ -275,4 +275,7 @@ Create account arguments are invalid. Provide valid account name and location. Account Name: {0}, Location: {1} Automation + + The Compilation Job having Id: {0} was not found. + \ No newline at end of file From 5449bf8ab95455d3f28c249e0ac9a4c7d32d9379 Mon Sep 17 00:00:00 2001 From: Balu Kambala Date: Sun, 12 Apr 2015 16:07:12 -0700 Subject: [PATCH 2/6] added dsc configuration job cmdlets --- .../GetAzureAutomationDscCompilationJob.cs | 103 +++++++ ...tAzureAutomationDscCompilationJobOutput.cs | 64 +++++ .../StartAzureAutomationDscCompilationJob.cs | 64 +++++ ...mands.ResourceManagement.Automation.csproj | 6 +- .../Common/AutomationClient.cs | 248 +--------------- .../Common/AutomationClientDSC.cs | 271 ++++++++++++++++++ .../Common/IAutomationClient.cs | 5 +- .../Commands.Automation/Model/JobStream.cs | 84 ++++++ .../Properties/Resources.Designer.cs | 36 +++ .../Properties/Resources.resx | 16 ++ 10 files changed, 647 insertions(+), 250 deletions(-) create mode 100644 src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs create mode 100644 src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJobOutput.cs create mode 100644 src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs create mode 100644 src/ResourceManager/Automation/Commands.Automation/Model/JobStream.cs diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs new file mode 100644 index 000000000000..a300ad991270 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs @@ -0,0 +1,103 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.WindowsAzure.Commands.Utilities.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets Azure automation compilation job + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationDscCompilationJob", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(DscCompilationJob))] + public class GetAzureAutomationDscCompilationJob : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the automation account name. + /// + [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] + [ValidateNotNullOrEmpty] + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the job id. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByJobId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The dsc compilation job id.")] + [Alias("JobId")] + public Guid Id { get; set; } + + /// + /// Gets or sets the runbook name of the job. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConfigurationName, Mandatory = true, HelpMessage = "The configuration name of the job.")] + [Alias("Name")] + public string ConfigurationName { get; set; } + + /// + /// Gets or sets the status of a job. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConfigurationName, Mandatory = false, HelpMessage = "The configuration name of the job.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByAll, Mandatory = false, HelpMessage = "Filter jobs based on their status.")] + [ValidateSet("Completed", "Failed", "Queued", "Starting", "Resuming", "Running", "Stopped", "Stopping", "Suspended", "Suspending", "Activating")] + public string Status { get; set; } + + /// + /// Gets or sets the start time filter. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConfigurationName, Mandatory = false, HelpMessage = "The configuration name of the job.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByAll, Mandatory = false, HelpMessage = "Filter jobs so that job start time >= StartTime.")] + public DateTimeOffset? StartTime { get; set; } + + /// + /// Gets or sets the end time filter. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConfigurationName, Mandatory = false, HelpMessage = "The configuration name of the job.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByAll, Mandatory = false, HelpMessage = "Filter jobs so that job end time <= EndTime.")] + public DateTimeOffset? EndTime { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable jobs; + + if (this.Id != null && !Guid.Empty.Equals(this.Id)) + { + // ByJobId + jobs = new List { this.AutomationClient.GetCompilationJob(this.ResourceGroupName, this.AutomationAccountName, this.Id) }; + } + else if (this.ConfigurationName != null) + { + // ByConfiguration + jobs = this.AutomationClient.ListCompilationJobsByConfigurationName(this.ResourceGroupName, this.AutomationAccountName, this.ConfigurationName, this.StartTime, this.EndTime, this.Status); + } + else + { + // ByAll + jobs = this.AutomationClient.ListCompilationJobs(this.ResourceGroupName, this.AutomationAccountName, this.StartTime, this.EndTime, this.Status); + } + + this.WriteObject(jobs, true); + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJobOutput.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJobOutput.cs new file mode 100644 index 000000000000..aabb97d6a552 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJobOutput.cs @@ -0,0 +1,64 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.WindowsAzure.Commands.Utilities.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets stream for a compilation job + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationDscCompilationJobOutput")] + [OutputType(typeof(JobStream))] + public class GetAzureAutomationDscCompilationJobOutput : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the automation account name. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] + [ValidateNotNullOrEmpty] + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the job id + /// + [Alias("JobId")] + [Parameter(Mandatory = true, Position = 2, ValueFromPipelineByPropertyName = true, HelpMessage = "The job Id")] + public Guid Id { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The stream type. Defaults to Any.")] + public StreamType Stream { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Retrieves output created after this time")] + public DateTimeOffset? StartTime { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + var ret = this.AutomationClient.GetDscCompilationJobStream(this.ResourceGroupName, this.AutomationAccountName, this.Id, this.StartTime, this.Stream.ToString()); + + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs new file mode 100644 index 000000000000..a7677f0ff346 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs @@ -0,0 +1,64 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System.Collections; +using System.Collections.Generic; +using System.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.WindowsAzure.Commands.Utilities.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// starts azure automation compilation job + /// + [Cmdlet(VerbsLifecycle.Start, "AzureAutomationDscCompilationJob", DefaultParameterSetName = AutomationCmdletParameterSets.ByConfigurationName)] + [OutputType(typeof(DscCompilationJob))] + public class StartAzureAutomationDscCompilationJob : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the automation account name. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] + [ValidateNotNullOrEmpty] + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the configuration name. + /// + [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The configuration name.")] + public string ConfigurationName { get; set; } + + /// + /// Gets or sets the configuration parameters. + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The configuration parameters.")] + public IDictionary Parameters { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + DscCompilationJob job = null; + + job = this.AutomationClient.StartCompilationJob(this.ResourceGroupName, this.AutomationAccountName, this.ConfigurationName, this.Parameters); + + this.WriteObject(job); + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj index bb8cbdbc8230..870212d7bdf2 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj +++ b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj @@ -1,5 +1,5 @@  - + Debug @@ -116,6 +116,9 @@ + + + @@ -143,6 +146,7 @@ + True diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs index c86f6e9bd213..e869aea08925 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs @@ -190,252 +190,6 @@ public void DeleteAutomationAccount(string resourceGroupName, string automationA } #endregion - - #region compilationjob - - public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, Guid id) - { - throw new NotImplementedException(); - } - - public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid Id) - { - var job = this.automationManagementClient.CompilationJobs.Get(resourceGroupName, automationAccountName, Id).Job; - if (job == null) - { - throw new ResourceNotFoundException(typeof(Job), - string.Format(CultureInfo.CurrentCulture, Resources.CompilationJobNotFound, Id)); - } - - return new Model.DscCompilationJob(automationAccountName, job); - } - - public IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) - { - IEnumerable jobModels; - - if (startTime.HasValue && endTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - EndTime = FormatDateTime(endTime.Value), - ConfigurationName = configurationName, - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - else if (startTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - ConfigurationName = configurationName, - Status = jobStatus - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - else if (endTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - EndTime = FormatDateTime(endTime.Value), - ConfigurationName = configurationName, - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - else - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - Status = jobStatus, - ConfigurationName = configurationName - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - - return jobModels.Select(jobModel => new Commands.Automation.Model.DscCompilationJob(automationAccountName, jobModel)); - } - - public IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) - { - IEnumerable jobModels; - - if (startTime.HasValue && endTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - EndTime = FormatDateTime(endTime.Value), - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - else if (startTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - else if (endTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - EndTime = FormatDateTime(endTime.Value), - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - else - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters { Status = jobStatus }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - - return jobModels.Select(jobModel => new Model.DscCompilationJob(automationAccountName, jobModel)); - } - - DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters) - { - var createJobProperties = new DscCompilationJobCreateProperties(); - createJobProperties.Configuration = new DscConfigurationAssociationProperty() { Name = configurationName }; - createJobProperties.Parameters = parameters; - this.automationManagementClient.CompilationJobs.Compile(resourceGroupName, automationAccountName, createJobProperties); - } - - #endregion - - #region privatemethods - - private string FormatDateTime(DateTimeOffset dateTime) - { - return string.Format(CultureInfo.InvariantCulture, "{0:O}", dateTime.ToUniversalTime()); - } - - private IDictionary ProcessConfigurationParameters(string automationAccountName, string configurationName, IDictionary parameters) - { - parameters = parameters ?? new Dictionary(); - IEnumerable> runbookParameters = this.ListConfigurationParameters(automationAccountName, configurationName); - var filteredParameters = new Dictionary(); - - foreach (var runbookParameter in runbookParameters) - { - if (parameters.Contains(runbookParameter.Key)) - { - object paramValue = parameters[runbookParameter.Key]; - try - { - filteredParameters.Add(runbookParameter.Key, PowerShellJsonConverter.Serialize(paramValue)); - } - catch (JsonSerializationException) - { - throw new ArgumentException( - string.Format( - CultureInfo.CurrentCulture, Resources.RunbookParameterCannotBeSerializedToJson, runbookParameter.Key)); - } - } - else if (runbookParameter.Value.IsMandatory) - { - throw new ArgumentException( - string.Format( - CultureInfo.CurrentCulture, Resources.RunbookParameterValueRequired, runbookParameter.Key)); - } - } - - if (filteredParameters.Count != parameters.Count) - { - throw new ArgumentException( - string.Format(CultureInfo.CurrentCulture, Resources.InvalidRunbookParameters)); - } - - var hasJobStartedBy = filteredParameters.Any(filteredParameter => filteredParameter.Key == Constants.JobStartedByParameterName); - - if (!hasJobStartedBy) - { - filteredParameters.Add(Constants.JobStartedByParameterName, PowerShellJsonConverter.Serialize(Constants.ClientIdentity)); - } - - return filteredParameters; - } - - private IEnumerable> ListConfigurationParameters(string automationAccountName, string runbookName) - { - Runbook runbook = this.GetRunbook(automationAccountName, runbookName); - if (0 == String.Compare(runbook.State, RunbookState.New, CultureInfo.InvariantCulture, - CompareOptions.IgnoreCase)) - { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoPublishedVersion, runbookName)); - } - return runbook.Parameters.Cast().ToDictionary(k => k.Key.ToString(), k => (RunbookParameter)k.Value); - } - - #endregion - + } } \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs index a5de480166c5..dd95ca45464d 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs @@ -161,5 +161,276 @@ public Model.AgentRegistration NewAgentRegistrationKey( return new Model.AgentRegistration(resourceGroupName, automationAccountName, agentRegistration); } #endregion + + #region compilationjob + + public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid Id) + { + var job = this.automationManagementClient.CompilationJobs.Get(resourceGroupName, automationAccountName, Id).DscCompilationJob; + if (job == null) + { + throw new ResourceNotFoundException(typeof(Job), + string.Format(CultureInfo.CurrentCulture, Resources.CompilationJobNotFound, Id)); + } + + return new Model.DscCompilationJob(automationAccountName, job); + } + + public IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + { + IEnumerable jobModels; + + if (startTime.HasValue && endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + EndTime = FormatDateTime(endTime.Value), + ConfigurationName = configurationName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (startTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + ConfigurationName = configurationName, + Status = jobStatus + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + EndTime = FormatDateTime(endTime.Value), + ConfigurationName = configurationName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + Status = jobStatus, + ConfigurationName = configurationName + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + + return jobModels.Select(jobModel => new Commands.Automation.Model.DscCompilationJob(automationAccountName, jobModel)); + } + + public IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + { + IEnumerable jobModels; + + if (startTime.HasValue && endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + EndTime = FormatDateTime(endTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (startTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + EndTime = FormatDateTime(endTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters { Status = jobStatus }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + + return jobModels.Select(jobModel => new Model.DscCompilationJob(automationAccountName, jobModel)); + } + + public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters) + { + var createJobParameters = new DscCompilationJobCreateParameters() + { + Properties = new DscCompilationJobCreateProperties() + { + Configuration = new DscConfigurationAssociationProperty() + { + Name = configurationName + }, + Parameters = this.ProcessConfigurationParameters(resourceGroupName, automationAccountName, configurationName, parameters) + } + }; + + var job = this.automationManagementClient.CompilationJobs.Compile(resourceGroupName, automationAccountName, createJobParameters); + + return new Model.DscCompilationJob(automationAccountName, job.DscCompilationJob); + } + + public IEnumerable GetDscCompilationJobStream(string resourceGroupName, string automationAccountName, Guid jobId, DateTimeOffset? time, string streamType) + { + var listParams = new AutomationManagement.Models.JobStreamListParameters(); + + if (time.HasValue) + { + listParams.Time = this.FormatDateTime(time.Value); + } + + if (streamType != null) + { + listParams.StreamType = streamType; + } + + var jobStreams = this.automationManagementClient.JobStreams.List(resourceGroupName, automationAccountName, jobId, listParams).JobStreams; + return jobStreams.Select(stream => this.CreateJobStreamFromJobStreamModel(stream, automationAccountName, jobId)).ToList(); + } + + #endregion + + #region privatemethods + + private string FormatDateTime(DateTimeOffset dateTime) + { + return string.Format(CultureInfo.InvariantCulture, "{0:O}", dateTime.ToUniversalTime()); + } + + private IDictionary ProcessConfigurationParameters(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters) + { + parameters = parameters ?? new Dictionary(); + IEnumerable> configurationParameters = this.ListConfigurationParameters(resourceGroupName, automationAccountName, configurationName); + var filteredParameters = new Dictionary(); + + foreach (var configParameter in configurationParameters) + { + if (parameters.Contains(configParameter.Key)) + { + object paramValue = parameters[configParameter.Key]; + try + { + filteredParameters.Add(configParameter.Key, paramValue.ToString()); + } + catch (JsonSerializationException) + { + throw new ArgumentException( + string.Format( + CultureInfo.CurrentCulture, Resources.ConfigurationParameterCannotBeSerializedToJson, configParameter.Key)); + } + } + else if (configParameter.Value.IsMandatory) + { + throw new ArgumentException( + string.Format( + CultureInfo.CurrentCulture, Resources.ConfigurationParameterValueRequired, configParameter.Key)); + } + } + + if (filteredParameters.Count != parameters.Count) + { + throw new ArgumentException( + string.Format(CultureInfo.CurrentCulture, Resources.InvalidConfigurationParameters)); + } + + return filteredParameters; + } + + private IEnumerable> ListConfigurationParameters(string resourceGroupName, string automationAccountName, string configurationName) + { + Model.DscConfiguration configuration = this.GetConfiguration(resourceGroupName, automationAccountName, configurationName); + if (configuration == null || 0 == String.Compare(configuration.State, RunbookState.New, CultureInfo.InvariantCulture, + CompareOptions.IgnoreCase)) + { + throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.ConfigurationHasNoPublishedVersion, configurationName)); + } + return configuration.Parameters.Cast().ToDictionary(k => k.Key.ToString(), k => (DscConfigurationParameter)k.Value); + } + + private Model.JobStream CreateJobStreamFromJobStreamModel(AutomationManagement.Models.JobStream jobStream, string automationAccountName, Guid jobId) + { + Requires.Argument("jobStream", jobStream).NotNull(); + Requires.Argument("automationAccountName", automationAccountName).NotNull(); + Requires.Argument("jobId", jobId).NotNull(); + return new Model.JobStream(jobStream, automationAccountName, jobId); + } + + #endregion } } \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs index 3335bf5dec80..1bd07a66dab5 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs @@ -39,7 +39,7 @@ public interface IAutomationClient #endregion - #region dsccompilationjob + #region Compilationjobs DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid id); @@ -47,8 +47,9 @@ public interface IAutomationClient IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); - DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, IDictionary parameters); + DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters); + IEnumerable GetDscCompilationJobStream(string resourceGroupName, string automationAccountname, Guid jobId, DateTimeOffset? time, string streamType); #endregion #region Configurations diff --git a/src/ResourceManager/Automation/Commands.Automation/Model/JobStream.cs b/src/ResourceManager/Automation/Commands.Automation/Model/JobStream.cs new file mode 100644 index 000000000000..ee9c2f273da9 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Model/JobStream.cs @@ -0,0 +1,84 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + using AutomationManagement = Microsoft.Azure.Management.Automation; + + /// + /// The Job Stream. + /// + public class JobStream + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The job stream. + /// + /// + /// + public JobStream(AutomationManagement.Models.JobStream jobStream, string automationAccountName, Guid jobId ) + { + Requires.Argument("jobStream", jobStream).NotNull(); + + this.JobStreamId = jobStream.Properties.JobStreamId; + this.Type = jobStream.Properties.StreamType; + this.Text = jobStream.Properties.Summary; + this.Time = jobStream.Properties.Time; + this.AutomationAccountName = automationAccountName; + this.Id = jobId; + } + + /// + /// Initializes a new instance of the class. + /// + public JobStream() + { + } + + /// + /// Gets or sets the automation account name. + /// + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the Job Id. + /// + public Guid Id { get; set; } + + /// + /// Gets or sets the stream id + /// + public string JobStreamId { get; set; } + + /// + /// Gets or sets the stream time. + /// + public DateTimeOffset Time { get; set; } + + /// + /// Gets or sets the stream text. + /// + public string Text { get; set; } + + /// + /// Gets or sets the stream Type. + /// + public string Type { get; set; } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs index 3db90ac47b88..e798c66cf3f9 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs @@ -123,6 +123,33 @@ internal static string CompilationJobNotFound { } } + /// + /// Looks up a localized string similar to The configuration has no published version. Configuration name {0}.. + /// + internal static string ConfigurationHasNoPublishedVersion { + get { + return ResourceManager.GetString("ConfigurationHasNoPublishedVersion", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Configuration parameter cannot be serialized to json. Parameter name {0}.. + /// + internal static string ConfigurationParameterCannotBeSerializedToJson { + get { + return ResourceManager.GetString("ConfigurationParameterCannotBeSerializedToJson", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Configuration mandatory parameter not specified. Parameter name {0}.. + /// + internal static string ConfigurationParameterValueRequired { + get { + return ResourceManager.GetString("ConfigurationParameterValueRequired", resourceCulture); + } + } + /// /// Looks up a localized string similar to The Connection already exists. Connection name: {0}.. /// @@ -159,6 +186,15 @@ internal static string CredentialNotFound { } } + /// + /// Looks up a localized string similar to Invalid configuration parameters.. + /// + internal static string InvalidConfigurationParameters { + get { + return ResourceManager.GetString("InvalidConfigurationParameters", resourceCulture); + } + } + /// /// Looks up a localized string similar to Invalid runbook parameters.. /// diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx index d6811d83fa86..c88e76798cf6 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx @@ -278,4 +278,20 @@ The Compilation Job having Id: {0} was not found. + + The configuration has no published version. Configuration name {0}. + Automation + + + Configuration parameter cannot be serialized to json. Parameter name {0}. + Automation + + + Configuration mandatory parameter not specified. Parameter name {0}. + Automation + + + Invalid configuration parameters. + Automation + \ No newline at end of file From 2217800da6561400c47288d02ddf524d02c687e9 Mon Sep 17 00:00:00 2001 From: Balu Kambala Date: Sun, 12 Apr 2015 16:10:18 -0700 Subject: [PATCH 3/6] added settings for dsc configuration --- .../Properties/Resources.Designer.cs | 11 ++++++++++- .../Commands.Automation/Properties/Resources.resx | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs index 112e6f07708b..3db90ac47b88 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34014 +// Runtime Version:4.0.30319.18449 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -114,6 +114,15 @@ internal static string CertificateNotFound { } } + /// + /// Looks up a localized string similar to The Compilation Job having Id: {0} was not found.. + /// + internal static string CompilationJobNotFound { + get { + return ResourceManager.GetString("CompilationJobNotFound", resourceCulture); + } + } + /// /// Looks up a localized string similar to The Connection already exists. Connection name: {0}.. /// diff --git a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx index ac55f018ec24..d6811d83fa86 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx +++ b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx @@ -275,4 +275,7 @@ Create account arguments are invalid. Provide valid account name and location. Account Name: {0}, Location: {1} Automation + + The Compilation Job having Id: {0} was not found. + \ No newline at end of file From 5c7e45a22667b700ad87dfc43d68b63fe7a5147a Mon Sep 17 00:00:00 2001 From: Balu Kambala Date: Sun, 12 Apr 2015 21:17:30 -0700 Subject: [PATCH 4/6] fixed code review comments-safeer --- ...mands.ResourceManagement.Automation.csproj | 1 + .../Common/AutomationClientDSC.cs | 333 +++++++++--------- .../Common/RequestSettings.cs | 41 +++ 3 files changed, 216 insertions(+), 159 deletions(-) create mode 100644 src/ResourceManager/Automation/Commands.Automation/Common/RequestSettings.cs diff --git a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj index 870212d7bdf2..5623de19eae5 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj +++ b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj @@ -134,6 +134,7 @@ + diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs index dd95ca45464d..3000993cad10 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs @@ -166,202 +166,217 @@ public Model.AgentRegistration NewAgentRegistrationKey( public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid Id) { - var job = this.automationManagementClient.CompilationJobs.Get(resourceGroupName, automationAccountName, Id).DscCompilationJob; - if (job == null) + using (var request = new RequestSettings(this.automationManagementClient)) { - throw new ResourceNotFoundException(typeof(Job), - string.Format(CultureInfo.CurrentCulture, Resources.CompilationJobNotFound, Id)); - } + var job = this.automationManagementClient.CompilationJobs.Get(resourceGroupName, automationAccountName, Id).DscCompilationJob; + if (job == null) + { + throw new ResourceNotFoundException(typeof(Job), + string.Format(CultureInfo.CurrentCulture, Resources.CompilationJobNotFound, Id)); + } - return new Model.DscCompilationJob(automationAccountName, job); + return new Model.DscCompilationJob(automationAccountName, job); + } } public IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) { - IEnumerable jobModels; - - if (startTime.HasValue && endTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - EndTime = FormatDateTime(endTime.Value), - ConfigurationName = configurationName, - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } - else if (startTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - ConfigurationName = configurationName, - Status = jobStatus - }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } - else if (endTime.HasValue) + using (var request = new RequestSettings(this.automationManagementClient)) { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( + IEnumerable jobModels; + + if (startTime.HasValue && endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + EndTime = FormatDateTime(endTime.Value), + ConfigurationName = configurationName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (startTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + ConfigurationName = configurationName, + Status = jobStatus + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + EndTime = FormatDateTime(endTime.Value), + ConfigurationName = configurationName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.CompilationJobs.List( resourceGroupName, automationAccountName, new AutomationManagement.Models.DscCompilationJobListParameters { - EndTime = FormatDateTime(endTime.Value), - ConfigurationName = configurationName, Status = jobStatus, + ConfigurationName = configurationName }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } - else - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - Status = jobStatus, - ConfigurationName = configurationName - }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } - return jobModels.Select(jobModel => new Commands.Automation.Model.DscCompilationJob(automationAccountName, jobModel)); + return jobModels.Select(jobModel => new Commands.Automation.Model.DscCompilationJob(automationAccountName, jobModel)); + } } public IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) { - IEnumerable jobModels; - - if (startTime.HasValue && endTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - EndTime = FormatDateTime(endTime.Value), - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } - else if (startTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - StartTime = FormatDateTime(startTime.Value), - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } - else if (endTime.HasValue) + using (var request = new RequestSettings(this.automationManagementClient)) { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.CompilationJobs.List( + IEnumerable jobModels; + + if (startTime.HasValue && endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + EndTime = FormatDateTime(endTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (startTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + StartTime = FormatDateTime(startTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else if (endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.CompilationJobs.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscCompilationJobListParameters + { + EndTime = FormatDateTime(endTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } + else + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.CompilationJobs.List( resourceGroupName, automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters - { - EndTime = FormatDateTime(endTime.Value), - Status = jobStatus, - }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } - else - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = this.automationManagementClient.CompilationJobs.List( - resourceGroupName, - automationAccountName, - new AutomationManagement.Models.DscCompilationJobListParameters { Status = jobStatus }); - return new ResponseWithSkipToken(response, response.DscCompilationJobs); - }); - } + new AutomationManagement.Models.DscCompilationJobListParameters { Status = jobStatus }); + return new ResponseWithSkipToken(response, response.DscCompilationJobs); + }); + } - return jobModels.Select(jobModel => new Model.DscCompilationJob(automationAccountName, jobModel)); + return jobModels.Select(jobModel => new Model.DscCompilationJob(automationAccountName, jobModel)); + } } public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters) { - var createJobParameters = new DscCompilationJobCreateParameters() + using (var request = new RequestSettings(this.automationManagementClient)) { - Properties = new DscCompilationJobCreateProperties() + var createJobParameters = new DscCompilationJobCreateParameters() { - Configuration = new DscConfigurationAssociationProperty() + Properties = new DscCompilationJobCreateProperties() { - Name = configurationName - }, - Parameters = this.ProcessConfigurationParameters(resourceGroupName, automationAccountName, configurationName, parameters) - } - }; - - var job = this.automationManagementClient.CompilationJobs.Compile(resourceGroupName, automationAccountName, createJobParameters); + Configuration = new DscConfigurationAssociationProperty() + { + Name = configurationName + }, + Parameters = this.ProcessConfigurationParameters(resourceGroupName, automationAccountName, configurationName, parameters) + } + }; + + var job = this.automationManagementClient.CompilationJobs.Create(resourceGroupName, automationAccountName, createJobParameters); - return new Model.DscCompilationJob(automationAccountName, job.DscCompilationJob); + return new Model.DscCompilationJob(automationAccountName, job.DscCompilationJob); + } } public IEnumerable GetDscCompilationJobStream(string resourceGroupName, string automationAccountName, Guid jobId, DateTimeOffset? time, string streamType) { - var listParams = new AutomationManagement.Models.JobStreamListParameters(); - - if (time.HasValue) + using (var request = new RequestSettings(this.automationManagementClient)) { - listParams.Time = this.FormatDateTime(time.Value); - } + var listParams = new AutomationManagement.Models.JobStreamListParameters(); - if (streamType != null) - { - listParams.StreamType = streamType; - } + if (time.HasValue) + { + listParams.Time = this.FormatDateTime(time.Value); + } - var jobStreams = this.automationManagementClient.JobStreams.List(resourceGroupName, automationAccountName, jobId, listParams).JobStreams; - return jobStreams.Select(stream => this.CreateJobStreamFromJobStreamModel(stream, automationAccountName, jobId)).ToList(); + if (streamType != null) + { + listParams.StreamType = streamType; + } + + var jobStreams = this.automationManagementClient.JobStreams.List(resourceGroupName, automationAccountName, jobId, listParams).JobStreams; + return jobStreams.Select(stream => this.CreateJobStreamFromJobStreamModel(stream, automationAccountName, jobId)).ToList(); + } } #endregion diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/RequestSettings.cs b/src/ResourceManager/Automation/Commands.Automation/Common/RequestSettings.cs new file mode 100644 index 000000000000..86bae5056116 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Common/RequestSettings.cs @@ -0,0 +1,41 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Management.Automation; + +namespace Microsoft.Azure.Commands.Automation +{ + public class RequestSettings : IDisposable + { + private readonly AutomationManagementClient client; + + public RequestSettings(IAutomationManagementClient automationClient) + { + client = ((AutomationManagementClient)automationClient); + client.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName); + client.HttpClient.DefaultRequestHeaders.Add(Constants.ClientRequestIdHeaderName, Guid.NewGuid().ToString()); + } + + public void Dispose() + { + client.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName); + } + } +} From 3dd6f8a0ac8b91dee1e8ccf93e90493e1a5de8e2 Mon Sep 17 00:00:00 2001 From: Balu Kambala Date: Sun, 12 Apr 2015 23:55:02 -0700 Subject: [PATCH 5/6] dsc node configuration cmdlets --- .../GetAzureAutomationDscCompilationJob.cs | 6 +- .../GetAzureAutomationDscNodeConfiguration.cs | 80 +++++++++++++++++ .../StartAzureAutomationDscCompilationJob.cs | 4 +- ...mands.ResourceManagement.Automation.csproj | 4 +- .../Common/AutomationClientDSC.cs | 80 +++++++++++++++-- .../Common/AutomationCmdletParameterSet.cs | 5 ++ .../Common/IAutomationClient.cs | 16 +++- ...DscCompilationJob.cs => CompilationJob.cs} | 12 +-- .../Model/NodeConfiguration.cs | 90 +++++++++++++++++++ .../Properties/Resources.Designer.cs | 9 ++ .../Properties/Resources.resx | 4 + 11 files changed, 286 insertions(+), 24 deletions(-) create mode 100644 src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscNodeConfiguration.cs rename src/ResourceManager/Automation/Commands.Automation/Model/{DscCompilationJob.cs => CompilationJob.cs} (91%) create mode 100644 src/ResourceManager/Automation/Commands.Automation/Model/NodeConfiguration.cs diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs index a300ad991270..447116f5e7c4 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscCompilationJob.cs @@ -27,7 +27,7 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// Gets Azure automation compilation job /// [Cmdlet(VerbsCommon.Get, "AzureAutomationDscCompilationJob", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] - [OutputType(typeof(DscCompilationJob))] + [OutputType(typeof(CompilationJob))] public class GetAzureAutomationDscCompilationJob : AzureAutomationBaseCmdlet { /// @@ -79,12 +79,12 @@ public class GetAzureAutomationDscCompilationJob : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - IEnumerable jobs; + IEnumerable jobs; if (this.Id != null && !Guid.Empty.Equals(this.Id)) { // ByJobId - jobs = new List { this.AutomationClient.GetCompilationJob(this.ResourceGroupName, this.AutomationAccountName, this.Id) }; + jobs = new List { this.AutomationClient.GetCompilationJob(this.ResourceGroupName, this.AutomationAccountName, this.Id) }; } else if (this.ConfigurationName != null) { diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscNodeConfiguration.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscNodeConfiguration.cs new file mode 100644 index 000000000000..95e559421754 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationDscNodeConfiguration.cs @@ -0,0 +1,80 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.WindowsAzure.Commands.Utilities.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets Azure automation node configurations + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationDscNodeConfiguration", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(CompilationJob))] + public class GetAzureAutomationDscNodeConfiguration : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the automation account name. + /// + [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] + [ValidateNotNullOrEmpty] + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the job id. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByNodeConfigurationName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The dsc node configuration name.")] + [Alias("NodeConfigurationName")] + public string Name { get; set; } + + /// + /// Gets or sets the runbook name of the job. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConfigurationName, Mandatory = true, HelpMessage = "The configuration name.")] + public string ConfigurationName { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable nodeConfigurations; + + if (this.Name != null && !Guid.Empty.Equals(this.Name)) + { + // ByJobId + nodeConfigurations = new List { this.AutomationClient.GetNodeConfiguration(this.ResourceGroupName, this.AutomationAccountName, this.Name) }; + } + else if (this.ConfigurationName != null) + { + // ByConfiguration + nodeConfigurations = this.AutomationClient.ListNodeConfigurationsByConfigurationName(this.ResourceGroupName, this.AutomationAccountName, this.ConfigurationName); + } + else + { + // ByAll + nodeConfigurations = this.AutomationClient.ListNodeConfigurations(this.ResourceGroupName, this.AutomationAccountName); + } + + this.WriteObject(nodeConfigurations, true); + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs index a7677f0ff346..071a610f59e1 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/StartAzureAutomationDscCompilationJob.cs @@ -26,7 +26,7 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// starts azure automation compilation job /// [Cmdlet(VerbsLifecycle.Start, "AzureAutomationDscCompilationJob", DefaultParameterSetName = AutomationCmdletParameterSets.ByConfigurationName)] - [OutputType(typeof(DscCompilationJob))] + [OutputType(typeof(CompilationJob))] public class StartAzureAutomationDscCompilationJob : AzureAutomationBaseCmdlet { /// @@ -54,7 +54,7 @@ public class StartAzureAutomationDscCompilationJob : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - DscCompilationJob job = null; + CompilationJob job = null; job = this.AutomationClient.StartCompilationJob(this.ResourceGroupName, this.AutomationAccountName, this.ConfigurationName, this.Parameters); diff --git a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj index 5623de19eae5..c3a1e6eeeb81 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj +++ b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj @@ -118,6 +118,7 @@ + @@ -145,9 +146,10 @@ - + + True diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs index 3000993cad10..6b5233fae464 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs @@ -164,7 +164,7 @@ public Model.AgentRegistration NewAgentRegistrationKey( #region compilationjob - public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid Id) + public Model.CompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid Id) { using (var request = new RequestSettings(this.automationManagementClient)) { @@ -175,11 +175,11 @@ public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, strin string.Format(CultureInfo.CurrentCulture, Resources.CompilationJobNotFound, Id)); } - return new Model.DscCompilationJob(automationAccountName, job); + return new Model.CompilationJob(automationAccountName, job); } } - public IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + public IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) { using (var request = new RequestSettings(this.automationManagementClient)) { @@ -257,11 +257,11 @@ public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, strin }); } - return jobModels.Select(jobModel => new Commands.Automation.Model.DscCompilationJob(automationAccountName, jobModel)); + return jobModels.Select(jobModel => new Commands.Automation.Model.CompilationJob(automationAccountName, jobModel)); } } - public IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + public IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) { using (var request = new RequestSettings(this.automationManagementClient)) { @@ -332,11 +332,11 @@ public Model.DscCompilationJob GetCompilationJob(string resourceGroupName, strin }); } - return jobModels.Select(jobModel => new Model.DscCompilationJob(automationAccountName, jobModel)); + return jobModels.Select(jobModel => new Model.CompilationJob(automationAccountName, jobModel)); } } - public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters) + public Model.CompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters) { using (var request = new RequestSettings(this.automationManagementClient)) { @@ -354,7 +354,7 @@ public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, str var job = this.automationManagementClient.CompilationJobs.Create(resourceGroupName, automationAccountName, createJobParameters); - return new Model.DscCompilationJob(automationAccountName, job.DscCompilationJob); + return new Model.CompilationJob(automationAccountName, job.DscCompilationJob); } } @@ -381,6 +381,70 @@ public Model.DscCompilationJob StartCompilationJob(string resourceGroupName, str #endregion + #region node configuration + public Model.NodeConfiguration GetNodeConfiguration(string resourceGroupName, string automationAccountName, string nodeConfigurationName) + { + using (var request = new RequestSettings(this.automationManagementClient)) + { + var nodeConfiguration = this.automationManagementClient.NodeConfigurations.Get(resourceGroupName, automationAccountName, nodeConfigurationName).NodeConfiguration; + if (nodeConfiguration == null) + { + throw new ResourceNotFoundException(typeof(NodeConfiguration), + string.Format(CultureInfo.CurrentCulture, Resources.NodeConfigurationNotFound, nodeConfigurationName)); + } + + return new Model.NodeConfiguration(automationAccountName, nodeConfiguration); + } + } + + public IEnumerable ListNodeConfigurationsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName) + { + using (var request = new RequestSettings(this.automationManagementClient)) + { + IEnumerable nodeConfigModels; + + nodeConfigModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.NodeConfigurations.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscNodeConfigurationListParameters + { + ConfigurationName = configurationName + }); + return new ResponseWithSkipToken(response, response.DscNodeConfigurations); + }); + + + return nodeConfigModels.Select(nodeConfigModel => new Commands.Automation.Model.NodeConfiguration(automationAccountName, nodeConfigModel)); + } + } + + public IEnumerable ListNodeConfigurations(string resourceGroupName, string automationAccountName) + { + using (var request = new RequestSettings(this.automationManagementClient)) + { + IEnumerable nodeConfigModels; + + nodeConfigModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.NodeConfigurations.List( + resourceGroupName, + automationAccountName, + new AutomationManagement.Models.DscNodeConfigurationListParameters()); + + return new ResponseWithSkipToken(response, response.DscNodeConfigurations); + }); + + + return nodeConfigModels.Select(nodeConfigModel => new Model.NodeConfiguration(automationAccountName, nodeConfigModel)); + } + } + + #endregion + #region privatemethods private string FormatDateTime(DateTimeOffset dateTime) diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs index d8027a1a7a46..9e5cdaeaa0a0 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs @@ -73,6 +73,11 @@ internal static class AutomationCmdletParameterSets /// internal const string ByConfigurationName = "ByConfigurationName"; + /// + /// The Node Configuration name parameter set. + /// + internal const string ByNodeConfigurationName = "ByNodeConfigurationName"; + /// /// The Schedule name parameter set. /// diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs index 1bd07a66dab5..10adb832bf0f 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs @@ -41,17 +41,25 @@ public interface IAutomationClient #region Compilationjobs - DscCompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid id); + CompilationJob GetCompilationJob(string resourceGroupName, string automationAccountName, Guid id); - IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); + IEnumerable ListCompilationJobsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); - IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); + IEnumerable ListCompilationJobs(string resourceGroupName, string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); - DscCompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters); + CompilationJob StartCompilationJob(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters); IEnumerable GetDscCompilationJobStream(string resourceGroupName, string automationAccountname, Guid jobId, DateTimeOffset? time, string streamType); #endregion + #region NodeConfiguration + NodeConfiguration GetNodeConfiguration(string resourceGroupName, string automationAccountName, string nodeConfigurationName); + + IEnumerable ListNodeConfigurationsByConfigurationName(string resourceGroupName, string automationAccountName, string configurationName); + + IEnumerable ListNodeConfigurations(string resourceGroupName, string automationAccountName); + #endregion + #region Configurations IEnumerable ListAutomationConfigurations(string resourceGroupName, string automationAccountName); diff --git a/src/ResourceManager/Automation/Commands.Automation/Model/DscCompilationJob.cs b/src/ResourceManager/Automation/Commands.Automation/Model/CompilationJob.cs similarity index 91% rename from src/ResourceManager/Automation/Commands.Automation/Model/DscCompilationJob.cs rename to src/ResourceManager/Automation/Commands.Automation/Model/CompilationJob.cs index 0830213a551d..5e346bec8e11 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Model/DscCompilationJob.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Model/CompilationJob.cs @@ -26,10 +26,10 @@ namespace Microsoft.Azure.Commands.Automation.Model /// /// The Dsc Compilation Job /// - public class DscCompilationJob + public class CompilationJob { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// /// The account name. @@ -39,7 +39,7 @@ public class DscCompilationJob /// /// /// - public DscCompilationJob(string accountName, AutomationManagement.Models.DscCompilationJob job) + public CompilationJob(string accountName, AutomationManagement.Models.DscCompilationJob job) { Requires.Argument("job", job).NotNull(); Requires.Argument("accountName", accountName).NotNull(); @@ -62,14 +62,14 @@ public DscCompilationJob(string accountName, AutomationManagement.Models.DscComp foreach (var kvp in job.Properties.Parameters.Where(kvp => 0 != String.Compare(kvp.Key, Constants.JobStartedByParameterName, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase))) { - this.JobParameters.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); + this.JobParameters.Add(kvp.Key, (object)(kvp.Value)); } } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public DscCompilationJob() + public CompilationJob() { } diff --git a/src/ResourceManager/Automation/Commands.Automation/Model/NodeConfiguration.cs b/src/ResourceManager/Automation/Commands.Automation/Model/NodeConfiguration.cs new file mode 100644 index 000000000000..155b9e3412b8 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Model/NodeConfiguration.cs @@ -0,0 +1,90 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.Commands.Automation.Model +{ + using System; + using System.Collections; + using System.Globalization; + using System.Linq; + + using Microsoft.Azure.Commands.Automation.Common; + + using AutomationManagement = Microsoft.Azure.Management.Automation; + + /// + /// The Dsc Node configuration + /// + public class NodeConfiguration + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The account name. + /// + /// + /// The NodeConfiguration. + /// + /// + /// + public NodeConfiguration(string accountName, AutomationManagement.Models.DscNodeConfiguration nodeConfiguration) + { + Requires.Argument("nodeConfiguration", nodeConfiguration).NotNull(); + Requires.Argument("accountName", accountName).NotNull(); + + this.AutomationAccountName = accountName; + + this.Name = nodeConfiguration.Name; + this.CreationTime = nodeConfiguration.CreationTime.ToLocalTime(); + this.LastModifiedTime = nodeConfiguration.LastModifiedTime.ToLocalTime(); + if (nodeConfiguration.Configuration != null) + { + this.ConfigurationName = nodeConfiguration.Configuration.Name; + } + } + + /// + /// Initializes a new instance of the class. + /// + public NodeConfiguration() + { + } + + /// + /// Gets or sets the automaiton account name. + /// + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the node configuration name + /// + public string Name { get; set; } + + /// + /// Gets or sets the tags. + /// + public DateTimeOffset CreationTime { get; set; } + + /// + /// Gets or sets the last modified time of the job. + /// + public DateTimeOffset LastModifiedTime { get; set; } + + /// + /// Gets or sets the configuration. + /// + public string ConfigurationName { get; set; } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs index e798c66cf3f9..0b86913799b8 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs @@ -240,6 +240,15 @@ internal static string ModuleNotFound { } } + /// + /// Looks up a localized string similar to NodeConfiguration {0} not found.. + /// + internal static string NodeConfigurationNotFound { + get { + return ResourceManager.GetString("NodeConfigurationNotFound", resourceCulture); + } + } + /// /// Looks up a localized string similar to {0} is empty.. /// diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx index c88e76798cf6..5ecd6cc403b6 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx @@ -294,4 +294,8 @@ Invalid configuration parameters. Automation + + NodeConfiguration {0} not found. + Automation + \ No newline at end of file From b373fab07fd56ea3eec8eda3566c711588e4adae Mon Sep 17 00:00:00 2001 From: Balu Kambala Date: Sun, 12 Apr 2015 23:56:54 -0700 Subject: [PATCH 6/6] fixed build version in csproj --- .../Commands.ResourceManagement.Automation.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj index c3a1e6eeeb81..426a44b99e05 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj +++ b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj @@ -1,5 +1,5 @@  - + Debug