diff --git a/setup/azurecmdfiles.wxi b/setup/azurecmdfiles.wxi index a220c5cc4f8f..05a52b0633ba 100644 --- a/setup/azurecmdfiles.wxi +++ b/setup/azurecmdfiles.wxi @@ -823,7 +823,7 @@ - + diff --git a/src/Common/Commands.ScenarioTest/Resources/Automation/AutomationTests.ps1 b/src/Common/Commands.ScenarioTest/Resources/Automation/AutomationTests.ps1 index 62285b98090c..9cee718e407d 100644 --- a/src/Common/Commands.ScenarioTest/Resources/Automation/AutomationTests.ps1 +++ b/src/Common/Commands.ScenarioTest/Resources/Automation/AutomationTests.ps1 @@ -17,7 +17,8 @@ Checks whether the first string contains the second one #> -$accountName='AutomationAccount' +$accountName='safeer' +$location = "East US" function AssertContains { @@ -108,10 +109,10 @@ function Test-RunbookWithParameter $runbook = CreateRunbook $runbookPath Assert-NotNull $runbook "runBook $runbookPath does not import successfully." - $automationAccount | Publish-AzureAutomationRunbook -Id $runbook.Id + $automationAccount | Publish-AzureAutomationRunbook -Name $runbook.Name #Test - $job = $automationAccount | Start-AzureAutomationRunbook -Id $runbook.Id -Parameters $parameters + $job = $automationAccount | Start-AzureAutomationRunbook -Name $runbook.Name -Parameters $parameters WaitForJobStatus -Id $job.Id -Status "Completed" $jobOutput = $automationAccount | Get-AzureAutomationJobOutput -Id $job.Id -Stream Output $automationAccount | Remove-AzureAutomationRunbook -Name $runbook.Name -Force @@ -126,16 +127,15 @@ function Test-AutomationStartAndStopRunbook { param([string] $runbookPath) - #Setup - $automationAccount = Get-AzureAutomationAccount -Name $accountName + $automationAccount = Get-AzureAutomationAccount -Name $accountName Assert-NotNull $automationAccount "Automation account $accountName does not exist." $runbook = CreateRunbook $runbookPath Assert-NotNull $runbook "runBook $runbookPath does not import successfully." - $automationAccount | Publish-AzureAutomationRunbook -Id $runbook.Id + $automationAccount | Publish-AzureAutomationRunbook -Name $runbook.Name #Test - $job = $automationAccount | Start-AzureAutomationRunbook -Id $runbook.Id + $job = Start-AzureAutomationRunbook -Name $runbook.Name -AutomationAccountName $accountName WaitForJobStatus -Id $job.Id -Status "Running" $automationAccount | Stop-AzureAutomationJob -Id $job.Id WaitForJobStatus -Id $job.Id -Status "Stopped" @@ -153,31 +153,28 @@ function Test-AutomationPublishAndEditRunbook $runbook = CreateRunbook $runbookPath $true - #Test - - Assert-Null $runbook.PublishedRunbookVersionId - Assert-NotNull $runbook.DraftRunbookVersionId #Publish Runbook - $publishedRunbook = Publish-AzureAutomationRunbook $accountName -Id $runbook.Id - Assert-NotNull $publishedRunbook.PublishedRunbookVersionId - Assert-Null $publishedRunbook.DraftRunbookVersionId - $publishedRunbookDefn = Get-AzureAutomationRunbookDefinition $accountName -VersionId $publishedRunbook.PublishedRunbookVersionId + Publish-AzureAutomationRunbook $accountName -Name $runbook.Name + $publishedRunbook = Get-AzureAutomationRunbook $accountName -Name $runbook.Name + $runbookState = "Published" + Assert-AreEqual $publishedRunbook.State $runbookState "Runbook should be in $runbookState state" + $publishedRunbookDefn = Get-AzureAutomationRunbookDefinition $accountName -Name $runbook.Name #Edit Runbook - Set-AzureAutomationRunbookDefinition $accountName -Id $runbook.Id -Path $runbookPath -Overwrite + Set-AzureAutomationRunbookDefinition $accountName -Name $runbook.Name -Path $runbookPath -Overwrite $runbook = Get-AzureAutomationRunbook $accountName -Name $runbook.Name - Assert-AreEqual $publishedRunbook.PublishedRunbookVersionId $runbook.PublishedRunbookVersionId - Assert-NotNull $runbook.DraftRunbookVersionId "Runbook should be in draft mode" - $editedRunbookDefn = Get-AzureAutomationRunbookDefinition $accountName -VersionId $runbook.DraftRunbookVersionId + $runbookState = "Edit" + Assert-AreEqual $runbook.State $runbookState "Runbook should be in $runbookState state" + $editedRunbookDefn = Get-AzureAutomationRunbookDefinition $accountName -Name $runbook.Name -Slot "Draft" Assert-AreNotEqual $editedRunbookDefn.Content $publishedRunbookDefn.Content "Old content and edited content of the runbook shouldn't be equal" Assert-Throws {Set-AzureAutomationRunbookDefinition $accountName -Name $runbook.Name -Path $editRunbookPath -PassThru -ErrorAction Stop} Set-AzureAutomationRunbookDefinition $accountName -Name $runbook.Name -Path $editRunbookPath -Overwrite - $editedRunbookDefn2 = Get-AzureAutomationRunbookDefinition $accountName -VersionId $runbook.DraftRunbookVersionId + $editedRunbookDefn2 = Get-AzureAutomationRunbookDefinition $accountName -Name $runbook.Name -Slot "Draft" Assert-AreNotEqual $editedRunbookDefn2.Content $editedRunbookDefn.Content "Old content and edited content of the runbook shouldn't be equal" - Remove-AzureAutomationRunbook $accountName -Id $runbook.Id -Force - Assert-Throws {Get-AzureAutomationRunbook $accountName -Id $runbook.Id} + Remove-AzureAutomationRunbook $accountName -Name $runbook.Name -Force + Assert-Throws {Get-AzureAutomationRunbook $accountName -Name $runbook.Name} } @@ -194,30 +191,26 @@ function Test-AutomationConfigureRunbook Assert-NotNull $automationAccount "Automation account $accountName does not exist." $runbook = CreateRunbook $runbookPath Assert-NotNull $runbook "runbook ($runbookPath) isn't imported successfully." - Publish-AzureAutomationRunbook -Id $runbook.Id -AutomationAccountName $accountName + Publish-AzureAutomationRunbook -Name $runbook.Name -AutomationAccountName $accountName #Test #Change the runbook configuration - $automationAccount | Set-AzureAutomationRunbook -Id $runbook.Id -LogDebug $true -LogVerbose $true -LogProgress $false + $automationAccount | Set-AzureAutomationRunbook -Name $runbook.Name -LogVerbose $true -LogProgress $false $runbook = $automationAccount | Get-AzureAutomationRunbook -Name $runbook.Name Assert-NotNull $runbook "Runbook shouldn't be Null" - Assert-AreEqual $true $runbook.LogDebug "Log Debug mode should be true." Assert-AreEqual $true $runbook.LogVerbose "Log Verbose mode should be true." Assert-AreEqual $false $runbook.LogProgress "Log Progress mode should be false." #Start runbook and wait for job complete - $job = $automationAccount | Start-AzureAutomationRunbook -Id $runbook.Id + $job = $automationAccount | Start-AzureAutomationRunbook -Name $runbook.Name WaitForJobStatus -Id $job.Id -Status "Completed" #Check job output streams $jobOutputs = $automationAccount | Get-AzureAutomationJobOutput -Id $job.Id -Stream "Output" Assert-AreEqual 1 $jobOutputs.Count AssertContains $jobOutputs[0].Text "output message" "The output stream is wrong." - #Verify that debug and verbose streams are logged - $jobDebugOutputs = $automationAccount | Get-AzureAutomationJobOutput -Id $job.Id -Stream "Debug" - Assert-AreEqual 1 $jobDebugOutputs.Count - AssertContains $jobDebugOutputs[0].Text "debug message" "The debug stream is wrong." + #Verify that verbose streams are logged $jobVerboseOutputs = Get-AzureAutomationJobOutput $accountName -Id $job.Id -Stream "Verbose" Assert-AreEqual 1 $jobVerboseOutputs.Count AssertContains $jobVerboseOutputs[0].Text "verbose message" "The verbose stream is wrong." @@ -226,26 +219,24 @@ function Test-AutomationConfigureRunbook Assert-AreEqual 0 $jobProgressOutputs.Count #Change the runbook configuration again and start the runbook - Set-AzureAutomationRunbook $accountName -Id $runbook.Id -LogDebug $false -LogVerbose $false -LogProgress $true + Set-AzureAutomationRunbook $accountName -Name $runbook.Name -LogVerbose $false -LogProgress $true $job = Start-AzureAutomationRunbook $accountName -Name $runbook.Name WaitForJobStatus -Id $job.Id -Status "Completed" #Verify that progress stream is logged $jobProgressOutputs = Get-AzureAutomationJobOutput $accountName -Id $job.Id -Stream "Progress" Assert-AreNotEqual 0 $jobProgressOutputs.Count Assert-AreEqual $jobProgressOutputs[0].Type "Progress" - #Verify that debug and verbose streams aren't logged - $jobDebugOutputs = Get-AzureAutomationJobOutput $accountName -Id $job.Id -Stream "Debug" - Assert-AreEqual 0 $jobDebugOutputs.Count + #Verify that verbose streams aren't logged $jobVerboseOutputs = Get-AzureAutomationJobOutput $accountName -Id $job.Id -Stream "Verbose" Assert-AreEqual 0 $jobVerboseOutputs.Count #Check whether the total number of jobs for the runbook is correct - $jobs = Get-AzureAutomationJob $accountName -RunbookId $runbook.Id + $jobs = Get-AzureAutomationJob $accountName -RunbookName $runbook.Name Assert-AreEqual 2 $jobs.Count "There should be 2 jobs in total for this runbook." #Remove runbook $automationAccount | Remove-AzureAutomationRunbook -Name $runbook.Name -Force - Assert-Throws {$automationAccount | Get-AzureAutomationRunbook -Id $runbook.Id} + Assert-Throws {$automationAccount | Get-AzureAutomationRunbook -Name $runbook.Name} } <# @@ -263,9 +254,9 @@ function Test-AutomationSuspendAndResumeJob #Test - $automationAccount | Publish-AzureAutomationRunbook -Id $runbook.Id + $automationAccount | Publish-AzureAutomationRunbook -Name $runbook.Name #Start, suspend, and then resume job - $job = Start-AzureAutomationRunbook $accountName -Id $runbook.Id + $job = Start-AzureAutomationRunbook $accountName -Name $runbook.Name WaitForJobStatus -Id $job.Id -Status "Running" Suspend-AzureAutomationJob $accountName -Id $job.Id WaitForJobStatus -Id $job.Id -Status "Suspended" @@ -273,7 +264,7 @@ function Test-AutomationSuspendAndResumeJob WaitForJobStatus -Id $job.Id -Status "Completed" #Remove runbook - Remove-AzureAutomationRunbook -AutomationAccountName $accountName -Id $runbook.Id -Force + Remove-AzureAutomationRunbook -AutomationAccountName $accountName -Name $runbook.Name -Force Assert-Throws {Get-AzureAutomationRunbook $accountName -Name $runbook.Name} } @@ -319,31 +310,34 @@ function Test-AutomationStartRunbookOnASchedule $runbook = Register-AzureAutomationScheduledRunbook $accountName -Name $runbook.Name -ScheduleName $oneTimeScheName Assert-AreEqual $oneTimeScheName $runbook.ScheduleNames "The runbook should be associated with $oneTimeScheName" - $runbook = Register-AzureAutomationScheduledRunbook $accountName -Id $runbook.Id -ScheduleName $dailyScheName + $runbook = Register-AzureAutomationScheduledRunbook $accountName -Name $runbook.Name -ScheduleName $dailyScheName Assert-True { $runbook.ScheduleNames -Contains $dailyScheName} "The runbook should be associated with $dailyScheName" #waiting for seven minutes Wait-Seconds 420 - $job = Get-AzureAutomationJob $accountName -RunbookId $runbook.Id | where {$_.ScheduleName -eq $oneTimeScheName} + $job = Get-AzureAutomationJob $accountName -Name $runbook.Name | where {$_.ScheduleName -eq $oneTimeScheName} + $jobSchedule = Get-AzureAutomationScheduledRunbook $accountName -RunbookName $runbook.Name -ScheduleName $oneTimeScheName + Assert-AreEqual 1 $jobSchedule.Count Assert-AreEqual 1 $job.Count WaitForJobStatus -Id $job.Id -Status "Completed" #Edit schedule $description = "Daily Schedule Description" - Set-AzureAutomationSchedule $accountName -Name $oneTimeScheName -Description $description - $dailySchedule = Get-AzureAutomationSchedule $accountName -Name $oneTimeScheName + Set-AzureAutomationSchedule $accountName -Name $dailyScheName -Description $description + $dailySchedule = Get-AzureAutomationSchedule $accountName -Name $dailyScheName Assert-AreEqual $description $dailySchedule.Description - $runbook = Unregister-AzureAutomationScheduledRunbook $accountName -Name $runbook.Name -ScheduleName $dailyScheName - Assert-False {$runbook.ScheduleNames -Contains $dailyScheName} "The runbook shouldn't have an association with $dailyScheName" + Unregister-AzureAutomationScheduledRunbook $accountName -Name $runbook.Name -ScheduleName $dailyScheName + $jobSchedule = Get-AzureAutomationScheduledRunbook $accountName -RunbookName $runbook.Name -ScheduleName $dailyScheName + Assert-Null $jobSchedule "The runbook shouldn't have an association with $dailyScheName" #Remove runbook and schedule Remove-AzureAutomationSchedule $accountName -Name $oneTimeScheName -Force Assert-Throws {$automationAccount | Get-AzureAutomationSchedule -Name $oneTimeScheName} $automationAccount | Remove-AzureAutomationSchedule -Name $dailyScheName -Force Assert-Throws {$automationAccount | Get-AzureAutomationSchedule -Name $dailyScheName} - Remove-AzureAutomationRunbook $accountName -Id $runbook.Id -Force - Assert-Throws {Get-AzureAutomationRunbook $accountName -Id $runbook.Id} + Remove-AzureAutomationRunbook $accountName -Name $runbook.Name -Force + Assert-Throws {Get-AzureAutomationRunbook $accountName -Name $runbook.Name} } <# @@ -362,8 +356,8 @@ function Test-AutomationStartUnpublishedRunbook Assert-NotNull $runbook "runBook $runbookPath does not import successfully." Assert-NotNull $runbook.Tags "Tags of the runbook shouldn't be Null." Assert-NotNull $runbook.Description "Description of the runbook shouldn't be Null." - Assert-Throws {Start-AzureAutomationRunbook $accountName -Id $runbook.Id -Parameters $runbookParameters -PassThru -ErrorAction Stop} + Assert-Throws {Start-AzureAutomationRunbook $accountName -Name $runbook.Name -Parameters $runbookParameters -PassThru -ErrorAction Stop} - Remove-AzureAutomationRunbook $accountName -Id $runbook.Id -Force - Assert-Throws {Get-AzureAutomationRunbook $accountName -Id $runbook.Id -Parameters $runbookParameters -PassThru -ErrorAction Stop} + Remove-AzureAutomationRunbook $accountName -Name $runbook.Name -Force + Assert-Throws {Get-AzureAutomationRunbook $accountName -Name $runbook.Name -Parameters $runbookParameters -PassThru -ErrorAction Stop} } diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/Commands.Automation.Test.csproj b/src/ServiceManagement/Automation/Commands.Automation.Test/Commands.Automation.Test.csproj index 1bcd70e9a236..f837e2b26dec 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/Commands.Automation.Test.csproj +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/Commands.Automation.Test.csproj @@ -111,25 +111,42 @@ + + + - - - + + + - - + + + + - - + + + + + + + + + + + + + + - + @@ -148,6 +165,7 @@ + diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationJobOutputTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationCertificateTest.cs similarity index 62% rename from src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationJobOutputTest.cs rename to src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationCertificateTest.cs index de8e06091527..16f563ebc4e6 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationJobOutputTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationCertificateTest.cs @@ -13,30 +13,33 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; using Moq; namespace Microsoft.Azure.Commands.Automation.Test.UnitTests { [TestClass] - public class GetAzureAutomationJobOutputTest : TestBase + public class GetAzureAutomationCertificateTest : TestBase { private Mock mockAutomationClient; private MockCommandRuntime mockCommandRuntime; - private GetAzureAutomationJobOutput cmdlet; + private GetAzureAutomationCertificate cmdlet; [TestInitialize] public void SetupTest() { this.mockAutomationClient = new Mock(); this.mockCommandRuntime = new MockCommandRuntime(); - this.cmdlet = new GetAzureAutomationJobOutput + this.cmdlet = new GetAzureAutomationCertificate { AutomationClient = this.mockAutomationClient.Object, CommandRuntime = this.mockCommandRuntime @@ -44,47 +47,38 @@ public void SetupTest() } [TestMethod] - public void GetAzureAutomationJobOutputStreamAnySuccessfull() + public void GetAzureAutomationCertificateByNameSuccessfull() { // Setup string accountName = "automation"; - string stream = Constants.JobOutputParameter.Any; - DateTime startTime = default(DateTime); - var jobId = new Guid(); + string certificateName = "certificate"; - this.mockAutomationClient.Setup(f => f.ListJobStreamItems(accountName, jobId, startTime, null)); + this.mockAutomationClient.Setup(f => f.GetCertificate(accountName, certificateName)); // Test this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = jobId; - this.cmdlet.StartTime = startTime; - this.cmdlet.Stream = stream; + this.cmdlet.Name = certificateName; + this.cmdlet.SetParameterSet("ByCertificateName"); this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.ListJobStreamItems(accountName, jobId, startTime, null), Times.Once()); + this.mockAutomationClient.Verify(f => f.GetCertificate(accountName, certificateName), Times.Once()); } [TestMethod] - public void GetAzureAutomationJobOutputStreamDebugSuccessfull() + public void GetAzureAutomationCertificateByAllSuccessfull() { // Setup string accountName = "automation"; - DateTime startTime = default(DateTime); - string stream = Constants.JobOutputParameter.Debug; - var jobId = new Guid(); - this.mockAutomationClient.Setup(f => f.ListJobStreamItems(accountName, jobId, startTime, stream)); + this.mockAutomationClient.Setup(f => f.ListCertificates(accountName)).Returns((string a) => new List()); // Test this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = jobId; - this.cmdlet.StartTime = startTime; - this.cmdlet.Stream = stream; this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.ListJobStreamItems(accountName, jobId, startTime, stream), Times.Once()); + this.mockAutomationClient.Verify(f => f.ListCertificates(accountName), Times.Once()); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationConnectionTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationConnectionTest.cs new file mode 100644 index 000000000000..7f45706705a3 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationConnectionTest.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.Generic; +using Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class GetAzureAutomationConnectionTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private GetAzureAutomationConnection cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new GetAzureAutomationConnection + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void GetAzureAutomationConnectionByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string connectionName = "connection"; + + this.mockAutomationClient.Setup(f => f.GetConnection(accountName, connectionName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = connectionName; + this.cmdlet.SetParameterSet("ByConnectionName"); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.GetConnection(accountName, connectionName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationConnectionByAllSuccessfull() + { + // Setup + string accountName = "automation"; + + this.mockAutomationClient.Setup(f => f.ListConnections(accountName)).Returns((string a) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListConnections(accountName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationConnectionByTypeSuccessfull() + { + // Setup + string accountName = "automation"; + string connectionTypeName = "connectionType"; + + this.mockAutomationClient.Setup(f => f.ListConnectionsByType(accountName, connectionTypeName)).Returns((string a, string b) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.ConnectionTypeName = connectionTypeName; + this.cmdlet.SetParameterSet("ByConnectionTypeName"); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListConnectionsByType(accountName, connectionTypeName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationCredentialTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationCredentialTest.cs new file mode 100644 index 000000000000..90594d48ef62 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationCredentialTest.cs @@ -0,0 +1,82 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class GetAzureAutomationCredentialTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private GetAzureAutomationCredential cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new GetAzureAutomationCredential + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void GetAzureAutomationCredentialByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string credentialName = "credential"; + + this.mockAutomationClient.Setup(f => f.GetCredential(accountName, credentialName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = credentialName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.GetCredential(accountName, credentialName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationCredentialByAllSuccessfull() + { + // Setup + string accountName = "automation"; + + this.mockAutomationClient.Setup(f => f.ListCredentials(accountName)).Returns((string a) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListCredentials(accountName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationJobTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationJobTest.cs index 63c144e2a11d..71f4b5897569 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationJobTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationJobTest.cs @@ -13,8 +13,10 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; @@ -44,75 +46,140 @@ public void SetupTest() } [TestMethod] - public void GetAzureAutomationJobByJobIdSuccessfull() + public void GetAzureAutomationJobByRunbookNameSuccessfull() { // Setup string accountName = "automation"; - var jobId = new Guid(); + string runbookName = "runbook"; - this.mockAutomationClient.Setup(f => f.GetJob(accountName, jobId)); + this.mockAutomationClient.Setup(f => f.ListJobsByRunbookName(accountName, runbookName, null, null, null)); // Test this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = jobId; + this.cmdlet.RunbookName = runbookName; this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.GetJob(accountName, jobId), Times.Once()); + this.mockAutomationClient.Verify(f => f.ListJobsByRunbookName(accountName, runbookName, null, null, null), Times.Once()); + } + + public void GetAzureAutomationJobByRunbookNamAndStartTimeEndTimeeSuccessfull() + { + // Setup + string accountName = "automation"; + string runbookName = "runbook"; + DateTime startTime = new DateTime(2014, 12, 30, 17, 0, 0, 0); + DateTime endTime = new DateTime(2014, 12, 30, 18, 0, 0, 0); + + this.mockAutomationClient.Setup(f => f.ListJobsByRunbookName(accountName, runbookName, startTime, endTime, null)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.RunbookName = runbookName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListJobsByRunbookName(accountName, runbookName, startTime, endTime, null), Times.Once()); + } + + public void GetAzureAutomationCompletedJobByRunbookNamAndStartTimeEndTimeeSuccessfull() + { + // Setup + string accountName = "automation"; + string runbookName = "runbook"; + DateTime startTime = new DateTime(2014, 12, 30, 17, 0, 0, 0); + DateTime endTime = new DateTime(2014, 12, 30, 18, 0, 0, 0); + string status = "Completed"; + + this.mockAutomationClient.Setup(f => f.ListJobsByRunbookName(accountName, runbookName, startTime, endTime, status)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.RunbookName = runbookName; + this.cmdlet.Status = status; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListJobsByRunbookName(accountName, runbookName, startTime, endTime, status), Times.Once()); } [TestMethod] - public void GetAzureAutomationJobByRunbookIdSuccessfull() + public void GetAzureAutomationAllJobsSuccessfull() { // Setup string accountName = "automation"; - var runbookId = new Guid(); - this.mockAutomationClient.Setup( - f => f.ListJobsByRunbookId(accountName, runbookId, It.IsAny(), It.IsAny())); + this.mockAutomationClient.Setup(f => f.ListJobs(accountName, null, null, null)); // Test this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.RunbookId = runbookId; this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.ListJobsByRunbookId(accountName, runbookId, It.IsAny(), It.IsAny()), Times.Once()); + this.mockAutomationClient.Verify(f => f.ListJobs(accountName, null, null, null), Times.Once()); } [TestMethod] - public void GetAzureAutomationJobByRunbookNameSuccessfull() + public void GetAzureAutomationAllJobsBetweenStartAndEndTimeSuccessfull() { // Setup string accountName = "automation"; - var runbookName = "runbook"; + DateTime startTime = new DateTime(2014, 12, 30, 17, 0, 0, 0); + DateTime endTime = new DateTime(2014, 12, 30, 18, 0, 0, 0); - this.mockAutomationClient.Setup( - f => f.ListJobsByRunbookName(accountName, runbookName, It.IsAny(), It.IsAny())); + // look for jobs between 5pm to 6pm on 30th december 2014 + this.mockAutomationClient.Setup(f => f.ListJobs(accountName, startTime, endTime, null)); // Test this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.RunbookName = runbookName; + this.cmdlet.StartTime = startTime; + this.cmdlet.EndTime = endTime; this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.ListJobsByRunbookName(accountName, runbookName, It.IsAny(), It.IsAny()), Times.Once()); + this.mockAutomationClient.Verify(f => f.ListJobs(accountName, startTime, endTime, null), Times.Once()); } [TestMethod] - public void GetAzureAutomationJobByAllSuccessfull() + public void GetAzureAutomationAllCompletedJobsBetweenStartAndEndTimeSuccessfull() + { + // Setup + string accountName = "automation"; + DateTime startTime = new DateTime(2014, 12, 30, 17, 0, 0, 0); + DateTime endTime = new DateTime(2014, 12, 30, 18, 0, 0, 0); + string status = "Completed"; + + // look for jobs between 5pm to 6pm on 30th december 2014 + this.mockAutomationClient.Setup(f => f.ListJobs(accountName, startTime, endTime, status)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.StartTime = startTime; + this.cmdlet.EndTime = endTime; + this.cmdlet.Status = status; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListJobs(accountName, startTime, endTime, status), Times.Once()); + } + + public void GetAzureAutomationJobByIdSuccessfull() { // Setup string accountName = "automation"; + Guid jobId = Guid.NewGuid(); - this.mockAutomationClient.Setup(f => f.ListJobs(accountName, It.IsAny(), It.IsAny())); + // look for jobs between 5pm to 6pm on 30th december 2014 + this.mockAutomationClient.Setup(f => f.GetJob(accountName, jobId)); // Test this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Id = jobId; this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.ListJobs(accountName, It.IsAny(), It.IsAny()), Times.Once()); + this.mockAutomationClient.Verify(f => f.GetJob(accountName, jobId), Times.Once()); } + } } diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationModuleTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationModuleTest.cs new file mode 100644 index 000000000000..47133a6aa2e9 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationModuleTest.cs @@ -0,0 +1,82 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class GetAzureAutomationModuleTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private GetAzureAutomationModule cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new GetAzureAutomationModule + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void GetAzureAutomationModuleByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string moduleName = "module"; + + this.mockAutomationClient.Setup(f => f.GetModule(accountName, moduleName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = moduleName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.GetModule(accountName, moduleName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationModuleByAllSuccessfull() + { + // Setup + string accountName = "automation"; + + this.mockAutomationClient.Setup(f => f.ListModules(accountName)).Returns((string a) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListModules(accountName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookDefinitionTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookDefinitionTest.cs index 862af2fc70de..315ca0831544 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookDefinitionTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookDefinitionTest.cs @@ -43,43 +43,6 @@ public void SetupTest() }; } - [TestMethod] - public void GetAzureAutomationRunbookDefinitionByRunbookIdWithoutSlotSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.ListRunbookDefinitionsByRunbookId(accountName, runbookId, null)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.ListRunbookDefinitionsByRunbookId(accountName, runbookId, null), Times.Once()); - } - - [TestMethod] - public void GetAzureAutomationRunbookDefinitionByRunbookIdSlotDraftSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.ListRunbookDefinitionsByRunbookId(accountName, runbookId, true)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.Slot = "Draft"; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.ListRunbookDefinitionsByRunbookId(accountName, runbookId, true), Times.Once()); - } - [TestMethod] public void GetAzureAutomationRunbookDefinitionByRunbookNameWithoutSlotSuccessfull() { @@ -116,24 +79,5 @@ public void GetAzureAutomationRunbookDefinitionByRunbookNameSlotPublishedSuccess // Assert this.mockAutomationClient.Verify(f => f.ListRunbookDefinitionsByRunbookName(accountName, runbookName, false), Times.Once()); } - - [TestMethod] - public void GetAzureAutomationRunbookDefinitionByRunbookVersionIdWithoutSlotSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookVersionId = new Guid(); - - this.mockAutomationClient.Setup( - f => f.ListRunbookDefinitionsByRunbookVersionId(accountName, runbookVersionId, null)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.VersionId = runbookVersionId; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.ListRunbookDefinitionsByRunbookVersionId(accountName, runbookVersionId, null), Times.Once()); - } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookTest.cs index cb51efa30d15..28b0ec8bcae5 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationRunbookTest.cs @@ -13,11 +13,14 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; using Moq; namespace Microsoft.Azure.Commands.Automation.Test.UnitTests @@ -43,24 +46,6 @@ public void SetupTest() }; } - [TestMethod] - public void GetAzureAutomationRunbookByIdSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.GetRunbook(accountName, runbookId)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.GetRunbook(accountName, runbookId), Times.Once()); - } - [TestMethod] public void GetAzureAutomationRunbookByNameSuccessfull() { @@ -73,40 +58,24 @@ public void GetAzureAutomationRunbookByNameSuccessfull() // Test this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = runbookName; + this.cmdlet.SetParameterSet("ByRunbookName"); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient.Verify(f => f.GetRunbook(accountName, runbookName), Times.Once()); } - [TestMethod] - public void GetAzureAutomationRunbookByScheduleNameSuccessfull() - { - // Setup - string accountName = "automation"; - string scheduleName = "schedule"; - - this.mockAutomationClient.Setup(f => f.ListRunbookByScheduleName(accountName, scheduleName)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.ScheduleName = scheduleName; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.ListRunbookByScheduleName(accountName, scheduleName), Times.Once()); - } - [TestMethod] public void GetAzureAutomationRunbookByAllSuccessfull() { // Setup string accountName = "automation"; - this.mockAutomationClient.Setup(f => f.ListRunbooks(accountName)); + this.mockAutomationClient.Setup(f => f.ListRunbooks(accountName)).Returns((string a) => new List()); ; // Test this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.SetParameterSet("ByAll"); this.cmdlet.ExecuteCmdlet(); // Assert diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationScheduleTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationScheduleTest.cs index 3321454d46c7..f0866f66514f 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationScheduleTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationScheduleTest.cs @@ -20,6 +20,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; using Moq; namespace Microsoft.Azure.Commands.Automation.Test.UnitTests @@ -45,24 +46,6 @@ public void SetupTest() }; } - [TestMethod] - public void GetAzureAutomationScheduleByIdSuccessfull() - { - // Setup - string accountName = "automation"; - var scheduleId = new Guid(); - - this.mockAutomationClient.Setup(f => f.GetSchedule(accountName, scheduleId)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = scheduleId; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.GetSchedule(accountName, scheduleId), Times.Once()); - } - [TestMethod] public void GetAzureAutomationScheduleByNameSuccessfull() { @@ -75,6 +58,7 @@ public void GetAzureAutomationScheduleByNameSuccessfull() // Test this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByName); this.cmdlet.ExecuteCmdlet(); // Assert @@ -91,6 +75,7 @@ public void GetAzureAutomationScheduleByAllSuccessfull() // Test this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByAll); this.cmdlet.ExecuteCmdlet(); // Assert diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationScheduledRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationScheduledRunbookTest.cs new file mode 100644 index 000000000000..952486533e93 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationScheduledRunbookTest.cs @@ -0,0 +1,144 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class GetAzureAutomationScheduledRunbookTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private GetAzureAutomationScheduledRunbook cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new GetAzureAutomationScheduledRunbook + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void GetAzureAutomationScheduledRunbookByIdSuccessfull() + { + // Setup + string accountName = "automation"; + var jobScheduleId = new Guid(); + + this.mockAutomationClient.Setup(f => f.GetJobSchedule(accountName, jobScheduleId)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.JobScheduleId = jobScheduleId; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByJobScheduleId); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.GetJobSchedule(accountName, jobScheduleId), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationScheduledRunbookByrunbookNameAndScheduleNameSuccessfull() + { + // Setup + string accountName = "automation"; + string runbookName = "runbook"; + string scheduleName = "schedule"; + + this.mockAutomationClient.Setup(f => f.GetJobSchedule(accountName, runbookName, scheduleName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.RunbookName = runbookName; + this.cmdlet.ScheduleName = scheduleName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByRunbookNameAndScheduleName); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.GetJobSchedule(accountName, runbookName, scheduleName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationScheduledRunbookByRunbookNameSuccessfull() + { + // Setup + string accountName = "automation"; + string runbookName = "runbook"; + + this.mockAutomationClient.Setup(f => f.ListJobSchedulesByRunbookName(accountName, runbookName)).Returns((string a, string b) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.RunbookName = runbookName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByRunbookName); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListJobSchedulesByRunbookName(accountName, runbookName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationScheduledRunbookByScheduleNameSuccessfull() + { + // Setup + string accountName = "automation"; + string scheduleName = "schedule"; + + this.mockAutomationClient.Setup(f => f.ListJobSchedulesByScheduleName(accountName, scheduleName)).Returns((string a, string b) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.ScheduleName = scheduleName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByScheduleName); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListJobSchedulesByScheduleName(accountName, scheduleName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationScheduledRunbookByAllSuccessfull() + { + // Setup + string accountName = "automation"; + + this.mockAutomationClient.Setup(f => f.ListJobSchedules(accountName)).Returns((string a) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByAll); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListJobSchedules(accountName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationVariableTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationVariableTest.cs new file mode 100644 index 000000000000..f1bef99670f0 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationVariableTest.cs @@ -0,0 +1,85 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class GetAzureAutomationVariableTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private GetAzureAutomationVariable cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new GetAzureAutomationVariable + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void GetAzureAutomationVariableByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string variableName = "variable"; + + this.mockAutomationClient.Setup(f => f.GetVariable(accountName, variableName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = variableName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByName); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.GetVariable(accountName, variableName), Times.Once()); + } + + [TestMethod] + public void GetAzureAutomationVariableByAllSuccessfull() + { + // Setup + string accountName = "automation"; + + this.mockAutomationClient.Setup(f => f.ListVariables(accountName)).Returns((string a) => new List()); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByAll); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.ListVariables(accountName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationAccountTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationAccountTest.cs new file mode 100644 index 000000000000..27d819afef70 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationAccountTest.cs @@ -0,0 +1,63 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class NewAzureAutomationAccountTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private NewAzureAutomationAccount cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new NewAzureAutomationAccount + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void NewAzureAutomationAccountByNameSuccessfull() + { + // Setup + string accountName = "account"; + string location = "East US"; + + this.mockAutomationClient.Setup(f => f.CreateAutomationAccount(accountName, location)); + + // Test + this.cmdlet.Name = accountName; + this.cmdlet.Location = location; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.CreateAutomationAccount(accountName, location), Times.Once()); + } + } +} \ No newline at end of file diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationCertificateTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationCertificateTest.cs new file mode 100644 index 000000000000..82d10aa6c4f1 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationCertificateTest.cs @@ -0,0 +1,78 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Generic; +using Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; +using System.Management.Automation; +using System.Security; +using System; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class NewAzureAutomationCertificateTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private NewAzureAutomationCertificate cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new NewAzureAutomationCertificate + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void NewAzureAutomationCertificateByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string certificateName = "certificate"; + string path = "testCert.pfx"; + string password = "password"; + string description = "desc"; + + var secureString = new SecureString(); + Array.ForEach(password.ToCharArray(), secureString.AppendChar); + secureString.MakeReadOnly(); + + this.mockAutomationClient.Setup( + f => f.CreateCertificate(accountName, certificateName, path, secureString, description, false)); + + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = certificateName; + this.cmdlet.Description = description; + this.cmdlet.Path = path; + this.cmdlet.Password = secureString; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.CreateCertificate(accountName, certificateName, path, secureString, description, false), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationConnectionTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationConnectionTest.cs new file mode 100644 index 000000000000..47ee34070811 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationConnectionTest.cs @@ -0,0 +1,74 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Generic; +using Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; +using System.Management.Automation; +using System.Security; +using System; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class NewAzureAutomationConnectionTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private NewAzureAutomationConnection cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new NewAzureAutomationConnection + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void NewAzureAutomationConnectionByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string connectionName = "connection"; + string connectionTypeName = "connectiontype"; + string password = "password"; + string description = "desc"; + + this.mockAutomationClient.Setup( + f => f.CreateConnection(accountName, connectionName, connectionTypeName, null, description)); + + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = connectionName; + this.cmdlet.Description = description; + this.cmdlet.ConnectionTypeName = connectionTypeName; + this.cmdlet.ConnectionFieldValues = null; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.CreateConnection(accountName, connectionName, connectionTypeName, null, description), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationCredentialTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationCredentialTest.cs new file mode 100644 index 000000000000..b36ba048387a --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationCredentialTest.cs @@ -0,0 +1,79 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Generic; +using Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; +using System.Management.Automation; +using System.Security; +using System; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class NewAzureAutomationCredentialTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private NewAzureAutomationCredential cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new NewAzureAutomationCredential + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void NewAzureAutomationCredentialByPathSuccessfull() + { + // Setup + string accountName = "automation"; + string credentialName = "credential"; + string username = "testUser"; + string password = "password"; + string description = "desc"; + + var secureString = new SecureString(); + Array.ForEach(password.ToCharArray(), secureString.AppendChar); + secureString.MakeReadOnly(); + + var value = new PSCredential(username, secureString); + + this.mockAutomationClient.Setup( + f => f.CreateCredential(accountName, credentialName, username, password, description)); + + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = credentialName; + this.cmdlet.Description = description; + this.cmdlet.Value = value; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.CreateCredential(accountName, credentialName, username, password, description), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationModuleTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationModuleTest.cs new file mode 100644 index 000000000000..86b9e7257e67 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationModuleTest.cs @@ -0,0 +1,73 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Generic; +using Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; +using System.Management.Automation; +using System.Security; +using System; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class NewAzureAutomationModuleTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private NewAzureAutomationModule cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new NewAzureAutomationModule + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void NewAzureAutomationModuleSuccessfull() + { + // Setup + string accountName = "automation"; + string moduleName = "credential"; + Uri contentLink = new Uri("http://www.example.com"); + var tags = new Dictionary(); + tags.Add("tag1", "tags2"); + + this.mockAutomationClient.Setup( + f => f.CreateModule(accountName, contentLink, moduleName, tags)); + + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = moduleName; + this.cmdlet.ContentLink = contentLink; + this.cmdlet.Tags = tags; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.CreateModule(accountName, contentLink, moduleName, tags), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationRunbookTest.cs index b81734a10c44..ad9ca1f468d2 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationRunbookTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationRunbookTest.cs @@ -12,11 +12,13 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; using Moq; namespace Microsoft.Azure.Commands.Automation.Test.UnitTests @@ -49,7 +51,7 @@ public void NewAzureAutomationRunbookByPathSuccessfull() string accountName = "automation"; string runbookPath = "runbook.ps1"; string description = "desc"; - var tags = new string[] { "tag1", "tags2" }; + string[] tags = { "tag1", "tags2" }; this.mockAutomationClient.Setup( f => f.CreateRunbookByPath(accountName, runbookPath, description, tags)); @@ -58,6 +60,7 @@ public void NewAzureAutomationRunbookByPathSuccessfull() this.cmdlet.Path = runbookPath; this.cmdlet.Description = description; this.cmdlet.Tags = tags; + this.cmdlet.SetParameterSet("ByPath"); this.cmdlet.ExecuteCmdlet(); // Assert @@ -71,7 +74,7 @@ public void NewAzureAutomationRunbookByNameSuccessfull() string accountName = "automation"; string runbookName = "runbook"; string description = "desc"; - var tags = new string[] { "tag1", "tags2" }; + string[] tags = { "tag1", "tags2" }; this.mockAutomationClient.Setup( f => f.CreateRunbookByName(accountName, runbookName, description, tags)); @@ -80,6 +83,7 @@ public void NewAzureAutomationRunbookByNameSuccessfull() this.cmdlet.Name = runbookName; this.cmdlet.Description = description; this.cmdlet.Tags = tags; + this.cmdlet.SetParameterSet("ByRunbookName"); this.cmdlet.ExecuteCmdlet(); // Assert diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationScheduleTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationScheduleTest.cs index bdc2457c8e7a..09aee79f20ed 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationScheduleTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationScheduleTest.cs @@ -20,6 +20,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; using Moq; namespace Microsoft.Azure.Commands.Automation.Test.UnitTests @@ -39,10 +40,10 @@ public void SetupTest() this.mockAutomationClient = new Mock(); this.mockCommandRuntime = new MockCommandRuntime(); this.cmdlet = new NewAzureAutomationSchedule - { - AutomationClient = this.mockAutomationClient.Object, - CommandRuntime = this.mockCommandRuntime - }; + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; } [TestMethod] @@ -52,17 +53,18 @@ public void NewAzureAutomationScheduleByOneTimeSuccessfull() string accountName = "automation"; string scheduleName = "schedule"; - this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny())); + this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny())); this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; - this.cmdlet.StartTime = DateTime.Now; + this.cmdlet.StartTime = DateTimeOffset.Now; this.cmdlet.OneTime = true; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByOneTime); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient - .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); + .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); } [TestMethod] @@ -71,19 +73,20 @@ public void NewAzureAutomationScheduleByDailySuccessfull() // Setup string accountName = "automation"; string scheduleName = "schedule"; - int dayInterval = 1; + byte dayInterval = 1; - this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny())); + this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny())); this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; - this.cmdlet.StartTime = DateTime.Now; + this.cmdlet.StartTime = DateTimeOffset.Now; this.cmdlet.DayInterval = dayInterval; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByDaily); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient - .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); + .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); } [TestMethod] @@ -92,19 +95,20 @@ public void NewAzureAutomationScheduleByHourlySuccessfull() // Setup string accountName = "automation"; string scheduleName = "schedule"; - int hourInterval = 1; + byte hourInterval = 1; - this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny())); + this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny())); this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; - this.cmdlet.StartTime = DateTime.Now; + this.cmdlet.StartTime = DateTimeOffset.Now; this.cmdlet.HourInterval = hourInterval; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByHourly); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient - .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); + .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); } [TestMethod] @@ -113,24 +117,25 @@ public void NewAzureAutomationScheduleByDailyWithDefaultExpiryTimeDayIntervalSuc // Setup string accountName = "automation"; string scheduleName = "schedule"; - int dayInterval = 1; + byte dayInterval = 1; this.mockAutomationClient - .Setup(f => f.CreateSchedule(accountName, It.IsAny())) - .Returns((string a, DailySchedule s) => s); + .Setup(f => f.CreateSchedule(accountName, It.IsAny())) + .Returns((string a, Schedule s) => s); this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; - this.cmdlet.StartTime = DateTime.Now; + this.cmdlet.StartTime = DateTimeOffset.Now; this.cmdlet.DayInterval = dayInterval; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByDaily); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient - .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); + .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); Assert.AreEqual(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count); - var schedule = (DailySchedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) + var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) .OutputPipeline .FirstOrDefault(); Assert.IsNotNull(schedule); @@ -138,15 +143,20 @@ public void NewAzureAutomationScheduleByDailyWithDefaultExpiryTimeDayIntervalSuc // Test for default values Assert.AreEqual( - Constants.DefaultScheduleExpiryTime, - schedule.ExpiryTime, - "Expiry time is unexpectedly {0}", + Constants.DefaultScheduleExpiryTime, + schedule.ExpiryTime, + "Expiry time is unexpectedly {0}", schedule.ExpiryTime); Assert.AreEqual( - dayInterval, - schedule.DayInterval, - "Day Interval is unexpectedly {0}", - schedule.DayInterval); + dayInterval, + schedule.Interval, + "Day Interval is unexpectedly {0}", + schedule.Interval); + Assert.AreEqual( + ScheduleFrequency.Day, + schedule.Frequency, + "Day Frequency is unexpectedly {0}", + schedule.Frequency); } [TestMethod] @@ -155,24 +165,25 @@ public void NewAzureAutomationScheduleByHourlyWithDefaultExpiryTimeDayIntervalSu // Setup string accountName = "automation"; string scheduleName = "schedule"; - int hourInterval = 1; + byte hourInterval = 1; this.mockAutomationClient - .Setup(f => f.CreateSchedule(accountName, It.IsAny())) - .Returns((string a, HourlySchedule s) => s); + .Setup(f => f.CreateSchedule(accountName, It.IsAny())) + .Returns((string a, Schedule s) => s); this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; - this.cmdlet.StartTime = DateTime.Now; + this.cmdlet.StartTime = DateTimeOffset.Now; this.cmdlet.HourInterval = hourInterval; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByHourly); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient - .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); + .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); Assert.AreEqual(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count); - var schedule = (HourlySchedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) + var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) .OutputPipeline .FirstOrDefault(); Assert.IsNotNull(schedule); @@ -180,105 +191,121 @@ public void NewAzureAutomationScheduleByHourlyWithDefaultExpiryTimeDayIntervalSu // Test for default values Assert.AreEqual( - Constants.DefaultScheduleExpiryTime, - schedule.ExpiryTime, - "Expiry time is unexpectedly {0}", + Constants.DefaultScheduleExpiryTime, + schedule.ExpiryTime, + "Expiry time is unexpectedly {0}", schedule.ExpiryTime); Assert.AreEqual( - hourInterval, - schedule.HourInterval, - "Hour Interval is unexpectedly {0}", - schedule.HourInterval); + hourInterval, + schedule.Interval, + "Hour Interval is unexpectedly {0}", + schedule.Interval); + Assert.AreEqual( + ScheduleFrequency.Hour, + schedule.Frequency, + "Hour Frequency is unexpectedly {0}", + schedule.Frequency); } [TestMethod] - public void NewAzureAutomationScheduleByDailyWithUnspecificedDateTimeKindSuccessfull() + public void NewAzureAutomationScheduleByDailyWithExpiryTimeSuccessfull() { // Setup string accountName = "automation"; string scheduleName = "schedule"; - int dayInterval = 1; - var startTime = DateTime.Now; - var expiryTime = new DateTime(2048, 4, 2); + byte dayInterval = 1; + var startTime = DateTimeOffset.Now; + var expiryTime = startTime.AddDays(10); this.mockAutomationClient - .Setup(f => f.CreateSchedule(accountName, It.IsAny())) - .Returns((string a, DailySchedule s) => s); + .Setup(f => f.CreateSchedule(accountName, It.IsAny())) + .Returns((string a, Schedule s) => s); this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; this.cmdlet.StartTime = startTime; this.cmdlet.ExpiryTime = expiryTime; this.cmdlet.DayInterval = dayInterval; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByDaily); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient - .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); + .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); Assert.AreEqual(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count); - var schedule = (DailySchedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) + var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) .OutputPipeline .FirstOrDefault(); Assert.IsNotNull(schedule); Assert.AreEqual(scheduleName, schedule.Name, "Schedule name is unexpectedly {0}", schedule.Name); - // If startTime or expiryTime is unspecified DateTimeKind, we assume they are local time Assert.AreEqual( - DateTimeKind.Local, - schedule.StartTime.Kind, - "DateTimeKind of start time is unexpectedly {0}", - schedule.StartTime.Kind); + expiryTime, + schedule.ExpiryTime, + "Expiry time is unexpectedly {0}", + schedule.ExpiryTime); + Assert.AreEqual( + dayInterval, + schedule.Interval, + "Day Interval is unexpectedly {0}", + schedule.Interval); Assert.AreEqual( - DateTimeKind.Local, - schedule.ExpiryTime.Kind, - "DateTimeKind of expiry time is unexpectedly {0}", - schedule.ExpiryTime.Kind); + ScheduleFrequency.Day, + schedule.Frequency, + "Day Frequency is unexpectedly {0}", + schedule.Frequency); } [TestMethod] - public void NewAzureAutomationScheduleByHourlyWithUnspecificedDateTimeKindSuccessfull() + public void NewAzureAutomationScheduleByHourlyWithExpiryTimeSuccessfull() { // Setup string accountName = "automation"; string scheduleName = "schedule"; - int hourInterval = 1; - var startTime = DateTime.Now; - var expiryTime = new DateTime(2048, 4, 2); + byte hourInterval = 2; + var startTime = DateTimeOffset.Now; + var expiryTime = startTime.AddDays(10); this.mockAutomationClient - .Setup(f => f.CreateSchedule(accountName, It.IsAny())) - .Returns((string a, HourlySchedule s) => s); + .Setup(f => f.CreateSchedule(accountName, It.IsAny())) + .Returns((string a, Schedule s) => s); this.cmdlet.AutomationAccountName = accountName; this.cmdlet.Name = scheduleName; this.cmdlet.StartTime = startTime; this.cmdlet.ExpiryTime = expiryTime; this.cmdlet.HourInterval = hourInterval; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByHourly); this.cmdlet.ExecuteCmdlet(); // Assert this.mockAutomationClient - .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); + .Verify(f => f.CreateSchedule(accountName, It.IsAny()), Times.Once()); Assert.AreEqual(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count); - var schedule = (HourlySchedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) + var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime) .OutputPipeline .FirstOrDefault(); Assert.IsNotNull(schedule); Assert.AreEqual(scheduleName, schedule.Name, "Schedule name is unexpectedly {0}", schedule.Name); - // If startTime or expiryTime is unspecified DateTimeKind, we assume they are local time + // Test for default values + Assert.AreEqual( + expiryTime, + schedule.ExpiryTime, + "Expiry time is unexpectedly {0}", + schedule.ExpiryTime); Assert.AreEqual( - DateTimeKind.Local, - schedule.StartTime.Kind, - "DateTimeKind of start time is unexpectedly {0}", - schedule.StartTime.Kind); + hourInterval, + schedule.Interval, + "Hour Interval is unexpectedly {0}", + schedule.Interval); Assert.AreEqual( - DateTimeKind.Local, - schedule.ExpiryTime.Kind, - "DateTimeKind of expiry time is unexpectedly {0}", - schedule.ExpiryTime.Kind); + ScheduleFrequency.Hour, + schedule.Frequency, + "Hour Frequency is unexpectedly {0}", + schedule.Frequency); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationVariableTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationVariableTest.cs new file mode 100644 index 000000000000..fb389a1ba048 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationVariableTest.cs @@ -0,0 +1,81 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Linq; +using Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class NewAzureAutomationVariableTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private NewAzureAutomationVariable cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new NewAzureAutomationVariable + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + [Ignore] + public void NewAzureAutomationVariableByPathSuccessfull() + { + // Setup + string accountName = "automation"; + string variableName = "variable"; + string value = "value"; + string description = "desc"; + + var variable = new Variable(); + variable.Name = variableName; + variable.Value = value; + variable.Description = description; + variable.Encrypted = true; + variable.AutomationAccountName = accountName; + + this.mockAutomationClient.Setup( + f => f.CreateVariable(variable)); + + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = variableName; + this.cmdlet.Description = description; + this.cmdlet.Value = value; + this.cmdlet.Encrypted = true; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByName); + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.CreateVariable(variable), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/PublishAzureAutomationRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/PublishAzureAutomationRunbookTest.cs index 69c9fd07c6e0..744ee65ecc56 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/PublishAzureAutomationRunbookTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/PublishAzureAutomationRunbookTest.cs @@ -43,24 +43,6 @@ public void SetupTest() }; } - [TestMethod] - public void PublishAzureAutomationRunbookByIdSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.PublishRunbook(accountName, runbookId)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.PublishRunbook(accountName, runbookId), Times.Once()); - } - [TestMethod] public void PublishAzureAutomationRunbookByNameSuccessfull() { diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RegisterAzureAutomationScheduledJobTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RegisterAzureAutomationScheduledRunbookTest.cs similarity index 70% rename from src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RegisterAzureAutomationScheduledJobTest.cs rename to src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RegisterAzureAutomationScheduledRunbookTest.cs index 2429a48e5a7a..07de5225281e 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RegisterAzureAutomationScheduledJobTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RegisterAzureAutomationScheduledRunbookTest.cs @@ -44,27 +44,7 @@ public void SetupTest() } [TestMethod] - public void RegisterAzureAutomationScheduledRunbookByRunbookIdSuccessfull() - { - // Setup - string accountName = "automation"; - string scheduleName = "schedule"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.RegisterScheduledRunbook(accountName, runbookId, null, scheduleName)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.ScheduleName = scheduleName; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.RegisterScheduledRunbook(accountName, runbookId, null, scheduleName), Times.Once()); - } - - [TestMethod] - public void RegisterAzureAutomationScheduledRunbookByRunbookNameSuccessfull() + public void RegisterAzureAutomationScheduledRunbookSuccessfull() { // Setup string accountName = "automation"; @@ -72,16 +52,16 @@ public void RegisterAzureAutomationScheduledRunbookByRunbookNameSuccessfull() string scheduleName = "schedule"; this.mockAutomationClient.Setup( - f => f.RegisterScheduledRunbook(accountName, runbookName, null, scheduleName)); + f => f.RegisterScheduledRunbook(accountName, runbookName, scheduleName, null)); // Test this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Name = runbookName; + this.cmdlet.RunbookName = runbookName; this.cmdlet.ScheduleName = scheduleName; this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.RegisterScheduledRunbook(accountName, runbookName, null, scheduleName), Times.Once()); + this.mockAutomationClient.Verify(f => f.RegisterScheduledRunbook(accountName, runbookName, scheduleName, null), Times.Once()); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationAccountTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationAccountTest.cs new file mode 100644 index 000000000000..92fb5988a77a --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationAccountTest.cs @@ -0,0 +1,63 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class RemoveAzureAutomationAccountTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private RemoveAzureAutomationAccount cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new RemoveAzureAutomationAccount + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void RemoveAzureAutomationAccountByNameSuccessfull() + { + // Setup + string accountName = "automation"; + + this.mockAutomationClient.Setup(f => f.DeleteAutomationAccount(accountName)); + + // Test + this.cmdlet.Name = accountName; + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.DeleteAutomationAccount(accountName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationCertificateTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationCertificateTest.cs new file mode 100644 index 000000000000..39836e917d9e --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationCertificateTest.cs @@ -0,0 +1,65 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class RemoveAzureAutomationCertificateTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private RemoveAzureAutomationCertificate cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new RemoveAzureAutomationCertificate + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void RemoveAzureAutomationCertificateByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string certificateName = "cert"; + + this.mockAutomationClient.Setup(f => f.DeleteCertificate(accountName, certificateName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = certificateName; + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.DeleteCertificate(accountName, certificateName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationConnectionTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationConnectionTest.cs new file mode 100644 index 000000000000..5c9749629d35 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationConnectionTest.cs @@ -0,0 +1,65 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class RemoveAzureAutomationConnectionTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private RemoveAzureAutomationConnection cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new RemoveAzureAutomationConnection + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void RemoveAzureAutomationConnectionByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string connectionName = "connection"; + + this.mockAutomationClient.Setup(f => f.DeleteConnection(accountName, connectionName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = connectionName; + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.DeleteConnection(accountName, connectionName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationCredentialTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationCredentialTest.cs new file mode 100644 index 000000000000..e49226437030 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationCredentialTest.cs @@ -0,0 +1,65 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class RemoveAzureAutomationCredentialTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private RemoveAzureAutomationCredential cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new RemoveAzureAutomationCredential + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void RemoveAzureAutomationCredentialByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string credentialName = "credential"; + + this.mockAutomationClient.Setup(f => f.DeleteCredential(accountName, credentialName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = credentialName; + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.DeleteCredential(accountName, credentialName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationModuleTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationModuleTest.cs new file mode 100644 index 000000000000..3c5e593edb73 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationModuleTest.cs @@ -0,0 +1,65 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class RemoveAzureAutomationModuleTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private RemoveAzureAutomationModule cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new RemoveAzureAutomationModule + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void RemoveAzureAutomationModuleByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string moduleName = "module"; + + this.mockAutomationClient.Setup(f => f.DeleteModule(accountName, moduleName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = moduleName; + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.DeleteModule(accountName, moduleName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationRunbookTest.cs index ea87566406a3..1d0442047500 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationRunbookTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationRunbookTest.cs @@ -43,25 +43,6 @@ public void SetupTest() }; } - [TestMethod] - public void RemoveAzureAutomationRunbookByIdSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.DeleteRunbook(accountName, runbookId)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.Force = true; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.DeleteRunbook(accountName, runbookId), Times.Once()); - } - [TestMethod] public void RemoveAzureAutomationRunbookByNameSuccessfull() { diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationScheduleTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationScheduleTest.cs index c33c4626ca81..f68ba4b9084c 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationScheduleTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationScheduleTest.cs @@ -43,25 +43,6 @@ public void SetupTest() }; } - [TestMethod] - public void RemoveAzureAutomationScheduleByIdSuccessfull() - { - // Setup - string accountName = "automation"; - var scheduleId = new Guid(); - - this.mockAutomationClient.Setup(f => f.DeleteSchedule(accountName, scheduleId)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = scheduleId; - this.cmdlet.Force = true; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.DeleteSchedule(accountName, scheduleId), Times.Once()); - } - [TestMethod] public void RemoveAzureAutomationScheduleByNameSuccessfull() { diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationVariableTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationVariableTest.cs new file mode 100644 index 000000000000..89f4ea97f004 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationVariableTest.cs @@ -0,0 +1,65 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class RemoveAzureAutomationVariableTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private RemoveAzureAutomationVariable cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new RemoveAzureAutomationVariable + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void RemoveAzureAutomationVariableByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string variableName = "variable"; + + this.mockAutomationClient.Setup(f => f.DeleteVariable(accountName, variableName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = variableName; + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.DeleteVariable(accountName, variableName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/ResumeAzureAutomationJobTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/ResumeAzureAutomationJobTest.cs index b21bd66c857b..0956d9232e0c 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/ResumeAzureAutomationJobTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/ResumeAzureAutomationJobTest.cs @@ -13,8 +13,10 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; @@ -48,7 +50,7 @@ public void ResumeAzureAutomationJobSuccessfull() { // Setup string accountName = "automation"; - var jobId = new Guid(); + Guid jobId = Guid.NewGuid(); this.mockAutomationClient.Setup(f => f.ResumeJob(accountName, jobId)); diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationCredentialTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationCredentialTest.cs new file mode 100644 index 000000000000..f07136bd71a0 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationCredentialTest.cs @@ -0,0 +1,96 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Moq; +using System.Security; +using System.Management.Automation; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class SetAzureAutomationCredentialTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private SetAzureAutomationCredential cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new SetAzureAutomationCredential + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void SetAzureAutomationCredentialByNameSuccessfull() + { + // Setup + string accountName = "automation"; + string credentialName = "credential"; + + this.mockAutomationClient.Setup(f => f.UpdateCredential(accountName, credentialName, null, null, null)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = credentialName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.UpdateCredential(accountName, credentialName, null, null, null), Times.Once()); + } + + [TestMethod] + public void SetAzureAutomationCredentialByNameWithParametersSuccessfull() + { + // Setup + string accountName = "automation"; + string credentialName = "credential"; + string username = "testUser"; + string password = "password"; + string description = "desc"; + + var secureString = new SecureString(); + Array.ForEach(password.ToCharArray(), secureString.AppendChar); + secureString.MakeReadOnly(); + + var value = new PSCredential(username, secureString); + + this.mockAutomationClient.Setup(f => f.UpdateCredential(accountName, credentialName, username, password, description)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = credentialName; + this.cmdlet.Description = description; + this.cmdlet.Value = value; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.UpdateCredential(accountName, credentialName, username, password, description), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookDefinitionTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookDefinitionTest.cs index db89e13875cb..e428ec638686 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookDefinitionTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookDefinitionTest.cs @@ -43,28 +43,6 @@ public void SetupTest() }; } - [TestMethod] - public void SetAzureAutomationRunbookDefinitionByIdSuccessfull() - { - // Setup - string accountName = "automation"; - string runbookPath = "runbook.ps1"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup( - f => f.UpdateRunbookDefinition(accountName, runbookId, runbookPath, false)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.Path = runbookPath; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify( - f => f.UpdateRunbookDefinition(accountName, runbookId, runbookPath, false), - Times.Once()); - } [TestMethod] public void SetAzureAutomationRunbookDefinitionByNameSuccessfull() @@ -89,30 +67,6 @@ public void SetAzureAutomationRunbookDefinitionByNameSuccessfull() Times.Once()); } - [TestMethod] - public void SetAzureAutomationRunbookDefinitionByIdWithOverwriteSuccessfull() - { - // Setup - string accountName = "automation"; - string runbookPath = "runbook.ps1"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup( - f => f.UpdateRunbookDefinition(accountName, runbookId, runbookPath, true)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.Path = runbookPath; - this.cmdlet.Overwrite = true; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify( - f => f.UpdateRunbookDefinition(accountName, runbookId, runbookPath, true), - Times.Once()); - } - [TestMethod] public void SetAzureAutomationRunbookDefinitionByNameWithOverwriteSuccessfull() { diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookTest.cs index dd590ef060ba..8a3dbc8b729b 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationRunbookTest.cs @@ -13,6 +13,7 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -43,24 +44,6 @@ public void SetupTest() }; } - [TestMethod] - public void SetAzureAutomationRunbookByIdSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.UpdateRunbook(accountName, runbookId, null, null, null, null, null)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.UpdateRunbook(accountName, runbookId, null, null, null, null, null), Times.Once()); - } - [TestMethod] public void SetAzureAutomationRunbookByNameSuccessfull() { @@ -68,7 +51,7 @@ public void SetAzureAutomationRunbookByNameSuccessfull() string accountName = "automation"; string runbookName = "runbook"; - this.mockAutomationClient.Setup(f => f.UpdateRunbook(accountName, runbookName, null, null, null, null, null)); + this.mockAutomationClient.Setup(f => f.UpdateRunbook(accountName, runbookName, null, null, null, null)); // Test this.cmdlet.AutomationAccountName = accountName; @@ -76,31 +59,7 @@ public void SetAzureAutomationRunbookByNameSuccessfull() this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.UpdateRunbook(accountName, runbookName, null, null, null, null, null), Times.Once()); - } - - [TestMethod] - public void SetAzureAutomationRunbookByIdWithParametersSuccessfull() - { - // Setup - string accountName = "automation"; - string description = "desc"; - bool? logDebug = true; - bool? logVerbose = false; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.UpdateRunbook(accountName, runbookId, description, null, logDebug, null, logVerbose)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.Description = description; - this.cmdlet.LogDebug = logDebug; - this.cmdlet.LogVerbose = logVerbose; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.UpdateRunbook(accountName, runbookId, description, null, logDebug, null, logVerbose), Times.Once()); + this.mockAutomationClient.Verify(f => f.UpdateRunbook(accountName, runbookName, null, null, null, null), Times.Once()); } [TestMethod] @@ -110,9 +69,9 @@ public void SetAzureAutomationRunbookByNameWithParametersSuccessfull() string accountName = "automation"; string runbookName = "runbook"; bool? logProgress = false; - var tags = new string[] { "tag1", "tag2" }; + string[] tags = { "tag1", "tags2" }; - this.mockAutomationClient.Setup(f => f.UpdateRunbook(accountName, runbookName, null, tags, null, logProgress, null)); + this.mockAutomationClient.Setup(f => f.UpdateRunbook(accountName, runbookName, null, tags, logProgress, null)); // Test this.cmdlet.AutomationAccountName = accountName; @@ -122,7 +81,7 @@ public void SetAzureAutomationRunbookByNameWithParametersSuccessfull() this.cmdlet.ExecuteCmdlet(); // Assert - this.mockAutomationClient.Verify(f => f.UpdateRunbook(accountName, runbookName, null, tags, null, logProgress, null), Times.Once()); + this.mockAutomationClient.Verify(f => f.UpdateRunbook(accountName, runbookName, null, tags, logProgress, null), Times.Once()); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationScheduleTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationScheduleTest.cs index 696073a7323c..0cb30be3bb58 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationScheduleTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationScheduleTest.cs @@ -37,32 +37,10 @@ public void SetupTest() this.mockAutomationClient = new Mock(); this.mockCommandRuntime = new MockCommandRuntime(); this.cmdlet = new SetAzureAutomationSchedule - { - AutomationClient = this.mockAutomationClient.Object, - CommandRuntime = this.mockCommandRuntime - }; - } - - [TestMethod] - public void SetAzureAutomationScheduleByIdSuccessfull() - { - // Setup - string accountName = "automation"; - string description = "desc"; - bool? isEnabled = true; - var scheduleId = new Guid(); - - this.mockAutomationClient.Setup(f => f.UpdateSchedule(accountName, scheduleId, isEnabled, description)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = scheduleId; - this.cmdlet.IsEnabled = isEnabled; - this.cmdlet.Description = description; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.UpdateSchedule(accountName, scheduleId, isEnabled, description), Times.Once()); + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; } [TestMethod] diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StartAzureAutomationRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StartAzureAutomationRunbookTest.cs index 00c01e7053eb..d8f702f6e8ef 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StartAzureAutomationRunbookTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StartAzureAutomationRunbookTest.cs @@ -43,24 +43,6 @@ public void SetupTest() }; } - [TestMethod] - public void StartAzureAutomationRunbookByIdSuccessfull() - { - // Setup - string accountName = "automation"; - var runbookId = new Guid(); - - this.mockAutomationClient.Setup(f => f.StartRunbook(accountName, runbookId, null)); - - // Test - this.cmdlet.AutomationAccountName = accountName; - this.cmdlet.Id = runbookId; - this.cmdlet.ExecuteCmdlet(); - - // Assert - this.mockAutomationClient.Verify(f => f.StartRunbook(accountName, runbookId, null), Times.Once()); - } - [TestMethod] public void StartAzureAutomationRunbookByNameSuccessfull() { diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StopAzureAutomationJobTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StopAzureAutomationJobTest.cs index 5492f685f417..77e7a036fc37 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StopAzureAutomationJobTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/StopAzureAutomationJobTest.cs @@ -13,8 +13,10 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; @@ -48,7 +50,7 @@ public void StopAzureAutomationJobSuccessfull() { // Setup string accountName = "automation"; - var jobId = new Guid(); + Guid jobId = Guid.NewGuid(); this.mockAutomationClient.Setup(f => f.StopJob(accountName, jobId)); diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SuspendAzureAutomationJobTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SuspendAzureAutomationJobTest.cs index b4da25134da0..31d1cefde21f 100644 --- a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SuspendAzureAutomationJobTest.cs +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/SuspendAzureAutomationJobTest.cs @@ -13,8 +13,10 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using Microsoft.Azure.Commands.Automation.Cmdlet; using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; @@ -48,7 +50,7 @@ public void SuspendAzureAutomationJobSuccessfull() { // Setup string accountName = "automation"; - var jobId = new Guid(); + Guid jobId = Guid.NewGuid(); this.mockAutomationClient.Setup(f => f.SuspendJob(accountName, jobId)); diff --git a/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/UnregisterAzureAutomationScheduledRunbookTest.cs b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/UnregisterAzureAutomationScheduledRunbookTest.cs new file mode 100644 index 000000000000..849979cd5604 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/UnregisterAzureAutomationScheduledRunbookTest.cs @@ -0,0 +1,89 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Cmdlet; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class UnregisterAzureAutomationScheduledRunbookTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private UnregisterAzureAutomationScheduledRunbook cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new UnregisterAzureAutomationScheduledRunbook + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void UnregisterAzureAutomationScheduledRunbookByIdSuccessfull() + { + // Setup + string accountName = "automation"; + var jobScheduleId = new Guid(); + + this.mockAutomationClient.Setup(f => f.UnregisterScheduledRunbook(accountName, jobScheduleId)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.JobScheduleId = jobScheduleId; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByJobScheduleId); + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.UnregisterScheduledRunbook(accountName, jobScheduleId), Times.Once()); + } + + [TestMethod] + public void UnregisterAzureAutomationScheduledRunbookByRunbookNameAndScheduleNameSuccessfull() + { + // Setup + string accountName = "automation"; + string runbookName = "runbook"; + string scheduleName = "schedule"; + + this.mockAutomationClient.Setup(f => f.UnregisterScheduledRunbook(accountName, runbookName, scheduleName)); + + // Test + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.RunbookName = runbookName; + this.cmdlet.ScheduleName = scheduleName; + this.cmdlet.SetParameterSet(AutomationCmdletParameterSets.ByRunbookNameAndScheduleName); + this.cmdlet.Force = true; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.UnregisterScheduledRunbook(accountName, runbookName, scheduleName), Times.Once()); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/AzureAutomationBaseCmdlet.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/AzureAutomationBaseCmdlet.cs index 915e5347b8b1..8c188f0e8abd 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/AzureAutomationBaseCmdlet.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/AzureAutomationBaseCmdlet.cs @@ -13,6 +13,7 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Management.Automation; @@ -58,8 +59,7 @@ public IAutomationClient AutomationClient /// /// Gets or sets the automation account name. /// - [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The automation account name.")] + [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] public string AutomationAccountName { get; set; } protected virtual void AutomationExecuteCmdlet() @@ -94,12 +94,44 @@ public override void ExecuteCmdlet() } } + protected bool GenerateCmdletOutput(object result) + { + var ret = true; + + try + { + WriteObject(result); + } + catch (PipelineStoppedException) + { + ret = false; + } + + return ret; + } + + protected bool GenerateCmdletOutput(IEnumerable results) + { + var ret = true; + foreach (var result in results) + { + try + { + WriteObject(result); + } + catch (PipelineStoppedException) + { + ret = false; + } + } + + return ret; + } + private string ParseErrorMessage(string errorMessage) { // The errorMessage is expected to be the error details in JSON format. - // e.g. - // {"odata.error":{"code":"","message":{"lang":"en-US","value":"Runbook definition is invalid. Missing closing '}' in statement block."}}} - // + // e.g. {"code":"NotFound","message":"Certificate not found."} try { using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(XDocument.Load(new StringReader(errorMessage)).Root.Value))) @@ -107,9 +139,9 @@ private string ParseErrorMessage(string errorMessage) var serializer = new DataContractJsonSerializer(typeof(ErrorResponse)); var errorResponse = (ErrorResponse)serializer.ReadObject(memoryStream); - if (!string.IsNullOrWhiteSpace(errorResponse.OdataError.Message.Value)) + if (!string.IsNullOrWhiteSpace(errorResponse.Message)) { - return errorResponse.OdataError.Message.Value; + return errorResponse.Message; } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationAccount.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationAccount.cs index 7a2685c0b7e5..b66f5b301398 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationAccount.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationAccount.cs @@ -52,15 +52,15 @@ public IAutomationClient AutomationClient /// /// Gets or sets the automation account name. /// - [Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "The automation account name.")] + [Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] + [Alias("AutomationAccountName")] + [ValidateNotNullOrEmpty] public string Name { get; set; } /// /// Gets or sets the location. /// - [Parameter(Position = 1, Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "The geo region of the automation account")] + [Parameter(Position = 1, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The geo region of the automation account")] public string Location { get; set; } /// @@ -69,7 +69,7 @@ public IAutomationClient AutomationClient [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] public override void ExecuteCmdlet() { - IEnumerable accounts = this.AutomationClient.ListAutomationAccounts(this.Name, this.Location); + var accounts = this.AutomationClient.ListAutomationAccounts(this.Name, this.Location); this.WriteObject(accounts, true); } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationCertificate.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationCertificate.cs new file mode 100644 index 000000000000..7d4254ad206e --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationCertificate.cs @@ -0,0 +1,60 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets a certificate for automation. + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationCertificate", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(CertificateInfo))] + public class GetAzureAutomationCertificate : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the certificate name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The certificate name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable ret = null; + if (this.ParameterSetName == AutomationCmdletParameterSets.ByCertificateName) + { + ret = new List + { + this.AutomationClient.GetCertificate(this.AutomationAccountName, this.Name) + }; + } + else + { + ret = this.AutomationClient.ListCertificates(this.AutomationAccountName); + } + + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationConnection.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationConnection.cs new file mode 100644 index 000000000000..4f47be72fef7 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationConnection.cs @@ -0,0 +1,71 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets a connection for automation. + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationConnection", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(Connection))] + public class GetAzureAutomationConnection : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the connection name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The connection name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the connection name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionTypeName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The connection name.")] + [ValidateNotNullOrEmpty] + public string ConnectionTypeName { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable ret = null; + if (this.ParameterSetName == AutomationCmdletParameterSets.ByConnectionName) + { + ret = new List + { + this.AutomationClient.GetConnection(this.AutomationAccountName, this.Name) + }; + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByConnectionTypeName) + { + ret = this.AutomationClient.ListConnectionsByType(this.AutomationAccountName, this.ConnectionTypeName); + } + else + { + ret = this.AutomationClient.ListConnections(this.AutomationAccountName); + } + + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationCredential.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationCredential.cs new file mode 100644 index 000000000000..1155918c4b03 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationCredential.cs @@ -0,0 +1,60 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets a Credential for automation. + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationCredential", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(CredentialInfo))] + public class GetAzureAutomationCredential : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the credential name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The credential name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable ret = null; + if (!string.IsNullOrEmpty(this.Name)) + { + ret = new List + { + this.AutomationClient.GetCredential(this.AutomationAccountName, this.Name) + }; + } + else + { + ret = this.AutomationClient.ListCredentials(this.AutomationAccountName); + } + + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJob.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJob.cs index a8c0fdfb8a55..625f40d73de9 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJob.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJob.cs @@ -16,71 +16,53 @@ using System.Collections.Generic; using System.Management.Automation; using System.Security.Permissions; -using Job = Microsoft.Azure.Commands.Automation.Model.Job; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; namespace Microsoft.Azure.Commands.Automation.Cmdlet { /// - /// Gets azure automation jobs for a given account, filtered by one of multiple criteria. + /// Gets a Job object for automation. /// - [Cmdlet(VerbsCommon.Get, "AzureAutomationJob", DefaultParameterSetName = ByAll)] - [OutputType(typeof(Job))] + [Cmdlet(VerbsCommon.Get, "AzureAutomationJob", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(Microsoft.Azure.Commands.Automation.Model.Job))] public class GetAzureAutomationJob : AzureAutomationBaseCmdlet { - /// - /// The get job by job id parameter set. - /// - private const string ByJobId = "ByJobId"; - - /// - /// The get job by runbook id parameter set. - /// - private const string ByRunbookId = "ByRunbookId"; - - /// - /// The get job by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - - /// - /// The get all parameter set. - /// - private const string ByAll = "ByAll"; - - /// - /// Gets or sets the job id. - /// - [Parameter(ParameterSetName = ByJobId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The job id.")] - [Alias("JobId")] - public Guid? Id { get; set; } + /// + /// Gets or sets the job id. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByJobId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The job id.")] + [Alias("JobId")] + public Guid Id { get; set; } + + /// + /// Gets or sets the runbook name of the job. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Mandatory = true, HelpMessage = "The runbook name of the job.")] + [Alias("Name")] + public string RunbookName { get; set; } - /// - /// Gets or sets the runbook id of the job. - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, HelpMessage = "The runbook id of the job.")] - public Guid? RunbookId { get; set; } + /// + /// Gets or sets the status of a job. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Mandatory = false, HelpMessage = "The runbook 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 runbook name of the job. - /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, HelpMessage = "The runbook name of the job.")] - public string RunbookName { get; set; } - - /// - /// Gets or sets the start time filter. - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = false, HelpMessage = "Filter jobs so that job start time >= StartTime.")] - [Parameter(ParameterSetName = ByRunbookName, Mandatory = false, HelpMessage = "Filter jobs so that job start time >= StartTime.")] - [Parameter(ParameterSetName = ByAll, Mandatory = false, HelpMessage = "Filter jobs so that job start time >= StartTime.")] - public DateTime? StartTime { get; set; } - - /// - /// Gets or sets the end time filter. - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = false, HelpMessage = "Filter jobs so that job end time <= EndTime.")] - [Parameter(ParameterSetName = ByRunbookName, Mandatory = false, HelpMessage = "Filter jobs so that job end time <= EndTime.")] - [Parameter(ParameterSetName = ByAll, Mandatory = false, HelpMessage = "Filter jobs so that job end time <= EndTime.")] - public DateTime? EndTime { get; set; } + /// + /// Gets or sets the start time filter. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Mandatory = false, HelpMessage = "Filter jobs so that job start time >= StartTime.")] + [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.ByRunbookName, Mandatory = false, HelpMessage = "Filter jobs so that job end time <= EndTime.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByAll, Mandatory = false, HelpMessage = "Filter jobs so that job end time <= EndTime.")] + public DateTimeOffset? EndTime { get; set; } /// /// Execute this cmdlet. @@ -88,30 +70,25 @@ public class GetAzureAutomationJob : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - IEnumerable jobs; - - if (this.Id.HasValue) - { - // ByJobId - jobs = new List { this.AutomationClient.GetJob(this.AutomationAccountName, this.Id.Value) }; - } - else if (this.RunbookId.HasValue) - { - // ByRunbookId - jobs = this.AutomationClient.ListJobsByRunbookId(this.AutomationAccountName, this.RunbookId.Value, this.StartTime, this.EndTime); - } - else if (this.RunbookName != null) - { - // ByRunbookName - jobs = this.AutomationClient.ListJobsByRunbookName(this.AutomationAccountName, this.RunbookName, this.StartTime, this.EndTime); - } - else - { - // ByAll - jobs = this.AutomationClient.ListJobs(this.AutomationAccountName, this.StartTime, this.EndTime); - } - - this.WriteObject(jobs, true); + IEnumerable jobs; + + if (this.Id != null && !Guid.Empty.Equals(this.Id)) + { + // ByJobId + jobs = new List { this.AutomationClient.GetJob(this.AutomationAccountName, this.Id) }; + } + else if (this.RunbookName != null) + { + // ByRunbookName + jobs = this.AutomationClient.ListJobsByRunbookName(this.AutomationAccountName, this.RunbookName, this.StartTime, this.EndTime, this.Status); + } + else + { + // ByAll + jobs = this.AutomationClient.ListJobs(this.AutomationAccountName, this.StartTime, this.EndTime, this.Status); + } + + this.WriteObject(jobs, true); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJobOutput.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJobOutput.cs index 0d474beefa30..d129635a3aa7 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJobOutput.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationJobOutput.cs @@ -14,7 +14,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Management.Automation; using System.Security.Permissions; using Microsoft.Azure.Commands.Automation.Common; @@ -23,34 +22,24 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet { /// - /// Gets azure automation job output streams for a given account and a given job. + /// Gets azure automation variables for a given account. /// [Cmdlet(VerbsCommon.Get, "AzureAutomationJobOutput")] - [OutputType(typeof(JobStreamItem))] + [OutputType(typeof(JobStream))] public class GetAzureAutomationJobOutput : AzureAutomationBaseCmdlet { /// - /// Gets or sets the job id. + /// Gets or sets the job id /// - [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The job id.")] [Alias("JobId")] + [Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, HelpMessage = "The job name or Id")] public Guid Id { get; set; } - /// - /// Gets or sets the job start time. - /// - [Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "The time from which job output streams should be retrieved.")] - public DateTime StartTime { get; set; } + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The stream type. Defaults to Any.")] + public StreamType Stream { get; set; } - /// - /// Gets or sets the output type. - /// - [Parameter(Mandatory = true)] - [ValidateSet(Constants.JobOutputParameter.Any, Constants.JobOutputParameter.Progress, Constants.JobOutputParameter.Output, Constants.JobOutputParameter.Warning, Constants.JobOutputParameter.Error, Constants.JobOutputParameter.Debug, Constants.JobOutputParameter.Verbose, IgnoreCase = true)] - [Alias("OutputType")] - public string Stream { get; set; } + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Retrieves output created after this time")] + public DateTimeOffset? StartTime { get; set; } /// /// Execute this cmdlet. @@ -58,24 +47,8 @@ public class GetAzureAutomationJobOutput : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - // Assume local time if DateTimeKind.Unspecified - if (this.StartTime.Kind == DateTimeKind.Unspecified) - { - this.StartTime = DateTime.SpecifyKind(this.StartTime, DateTimeKind.Local); - } - - var streamTypeNames = new string[] - { - Constants.JobOutputParameter.Progress, Constants.JobOutputParameter.Output, - Constants.JobOutputParameter.Warning, Constants.JobOutputParameter.Error, - Constants.JobOutputParameter.Debug, Constants.JobOutputParameter.Verbose - }; - string streamTypeName = - streamTypeNames.FirstOrDefault( - name => string.Equals(this.Stream, name, StringComparison.OrdinalIgnoreCase)); - IEnumerable streamItems = this.AutomationClient.ListJobStreamItems( - this.AutomationAccountName, this.Id, this.StartTime, streamTypeName); - this.WriteObject(streamItems, true); + var ret = this.AutomationClient.GetJobStream(this.AutomationAccountName, this.Id, this.StartTime, this.Stream.ToString() ); + this.GenerateCmdletOutput(ret); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationModule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationModule.cs new file mode 100644 index 000000000000..8f1fcc65806c --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationModule.cs @@ -0,0 +1,60 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets a Module for automation. + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationModule", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(Module))] + public class GetAzureAutomationModule : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the module name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The module name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable ret = null; + if (!string.IsNullOrEmpty(this.Name)) + { + ret = new List + { + this.AutomationClient.GetModule(this.AutomationAccountName, this.Name) + }; + } + else + { + ret = this.AutomationClient.ListModules(this.AutomationAccountName); + } + + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbook.cs index 79dd6f9160d7..cb79cc71c67d 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbook.cs @@ -16,6 +16,7 @@ using System.Collections.Generic; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Model; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -23,84 +24,38 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Gets azure automation runbooks for a given account. /// - [Cmdlet(VerbsCommon.Get, "AzureAutomationRunbook", DefaultParameterSetName = ByAll)] + [Cmdlet(VerbsCommon.Get, "AzureAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] [OutputType(typeof(Runbook))] public class GetAzureAutomationRunbook : AzureAutomationBaseCmdlet { /// - /// The get runbook by runbook id parameter set. + /// Gets or sets the runbook name. /// - private const string ByRunbookId = "ByRunbookId"; - - /// - /// The get runbook by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - - /// - /// The get runbook by schedule name parameter set. - /// - private const string ByScheduleName = "ByScheduleName"; - - /// - /// The get all parameter set. - /// - private const string ByAll = "ByAll"; - - /// - /// Gets or sets the runbook Id - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } - - /// - /// Gets or sets the runbook name - /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [Alias("RunbookName")] + [ValidateNotNullOrEmpty] public string Name { get; set; } - /// - /// Gets or sets the runbook name - /// - [Parameter(ParameterSetName = ByScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The schedule name.")] - public string ScheduleName { get; set; } - /// /// Execute this cmdlet. /// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - IEnumerable runbooks; - if (this.Id.HasValue) - { - // ByRunbookId - runbooks = new List - { - this.AutomationClient.GetRunbook( - this.AutomationAccountName, this.Id.Value) - }; - } - else if (this.Name != null) - { - // ByRunbookName - runbooks = new List { this.AutomationClient.GetRunbook(this.AutomationAccountName, this.Name) }; - } - else if (this.ScheduleName != null) + IEnumerable ret = null; + if (this.ParameterSetName == AutomationCmdletParameterSets.ByRunbookName) { - // ByScheduleName - runbooks = this.AutomationClient.ListRunbookByScheduleName( - this.AutomationAccountName, this.ScheduleName); + ret = new List + { + this.AutomationClient.GetRunbook(this.AutomationAccountName, this.Name) + }; } - else + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByAll) { - // ByAll - runbooks = this.AutomationClient.ListRunbooks(this.AutomationAccountName); + ret = this.AutomationClient.ListRunbooks(this.AutomationAccountName); } - this.WriteObject(runbooks, true); + this.GenerateCmdletOutput(ret); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbookDefinition.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbookDefinition.cs index 80022897c454..5902f63ca28c 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbookDefinition.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationRunbookDefinition.cs @@ -16,67 +16,32 @@ 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 System.Globalization; namespace Microsoft.Azure.Commands.Automation.Cmdlet { /// /// Gets azure automation runbook definitions for a given account. /// - [Cmdlet(VerbsCommon.Get, "AzureAutomationRunbookDefinition", DefaultParameterSetName = ByRunbookName)] + [Cmdlet(VerbsCommon.Get, "AzureAutomationRunbookDefinition", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] [OutputType(typeof(RunbookDefinition))] public class GetAzureAutomationRunbookDefinition : AzureAutomationBaseCmdlet { - /// - /// The get runbook defintion by runbook id parameter set. - /// - private const string ByRunbookId = "ByRunbookId"; - - /// - /// The get runbook defintion by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - - /// - /// The get runbook defintion by runbook version id parameter set. - /// - private const string ByVersionId = "ByVersionId"; - - /// - /// The published slot. - /// - private const string Published = "Published"; - - /// - /// The draft slot. - /// - private const string Draft = "Draft"; - - /// - /// Gets or sets the runbook id - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } - /// /// Gets or sets the runbook name /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [Alias("RunbookName")] + [ValidateNotNullOrEmpty] public string Name { get; set; } - /// - /// Gets or sets the runbook version id - /// - [Parameter(ParameterSetName = ByVersionId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook version id.")] - public Guid? VersionId { get; set; } - /// /// Gets or sets the runbook version type /// [Parameter(Mandatory = false, HelpMessage = "Returns the draft or the published runbook version only. If not set, return both.")] - [ValidateSet(Published, Draft)] + [ValidateSet(Constants.Published, Constants.Draft)] public string Slot { get; set; } /// @@ -87,27 +52,8 @@ protected override void AutomationExecuteCmdlet() { bool? isDraft = this.IsDraft(); - IEnumerable runbookDefinitions = null; - if (this.Id.HasValue) - { - // ByRunbookId - runbookDefinitions = this.AutomationClient.ListRunbookDefinitionsByRunbookId( - this.AutomationAccountName, this.Id.Value, isDraft); - } - else if (this.Name != null) - { - // ByRunbookName - runbookDefinitions = - this.AutomationClient.ListRunbookDefinitionsByRunbookName( - this.AutomationAccountName, this.Name, isDraft); - } - else if (this.VersionId.HasValue) - { - // ByVersionId - runbookDefinitions = - this.AutomationClient.ListRunbookDefinitionsByRunbookVersionId( - this.AutomationAccountName, this.VersionId.Value, isDraft); - } + // ByRunbookName + var runbookDefinitions = this.AutomationClient.ListRunbookDefinitionsByRunbookName(this.AutomationAccountName, this.Name, isDraft); this.WriteObject(runbookDefinitions, true); } @@ -124,7 +70,8 @@ protected override void AutomationExecuteCmdlet() if (this.Slot != null) { - isDraft = this.Slot == Draft; + isDraft = (0 == String.Compare(this.Slot, Constants.Draft, CultureInfo.InvariantCulture, + CompareOptions.OrdinalIgnoreCase)); } return isDraft; diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationSchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationSchedule.cs index 7a554e9f6efe..48e8e2b534a4 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationSchedule.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationSchedule.cs @@ -16,6 +16,7 @@ using System.Collections.Generic; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Model; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -23,37 +24,17 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Gets azure automation schedules for a given account. /// - [Cmdlet(VerbsCommon.Get, "AzureAutomationSchedule", DefaultParameterSetName = ByAll)] + [Cmdlet(VerbsCommon.Get, "AzureAutomationSchedule", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] [OutputType(typeof(Schedule))] public class GetAzureAutomationSchedule : AzureAutomationBaseCmdlet { - /// - /// The get schedule by schedule id parameter set. - /// - private const string ByScheduleId = "ByScheduleId"; - - /// - /// The get schedule by schedule name parameter set. - /// - private const string ByScheduleName = "ByScheduleName"; - - /// - /// The get all parameter set. - /// - private const string ByAll = "ByAll"; - - /// - /// Gets or sets the schedule id. - /// - [Parameter(ParameterSetName = ByScheduleId, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The schedule id.")] - public Guid? Id { get; set; } - /// /// Gets or sets the schedule name. /// - [Parameter(ParameterSetName = ByScheduleName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The schedule name.")] + [Alias("ScheduleName")] + [ValidateNotNullOrEmpty] public string Name { get; set; } /// @@ -62,65 +43,21 @@ public class GetAzureAutomationSchedule : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - IEnumerable schedules; - if (this.Id.HasValue) - { - // ByScheduleId - schedules = new List - { - this.AutomationClient.GetSchedule( - this.AutomationAccountName, this.Id.Value) - }; - - } - else if (this.Name != null) + IEnumerable schedules = null; + if (this.ParameterSetName == AutomationCmdletParameterSets.ByName) { - // ByScheduleName schedules = new List { this.AutomationClient.GetSchedule( this.AutomationAccountName, this.Name) }; } - else + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByAll) { - // ByAll schedules = this.AutomationClient.ListSchedules(this.AutomationAccountName); } - this.WriteSchedule(schedules); - } - - /// - /// Writes a OneTimeSchedule, DailySchedule or HourlySchedule to the pipeline. - /// - /// - /// The schedules. - /// - private void WriteSchedule(IEnumerable schedules) - { - foreach (var schedule in schedules) - { - var dailySchedule = schedule as DailySchedule; - if (dailySchedule != null) - { - this.WriteObject(dailySchedule); - continue; - } - - var hourlySchedule = schedule as HourlySchedule; - if (hourlySchedule != null) - { - this.WriteObject(hourlySchedule); - continue; - } - - var oneTimeSchedule = schedule as OneTimeSchedule; - if (oneTimeSchedule != null) - { - this.WriteObject(oneTimeSchedule); - } - } + this.GenerateCmdletOutput(schedules); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationScheduledRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationScheduledRunbook.cs new file mode 100644 index 000000000000..d72d9f0e1fda --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationScheduledRunbook.cs @@ -0,0 +1,92 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets azure automation job schedules for a given account. + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationScheduledRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(JobSchedule))] + public class GetAzureAutomationScheduledRunbook : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the job id. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByJobScheduleId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The job schedule id.")] + public Guid? JobScheduleId { get; set; } + + /// + /// Gets or sets the runbook name of the job. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name of the job schedule.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookNameAndScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name of the job schedule.")] + [Alias("Name")] + public string RunbookName { get; set; } + + /// + /// Gets or sets the runbook name of the job. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The schedule name of the job schedule.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookNameAndScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The schedule name of the job schedule.")] + public string ScheduleName { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable jobSchedules = null; + + if (this.ParameterSetName == AutomationCmdletParameterSets.ByJobScheduleId) + { + jobSchedules = new List + { + this.AutomationClient.GetJobSchedule( + this.AutomationAccountName, this.JobScheduleId .Value) + }; + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByRunbookNameAndScheduleName) + { + jobSchedules = new List + { + this.AutomationClient.GetJobSchedule( + this.AutomationAccountName, this.RunbookName, this.ScheduleName) + }; + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByRunbookName) + { + jobSchedules = this.AutomationClient.ListJobSchedulesByRunbookName(this.AutomationAccountName, this.RunbookName); + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByScheduleName) + { + jobSchedules = this.AutomationClient.ListJobSchedulesByScheduleName(this.AutomationAccountName, this.ScheduleName); + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByAll) + { + jobSchedules = this.AutomationClient.ListJobSchedules(this.AutomationAccountName); + } + + this.GenerateCmdletOutput(jobSchedules); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationVariable.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationVariable.cs new file mode 100644 index 000000000000..a20d9d05ff86 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/GetAzureAutomationVariable.cs @@ -0,0 +1,60 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets azure automation variables for a given account. + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationVariable", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(Variable))] + public class GetAzureAutomationVariable : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the variable name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable ret = null; + if (this.ParameterSetName == AutomationCmdletParameterSets.ByName) + { + ret = new List + { + this.AutomationClient.GetVariable(this.AutomationAccountName, this.Name) + }; + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByAll) + { + ret = this.AutomationClient.ListVariables(this.AutomationAccountName); + } + + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationAccount.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationAccount.cs new file mode 100644 index 000000000000..613f7006d92d --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationAccount.cs @@ -0,0 +1,76 @@ +// ---------------------------------------------------------------------------------- +// +// 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.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 +{ + /// + /// Creates azure automation accounts based on automation account name and location. + /// + [Cmdlet(VerbsCommon.New, "AzureAutomationAccount", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] + [OutputType(typeof(AutomationAccount))] + public class NewAzureAutomationAccount : AzurePSCmdlet + { + /// + /// The automation client. + /// + private IAutomationClient automationClient; + + /// + /// Gets or sets the automation client base. + /// + public IAutomationClient AutomationClient + { + get + { + return this.automationClient = this.automationClient ?? new AutomationClient(CurrentContext.Subscription); + } + + set + { + this.automationClient = value; + } + } + + /// + /// Gets or sets the automation account name. + /// + [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] + [Alias("AutomationAccountName")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the location. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The geo region of the automation account")] + public string Location { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + public override void ExecuteCmdlet() + { + var account = this.AutomationClient.CreateAutomationAccount(this.Name, this.Location); + this.WriteObject(account); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationCertificate.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationCertificate.cs new file mode 100644 index 000000000000..fc1af3f1e86b --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationCertificate.cs @@ -0,0 +1,79 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Create a new Certificate for automation. + /// + [Cmdlet(VerbsCommon.New, "AzureAutomationCertificate", DefaultParameterSetName = AutomationCmdletParameterSets.ByCertificateName)] + [OutputType(typeof(CertificateInfo))] + public class NewAzureAutomationCertificate : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the certificate name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the certificate description. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate description.")] + public string Description { get; set; } + + /// + /// Gets or sets the certificate password. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate password.")] + public SecureString Password { get; set; } + + /// + /// Gets or sets the certificate path. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate file path.")] + public string Path { get; set; } + + /// + /// Gets or sets the certificate exportable Property. + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The exportable property of the certificate.")] + public SwitchParameter Exportable { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + + var createdCertificate = this.AutomationClient.CreateCertificate(this.AutomationAccountName, this.Name, this.Path, this.Password, this.Description, this.Exportable.IsPresent); + + this.WriteObject(createdCertificate); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationConnection.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationConnection.cs new file mode 100644 index 000000000000..0233ef64e484 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationConnection.cs @@ -0,0 +1,76 @@ +// ---------------------------------------------------------------------------------- +// +// 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; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Create a new Connection for automation. + /// + [Cmdlet(VerbsCommon.New, "AzureAutomationConnection", DefaultParameterSetName = AutomationCmdletParameterSets.ByConnectionName)] + [OutputType(typeof(Connection))] + public class NewAzureAutomationConnection : AzureAutomationBaseCmdlet + { + + /// + /// Gets or sets the connection name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The connection name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the connection type name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The connection type name.")] + [ValidateNotNullOrEmpty] + public string ConnectionTypeName { get; set; } + + /// + /// Gets or sets the connection field values. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, Position = 3, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The connection field values.")] + public IDictionary ConnectionFieldValues { get; set; } + + /// + /// Gets or sets the connection description. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, ValueFromPipelineByPropertyName = true, + HelpMessage = "The connection description.")] + public string Description { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + + var createdConnection = this.AutomationClient.CreateConnection(this.AutomationAccountName, this.Name, this.ConnectionTypeName, this.ConnectionFieldValues, this.Description); + + this.WriteObject(createdConnection); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationCredential.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationCredential.cs new file mode 100644 index 000000000000..61f930c47291 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationCredential.cs @@ -0,0 +1,72 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Create a new Credential for automation. + /// + [Cmdlet(VerbsCommon.New, "AzureAutomationCredential", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] + [OutputType(typeof(CredentialInfo))] + public class NewAzureAutomationCredential : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the credential name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The credential name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the credential name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, ValueFromPipelineByPropertyName = true, + HelpMessage = "The credential description.")] + public string Description { get; set; } + + /// + /// Gets or sets the credential UserName. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The credential value.")] + public PSCredential Value { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + string userName = null, password = null; + + if (Value != null) + { + userName = Value.UserName; + password = Value.GetNetworkCredential().Password; + } + + var createdCredential = this.AutomationClient.CreateCredential(this.AutomationAccountName, Name, userName, password, Description); + + this.WriteObject(createdCredential); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationModule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationModule.cs new file mode 100644 index 000000000000..cc8402cd29e2 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationModule.cs @@ -0,0 +1,66 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; +using System.Collections; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Create a new Module for automation. + /// + [Cmdlet(VerbsCommon.New, "AzureAutomationModule")] + [OutputType(typeof(Module))] + public class NewAzureAutomationModule : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the module name. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The module name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the contentLink + /// + [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The ContentLink.")] + [ValidateNotNullOrEmpty] + public Uri ContentLink { get; set; } + + /// + /// Gets or sets the module tags. + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The module tags.")] + [Alias("Tag")] + public IDictionary Tags { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + var createdModule = this.AutomationClient.CreateModule(this.AutomationAccountName, ContentLink, Name, Tags); + + this.WriteObject(createdModule); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationRunbook.cs index d9956d09e70b..72c40ba2746b 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationRunbook.cs @@ -12,34 +12,26 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using System.Collections; 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 { /// - /// Creates an azure automation runbook. + /// Gets azure automation schedules for a given account. /// - [Cmdlet(VerbsCommon.New, "AzureAutomationRunbook", DefaultParameterSetName = ByRunbookName)] - [OutputType(typeof(Runbook))] + [Cmdlet(VerbsCommon.New, "AzureAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] + [OutputType(typeof (Runbook))] public class NewAzureAutomationRunbook : AzureAutomationBaseCmdlet { - /// - /// The create runbook by runbook path parameter set. - /// - private const string ByRunbookPath = "ByRunbookPath"; - - /// - /// The create runbook by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - /// /// Gets or sets the runbook name /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [Alias("RunbookName")] [ValidateNotNullOrEmpty] public string Name { get; set; } @@ -47,7 +39,7 @@ public class NewAzureAutomationRunbook : AzureAutomationBaseCmdlet /// /// Gets or sets the path of the runbook script /// - [Parameter(ParameterSetName = ByRunbookPath, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook file path.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByPath, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook file path.")] [Alias("RunbookPath")] [ValidateNotNullOrEmpty] public string Path { get; set; } @@ -62,6 +54,7 @@ public class NewAzureAutomationRunbook : AzureAutomationBaseCmdlet /// Gets or sets the runbook tags. /// [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook tags.")] + [Alias("Tag")] public string[] Tags { get; set; } /// @@ -70,15 +63,15 @@ public class NewAzureAutomationRunbook : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - Runbook runbook; + Runbook runbook = null; - if (this.Path != null) + if (this.ParameterSetName == AutomationCmdletParameterSets.ByPath) { // ByRunbookPath runbook = this.AutomationClient.CreateRunbookByPath( this.AutomationAccountName, this.ResolvePath(this.Path), this.Description, this.Tags); } - else + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByRunbookName) { // ByRunbookName runbook = this.AutomationClient.CreateRunbookByName( diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationSchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationSchedule.cs index 8d65beb468ff..f96156bdab41 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationSchedule.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationSchedule.cs @@ -23,25 +23,10 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Creates an azure automation Schedule. /// - [Cmdlet(VerbsCommon.New, "AzureAutomationSchedule", DefaultParameterSetName = ByDaily)] + [Cmdlet(VerbsCommon.New, "AzureAutomationSchedule", DefaultParameterSetName = AutomationCmdletParameterSets.ByDaily)] [OutputType(typeof(Schedule))] public class NewAzureAutomationSchedule : AzureAutomationBaseCmdlet { - /// - /// The one time schedule parameter set. - /// - private const string ByOneTime = "ByOneTime"; - - /// - /// The daily schedule parameter set. - /// - private const string ByDaily = "ByDaily"; - - /// - /// The hourly schedule parameter set. - /// - private const string ByHourly = "ByHourly"; - /// /// Initializes a new instance of the class. /// @@ -49,7 +34,7 @@ public NewAzureAutomationSchedule() { this.ExpiryTime = Constants.DefaultScheduleExpiryTime; } - + /// /// Gets or sets the schedule name. /// @@ -61,10 +46,10 @@ public NewAzureAutomationSchedule() /// /// Gets or sets the schedule start time. /// - [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The schedule start time.")] [ValidateNotNull] - public DateTime StartTime { get; set; } + public DateTimeOffset StartTime { get; set; } /// /// Gets or sets the schedule description. @@ -75,29 +60,29 @@ public NewAzureAutomationSchedule() /// /// Gets or sets the switch parameter to create a one time schedule. /// - [Parameter(ParameterSetName = ByOneTime, Mandatory = true, HelpMessage = "To create a one time schedule.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByOneTime, Mandatory = true, HelpMessage = "To create a one time schedule.")] public SwitchParameter OneTime { get; set; } /// /// Gets or sets the schedule expiry time. /// - [Parameter(ParameterSetName = ByDaily, Mandatory = false, HelpMessage = "The schedule expiry time.")] - [Parameter(ParameterSetName = ByHourly, Mandatory = false, HelpMessage = "The schedule expiry time.")] - public DateTime ExpiryTime { get; set; } + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByDaily, Mandatory = false, HelpMessage = "The schedule expiry time.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByHourly, Mandatory = false, HelpMessage = "The schedule expiry time.")] + public DateTimeOffset ExpiryTime { get; set; } /// /// Gets or sets the daily schedule day interval. /// - [Parameter(ParameterSetName = ByDaily, Mandatory = true, HelpMessage = "The daily schedule day interval.")] - [ValidateRange(1, int.MaxValue)] - public int DayInterval { get; set; } + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByDaily, Mandatory = true, HelpMessage = "The daily schedule day interval.")] + [ValidateRange(1, byte.MaxValue)] + public byte DayInterval { get; set; } /// /// Gets or sets the hourly schedule hour interval. /// - [Parameter(ParameterSetName = ByHourly, Mandatory = true, HelpMessage = "The hourly schedule hour interval.")] - [ValidateRange(1, int.MaxValue)] - public int HourInterval { get; set; } + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByHourly, Mandatory = true, HelpMessage = "The hourly schedule hour interval.")] + [ValidateRange(1, byte.MaxValue)] + public byte HourInterval { get; set; } /// /// Execute this cmdlet. @@ -105,61 +90,31 @@ public NewAzureAutomationSchedule() [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - // Assume local time if DateTimeKind.Unspecified - if (this.StartTime.Kind == DateTimeKind.Unspecified) + var schedule = new Schedule { - this.StartTime = DateTime.SpecifyKind(this.StartTime, DateTimeKind.Local); - } + Name = this.Name, + StartTime = this.StartTime, + Description = this.Description, + ExpiryTime = this.ExpiryTime + }; - if (this.ExpiryTime.Kind == DateTimeKind.Unspecified) + if (this.ParameterSetName == AutomationCmdletParameterSets.ByOneTime) { - this.ExpiryTime = DateTime.SpecifyKind(this.ExpiryTime, DateTimeKind.Local); + schedule.Frequency = ScheduleFrequency.Onetime; } - - if (this.OneTime.IsPresent) + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByDaily) { - // ByOneTime - var oneTimeSchedule = new OneTimeSchedule - { - Name = this.Name, - StartTime = this.StartTime, - Description = this.Description, - ExpiryTime = this.ExpiryTime - }; - - Schedule schedule = this.AutomationClient.CreateSchedule(this.AutomationAccountName, oneTimeSchedule); - this.WriteObject(schedule); + schedule.Frequency = ScheduleFrequency.Day; + schedule.Interval = this.DayInterval; } - else if (this.DayInterval >= 1) + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByHourly) { - // ByDaily - var dailySchedule = new DailySchedule - { - Name = this.Name, - StartTime = this.StartTime, - DayInterval = this.DayInterval, - Description = this.Description, - ExpiryTime = this.ExpiryTime - }; - - Schedule schedule = this.AutomationClient.CreateSchedule(this.AutomationAccountName, dailySchedule); - this.WriteObject(schedule); + schedule.Frequency = ScheduleFrequency.Hour; + schedule.Interval = this.HourInterval; } - else if (this.HourInterval >= 1) - { - // ByHourly - var hourlySchedule = new HourlySchedule - { - Name = this.Name, - StartTime = this.StartTime, - HourInterval = this.HourInterval, - Description = this.Description, - ExpiryTime = this.ExpiryTime - }; - Schedule schedule = this.AutomationClient.CreateSchedule(this.AutomationAccountName, hourlySchedule); - this.WriteObject(schedule); - } + Schedule createdSchedule = this.AutomationClient.CreateSchedule(this.AutomationAccountName, schedule); + this.WriteObject(createdSchedule); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationVariable.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationVariable.cs new file mode 100644 index 000000000000..a358c6333296 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationVariable.cs @@ -0,0 +1,75 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets azure automation variables for a given account. + /// + [Cmdlet(VerbsCommon.New, "AzureAutomationVariable")] + [OutputType(typeof(Variable))] + public class NewAzureAutomationVariable : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the variable name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the variable encrypted Property. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The encrypted property of the variable.")] + [ValidateNotNull] + public bool Encrypted { get; set; } + + /// + /// Gets or sets the variable description. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The description of the variable.")] + public string Description { get; set; } + + /// + /// Gets or sets the variable value. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The value of the variable.")] + public object Value { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + Variable variable = new Variable() + { + Name = this.Name, + Encrypted = this.Encrypted, + Description = this.Description, + Value = this.Value, + AutomationAccountName = this.AutomationAccountName + }; + + var ret = this.AutomationClient.CreateVariable(variable); + + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/PublishAzureAutomationRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/PublishAzureAutomationRunbook.cs index 571f446db445..98d4093007bd 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/PublishAzureAutomationRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/PublishAzureAutomationRunbook.cs @@ -15,6 +15,7 @@ using System; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Model; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -22,31 +23,14 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Publishes an azure automation runbook. /// - [Cmdlet(VerbsData.Publish, "AzureAutomationRunbook", DefaultParameterSetName = ByRunbookName)] + [Cmdlet(VerbsData.Publish, "AzureAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] [OutputType(typeof(Runbook))] public class PublishAzureAutomationRunbook : AzureAutomationBaseCmdlet { - /// - /// The publish runbook by runbook id parameter set. - /// - private const string ByRunbookId = "ByRunbookId"; - - /// - /// The publish runbook by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - - /// - /// Gets or sets the runbook id - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } - /// /// Gets or sets the runbook name /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [ValidateNotNullOrEmpty] [Alias("RunbookName")] public string Name { get; set; } @@ -57,16 +41,7 @@ public class PublishAzureAutomationRunbook : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - Runbook runbook; - - if (this.Id.HasValue) - { - runbook = this.AutomationClient.PublishRunbook(this.AutomationAccountName, this.Id.Value); - } - else - { - runbook = this.AutomationClient.PublishRunbook(this.AutomationAccountName, this.Name); - } + var runbook = this.AutomationClient.PublishRunbook(this.AutomationAccountName, this.Name); this.WriteObject(runbook); } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RegisterAzureAutomationScheduledRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RegisterAzureAutomationScheduledRunbook.cs index dd8e7e4a30b0..ba39cb92f5d4 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RegisterAzureAutomationScheduledRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RegisterAzureAutomationScheduledRunbook.cs @@ -12,8 +12,10 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using System.Collections; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Model; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -21,38 +23,46 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Registers an azure automation scheduled runbook. /// - [Cmdlet(VerbsLifecycle.Register, "AzureAutomationScheduledRunbook", DefaultParameterSetName = ByRunbookName)] - [OutputType(typeof(Runbook))] - public class RegisterAzureAutomationScheduledRunbook : StartAzureAutomationRunbookBase + [Cmdlet(VerbsLifecycle.Register, "AzureAutomationScheduledRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] + [OutputType(typeof(JobSchedule))] + public class RegisterAzureAutomationScheduledRunbook : AzureAutomationBaseCmdlet { + /// + /// Gets or sets the runbook name + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookNameAndScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The runbook name.")] + [ValidateNotNullOrEmpty] + [Alias("Name")] + public string RunbookName { get; set; } + /// /// Gets or sets the schedule that will be used to start the runbook. /// - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookNameAndScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the schedule on which the runbook will be started.")] [ValidateNotNullOrEmpty] public string ScheduleName { get; set; } + /// + /// Gets or sets the runbook parameters. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookNameAndScheduleName, Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The runbook parameters.")] + public IDictionary Parameters { get; set; } + /// /// Execute this cmdlet. /// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - Runbook runbook; + JobSchedule jobSchedule; - if (this.Id.HasValue) - { - runbook = this.AutomationClient.RegisterScheduledRunbook( - this.AutomationAccountName, this.Id.Value, this.Parameters, this.ScheduleName); - } - else - { - runbook = this.AutomationClient.RegisterScheduledRunbook( - this.AutomationAccountName, this.Name, this.Parameters, this.ScheduleName); - } + jobSchedule = this.AutomationClient.RegisterScheduledRunbook( + this.AutomationAccountName, this.RunbookName, this.ScheduleName, this.Parameters); - this.WriteObject(runbook); + this.WriteObject(jobSchedule); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationAccount.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationAccount.cs new file mode 100644 index 000000000000..6d6007b56e64 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationAccount.cs @@ -0,0 +1,85 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Generic; +using System.Globalization; +using System.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Properties; +using Microsoft.WindowsAzure.Commands.Utilities.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Removes azure automation accounts, filterd by automation account name and location. + /// + [Cmdlet(VerbsCommon.Remove, "AzureAutomationAccount")] + [OutputType(typeof(AutomationAccount))] + public class RemoveAzureAutomationAccount : AzurePSCmdlet + { + /// + /// The automation client. + /// + private IAutomationClient automationClient; + + /// + /// Gets or sets the automation client base. + /// + public IAutomationClient AutomationClient + { + get + { + return this.automationClient = this.automationClient ?? new AutomationClient(CurrentContext.Subscription); + } + + set + { + this.automationClient = value; + } + } + + /// + /// Gets or sets the automation account name. + /// + [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")] + [Alias("AutomationAccountName")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the switch parameter not to confirm on removing the automaiton account. + /// + [Parameter(Mandatory = false, HelpMessage = "Forces the command to run without asking for user confirmation.")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + public override void ExecuteCmdlet() + { + this.ConfirmAction( + this.Force.IsPresent, + string.Format(CultureInfo.CurrentCulture, Resources.RemovingAzureAutomationResourceWarning, this.Name), + string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationResourceDescription, this.Name), + this.Name, + () => + { + AutomationClient.DeleteAutomationAccount(this.Name); + }); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationCertificate.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationCertificate.cs new file mode 100644 index 000000000000..e7e6b62d1e31 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationCertificate.cs @@ -0,0 +1,58 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Removes a Certificate for automation. + /// + [Cmdlet(VerbsCommon.Remove, "AzureAutomationCertificate", DefaultParameterSetName = AutomationCmdletParameterSets.ByCertificateName)] + public class RemoveAzureAutomationCertificate : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the certificate name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The certificate name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Position = 2, HelpMessage = "Confirm the removal of the certificate")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + ConfirmAction( + Force.IsPresent, + string.Format(Resources.RemovingAzureAutomationResourceWarning, "Certificate"), + string.Format(Resources.RemoveAzureAutomationResourceDescription, "Certificate"), + Name, + () => + { + this.AutomationClient.DeleteCertificate(this.AutomationAccountName, Name); + }); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationConnection.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationConnection.cs new file mode 100644 index 000000000000..0d25b368526e --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationConnection.cs @@ -0,0 +1,58 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Removes a Connection for automation. + /// + [Cmdlet(VerbsCommon.Remove, "AzureAutomationConnection", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] + public class RemoveAzureAutomationConnection : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the connection name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The connection name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 2, HelpMessage = "Confirm the removal of the connection")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + ConfirmAction( + Force.IsPresent, + string.Format(Resources.RemovingAzureAutomationResourceWarning, "Connection"), + string.Format(Resources.RemoveAzureAutomationResourceDescription, "Connection"), + Name, + () => + { + this.AutomationClient.DeleteConnection(this.AutomationAccountName, Name); + }); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationCredential.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationCredential.cs new file mode 100644 index 000000000000..1e1b425aee48 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationCredential.cs @@ -0,0 +1,58 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Removes a Credential for automation. + /// + [Cmdlet(VerbsCommon.Remove, "AzureAutomationCredential", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] + public class RemoveAzureAutomationCredential : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the credential name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The credential name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 2, HelpMessage = "Confirm the removal of the credential")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + ConfirmAction( + Force.IsPresent, + string.Format(Resources.RemovingAzureAutomationResourceWarning, "Credential"), + string.Format(Resources.RemoveAzureAutomationResourceDescription, "Credential"), + Name, + () => + { + this.AutomationClient.DeleteCredential(this.AutomationAccountName, Name); + }); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationModule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationModule.cs new file mode 100644 index 000000000000..065184aee55e --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationModule.cs @@ -0,0 +1,58 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Remove a Module for automation. + /// + [Cmdlet(VerbsCommon.Remove, "AzureAutomationModule", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] + public class RemoveAzureAutomationModule : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the module name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The module name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 2, HelpMessage = "Confirm the removal of the module")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + ConfirmAction( + Force.IsPresent, + string.Format(Resources.RemovingAzureAutomationResourceWarning, "Module"), + string.Format(Resources.RemoveAzureAutomationResourceDescription, "Module"), + Name, + () => + { + this.AutomationClient.DeleteModule(this.AutomationAccountName, Name); + }); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationRunbook.cs index c63f8ef1b0db..c51c7ee46b3e 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationRunbook.cs @@ -16,6 +16,7 @@ using System.Globalization; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Properties; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -23,30 +24,13 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Removes an azure automation runbook. /// - [Cmdlet(VerbsCommon.Remove, "AzureAutomationRunbook", SupportsShouldProcess = true, DefaultParameterSetName = ByRunbookName)] + [Cmdlet(VerbsCommon.Remove, "AzureAutomationRunbook", SupportsShouldProcess = true, DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] public class RemoveAzureAutomationRunbook : AzureAutomationBaseCmdlet { - /// - /// The remove runbook by runbook id parameter set. - /// - private const string ByRunbookId = "ByRunbookId"; - - /// - /// The remove runbook by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - - /// - /// Gets or sets the runbook id - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } - /// /// Gets or sets the runbook name /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [Alias("RunbookName")] [ValidateNotNullOrEmpty] public string Name { get; set; } @@ -54,7 +38,7 @@ public class RemoveAzureAutomationRunbook : AzureAutomationBaseCmdlet /// /// Gets or sets the switch parameter not to confirm on removing the runbook. /// - [Parameter(Mandatory = false, HelpMessage = "Do not confirm on removing the runbook.")] + [Parameter(Mandatory = false, HelpMessage = "Forces the command to run without asking for user confirmation.")] public SwitchParameter Force { get; set; } /// @@ -67,20 +51,11 @@ protected override void AutomationExecuteCmdlet() this.Force.IsPresent, string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationRunbookWarning), string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationRunbookDescription), - this.Id.HasValue ? this.Id.Value.ToString() : this.Name, + this.Name, () => - { - if (this.Id.HasValue) - { - // ByRunbookId - this.AutomationClient.DeleteRunbook(this.AutomationAccountName, this.Id.Value); - } - else - { - // ByRunbookName - this.AutomationClient.DeleteRunbook(this.AutomationAccountName, this.Name); - } - }); + { + AutomationClient.DeleteRunbook(this.AutomationAccountName, this.Name); + }); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationSchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationSchedule.cs index 4f7f1d140875..687380e3a1fa 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationSchedule.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationSchedule.cs @@ -16,6 +16,7 @@ using System.Globalization; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Properties; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -23,37 +24,20 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Removes an azure automation Schedule. /// - [Cmdlet(VerbsCommon.Remove, "AzureAutomationSchedule", SupportsShouldProcess = true, DefaultParameterSetName = ByScheduleName)] + [Cmdlet(VerbsCommon.Remove, "AzureAutomationSchedule", SupportsShouldProcess = true, DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] public class RemoveAzureAutomationSchedule : AzureAutomationBaseCmdlet { - /// - /// The get schedule by schedule id parameter set. - /// - private const string ByScheduleId = "ByScheduleId"; - - /// - /// The get schedule by schedule name parameter set. - /// - private const string ByScheduleName = "ByScheduleName"; - - /// - /// Gets or sets the schedule id. - /// - [Parameter(ParameterSetName = ByScheduleId, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The schedule id.")] - public Guid? Id { get; set; } - /// /// Gets or sets the schedule name. /// - [Parameter(ParameterSetName = ByScheduleName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The schedule name.")] public string Name { get; set; } /// /// Gets or sets the switch parameter not to confirm on removing the schedule. /// - [Parameter(Mandatory = false, HelpMessage = "Do not confirm on removing the schedule.")] + [Parameter(Mandatory = false, HelpMessage = "Forces the command to run without asking for user confirmation.")] public SwitchParameter Force { get; set; } /// @@ -66,19 +50,10 @@ protected override void AutomationExecuteCmdlet() this.Force.IsPresent, string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationScheduleWarning), string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationScheduleDescription), - this.Id.HasValue ? this.Id.Value.ToString() : this.Name, + this.Name, () => { - if (this.Id.HasValue) - { - // ByScheduleId - this.AutomationClient.DeleteSchedule(this.AutomationAccountName, this.Id.Value); - } - else - { - // ByScheduleName - this.AutomationClient.DeleteSchedule(this.AutomationAccountName, this.Name); - } + this.AutomationClient.DeleteSchedule(this.AutomationAccountName, this.Name); }); } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationVariable.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationVariable.cs new file mode 100644 index 000000000000..2c2f28655bee --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationVariable.cs @@ -0,0 +1,59 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Properties; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets azure automation variables for a given account. + /// + [Cmdlet(VerbsCommon.Remove, "AzureAutomationVariable")] + [OutputType(typeof(Variable))] + public class RemoveAzureAutomationVariable : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the variable name. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + [Parameter(Position = 2, HelpMessage = "Confirm the removal of the variable")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + ConfirmAction( + Force.IsPresent, + string.Format(Resources.RemovingAzureAutomationResourceWarning, "Variable"), + string.Format(Resources.RemoveAzureAutomationResourceDescription, "Variable"), + Name, + () => + { + this.AutomationClient.DeleteVariable(this.AutomationAccountName, this.Name); + }); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/ResumeAzureAutomationJob.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/ResumeAzureAutomationJob.cs index 6394deee3264..4ec36ac8045a 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/ResumeAzureAutomationJob.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/ResumeAzureAutomationJob.cs @@ -13,32 +13,35 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; namespace Microsoft.Azure.Commands.Automation.Cmdlet { /// - /// Resumes an azure automation job. + /// Gets a Credential for automation. /// [Cmdlet(VerbsLifecycle.Resume, "AzureAutomationJob")] public class ResumeAzureAutomationJob : AzureAutomationBaseCmdlet { - /// - /// Gets or sets the job id. - /// - [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The job id.")] - [Alias("JobId")] - public Guid Id { get; set; } - - /// - /// Execute this cmdlet. - /// - [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] - protected override void AutomationExecuteCmdlet() - { - this.AutomationClient.ResumeJob(this.AutomationAccountName, this.Id); - } + /// + /// Gets or sets the job id. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The job id.")] + [Alias("JobId")] + public Guid Id { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + this.AutomationClient.ResumeJob(this.AutomationAccountName, this.Id); + } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationCertificate.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationCertificate.cs new file mode 100644 index 000000000000..e485e7790bf9 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationCertificate.cs @@ -0,0 +1,79 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Create a new Certificate for automation. + /// + [Cmdlet(VerbsCommon.Set, "AzureAutomationCertificate", DefaultParameterSetName = AutomationCmdletParameterSets.ByCertificateName)] + [OutputType(typeof(CertificateInfo))] + public class SetAzureAutomationCertificate : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the certificate name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the certificate description. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate description.")] + public string Description { get; set; } + + /// + /// Gets or sets the certificate password. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate password.")] + public SecureString Password { get; set; } + + /// + /// Gets or sets the certificate path. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByCertificateName, Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The certificate file path.")] + public string Path { get; set; } + + /// + /// Gets or sets the certificate exportable Property. + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The exportable property of the certificate.")] + public bool? Exportable { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + + var updateddCertificate = this.AutomationClient.UpdateCertificate(this.AutomationAccountName, this.Name, this.Path, this.Password, this.Description, this.Exportable); + + this.WriteObject(updateddCertificate); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationConnection.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationConnection.cs new file mode 100644 index 000000000000..2c5dfaa71f7a --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationConnection.cs @@ -0,0 +1,66 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Create a new Connection for automation. + /// + [Cmdlet(VerbsCommon.Set, "AzureAutomationConnectionFieldValue", DefaultParameterSetName = AutomationCmdletParameterSets.ByConnectionName)] + [OutputType(typeof(Connection))] + public class SetAzureAutomationConnectionFieldValue : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the connection name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The connection name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the connection field name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The connection field name.")] + public string ConnectionFieldName { get; set; } + + /// + /// Gets or sets the connection field value. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByConnectionName, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The connection field value.")] + public object Value { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + + var updatedConnection = this.AutomationClient.UpdateConnectionFieldValue(this.AutomationAccountName, this.Name, this.ConnectionFieldName, this.Value); + + this.WriteObject(updatedConnection); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationCredential.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationCredential.cs new file mode 100644 index 000000000000..be8cc9b99381 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationCredential.cs @@ -0,0 +1,72 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Sets a Credential for automation. + /// + [Cmdlet(VerbsCommon.Set, "AzureAutomationCredential", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] + [OutputType(typeof(CredentialInfo))] + public class SetAzureAutomationCredential : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the credential name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The credential name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the credential description. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, ValueFromPipelineByPropertyName = true, + HelpMessage = "The credential description.")] + public string Description { get; set; } + + /// + /// Gets or sets the credential Value. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, ValueFromPipelineByPropertyName = true, + HelpMessage = "The credential value.")] + public PSCredential Value { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + string userName = null, password = null; + + if(Value != null) + { + userName = Value.UserName; + password = Value.GetNetworkCredential().Password; + } + + var updatedCredential = this.AutomationClient.UpdateCredential(this.AutomationAccountName, Name, userName, password, Description); + + this.WriteObject(updatedCredential); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationModule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationModule.cs new file mode 100644 index 000000000000..9d9b9c483b56 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationModule.cs @@ -0,0 +1,74 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; +using System.Collections; +using System.Linq; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Sets a Module for automation. + /// + [Cmdlet(VerbsCommon.Set, "AzureAutomationModule", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] + [OutputType(typeof(Module))] + public class SetAzureAutomationModule : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the module name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, + HelpMessage = "The module name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the module tags. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The module tags.")] + [ValidateNotNullOrEmpty] + [Alias("Tag")] + public IDictionary Tags { get; set; } + + /// + /// Gets or sets the contentLink + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The ContentLinkUri.")] + public Uri ContentLinkUri { get; set; } + + /// + /// Gets or sets the contentLinkVersion + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The ContentLink version.")] + public string ContentLinkVersion { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + var updatedModule = this.AutomationClient.UpdateModule(this.AutomationAccountName, Tags, Name, ContentLinkUri, ContentLinkVersion); + + this.WriteObject(updatedModule); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbook.cs index 78291e27ccf5..71cb66a47631 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbook.cs @@ -12,9 +12,10 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using System; +using System.Collections; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Model; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -22,31 +23,14 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Sets an azure automation runbook's configuration values. /// - [Cmdlet(VerbsCommon.Set, "AzureAutomationRunbook", DefaultParameterSetName = ByRunbookName)] + [Cmdlet(VerbsCommon.Set, "AzureAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] [OutputType(typeof(Runbook))] public class SetAzureAutomationRunbook : AzureAutomationBaseCmdlet { - /// - /// The set runbook by runbook id parameter set. - /// - private const string ByRunbookId = "ByRunbookId"; - - /// - /// The set runbook by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - - /// - /// Gets or sets the runbook Id - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } - /// /// Gets or sets the runbook name /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [Alias("RunbookName")] [ValidateNotNullOrEmpty] public string Name { get; set; } @@ -54,36 +38,25 @@ public class SetAzureAutomationRunbook : AzureAutomationBaseCmdlet /// /// Gets or sets the runbook description. /// - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook description.")] + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook description.")] public string Description { get; set; } /// /// Gets or sets the runbook tags. /// - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook tags.")] + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook tags.")] public string[] Tags { get; set; } - /// - /// Gets or sets a value indicating whether debug logging should be turned on or off. - /// - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "Indicate whether debug logging should be turned on or off.")] - public bool? LogDebug { get; set; } - /// /// Gets or sets a value indicating whether progress logging should be turned on or off. /// - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "Indicate whether progress logging should be turned on or off.")] + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Indicate whether progress logging should be turned on or off.")] public bool? LogProgress { get; set; } /// /// Gets or sets a value indicating whether verbose logging should be turned on or off. /// - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "Indicate whether verbose logging should be turned on or off.")] + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Indicate whether verbose logging should be turned on or off.")] public bool? LogVerbose { get; set; } /// @@ -92,31 +65,14 @@ public class SetAzureAutomationRunbook : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - Runbook runbook; - if (this.Id.HasValue) - { - // ByRunbookId - runbook = this.AutomationClient.UpdateRunbook( - this.AutomationAccountName, - this.Id.Value, - this.Description, - this.Tags, - this.LogDebug, - this.LogProgress, - this.LogVerbose); - } - else - { - // ByRunbookName - runbook = this.AutomationClient.UpdateRunbook( - this.AutomationAccountName, - this.Name, - this.Description, - this.Tags, - this.LogDebug, - this.LogProgress, - this.LogVerbose); - } + // ByRunbookName + var runbook = this.AutomationClient.UpdateRunbook( + this.AutomationAccountName, + this.Name, + this.Description, + this.Tags, + this.LogProgress, + this.LogVerbose); this.WriteObject(runbook); } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbookDefinition.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbookDefinition.cs index f33069dc7d4a..bfbc3d91d265 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbookDefinition.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationRunbookDefinition.cs @@ -15,6 +15,7 @@ using System; 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; @@ -23,38 +24,19 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Sets an azure automation runbook definition. /// - [Cmdlet(VerbsCommon.Set, "AzureAutomationRunbookDefinition", DefaultParameterSetName = ByRunbookName)] + [Cmdlet(VerbsCommon.Set, "AzureAutomationRunbookDefinition", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] [OutputType(typeof(RunbookDefinition))] public class SetAzureAutomationRunbookDefinition : AzureAutomationBaseCmdlet { - /// - /// The set runbook definition by runbook id parameter set. - /// - private const string ByRunbookId = "ByRunbookId"; - - /// - /// The set runbook definition by runbook name parameter set. - /// - private const string ByRunbookName = "ByRunbookName"; - /// /// True to overwrite the existing draft runbook definition; false otherwise. /// private bool overwriteExistingRunbookDefinition; - /// - /// Gets or sets the runbook Id - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } - /// /// Gets or sets the runbook name /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [ValidateNotNullOrEmpty] [Alias("RunbookName")] public string Name { get; set; } @@ -62,8 +44,7 @@ public class SetAzureAutomationRunbookDefinition : AzureAutomationBaseCmdlet /// /// Gets or sets the path of the updated runbook script /// - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The path of the updated runbook script.")] + [Parameter(Mandatory = true, Position = 2, ValueFromPipelineByPropertyName = true, HelpMessage = "The path to the updated runbook script.")] [ValidateNotNullOrEmpty] [Alias("RunbookPath")] public string Path { get; set; } @@ -71,8 +52,7 @@ public class SetAzureAutomationRunbookDefinition : AzureAutomationBaseCmdlet /// /// Gets or sets a value indicating whether to overwrite the existing draft runbook definition. /// - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "To overwrite the exisiting draft runbook definition.")] + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Indicates that the new draft runbook overwrites the existing draft, if one exists.")] public SwitchParameter Overwrite { get { return this.overwriteExistingRunbookDefinition; } @@ -85,19 +65,9 @@ public SwitchParameter Overwrite [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - RunbookDefinition runbookDefinition; - if (this.Id.HasValue) - { - // ByRunbookId - runbookDefinition = this.AutomationClient.UpdateRunbookDefinition( - this.AutomationAccountName, this.Id.Value, this.ResolvePath(this.Path), this.Overwrite); - } - else - { - // ByRunbookName - runbookDefinition = this.AutomationClient.UpdateRunbookDefinition( + // ByRunbookName + var runbookDefinition = this.AutomationClient.UpdateRunbookDefinition( this.AutomationAccountName, this.Name, this.ResolvePath(this.Path), this.Overwrite); - } this.WriteObject(runbookDefinition); } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationSchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationSchedule.cs index 702a18895aeb..e66269bad1cb 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationSchedule.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationSchedule.cs @@ -15,6 +15,7 @@ using System; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Model; namespace Microsoft.Azure.Commands.Automation.Cmdlet @@ -22,31 +23,14 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Sets an azure automation schedule. /// - [Cmdlet(VerbsCommon.Set, "AzureAutomationSchedule", DefaultParameterSetName = ByScheduleName)] + [Cmdlet(VerbsCommon.Set, "AzureAutomationSchedule", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)] [OutputType(typeof(Schedule))] public class SetAzureAutomationSchedule : AzureAutomationBaseCmdlet { - /// - /// The get schedule by schedule id parameter set. - /// - private const string ByScheduleId = "ByScheduleId"; - - /// - /// The get schedule by schedule name parameter set. - /// - private const string ByScheduleName = "ByScheduleName"; - - /// - /// Gets or sets the schedule id. - /// - [Parameter(ParameterSetName = ByScheduleId, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The schedule id.")] - public Guid? Id { get; set; } - /// /// Gets or sets the schedule name. /// - [Parameter(ParameterSetName = ByScheduleName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The schedule name.")] [ValidateNotNullOrEmpty] public string Name { get; set; } @@ -55,7 +39,7 @@ public class SetAzureAutomationSchedule : AzureAutomationBaseCmdlet /// Gets or sets the indicator whether the schedule is enabled. /// [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "The schedule description.")] + HelpMessage = "Specifies whether the schedule is enabled. If a schedule is disabled, any runbooks using it will not run on the schedule until it is enabled.")] public bool? IsEnabled { get; set; } /// @@ -71,20 +55,9 @@ public class SetAzureAutomationSchedule : AzureAutomationBaseCmdlet [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - if (this.Id.HasValue) - { - // ByScheduleId - Schedule schedule = this.AutomationClient.UpdateSchedule( - this.AutomationAccountName, this.Id.Value, this.IsEnabled, this.Description); - this.WriteObject(schedule); - } - else - { - // ByScheduleName - Schedule schedule = this.AutomationClient.UpdateSchedule( - this.AutomationAccountName, this.Name, this.IsEnabled, this.Description); - this.WriteObject(schedule); - } + Schedule schedule = this.AutomationClient.UpdateSchedule( + this.AutomationAccountName, this.Name, this.IsEnabled, this.Description); + this.WriteObject(schedule); } } } \ No newline at end of file diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationVariable.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationVariable.cs new file mode 100644 index 000000000000..09fd2993df87 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationVariable.cs @@ -0,0 +1,86 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using Newtonsoft.Json; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Gets azure automation variables for a given account. + /// + [Cmdlet(VerbsCommon.Set, "AzureAutomationVariable")] + [OutputType(typeof(Variable))] + public class SetAzureAutomationVariable : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the variable name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableValue, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")] + [Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableDescription, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the variable encrypted Property. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableValue, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The encrypted property of the variable.")] + [ValidateNotNull] + public bool Encrypted { get; set; } + + /// + /// Gets or sets the variable description. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableDescription, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The description of the variable.")] + public string Description { get; set; } + + /// + /// Gets or sets the variable value. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableValue, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The value of the variable.")] + public object Value { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + Variable variable = new Variable() + { + Name = this.Name, + Description = this.Description, + Encrypted = this.Encrypted, + Value = this.Value, + AutomationAccountName = this.AutomationAccountName + }; + + Variable ret; + if (ParameterSetName == AutomationCmdletParameterSets.UpdateVariableValue) + { + ret = this.AutomationClient.UpdateVariable(variable, VariableUpdateFields.OnlyValue); + } + else + { + ret = this.AutomationClient.UpdateVariable(variable, VariableUpdateFields.OnlyDescription); + } + this.GenerateCmdletOutput(ret); + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StartAzureAutomationRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StartAzureAutomationRunbook.cs index 4140f787ac50..55f50028e24f 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StartAzureAutomationRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StartAzureAutomationRunbook.cs @@ -12,6 +12,8 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using System.Collections; +using Microsoft.Azure.Commands.Automation.Common; using System.Management.Automation; using System.Security.Permissions; using Job = Microsoft.Azure.Commands.Automation.Model.Job; @@ -21,30 +23,33 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet /// /// Starts an Azure automation runbook. /// - [Cmdlet(VerbsLifecycle.Start, "AzureAutomationRunbook", DefaultParameterSetName = ByRunbookName)] + [Cmdlet(VerbsLifecycle.Start, "AzureAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] [OutputType(typeof(Job))] - public class StartAzureAutomationRunbook : StartAzureAutomationRunbookBase + public class StartAzureAutomationRunbook : AzureAutomationBaseCmdlet { + /// + /// Gets or sets the runbook name + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] + [ValidateNotNullOrEmpty] + [Alias("RunbookName")] + public string Name { get; set; } + + /// + /// Gets or sets the runbook parameters. + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook parameters.")] + public IDictionary Parameters { get; set; } + /// /// Execute this cmdlet. /// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - Job job; + Job job = null; - if (this.Id.HasValue) - { - // ByRunbookId - job = this.AutomationClient.StartRunbook( - this.AutomationAccountName, this.Id.Value, this.Parameters); - } - else - { - // ByRunbookName - job = this.AutomationClient.StartRunbook( - this.AutomationAccountName, this.Name, this.Parameters); - } + job = this.AutomationClient.StartRunbook(this.AutomationAccountName, this.Name, this.Parameters); this.WriteObject(job); } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StartAzureAutomationRunbookBase.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StartAzureAutomationRunbookBase.cs deleted file mode 100644 index aaef222b2d61..000000000000 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StartAzureAutomationRunbookBase.cs +++ /dev/null @@ -1,57 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// 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.Management.Automation; - -namespace Microsoft.Azure.Commands.Automation.Cmdlet -{ - public abstract class StartAzureAutomationRunbookBase : AzureAutomationBaseCmdlet - { - /// - /// The start runbook by runbook id parameter set. - /// - protected const string ByRunbookId = "ByRunbookId"; - - /// - /// The start runbook by runbook name parameter set. - /// - protected const string ByRunbookName = "ByRunbookName"; - - /// - /// Gets or sets the runbook Id - /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } - - /// - /// Gets or sets the runbook name - /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook name.")] - [ValidateNotNullOrEmpty] - [Alias("RunbookName")] - public string Name { get; set; } - - /// - /// Gets or sets the runbook parameters. - /// - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook parameters.")] - public IDictionary Parameters { get; set; } - } -} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StopAzureAutomationJob.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StopAzureAutomationJob.cs index 3122cda9f0f7..9dfff694a25e 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StopAzureAutomationJob.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/StopAzureAutomationJob.cs @@ -13,32 +13,35 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; namespace Microsoft.Azure.Commands.Automation.Cmdlet { /// - /// Stops an azure automation job. + /// Gets a Credential for automation. /// [Cmdlet(VerbsLifecycle.Stop, "AzureAutomationJob")] public class StopAzureAutomationJob : AzureAutomationBaseCmdlet { - /// - /// Gets or sets the job id. - /// - [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The job id.")] - [Alias("JobId")] - public Guid Id { get; set; } - - /// - /// Execute this cmdlet. - /// - [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] - protected override void AutomationExecuteCmdlet() - { - this.AutomationClient.StopJob(this.AutomationAccountName, this.Id); - } + /// + /// Gets or sets the job id. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The job id.")] + [Alias("JobId")] + public Guid Id { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + this.AutomationClient.StopJob(this.AutomationAccountName, this.Id); + } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SuspendAzureAutomationJob.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SuspendAzureAutomationJob.cs index f66ba84acbba..2bc84f706217 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SuspendAzureAutomationJob.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SuspendAzureAutomationJob.cs @@ -13,32 +13,35 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Common; namespace Microsoft.Azure.Commands.Automation.Cmdlet { /// - /// Suspends an azure automation job. + /// Gets a Credential for automation. /// [Cmdlet(VerbsLifecycle.Suspend, "AzureAutomationJob")] public class SuspendAzureAutomationJob : AzureAutomationBaseCmdlet { - /// - /// Gets or sets the job id. - /// - [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The job id.")] - [Alias("JobId")] - public Guid Id { get; set; } - - /// - /// Execute this cmdlet. - /// - [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] - protected override void AutomationExecuteCmdlet() - { - this.AutomationClient.SuspendJob(this.AutomationAccountName, this.Id); - } + /// + /// Gets or sets the job id. + /// + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The job id.")] + [Alias("JobId")] + public Guid Id { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + this.AutomationClient.SuspendJob(this.AutomationAccountName, this.Id); + } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/UnregisterAzureAutomationScheduledRunbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/UnregisterAzureAutomationScheduledRunbook.cs index 441e99cb3afc..10c89dcbb703 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/UnregisterAzureAutomationScheduledRunbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Cmdlet/UnregisterAzureAutomationScheduledRunbook.cs @@ -13,74 +13,75 @@ // ---------------------------------------------------------------------------------- using System; +using System.Globalization; using System.Management.Automation; using System.Security.Permissions; +using Microsoft.Azure.Commands.Automation.Common; using Microsoft.Azure.Commands.Automation.Model; +using Microsoft.Azure.Commands.Automation.Properties; namespace Microsoft.Azure.Commands.Automation.Cmdlet { /// /// Unregisters an azure automation scheduled runbook. /// - [Cmdlet(VerbsLifecycle.Unregister, "AzureAutomationScheduledRunbook", DefaultParameterSetName = ByRunbookName)] - [OutputType(typeof(Runbook))] + [Cmdlet(VerbsLifecycle.Unregister, "AzureAutomationScheduledRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByJobScheduleId)] public class UnregisterAzureAutomationScheduledRunbook : AzureAutomationBaseCmdlet { - /// - /// The start runbook by runbook id parameter set. - /// - protected const string ByRunbookId = "ByRunbookId"; - - /// - /// The start runbook by runbook name parameter set. - /// - protected const string ByRunbookName = "ByRunbookName"; - /// /// Gets or sets the runbook Id /// - [Parameter(ParameterSetName = ByRunbookId, Mandatory = true, ValueFromPipelineByPropertyName = true, - HelpMessage = "The runbook id.")] - [Alias("RunbookId")] - public Guid? Id { get; set; } + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByJobScheduleId, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The job schedule id.")] + public Guid? JobScheduleId { get; set; } /// /// Gets or sets the runbook name /// - [Parameter(ParameterSetName = ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookNameAndScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] [ValidateNotNullOrEmpty] - [Alias("RunbookName")] - public string Name { get; set; } + [Alias("Name")] + public string RunbookName { get; set; } /// /// Gets or sets the schedule that will be used to start the runbook. /// - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookNameAndScheduleName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the schedule on which the runbook will be started.")] [ValidateNotNullOrEmpty] public string ScheduleName { get; set; } + /// + /// Gets or sets the switch parameter not to confirm on removing the runbook. + /// + [Parameter(Mandatory = false, HelpMessage = "Forces the command to run without asking for user confirmation.")] + public SwitchParameter Force { get; set; } + /// /// Execute this cmdlet. /// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] protected override void AutomationExecuteCmdlet() { - Runbook runbook; - - if (this.Id.HasValue) - { - runbook = this.AutomationClient.UnregisterScheduledRunbook( - this.AutomationAccountName, this.Id.Value, this.ScheduleName); - } - else - { - runbook = this.AutomationClient.UnregisterScheduledRunbook( - this.AutomationAccountName, this.Name, this.ScheduleName); - } - - this.WriteObject(runbook); + this.ConfirmAction( + this.Force.IsPresent, + string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationJobScheduleWarning), + string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationJobScheduleDescription), + this.JobScheduleId.HasValue ? this.JobScheduleId.Value.ToString() : this.RunbookName, + () => + { + if (this.ParameterSetName == AutomationCmdletParameterSets.ByJobScheduleId) + { + this.AutomationClient.UnregisterScheduledRunbook( + this.AutomationAccountName, this.JobScheduleId.Value); + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByRunbookNameAndScheduleName) + { + this.AutomationClient.UnregisterScheduledRunbook( + this.AutomationAccountName, this.RunbookName, this.ScheduleName); + } + }); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj b/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj index c32c9326e28f..2c50e5a289f4 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj +++ b/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj @@ -65,10 +65,6 @@ ..\..\..\packages\Microsoft.Azure.Common.2.0.1\lib\net45\Microsoft.Azure.Common.NetFramework.dll - - False - ..\..\..\packages\Microsoft.Azure.Management.Automation.0.15.1-preview\lib\net40\Microsoft.Azure.Management.Automation.dll - False ..\..\..\packages\Microsoft.Azure.Management.Resources.2.12.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll @@ -96,6 +92,9 @@ False ..\..\..\packages\Microsoft.WindowsAzure.Management.4.0.1\lib\net40\Microsoft.WindowsAzure.Management.dll + + ..\..\..\packages\Microsoft.WindowsAzure.Management.Automation.1.0.0\lib\net40\Microsoft.WindowsAzure.Management.Automation.dll + ..\..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll True @@ -120,48 +119,76 @@ + + + + + + + + + - - - - - - - + + + + + + + + + + + + + + + + + + - - + + + + + - + + + + + - - + + + + + - - - + - - + + True @@ -175,8 +202,11 @@ + - + + Designer + @@ -188,6 +218,7 @@ ResXFileCodeGenerator Resources.Designer.cs + Designer diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs index ef1d5de7d28e..587497bfb501 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs @@ -1,4 +1,4 @@ -// ---------------------------------------------------------------------------------- +// ---------------------------------------------------------------------------------- // // Copyright Microsoft Corporation // Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,20 +16,36 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; -using System.IO; using System.Linq; -using System.Text; +using System.IO; +using System.Net; +using System.Security; +using System.Security.Cryptography.X509Certificates; using Microsoft.Azure.Commands.Automation.Model; using Microsoft.Azure.Commands.Automation.Properties; -using Microsoft.Azure.Management.Automation; +using Microsoft.WindowsAzure.Management.Automation; +using Microsoft.WindowsAzure.Management.Automation.Models; using Microsoft.WindowsAzure.Commands.Common; -using Microsoft.Azure.Common.Extensions.Models; using Newtonsoft.Json; +using Runbook = Microsoft.Azure.Commands.Automation.Model.Runbook; +using Schedule = Microsoft.Azure.Commands.Automation.Model.Schedule; +using Job = Microsoft.Azure.Commands.Automation.Model.Job; +using Variable = Microsoft.Azure.Commands.Automation.Model.Variable; +using JobStream = Microsoft.Azure.Commands.Automation.Model.JobStream; +using Credential = Microsoft.Azure.Commands.Automation.Model.CredentialInfo; +using Module = Microsoft.Azure.Commands.Automation.Model.Module; +using JobSchedule = Microsoft.Azure.Commands.Automation.Model.JobSchedule; +using Certificate = Microsoft.Azure.Commands.Automation.Model.CertificateInfo; +using Connection = Microsoft.Azure.Commands.Automation.Model.Connection; + namespace Microsoft.Azure.Commands.Automation.Common { - using AutomationManagement = Management.Automation; + using AutomationManagement = WindowsAzure.Management.Automation; + using Microsoft.Azure.Common.Extensions.Models; using Microsoft.Azure.Common.Extensions; + using Hyak.Common; + public class AutomationClient : IAutomationClient { @@ -41,13 +57,13 @@ public AutomationClient() } public AutomationClient(AzureSubscription subscription) - : this(subscription, - AzureSession.ClientFactory.CreateClient(subscription, AzureEnvironment.Endpoint.ServiceManagement)) + : this(subscription, + AzureSession.ClientFactory.CreateClient(subscription, + AzureEnvironment.Endpoint.ServiceManagement)) { } - public AutomationClient( - AzureSubscription subscription, + public AutomationClient(AzureSubscription subscription, AutomationManagement.IAutomationManagementClient automationManagementClient) { Requires.Argument("automationManagementClient", automationManagementClient).NotNull(); @@ -58,307 +74,710 @@ public AutomationClient( public AzureSubscription Subscription { get; private set; } - #region Account Operations + #region Schedule Operations - public IEnumerable ListAutomationAccounts(string automationAccountName, string location) + public Schedule CreateSchedule(string automationAccountName, Schedule schedule) { - if (automationAccountName != null) + var scheduleCreateParameters = new AutomationManagement.Models.ScheduleCreateParameters { - Requires.Argument("AutomationAccountName", automationAccountName).ValidAutomationAccountName(); - } + Name = schedule.Name, + Properties = new AutomationManagement.Models.ScheduleCreateProperties + { + StartTime = schedule.StartTime, + ExpiryTime = schedule.ExpiryTime, + Description = schedule.Description, + Interval = schedule.Interval, + Frequency = schedule.Frequency.ToString() + } + }; - var automationAccounts = new List(); - var cloudServices = new List(this.automationManagementClient.CloudServices.List().CloudServices); + var scheduleCreateResponse = this.automationManagementClient.Schedules.Create( + automationAccountName, + scheduleCreateParameters); - foreach (var cloudService in cloudServices) + return this.GetSchedule(automationAccountName, schedule.Name); + } + + public void DeleteSchedule(string automationAccountName, string scheduleName) + { + try { - automationAccounts.AddRange(cloudService.Resources.Select(resource => new AutomationAccount(cloudService, resource))); + this.automationManagementClient.Schedules.Delete( + automationAccountName, + scheduleName); } - - // RDFE does not support server-side filtering, hence we filter on the client-side. - if (automationAccountName != null) + catch (CloudException cloudException) { - automationAccounts = automationAccounts.Where(account => string.Equals(account.AutomationAccountName, automationAccountName, StringComparison.OrdinalIgnoreCase)).ToList(); - - if (!automationAccounts.Any()) + if (cloudException.Response.StatusCode == HttpStatusCode.NoContent) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.AutomationAccountNotFound)); + throw new ResourceNotFoundException(typeof(Schedule), + string.Format(CultureInfo.CurrentCulture, Resources.ScheduleNotFound, scheduleName)); } - } - if (location != null) - { - automationAccounts = automationAccounts.Where(account => string.Equals(account.Location, location, StringComparison.OrdinalIgnoreCase)).ToList(); + throw; } + } - return automationAccounts; + public Schedule GetSchedule(string automationAccountName, string scheduleName) + { + AutomationManagement.Models.Schedule scheduleModel = this.GetScheduleModel(automationAccountName, + scheduleName); + return this.CreateScheduleFromScheduleModel(automationAccountName, scheduleModel); + } + + public IEnumerable ListSchedules(string automationAccountName) + { + IList scheduleModels = AutomationManagementClient + .ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.Schedules.List( + automationAccountName); + + return new ResponseWithSkipToken( + response, response.Schedules); + }); + + return scheduleModels.Select(scheduleModel => new Schedule(automationAccountName, scheduleModel)); + } + + public Schedule UpdateSchedule(string automationAccountName, string scheduleName, bool? isEnabled, string description) + { + AutomationManagement.Models.Schedule scheduleModel = this.GetScheduleModel(automationAccountName, + scheduleName); + isEnabled = (isEnabled.HasValue) ? isEnabled : scheduleModel.Properties.IsEnabled; + description = description ?? scheduleModel.Properties.Description; + return this.UpdateScheduleHelper(automationAccountName, scheduleName, isEnabled, description); } #endregion #region Runbook Operations - public Runbook CreateRunbookByName(string automationAccountName, string runbookName, string description, string[] tags) + public Runbook GetRunbook(string automationAccountName, string runbookName) { - var runbookScript = string.Format(CultureInfo.InvariantCulture, @"workflow {0}{1}{{{1}}}", runbookName, Environment.NewLine); - using (var streamReader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(runbookScript), false), Encoding.UTF8)) + var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName); + if (runbookModel == null) { - Stream runbookStream = streamReader.BaseStream; - Runbook runbook = this.CreateRunbook(automationAccountName, runbookStream); - this.UpdateRunbook(automationAccountName, runbook.Id, description, tags, null, null, null); - return this.GetRunbook(automationAccountName, runbook.Id); + throw new ResourceCommonException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName)); } + + return new Runbook(automationAccountName, runbookModel); + } + + public IEnumerable ListRunbooks(string automationAccountName) + { + return AutomationManagementClient + .ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.Runbooks.List( + automationAccountName); + return new ResponseWithSkipToken( + response, response.Runbooks); + }).Select(c => new Runbook(automationAccountName, c)); } - public Runbook CreateRunbookByPath(string automationAccountName, string runbookPath, string description, string[] tags) + public Runbook CreateRunbookByName(string automationAccountName, string runbookName, string description, + string[] tags) { - Runbook runbook = this.CreateRunbook(automationAccountName, File.OpenRead(runbookPath)); - this.UpdateRunbook(automationAccountName, runbook.Id, description, tags, null, null, null); - return this.GetRunbook(automationAccountName, runbook.Id); + var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName); + if (runbookModel != null) + { + throw new ResourceCommonException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyExists, runbookName)); + } + + var rdcprop = new RunbookCreateDraftProperties() + { + Description = description, + RunbookType = RunbookTypeEnum.Script, + Draft = new RunbookDraft() + }; + + var rdcparam = new RunbookCreateDraftParameters() { Name = runbookName, Properties = rdcprop, Tags = null }; + + this.automationManagementClient.Runbooks.CreateWithDraft(automationAccountName, rdcparam); + + return this.GetRunbook(automationAccountName, runbookName); } - public void DeleteRunbook(string automationAccountName, Guid runbookId) + public Runbook CreateRunbookByPath(string automationAccountName, string runbookPath, string description, + string[] tags) { - this.automationManagementClient.Runbooks.Delete( - automationAccountName, - runbookId.ToString()); + + var runbookName = Path.GetFileNameWithoutExtension(runbookPath); + + var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName); + if (runbookModel != null) + { + throw new ResourceCommonException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyExists, runbookName)); + } + + var runbook = this.CreateRunbookByName(automationAccountName, runbookName, description, tags); + + var rduprop = new RunbookDraftUpdateParameters() + { + Name = runbookName, + Stream = File.ReadAllText(runbookPath) + }; + + this.automationManagementClient.RunbookDraft.Update(automationAccountName, rduprop); + + return runbook; } public void DeleteRunbook(string automationAccountName, string runbookName) { - Runbook runbook = this.GetRunbook(automationAccountName, runbookName); - this.DeleteRunbook(automationAccountName, runbook.Id); - } - - /// - /// Gets the runbook identified by runbookId, with schedule names expanded. - /// - /// - /// The automation account name. - /// - /// - /// The runbook id. - /// - /// - /// The . - /// - public Runbook GetRunbook(string automationAccountName, Guid runbookId) - { - return new Runbook(this.GetRunbookModel(automationAccountName, runbookId, true)); - } - - /// - /// Gets the runbook identified by runbookId, with schedule names expanded. - /// - /// - /// The automation account name. - /// - /// - /// The runbook name. - /// - /// - /// The . - /// - public Runbook GetRunbook(string automationAccountName, string runbookName) + try + { + this.automationManagementClient.Runbooks.Delete(automationAccountName, runbookName); + } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NoContent) + { + throw new ResourceNotFoundException(typeof(Connection), string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName)); + } + + throw; + } + + } + + public Runbook UpdateRunbook(string automationAccountName, string runbookName, string description, + string[] tags, bool? logProgress, bool? logVerbose) { - return new Runbook(this.GetRunbookModel(automationAccountName, runbookName, true)); + var runbookModel = this.TryGetRunbookModel(automationAccountName, runbookName); + if (runbookModel == null) + { + throw new ResourceCommonException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName)); + } + + var runbookUpdateParameters = new RunbookUpdateParameters(); + runbookUpdateParameters.Name = runbookName; + runbookUpdateParameters.Tags = null; + runbookUpdateParameters.Properties = new RunbookUpdateProperties(); + runbookUpdateParameters.Properties.Description = description ?? runbookModel.Properties.Description; + runbookUpdateParameters.Properties.LogProgress = (logProgress.HasValue) ? logProgress.Value : runbookModel.Properties.LogProgress; + runbookUpdateParameters.Properties.LogVerbose = (logVerbose.HasValue) ? logVerbose.Value : runbookModel.Properties.LogVerbose; + + var runbook = this.automationManagementClient.Runbooks.Update(automationAccountName, runbookUpdateParameters).Runbook; + + return new Runbook(automationAccountName, runbook); } - public IEnumerable ListRunbooks(string automationAccountName) + public RunbookDefinition UpdateRunbookDefinition(string automationAccountName, string runbookName, + string runbookPath, bool overwrite) { - IList runbookModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var listRunbookResponse = - this.automationManagementClient.Runbooks.ListWithSchedules( - automationAccountName, skipToken); - return new ResponseWithSkipToken( - listRunbookResponse, listRunbookResponse.Runbooks); - }); + var runbook = this.TryGetRunbookModel(automationAccountName, runbookName); + if (runbook == null) + { + throw new ResourceNotFoundException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName)); + } + + if ((0 != + String.Compare(runbook.Properties.State, RunbookState.Published, CultureInfo.InvariantCulture, + CompareOptions.IgnoreCase) && overwrite == false)) + { + throw new ResourceCommonException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyHasDraft, runbookName)); + } - return runbookModels.Select(runbookModel => new Runbook(runbookModel)); + this.automationManagementClient.RunbookDraft.Update(automationAccountName, + new RunbookDraftUpdateParameters { Name = runbookName, Stream = File.ReadAllText(runbookPath) }); + + var content = + this.automationManagementClient.RunbookDraft.Content(automationAccountName, runbookName).Stream; + + return new RunbookDefinition(automationAccountName, runbook, content, Constants.Draft); } - public IEnumerable ListRunbookByScheduleName(string automationAccountName, string scheduleName) + public IEnumerable ListRunbookDefinitionsByRunbookName(string automationAccountName, + string runbookName, bool? isDraft) { - AutomationManagement.Models.Schedule scheduleModel = this.GetScheduleModel(automationAccountName, scheduleName); - IList runbookModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => + var ret = new List(); + + var runbook = this.TryGetRunbookModel(automationAccountName, runbookName); + if (runbook == null) + { + throw new ResourceNotFoundException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFound, runbookName)); + } + + var draftContent = String.Empty; + var publishedContent = String.Empty; + + if (0 != String.Compare(runbook.Properties.State, RunbookState.Published, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) && (!isDraft.HasValue || isDraft.Value)) + { + draftContent = this.automationManagementClient.RunbookDraft.Content(automationAccountName, runbookName).Stream; + } + if (0 != String.Compare(runbook.Properties.State, RunbookState.New, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) && (!isDraft.HasValue || !isDraft.Value)) + { + publishedContent = this.automationManagementClient.Runbooks.Content(automationAccountName, runbookName).Stream; + } + + // if no slot specified return both draft and publish content + if (false == isDraft.HasValue) + { + if (false == String.IsNullOrEmpty(draftContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, draftContent, Constants.Draft)); + if (false == String.IsNullOrEmpty(publishedContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, publishedContent, Constants.Published)); + } + else + { + if (true == isDraft.Value) { - var listRunbookResponse = - this.automationManagementClient.Runbooks.ListByScheduleNameWithSchedules( - automationAccountName, - new AutomationManagement.Models.RunbookListByScheduleNameParameters - { - ScheduleName = scheduleModel.Name, - SkipToken = skipToken - }); - return new ResponseWithSkipToken( - listRunbookResponse, listRunbookResponse.Runbooks); - }); - IEnumerable runbooks = runbookModels.Select(runbookModel => new Runbook(runbookModel)); - return runbooks.Where(runbook => runbook.ScheduleNames.Any()); + if (String.IsNullOrEmpty(draftContent)) throw new ResourceCommonException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoDraftVersion, runbookName)); + if (false == String.IsNullOrEmpty(draftContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, draftContent, Constants.Draft)); + } + else + { + if (String.IsNullOrEmpty(publishedContent)) throw new ResourceCommonException(typeof(Runbook), + string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoPublishedVersion, runbookName)); + + if (false == String.IsNullOrEmpty(publishedContent)) ret.Add(new RunbookDefinition(automationAccountName, runbook, publishedContent, Constants.Published)); + } + } + + return ret; } - public Runbook PublishRunbook(string automationAccountName, Guid runbookId) + public Runbook PublishRunbook(string automationAccountName, string runbookName) { - this.automationManagementClient.Runbooks.Publish( + this.automationManagementClient.RunbookDraft.Publish( automationAccountName, - new AutomationManagement.Models.RunbookPublishParameters + new RunbookDraftPublishParameters { - RunbookId = runbookId.ToString(), + Name = runbookName, PublishedBy = Constants.ClientIdentity }); - return this.GetRunbook(automationAccountName, runbookId); - } - - public Runbook PublishRunbook(string automationAccountName, string runbookName) - { - Guid runbookId = this.GetRunbookIdByRunbookName(automationAccountName, runbookName); - return this.PublishRunbook(automationAccountName, runbookId); + return this.GetRunbook(automationAccountName, runbookName); } - public Job StartRunbook(string automationAccountName, Guid runbookId, IDictionary parameters) + public Job StartRunbook(string automationAccountName, string runbookName, IDictionary parameters) { - IEnumerable processedParameters = this.ProcessRunbookParameters(automationAccountName, runbookId, parameters); - var startResponse = this.automationManagementClient.Runbooks.Start( + IDictionary processedParameters = this.ProcessRunbookParameters(automationAccountName, runbookName, parameters); + var job = this.automationManagementClient.Jobs.Create( automationAccountName, - new AutomationManagement.Models.RunbookStartParameters + new JobCreateParameters { - RunbookId = runbookId.ToString(), - Parameters = processedParameters.ToList() - }); + Properties = new JobCreateProperties + { + Runbook = new RunbookAssociationProperty + { + Name = runbookName + }, + Parameters = processedParameters ?? null + } + }).Job; - return this.GetJob(automationAccountName, new Guid(startResponse.JobId)); + return new Job(automationAccountName, job); } - public Job StartRunbook(string automationAccountName, string runbookName, IDictionary parameters) + #endregion + + #region Variables + + public Variable CreateVariable(Variable variable) { - Guid runbookId = this.GetRunbookIdByRunbookName(automationAccountName, runbookName); - return this.StartRunbook(automationAccountName, runbookId, parameters); + bool variableExists = true; + + try + { + this.GetVariable(variable.AutomationAccountName, variable.Name); + } + catch (ResourceNotFoundException) + { + variableExists = false; + } + + if (variableExists) + { + throw new AzureAutomationOperationException(string.Format(CultureInfo.CurrentCulture, + Resources.VariableAlreadyExists, variable.Name)); + } + + var createParams = new AutomationManagement.Models.VariableCreateParameters() + { + Name = variable.Name, + Properties = new AutomationManagement.Models.VariableCreateProperties() + { + Value = PowerShellJsonConverter.Serialize(variable.Value), + Description = variable.Description, + IsEncrypted = variable.Encrypted + } + }; + + var sdkCreatedVariable = + this.automationManagementClient.Variables.Create(variable.AutomationAccountName, createParams).Variable; + + return new Variable(sdkCreatedVariable, variable.AutomationAccountName); } - public Runbook RegisterScheduledRunbook( - string automationAccountName, Guid runbookId, IDictionary parameters, string scheduleName) + public void DeleteVariable(string automationAccountName, string variableName) { - Schedule schedule = this.GetSchedule(automationAccountName, scheduleName); - IEnumerable processedParameters = this.ProcessRunbookParameters(automationAccountName, runbookId, parameters); - this.automationManagementClient.Runbooks.CreateScheduleLink( - automationAccountName, - new AutomationManagement.Models.RunbookCreateScheduleLinkParameters + try + { + this.automationManagementClient.Variables.Delete(automationAccountName, variableName); + } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NoContent) { - RunbookId = runbookId.ToString(), - Parameters = processedParameters.ToList(), - ScheduleId = schedule.Id.ToString() - }); + throw new ResourceNotFoundException(typeof(Variable), + string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, variableName)); + } - return this.GetRunbook(automationAccountName, runbookId); + throw; + } } - public Runbook RegisterScheduledRunbook( - string automationAccountName, string runbookName, IDictionary parameters, string scheduleName) + public Variable UpdateVariable(Variable variable, VariableUpdateFields updateFields) { - Guid runbookId = this.GetRunbookIdByRunbookName(automationAccountName, runbookName); - return this.RegisterScheduledRunbook(automationAccountName, runbookId, parameters, scheduleName); + var existingVariable = this.GetVariable(variable.AutomationAccountName, variable.Name); + + if (existingVariable.Encrypted != variable.Encrypted) + { + throw new ResourceNotFoundException(typeof(Variable), + string.Format(CultureInfo.CurrentCulture, Resources.VariableEncryptionCannotBeChanged, variable.Name, existingVariable.Encrypted)); + } + + var updateParams = new AutomationManagement.Models.VariableUpdateParameters() + { + Name = variable.Name, + }; + + if (updateFields == VariableUpdateFields.OnlyDescription) + { + updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties() + { + Description = variable.Description + }; + } + else + { + updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties() + { + Value = PowerShellJsonConverter.Serialize(variable.Value) + }; + } + + this.automationManagementClient.Variables.Update(variable.AutomationAccountName, updateParams); + + return this.GetVariable(variable.AutomationAccountName, variable.Name); } - public Runbook UpdateRunbook(string automationAccountName, Guid runbookId, string description, string[] tags, bool? logDebug, bool? logProgress, bool? logVerbose) + public Variable GetVariable(string automationAccountName, string name) { - AutomationManagement.Models.Runbook runbookModel = this.GetRunbookModel(automationAccountName, runbookId, false); - return this.UpdateRunbookHelper(automationAccountName, runbookModel, description, tags, logDebug, logProgress, logVerbose); + try + { + var sdkVarible = this.automationManagementClient.Variables.Get(automationAccountName, name).Variable; + + if (sdkVarible != null) + { + return new Variable(sdkVarible, automationAccountName); + } + + throw new ResourceNotFoundException(typeof(Variable), + string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name)); + } + catch (CloudException) + { + throw new ResourceNotFoundException(typeof(Variable), + string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name)); + } } - public Runbook UpdateRunbook(string automationAccountName, string runbookName, string description, string[] tags, bool? logDebug, bool? logProgress, bool? logVerbose) + public IEnumerable ListVariables(string automationAccountName) { - AutomationManagement.Models.Runbook runbookModel = this.GetRunbookModel(automationAccountName, runbookName, false); - return this.UpdateRunbookHelper(automationAccountName, runbookModel, description, tags, logDebug, logProgress, logVerbose); + IList variables = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.Variables.List( + automationAccountName); + return new ResponseWithSkipToken( + response, response.Variables); + }); + + return variables.Select(variable => this.CreateVariableFromVariableModel(variable, automationAccountName)).ToList(); } + #endregion - public Runbook UnregisterScheduledRunbook(string automationAccountName, Guid runbookId, string scheduleName) + #region Credentials + + public CredentialInfo CreateCredential(string automationAccountName, string name, string userName, string password, + string description) { - Schedule schedule = this.GetSchedule(automationAccountName, scheduleName); - this.automationManagementClient.Runbooks.DeleteScheduleLink( - automationAccountName, - new AutomationManagement.Models.RunbookDeleteScheduleLinkParameters - { - RunbookId = runbookId.ToString(), - ScheduleId = - schedule.Id.ToString() - }); - return this.GetRunbook(automationAccountName, runbookId); + var credentialCreateParams = new AutomationManagement.Models.CredentialCreateParameters(); + credentialCreateParams.Name = name; + credentialCreateParams.Properties = new AutomationManagement.Models.CredentialCreateProperties(); + if (description != null) credentialCreateParams.Properties.Description = description; + + Requires.Argument("userName", userName).NotNull(); + Requires.Argument("password", password).NotNull(); + + credentialCreateParams.Properties.UserName = userName; + credentialCreateParams.Properties.Password = password; + + var createdCredential = this.automationManagementClient.PsCredentials.Create(automationAccountName, + credentialCreateParams); + + if (createdCredential == null || createdCredential.StatusCode != HttpStatusCode.Created) + { + new AzureAutomationOperationException(string.Format(Resources.AutomationOperationFailed, "Create", + "credential", name, automationAccountName)); + } + return new CredentialInfo(automationAccountName, createdCredential.Credential); } - public Runbook UnregisterScheduledRunbook(string automationAccountName, string runbookName, string scheduleName) + public CredentialInfo UpdateCredential(string automationAccountName, string name, string userName, string password, + string description) { - Guid runbookId = this.GetRunbookIdByRunbookName(automationAccountName, runbookName); - return this.UnregisterScheduledRunbook(automationAccountName, runbookId, scheduleName); + var exisitngCredential = this.GetCredential(automationAccountName, name); + var credentialUpdateParams = new AutomationManagement.Models.CredentialUpdateParameters(); + credentialUpdateParams.Name = name; + credentialUpdateParams.Properties = new AutomationManagement.Models.CredentialUpdateProperties(); + credentialUpdateParams.Properties.Description = description ?? exisitngCredential.Description; + + credentialUpdateParams.Properties.UserName = userName; + credentialUpdateParams.Properties.Password = password; + + var credential = this.automationManagementClient.PsCredentials.Update(automationAccountName, + credentialUpdateParams); + + if (credential == null || credential.StatusCode != HttpStatusCode.OK) + { + new AzureAutomationOperationException(string.Format(Resources.AutomationOperationFailed, "Update", + "credential", name, automationAccountName)); + } + + var updatedCredential = this.GetCredential(automationAccountName, name); + + return updatedCredential; } - #endregion + public CredentialInfo GetCredential(string automationAccountName, string name) + { + var credential = this.automationManagementClient.PsCredentials.Get(automationAccountName, name).Credential; + if (credential == null) + { + throw new ResourceNotFoundException(typeof(Credential), string.Format(CultureInfo.CurrentCulture, Resources.CredentialNotFound, name)); + } - #region Runbook Definition Operations + return new CredentialInfo(automationAccountName, credential); + } - public IEnumerable ListRunbookDefinitionsByRunbookName(string automationAccountName, string runbookName, bool? isDraft) + private Credential CreateCredentialFromCredentialModel(AutomationManagement.Models.Credential credential) { - Guid runbookId = this.GetRunbookIdByRunbookName(automationAccountName, runbookName); - return this.ListRunbookDefinitionsByValidRunbookId(automationAccountName, runbookId, isDraft); + Requires.Argument("credential", credential).NotNull(); + + return new Credential(null, credential); } - public IEnumerable ListRunbookDefinitionsByRunbookId(string automationAccountName, Guid runbookId, bool? isDraft) + public IEnumerable ListCredentials(string automationAccountName) { - AutomationManagement.Models.Runbook runbookModel = this.GetRunbookModel(automationAccountName, runbookId, false); - return this.ListRunbookDefinitionsByValidRunbookId(automationAccountName, new Guid(runbookModel.Id), isDraft); + IList credentialModels = AutomationManagementClient + .ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.PsCredentials.List(automationAccountName); + return new ResponseWithSkipToken( + response, response.Credentials); + }); + + return credentialModels.Select(c => new Credential(automationAccountName, c)); } - public IEnumerable ListRunbookDefinitionsByRunbookVersionId(string automationAccountName, Guid runbookVersionId, bool? isDraft) + public void DeleteCredential(string automationAccountName, string name) { - AutomationManagement.Models.RunbookVersion runbookVersionModel = this.GetRunbookVersionModel(automationAccountName, runbookVersionId); - if (!isDraft.HasValue || isDraft.Value == runbookVersionModel.IsDraft) + try { - return this.CreateRunbookDefinitionsFromRunbookVersionModels( - automationAccountName, new List { runbookVersionModel }); + var credential = this.automationManagementClient.PsCredentials.Delete(automationAccountName, name); } - else + catch (CloudException cloudException) { - return new List(); + if (cloudException.Response.StatusCode == HttpStatusCode.NotFound) + { + throw new ResourceNotFoundException(typeof(Credential), string.Format(CultureInfo.CurrentCulture, Resources.CredentialNotFound, name)); + } + + throw; } } - public RunbookDefinition UpdateRunbookDefinition(string automationAccountName, Guid runbookId, string runbookPath, bool overwrite) + #endregion + + #region Modules + public Module CreateModule(string automationAccountName, Uri contentLink, string moduleName, + IDictionary tags) { - return this.UpdateRunbookDefinition(automationAccountName, runbookId, File.OpenRead(runbookPath), overwrite); + IDictionary moduleTags = null; + if (tags != null) moduleTags = tags.Cast().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString()); + var createdModule = this.automationManagementClient.Modules.Create(automationAccountName, + new AutomationManagement.Models.ModuleCreateParameters() + { + Name = moduleName, + Tags = moduleTags, + Properties = new AutomationManagement.Models.ModuleCreateProperties() + { + ContentLink = new AutomationManagement.Models.ContentLink() + { + Uri = contentLink, + ContentHash = null, + Version = null + } + }, + }); + + return this.GetModule(automationAccountName, moduleName); } - public RunbookDefinition UpdateRunbookDefinition(string automationAccountName, string runbookName, string runbookPath, bool overwrite) + public Module GetModule(string automationAccountName, string name) { - Guid runbookId = this.GetRunbookIdByRunbookName(automationAccountName, runbookName); - return this.UpdateRunbookDefinition(automationAccountName, runbookId, runbookPath, overwrite); + try + { + var module = this.automationManagementClient.Modules.Get(automationAccountName, name).Module; + return new Module(automationAccountName, module); + } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NotFound) + { + throw new ResourceNotFoundException(typeof(Module), string.Format(CultureInfo.CurrentCulture, Resources.ModuleNotFound, name)); + } + + throw; + } } - #endregion + public IEnumerable ListModules(string automationAccountName) + { + IList modulesModels = AutomationManagementClient + .ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.Modules.List(automationAccountName); + return new ResponseWithSkipToken( + response, response.Modules); + }); - #region Job Operations + return modulesModels.Select(c => new Module(automationAccountName, c)); + } - public Job GetJob(string automationAccountName, Guid jobId) + public Module UpdateModule(string automationAccountName, IDictionary tags, string name, Uri contentLinkUri, string contentLinkVersion) { - return new Job(this.GetJobModel(automationAccountName, jobId)); + var moduleModel = this.automationManagementClient.Modules.Get(automationAccountName, name).Module; + if(tags != null && contentLinkUri != null) + { + var moduleCreateParameters = new AutomationManagement.Models.ModuleCreateParameters(); + + moduleCreateParameters.Name = name; + moduleCreateParameters.Tags = tags.Cast().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString()); + + moduleCreateParameters.Properties = new ModuleCreateProperties(); + moduleCreateParameters.Properties.ContentLink = new AutomationManagement.Models.ContentLink(); + moduleCreateParameters.Properties.ContentLink.Uri = contentLinkUri; + moduleCreateParameters.Properties.ContentLink.Version = + (String.IsNullOrWhiteSpace(contentLinkVersion)) + ? Guid.NewGuid().ToString() + : contentLinkVersion; + + this.automationManagementClient.Modules.Create(automationAccountName, + moduleCreateParameters); + + } + else if (contentLinkUri != null) + { + var moduleUpdateParameters = new AutomationManagement.Models.ModuleUpdateParameters(); + + moduleUpdateParameters.Name = name; + moduleUpdateParameters.Properties = new ModuleUpdateProperties(); + moduleUpdateParameters.Properties.ContentLink = new AutomationManagement.Models.ContentLink(); + moduleUpdateParameters.Properties.ContentLink.Uri = contentLinkUri; + moduleUpdateParameters.Properties.ContentLink.Version = + (String.IsNullOrWhiteSpace(contentLinkVersion)) + ? Guid.NewGuid().ToString() + : contentLinkVersion; + + moduleUpdateParameters.Tags = moduleModel.Tags; + + this.automationManagementClient.Modules.Update(automationAccountName, moduleUpdateParameters); + } + else if(tags != null) + { + var moduleUpdateParameters = new AutomationManagement.Models.ModuleUpdateParameters(); + + moduleUpdateParameters.Name = name; + moduleUpdateParameters.Tags = tags.Cast().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString()); + moduleUpdateParameters.Properties = new ModuleUpdateProperties(); + + this.automationManagementClient.Modules.Update(automationAccountName, moduleUpdateParameters); + } + + var updatedModule = this.automationManagementClient.Modules.Get(automationAccountName, name).Module; + return new Module(automationAccountName, updatedModule); } - public IEnumerable ListJobs(string automationAccountName, DateTime? startTime, DateTime? endTime) + public void DeleteModule(string automationAccountName, string name) { - // Assume local time if DateTimeKind.Unspecified - if (startTime.HasValue && startTime.Value.Kind == DateTimeKind.Unspecified) + try + { + var module = this.automationManagementClient.Modules.Delete(automationAccountName, name); + } + catch (CloudException cloudException) { - startTime = DateTime.SpecifyKind(startTime.Value, DateTimeKind.Local); + if (cloudException.Response.StatusCode == HttpStatusCode.NoContent) + { + throw new ResourceNotFoundException(typeof(Module), string.Format(CultureInfo.CurrentCulture, Resources.ModuleNotFound, name)); + } + + throw; } + } + + #endregion - if (endTime.HasValue && endTime.Value.Kind == DateTimeKind.Unspecified) + #region Jobs + public IEnumerable GetJobStream(string automationAccountName, Guid jobId, DateTimeOffset? time, + string streamType) + { + var listParams = new AutomationManagement.Models.JobStreamListParameters(); + + if (time.HasValue) { - endTime = DateTime.SpecifyKind(endTime.Value, DateTimeKind.Local); + listParams.Time = this.FormatDateTime(time.Value); } + if (streamType != null) + { + listParams.StreamType = streamType; + } + + var jobStreams = this.automationManagementClient.JobStreams.List(automationAccountName, jobId, listParams).JobStreams; + return jobStreams.Select(stream => this.CreateJobStreamFromJobStreamModel(stream, automationAccountName, jobId)).ToList(); + } + + public Job GetJob(string automationAccountName, Guid Id) + { + var job = this.automationManagementClient.Jobs.Get(automationAccountName, Id).Job; + if (job == null) + { + throw new ResourceNotFoundException(typeof(Job), + string.Format(CultureInfo.CurrentCulture, Resources.JobNotFound, Id)); + } + + return new Job(automationAccountName, job); + } + + public IEnumerable ListJobsByRunbookName(string automationAccountName, string runbookName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + { IEnumerable jobModels; if (startTime.HasValue && endTime.HasValue) @@ -367,45 +786,120 @@ public IEnumerable ListJobs(string automationAccountName, DateTime? startTi skipToken => { var response = - this.automationManagementClient.Jobs.ListFilteredByStartTimeEndTime( + this.automationManagementClient.Jobs.List( automationAccountName, new AutomationManagement.Models.JobListParameters { - StartTime = this.FormatDateTime(startTime.Value), - EndTime = this.FormatDateTime(endTime.Value), - SkipToken = skipToken + StartTime = FormatDateTime(startTime.Value), + EndTime = FormatDateTime(endTime.Value), + RunbookName = runbookName, + Status = jobStatus, }); return new ResponseWithSkipToken(response, response.Jobs); }); } else if (startTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.Jobs.List( + automationAccountName, + new AutomationManagement.Models.JobListParameters + { + StartTime = FormatDateTime(startTime.Value), + RunbookName = runbookName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else if (endTime.HasValue) + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = + this.automationManagementClient.Jobs.List( + automationAccountName, + new AutomationManagement.Models.JobListParameters + { + EndTime = FormatDateTime(endTime.Value), + RunbookName = runbookName, + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + else + { + jobModels = AutomationManagementClient.ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.Jobs.List( + automationAccountName, + new AutomationManagement.Models.JobListParameters + { + Status = jobStatus, + RunbookName = runbookName + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } + + return jobModels.Select(jobModel => new Job(automationAccountName, jobModel)); + } + + public IEnumerable ListJobs(string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus) + { + IEnumerable jobModels; + + if (startTime.HasValue && endTime.HasValue) { jobModels = AutomationManagementClient.ContinuationTokenHandler( skipToken => { var response = - this.automationManagementClient.Jobs.ListFilteredByStartTime( + this.automationManagementClient.Jobs.List( automationAccountName, new AutomationManagement.Models.JobListParameters { - StartTime = this.FormatDateTime(startTime.Value), - SkipToken = skipToken + 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.Jobs.List( + automationAccountName, + new AutomationManagement.Models.JobListParameters + { + StartTime = FormatDateTime(startTime.Value), + Status = jobStatus, + }); + return new ResponseWithSkipToken(response, response.Jobs); + }); + } else if (endTime.HasValue) { jobModels = AutomationManagementClient.ContinuationTokenHandler( skipToken => { var response = - this.automationManagementClient.Jobs.ListFilteredByStartTime( + this.automationManagementClient.Jobs.List( automationAccountName, new AutomationManagement.Models.JobListParameters { - EndTime = this.FormatDateTime(endTime.Value), - SkipToken = skipToken + EndTime = FormatDateTime(endTime.Value), + Status = jobStatus, }); return new ResponseWithSkipToken(response, response.Jobs); }); @@ -417,529 +911,589 @@ public IEnumerable ListJobs(string automationAccountName, DateTime? startTi { var response = this.automationManagementClient.Jobs.List( automationAccountName, - new AutomationManagement.Models.JobListParameters { SkipToken = skipToken, }); + new AutomationManagement.Models.JobListParameters { Status = jobStatus }); return new ResponseWithSkipToken(response, response.Jobs); }); } - return jobModels.Select(jobModel => new Job(jobModel)); + return jobModels.Select(jobModel => new Job(automationAccountName, jobModel)); } - public IEnumerable ListJobsByRunbookId(string automationAccountName, Guid runbookId, DateTime? startTime, DateTime? endTime) + public void ResumeJob(string automationAccountName, Guid id) { - AutomationManagement.Models.Runbook runbookModel = this.GetRunbookModel(automationAccountName, runbookId, false); - return this.ListJobsByValidRunbookId(automationAccountName, new Guid(runbookModel.Id), startTime, endTime); + this.automationManagementClient.Jobs.Resume(automationAccountName, id); } - public IEnumerable ListJobsByRunbookName(string automationAccountName, string runbookName, DateTime? startTime, DateTime? endTime) + public void StopJob(string automationAccountName, Guid id) { - Guid runbookId = this.GetRunbookIdByRunbookName(automationAccountName, runbookName); - return this.ListJobsByValidRunbookId(automationAccountName, runbookId, startTime, endTime); + this.automationManagementClient.Jobs.Stop(automationAccountName, id); } - public void ResumeJob(string automationAccountName, Guid jobId) + public void SuspendJob(string automationAccountName, Guid id) { - this.automationManagementClient.Jobs.Resume( - automationAccountName, - jobId.ToString()); + this.automationManagementClient.Jobs.Suspend(automationAccountName, id); } - public void StopJob(string automationAccountName, Guid jobId) - { - this.automationManagementClient.Jobs.Stop( - automationAccountName, - jobId.ToString()); - } + #endregion + + #region Account Operations - public void SuspendJob(string automationAccountName, Guid jobId) + public IEnumerable ListAutomationAccounts(string automationAccountName, string location) { - this.automationManagementClient.Jobs.Suspend( - automationAccountName, - jobId.ToString()); - } + if (automationAccountName != null) + { + Requires.Argument("AutomationAccountName", automationAccountName).ValidAutomationAccountName(); + } - #endregion + var automationAccounts = new List(); + var cloudServices = new List(this.automationManagementClient.CloudServices.List().CloudServices); - #region Job Stream Item Operations + foreach (var cloudService in cloudServices) + { + automationAccounts.AddRange(cloudService.Resources.Select(resource => new AutomationAccount(cloudService, resource))); + } - public IEnumerable ListJobStreamItems(string automationAccountName, Guid jobId, DateTime createdSince, string streamTypeName) - { - AutomationManagement.Models.Job jobModel = this.GetJobModel(automationAccountName, jobId); - IList jobStreamItemModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => + // RDFE does not support server-side filtering, hence we filter on the client-side. + if (automationAccountName != null) + { + automationAccounts = automationAccounts.Where(account => string.Equals(account.AutomationAccountName, automationAccountName, StringComparison.OrdinalIgnoreCase)).ToList(); + + if (!automationAccounts.Any()) { - var response = this.automationManagementClient.JobStreams.ListStreamItems( - automationAccountName, - new AutomationManagement.Models.JobStreamListStreamItemsParameters - { - JobId = jobModel.Id, - StartTime = this.FormatDateTime(createdSince), - StreamType = streamTypeName, - SkipToken = skipToken - }); - return new ResponseWithSkipToken( - response, response.JobStreamItems); - }); + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.AutomationAccountNotFound)); + } + } + + if (location != null) + { + automationAccounts = automationAccounts.Where(account => string.Equals(account.Location, location, StringComparison.OrdinalIgnoreCase)).ToList(); + } - return jobStreamItemModels.Select(jobStreamItemModel => new JobStreamItem(jobStreamItemModel)); + return automationAccounts; } - #endregion + public AutomationAccount CreateAutomationAccount(string automationAccountName, string location) + { - #region Schedule Operations + Requires.Argument("AutomationAccountName", automationAccountName).ValidAutomationAccountName(); - public Schedule CreateSchedule(string automationAccountName, OneTimeSchedule schedule) - { - this.ValidateScheduleName(automationAccountName, schedule.Name); + try + { + var existingAccount = this.ListAutomationAccounts(automationAccountName, location); - var scheduleModel = new AutomationManagement.Models.Schedule + if (existingAccount != null) + { + throw new ResourceCommonException(typeof (AutomationAccount), + string.Format(CultureInfo.CurrentCulture, Resources.AutomationAccountAlreadyExists, + automationAccountName)); + } + } + catch (ArgumentException) { - Name = schedule.Name, - StartTime = schedule.StartTime.ToUniversalTime(), - ExpiryTime = schedule.ExpiryTime.ToUniversalTime(), - Description = schedule.Description, - ScheduleType = - AutomationManagement.Models.ScheduleType - .OneTimeSchedule - }; + // ArgumentException is thrown when account does not exists, so ignore it + } - var scheduleCreateParameters = new AutomationManagement.Models.ScheduleCreateParameters + try { - Schedule = scheduleModel - }; + this.automationManagementClient.CreateAutomationAccount(automationAccountName, location); + } + catch (CloudException e) + { + if (e.Response.StatusCode == HttpStatusCode.NotFound) + { + throw new ResourceCommonException(typeof (AutomationAccount), + string.Format(CultureInfo.CurrentCulture, Resources.AccountCreateInvalidArgs, + automationAccountName, location)); + } + throw; + } - var scheduleCreateResponse = this.automationManagementClient.Schedules.Create( - automationAccountName, - scheduleCreateParameters); + return this.ListAutomationAccounts(automationAccountName, location).FirstOrDefault(); + } + + + public void DeleteAutomationAccount(string automationAccountName) + { + Requires.Argument("AutomationAccountName", automationAccountName).NotNull(); + + var csName = string.Empty; + + var cloudServices = this.automationManagementClient.CloudServices.List().CloudServices; + + foreach (var cloudService in cloudServices) + { + if (cloudService.Resources.Any(resource => 0 == String.Compare(resource.Name, automationAccountName, CultureInfo.InvariantCulture, + CompareOptions.IgnoreCase))) + { + csName = cloudService.Name; + break; + } + } - return this.GetSchedule(automationAccountName, new Guid(scheduleCreateResponse.Schedule.Id)); + this.automationManagementClient.AutomationAccounts.Delete(csName, automationAccountName); } - public Schedule CreateSchedule(string automationAccountName, DailySchedule schedule) - { - this.ValidateScheduleName(automationAccountName, schedule.Name); + #endregion - var scheduleModel = new AutomationManagement.Models.Schedule - { - Name = schedule.Name, - StartTime = schedule.StartTime.ToUniversalTime(), - ExpiryTime = schedule.ExpiryTime.ToUniversalTime(), - Description = schedule.Description, - DayInterval = schedule.DayInterval, - ScheduleType = - AutomationManagement.Models.ScheduleType - .DailySchedule - }; + #region Certificate Operations - var scheduleCreateParameters = new AutomationManagement.Models.ScheduleCreateParameters + public CertificateInfo CreateCertificate(string automationAccountName, string name, string path, SecureString password, + string description, bool exportable) + { + var certificateModel = this.TryGetCertificateModel(automationAccountName, name); + if (certificateModel != null) { - Schedule = scheduleModel - }; - - var scheduleCreateResponse = this.automationManagementClient.Schedules.Create( - automationAccountName, - scheduleCreateParameters); + throw new ResourceCommonException(typeof(CertificateInfo), + string.Format(CultureInfo.CurrentCulture, Resources.CertificateAlreadyExists, name)); + } - return this.GetSchedule(automationAccountName, new Guid(scheduleCreateResponse.Schedule.Id)); + return CreateCertificateInternal(automationAccountName, name, path, password, description, exportable); } - public Schedule CreateSchedule(string automationAccountName, HourlySchedule schedule) + + public CertificateInfo UpdateCertificate(string automationAccountName, string name, string path, SecureString password, + string description, bool? exportable) { - this.ValidateScheduleName(automationAccountName, schedule.Name); + if (String.IsNullOrWhiteSpace(path) && password != null && exportable.HasValue) + { + throw new ResourceCommonException(typeof(CertificateInfo), + string.Format(CultureInfo.CurrentCulture, Resources.SetCertificateInvalidArgs, name)); + } - var scheduleModel = new AutomationManagement.Models.Schedule + var certificateModel = this.TryGetCertificateModel(automationAccountName, name); + if (certificateModel == null) { - Name = schedule.Name, - StartTime = schedule.StartTime.ToUniversalTime(), - ExpiryTime = schedule.ExpiryTime.ToUniversalTime(), - Description = schedule.Description, - HourInterval = schedule.HourInterval, - ScheduleType = - AutomationManagement.Models.ScheduleType - .HourlySchedule - }; + throw new ResourceCommonException(typeof(CertificateInfo), + string.Format(CultureInfo.CurrentCulture, Resources.CertificateNotFound, name)); + } + + var createOrUpdateDescription = description ?? certificateModel.Properties.Description; + var createOrUpdateIsExportable = (exportable.HasValue) ? exportable.Value : certificateModel.Properties.IsExportable; - var scheduleCreateParameters = new AutomationManagement.Models.ScheduleCreateParameters + if (path != null) { - Schedule = scheduleModel - }; + return this.CreateCertificateInternal(automationAccountName, name, path, password, createOrUpdateDescription, + createOrUpdateIsExportable); + } - var scheduleCreateResponse = this.automationManagementClient.Schedules.Create( - automationAccountName, - scheduleCreateParameters); + var cuparam = new CertificateUpdateParameters() + { + Name = name, + Properties = new CertificateUpdateProperties() + { + Description = createOrUpdateDescription, + IsExportable = createOrUpdateIsExportable + } + }; - return this.GetSchedule(automationAccountName, new Guid(scheduleCreateResponse.Schedule.Id)); - } + this.automationManagementClient.Certificates.Update(automationAccountName, cuparam); - public void DeleteSchedule(string automationAccountName, Guid scheduleId) - { - this.automationManagementClient.Schedules.Delete( - automationAccountName, - scheduleId.ToString()); + return new CertificateInfo(automationAccountName, this.automationManagementClient.Certificates.Get(automationAccountName, name).Certificate); } - public void DeleteSchedule(string automationAccountName, string scheduleName) + public CertificateInfo GetCertificate(string automationAccountName, string name) { - Schedule schedule = this.GetSchedule(automationAccountName, scheduleName); - this.DeleteSchedule(automationAccountName, schedule.Id); - } + var certificateModel = this.TryGetCertificateModel(automationAccountName, name); + if (certificateModel == null) + { + throw new ResourceCommonException(typeof(CertificateInfo), + string.Format(CultureInfo.CurrentCulture, Resources.CertificateNotFound, name)); + } - public Schedule GetSchedule(string automationAccountName, Guid scheduleId) - { - AutomationManagement.Models.Schedule scheduleModel = this.GetScheduleModel(automationAccountName, scheduleId); - return this.CreateScheduleFromScheduleModel(scheduleModel); + return new Certificate(automationAccountName, certificateModel); } - public Schedule GetSchedule(string automationAccountName, string scheduleName) + public IEnumerable ListCertificates(string automationAccountName) { - AutomationManagement.Models.Schedule scheduleModel = this.GetScheduleModel(automationAccountName, scheduleName); - return this.CreateScheduleFromScheduleModel(scheduleModel); + return AutomationManagementClient + .ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.Certificates.List( + automationAccountName); + return new ResponseWithSkipToken( + response, response.Certificates); + }).Select(c => new CertificateInfo(automationAccountName, c)); } - public IEnumerable ListSchedules(string automationAccountName) + public void DeleteCertificate(string automationAccountName, string name) { - IList scheduleModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => + try + { + this.automationManagementClient.Certificates.Delete(automationAccountName, name); + } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NoContent) { - var response = this.automationManagementClient.Schedules.List( - automationAccountName, skipToken); - return new ResponseWithSkipToken( - response, response.Schedules); - }); - - return scheduleModels.Select(this.CreateScheduleFromScheduleModel); - } - - public Schedule UpdateSchedule(string automationAccountName, Guid scheduleId, bool? isEnabled, string description) - { - AutomationManagement.Models.Schedule scheduleModel = this.GetScheduleModel(automationAccountName, scheduleId); - return this.UpdateScheduleHelper(automationAccountName, scheduleModel, isEnabled, description); - } + throw new ResourceNotFoundException(typeof(Schedule), string.Format(CultureInfo.CurrentCulture, Resources.CertificateNotFound, name)); + } - public Schedule UpdateSchedule(string automationAccountName, string scheduleName, bool? isEnabled, string description) - { - AutomationManagement.Models.Schedule scheduleModel = this.GetScheduleModel(automationAccountName, scheduleName); - return this.UpdateScheduleHelper(automationAccountName, scheduleModel, isEnabled, description); + throw; + } } - #endregion + #endregion - #region Private Methods + #region Connection Operations - private Runbook CreateRunbook(string automationAccountName, Stream runbookStream) + public Connection CreateConnection(string automationAccountName, string name, string connectionTypeName, IDictionary connectionFieldValues, + string description) { - var createRunbookVersionResponse = this.automationManagementClient.RunbookVersions.Create( - automationAccountName, - runbookStream); + var connectionModel = this.TryGetConnectionModel(automationAccountName, name); + if (connectionModel != null) + { + throw new ResourceCommonException(typeof(Connection), + string.Format(CultureInfo.CurrentCulture, Resources.ConnectionAlreadyExists, name)); + } - var getRunbookVersionResponse = this.automationManagementClient.RunbookVersions.Get( - automationAccountName, - createRunbookVersionResponse.RunbookVersion.Id); + var ccprop = new ConnectionCreateProperties() + { + Description = description, + ConnectionType = new ConnectionTypeAssociationProperty() { Name = connectionTypeName }, + FieldDefinitionValues = connectionFieldValues.Cast().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString()) + }; - return this.GetRunbook(automationAccountName, new Guid(getRunbookVersionResponse.RunbookVersion.RunbookId)); - } + var ccparam = new ConnectionCreateParameters() { Name = name, Properties = ccprop }; - private IEnumerable CreateRunbookDefinitionsFromRunbookVersionModels( - string automationAccountName, IEnumerable runbookVersionModels) - { - foreach (AutomationManagement.Models.RunbookVersion runbookVersionModel in runbookVersionModels) - { - var getRunbookDefinitionResponse = - this.automationManagementClient.RunbookVersions.GetRunbookDefinition( - automationAccountName, - runbookVersionModel.Id); + var connection = this.automationManagementClient.Connections.Create(automationAccountName, ccparam).Connection; - yield return new RunbookDefinition(runbookVersionModel, getRunbookDefinitionResponse.RunbookDefinition); - } + return new Connection(automationAccountName, connection); } - private Schedule CreateScheduleFromScheduleModel(AutomationManagement.Models.Schedule schedule) + public Connection UpdateConnectionFieldValue(string automationAccountName, string name, string connectionFieldName, object value) { - Requires.Argument("schedule", schedule).NotNull(); - - if (schedule.ScheduleType == AutomationManagement.Models.ScheduleType.DailySchedule) + var connectionModel = this.TryGetConnectionModel(automationAccountName, name); + if (connectionModel == null) { - return new DailySchedule(schedule); + throw new ResourceCommonException(typeof(Connection), + string.Format(CultureInfo.CurrentCulture, Resources.ConnectionNotFound, name)); } - else if (schedule.ScheduleType == AutomationManagement.Models.ScheduleType.HourlySchedule) + + if (connectionModel.Properties.FieldDefinitionValues.ContainsKey(connectionFieldName)) { - return new HourlySchedule(schedule); + connectionModel.Properties.FieldDefinitionValues[connectionFieldName] = + PowerShellJsonConverter.Serialize(value); } else { - return new OneTimeSchedule(schedule); + throw new ResourceCommonException(typeof(Connection), + string.Format(CultureInfo.CurrentCulture, Resources.ConnectionFieldNameNotFound, name)); } + + var cuparam = new ConnectionUpdateParameters() + { + Name = name, + Properties = new ConnectionUpdateProperties() + { + Description = connectionModel.Properties.Description, + FieldDefinitionValues = connectionModel.Properties.FieldDefinitionValues + } + }; + + this.automationManagementClient.Connections.Update(automationAccountName, cuparam); + + return new Connection(automationAccountName, this.automationManagementClient.Connections.Get(automationAccountName, name).Connection); } - private Guid EditRunbook(string automationAccountName, Guid runbookId) + public Connection GetConnection(string automationAccountName, string name) { - return new Guid(this.automationManagementClient.Runbooks.Edit( - automationAccountName, - runbookId.ToString()) - .DraftRunbookVersionId); + var connectionModel = this.TryGetConnectionModel(automationAccountName, name); + if (connectionModel == null) + { + throw new ResourceCommonException(typeof(Connection), + string.Format(CultureInfo.CurrentCulture, Resources.ConnectionNotFound, name)); + } + + return new Connection(automationAccountName, connectionModel); } - private string FormatDateTime(DateTime dateTime) + public IEnumerable ListConnectionsByType(string automationAccountName, string typeName) { - return string.Format(CultureInfo.InvariantCulture, "{0:O}", dateTime.ToUniversalTime()); + var connections = this.ListConnections(automationAccountName); + + return connections.Where(c => c.ConnectionTypeName.Equals(typeName, StringComparison.InvariantCultureIgnoreCase)); } - private AutomationManagement.Models.Job GetJobModel(string automationAccountName, Guid jobId) + public IEnumerable ListConnections(string automationAccountName) { - AutomationManagement.Models.Job jobModel = this.automationManagementClient.Jobs.Get( - automationAccountName, - jobId.ToString()) - .Job; + return AutomationManagementClient + .ContinuationTokenHandler( + skipToken => + { + var response = this.automationManagementClient.Connections.List( + automationAccountName); + return new ResponseWithSkipToken( + response, response.Connection); + }).Select(c => new Connection(automationAccountName, c)); + } - if (jobModel == null) + public void DeleteConnection(string automationAccountName, string name) + { + try { - throw new ResourceNotFoundException(typeof(Job), string.Format(CultureInfo.CurrentCulture, Resources.JobNotFoundById, jobId)); + this.automationManagementClient.Connections.Delete(automationAccountName, name); } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NoContent) + { + throw new ResourceNotFoundException(typeof(Connection), string.Format(CultureInfo.CurrentCulture, Resources.ConnectionNotFound, name)); + } - return jobModel; + throw; + } } - private IEnumerable ListJobsByValidRunbookId(string automationAccountName, Guid runbookId, DateTime? startTime, DateTime? endTime) + #endregion + + #region JobSchedules + + public JobSchedule GetJobSchedule(string automationAccountName, Guid jobScheduleId) { - // Assume local time if DateTimeKind.Unspecified - if (startTime.HasValue && startTime.Value.Kind == DateTimeKind.Unspecified) + AutomationManagement.Models.JobSchedule jobScheduleModel = null; + + try { - startTime = DateTime.SpecifyKind(startTime.Value, DateTimeKind.Local); + jobScheduleModel = this.automationManagementClient.JobSchedules.Get( + automationAccountName, + jobScheduleId) + .JobSchedule; } - - if (endTime.HasValue && endTime.Value.Kind == DateTimeKind.Unspecified) + catch (CloudException cloudException) { - endTime = DateTime.SpecifyKind(endTime.Value, DateTimeKind.Local); + if (cloudException.Response.StatusCode == HttpStatusCode.NotFound) + { + throw new ResourceNotFoundException(typeof(JobSchedule), + string.Format(CultureInfo.CurrentCulture, Resources.JobScheduleWithIdNotFound, jobScheduleId)); + } + + throw; } + + return this.CreateJobScheduleFromJobScheduleModel(automationAccountName, jobScheduleModel); + } - IEnumerable jobModels; + public JobSchedule GetJobSchedule(string automationAccountName, string runbookName, string scheduleName) + { + var jobSchedules = this.ListJobSchedules(automationAccountName); + JobSchedule jobSchedule = null; + bool jobScheduleFound = false; - if (startTime.HasValue && endTime.HasValue) - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.Jobs.ListByRunbookIdFilteredByStartTimeEndTime( - automationAccountName, - new AutomationManagement.Models.JobListByRunbookIdParameters - { - RunbookId = runbookId.ToString(), - StartTime = this.FormatDateTime(startTime.Value), - EndTime = this.FormatDateTime(endTime.Value), - SkipToken = skipToken - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); - } - else if (startTime.HasValue) + foreach (var js in jobSchedules) { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.Jobs.ListByRunbookIdFilteredByStartTime( - automationAccountName, - new AutomationManagement.Models.JobListByRunbookIdParameters - { - RunbookId = runbookId.ToString(), - StartTime = this.FormatDateTime(startTime.Value), - SkipToken = skipToken, - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); + if (String.Equals(js.RunbookName, runbookName, StringComparison.OrdinalIgnoreCase) && + String.Equals(js.ScheduleName, scheduleName, StringComparison.OrdinalIgnoreCase)) + { + jobSchedule = this.GetJobSchedule(automationAccountName, new Guid(js.JobScheduleId)); + jobScheduleFound = true; + break; + } } - else if (endTime.HasValue) + if (!jobScheduleFound) { - jobModels = AutomationManagementClient.ContinuationTokenHandler( - skipToken => - { - var response = - this.automationManagementClient.Jobs.ListByRunbookIdFilteredByStartTime( - automationAccountName, - new AutomationManagement.Models.JobListByRunbookIdParameters - { - RunbookId = runbookId.ToString(), - EndTime = this.FormatDateTime(endTime.Value), - SkipToken = skipToken, - }); - return new ResponseWithSkipToken(response, response.Jobs); - }); + throw new ResourceNotFoundException(typeof(Schedule), + string.Format(CultureInfo.CurrentCulture, Resources.JobScheduleNotFound, runbookName, scheduleName)); } - else - { - jobModels = AutomationManagementClient.ContinuationTokenHandler( + + return jobSchedule; + } + + public IEnumerable ListJobSchedules(string automationAccountName) + { + IList jobScheduleModels = AutomationManagementClient + .ContinuationTokenHandler( skipToken => { - var response = this.automationManagementClient.Jobs.ListByRunbookId( - automationAccountName, - new AutomationManagement.Models.JobListByRunbookIdParameters - { - RunbookId = runbookId.ToString(), - SkipToken = skipToken, - }); - return new ResponseWithSkipToken(response, response.Jobs); + var response = this.automationManagementClient.JobSchedules.List( + automationAccountName); + + return new ResponseWithSkipToken( + response, response.JobSchedules); }); - } - return jobModels.Select(jobModel => new Job(jobModel)); + return jobScheduleModels.Select(jobScheduleModel => new JobSchedule(automationAccountName, jobScheduleModel)); } - private Guid GetRunbookIdByRunbookName(string automationAccountName, string runbookName) + public IEnumerable ListJobSchedulesByRunbookName(string automationAccountName, string runbookName) { - return new Guid(this.GetRunbookModel(automationAccountName, runbookName, false).Id); + var jobSchedules = this.ListJobSchedules(automationAccountName); + + IEnumerable jobSchedulesOfRunbook = new List(); + + jobSchedulesOfRunbook = jobSchedules.Where(js => String.Equals(js.RunbookName, runbookName, StringComparison.OrdinalIgnoreCase)); + + return jobSchedulesOfRunbook; } - private AutomationManagement.Models.Runbook GetRunbookModel(string automationAccountName, Guid runbookId, bool withSchedules) + public IEnumerable ListJobSchedulesByScheduleName(string automationAccountName, string scheduleName) { - AutomationManagement.Models.Runbook runbookModel = withSchedules - ? this.automationManagementClient.Runbooks.GetWithSchedules( - automationAccountName, - runbookId.ToString()).Runbook - : this.automationManagementClient.Runbooks.Get( - automationAccountName, - runbookId.ToString()).Runbook; + var jobSchedules = this.ListJobSchedules(automationAccountName); - if (runbookModel == null) - { - throw new ResourceNotFoundException(typeof(Runbook), string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFoundById, runbookId)); - } + IEnumerable jobSchedulesOfSchedule = new List(); + + jobSchedulesOfSchedule = jobSchedules.Where(js => String.Equals(js.ScheduleName, scheduleName, StringComparison.OrdinalIgnoreCase)); - return runbookModel; + return jobSchedulesOfSchedule; } - private AutomationManagement.Models.Runbook GetRunbookModel(string automationAccountName, string runbookName, bool withSchedules) + public JobSchedule RegisterScheduledRunbook(string automationAccountName, string runbookName, string scheduleName, IDictionary parameters) { - IList runbookModels = withSchedules - ? this.automationManagementClient.Runbooks.ListByNameWithSchedules( - automationAccountName, - runbookName).Runbooks - : this.automationManagementClient.Runbooks.ListByName( - automationAccountName, - runbookName).Runbooks; + var processedParameters = this.ProcessRunbookParameters(automationAccountName, runbookName, parameters); + var sdkJobSchedule = this.automationManagementClient.JobSchedules.Create( + automationAccountName, + new AutomationManagement.Models.JobScheduleCreateParameters + { + Properties = new AutomationManagement.Models.JobScheduleCreateProperties + { + Schedule = new ScheduleAssociationProperty { Name = scheduleName }, + Runbook = new RunbookAssociationProperty { Name = runbookName }, + Parameters = processedParameters + } + }).JobSchedule; + + return new JobSchedule(automationAccountName, sdkJobSchedule); + } - if (!runbookModels.Any()) + public void UnregisterScheduledRunbook(string automationAccountName, Guid jobScheduleId) + { + try { - throw new ResourceNotFoundException(typeof(Runbook), string.Format(CultureInfo.CurrentCulture, Resources.RunbookNotFoundByName, runbookName)); + this.automationManagementClient.JobSchedules.Delete( + automationAccountName, + jobScheduleId); } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NotFound) + { + throw new ResourceNotFoundException(typeof(Schedule), + string.Format(CultureInfo.CurrentCulture, Resources.JobScheduleWithIdNotFound, jobScheduleId)); + } - return runbookModels.First(); + throw; + } } - private AutomationManagement.Models.RunbookVersion GetRunbookVersionModel( - string automationAccountName, Guid runbookVersionId) + public void UnregisterScheduledRunbook(string automationAccountName, string runbookName, string scheduleName) { - AutomationManagement.Models.RunbookVersion runbookVersionModel = - this.automationManagementClient.RunbookVersions.Get( - automationAccountName, - runbookVersionId.ToString()).RunbookVersion; + var jobSchedules = this.ListJobSchedules(automationAccountName); + bool jobScheduleFound = false; - if (runbookVersionModel == null) + foreach (var jobSchedule in jobSchedules) + { + if (jobSchedule.RunbookName == runbookName && jobSchedule.ScheduleName == scheduleName) + { + this.UnregisterScheduledRunbook(automationAccountName, new Guid(jobSchedule.JobScheduleId)); + jobScheduleFound = true; + break; + } + } + if(!jobScheduleFound) { - throw new ResourceNotFoundException( - typeof(RunbookVersion), - string.Format(CultureInfo.CurrentCulture, Resources.RunbookVersionNotFoundById, runbookVersionId)); + throw new ResourceNotFoundException(typeof(Schedule), + string.Format(CultureInfo.CurrentCulture, Resources.JobScheduleNotFound, runbookName, scheduleName)); } - - return runbookVersionModel; } - private IEnumerable ListRunbookDefinitionsByValidRunbookId(string automationAccountName, Guid runbookId, bool? isDraft) + #endregion + + #region Private Methods + + private Schedule CreateScheduleFromScheduleModel(string automationAccountName, AutomationManagement.Models.Schedule schedule) { - IList runbookVersionModels = isDraft.HasValue - ? this.automationManagementClient.RunbookVersions.ListLatestByRunbookIdSlot( - automationAccountName, - new AutomationManagement.Models. - RunbookVersionListLatestByRunbookIdSlotParameters - { - RunbookId = - runbookId.ToString(), - IsDraft = - isDraft.Value - }) - .RunbookVersions - : this.automationManagementClient.RunbookVersions.ListLatestByRunbookId( - automationAccountName, - runbookId.ToString()).RunbookVersions; + Requires.Argument("schedule", schedule).NotNull(); - return this.CreateRunbookDefinitionsFromRunbookVersionModels(automationAccountName, runbookVersionModels); + return new Schedule(automationAccountName, schedule); } - private AutomationManagement.Models.Schedule GetScheduleModel(string automationAccountName, Guid scheduleId) + private JobSchedule CreateJobScheduleFromJobScheduleModel(string automationAccountName, AutomationManagement.Models.JobSchedule jobSchedule) { - AutomationManagement.Models.Schedule scheduleModel = this.automationManagementClient.Schedules.Get( - automationAccountName, - scheduleId.ToString()) - .Schedule; + Requires.Argument("jobSchedule", jobSchedule).NotNull(); + + return new JobSchedule(automationAccountName, jobSchedule); + } - if (scheduleModel == null) + private WindowsAzure.Management.Automation.Models.Runbook TryGetRunbookModel(string automationAccountName, string runbookName) + { + WindowsAzure.Management.Automation.Models.Runbook runbook = null; + try { - throw new ResourceNotFoundException(typeof(Schedule), string.Format(CultureInfo.CurrentCulture, Resources.ScheduleNotFoundById, scheduleId)); + runbook = this.automationManagementClient.Runbooks.Get(automationAccountName, runbookName).Runbook; } - - return scheduleModel; + catch (CloudException e) + { + if (e.Response.StatusCode == HttpStatusCode.NotFound) + { + runbook = null; + } + else + { + throw; + } + } + return runbook; } - private AutomationManagement.Models.Schedule GetScheduleModel(string automationAccountName, string scheduleName) + private WindowsAzure.Management.Automation.Models.Certificate TryGetCertificateModel(string automationAccountName, string certificateName) { - IList schedules = this.automationManagementClient.Schedules.ListByName( - automationAccountName, - scheduleName) - .Schedules; - - if (!schedules.Any()) + WindowsAzure.Management.Automation.Models.Certificate certificate = null; + try { - throw new ResourceNotFoundException(typeof(RunbookVersion), string.Format(CultureInfo.CurrentCulture, Resources.ScheduleNotFoundByName, scheduleName)); + certificate = this.automationManagementClient.Certificates.Get(automationAccountName, certificateName).Certificate; } - - return schedules.First(); + catch (CloudException e) + { + if (e.Response.StatusCode == HttpStatusCode.NotFound) + { + certificate = null; + } + else + { + throw; + } + } + return certificate; } - private IEnumerable ListRunbookParameters(string automationAccountName, Guid runbookId) + private IEnumerable> ListRunbookParameters(string automationAccountName, string runbookName) { - Runbook runbook = this.GetRunbook(automationAccountName, runbookId); - if (!runbook.PublishedRunbookVersionId.HasValue) + 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.RunbookHasNoPublishedVersionById, runbookId)); + throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.RunbookHasNoPublishedVersion, runbookName)); } - - return this.automationManagementClient.RunbookParameters.ListByRunbookVersionId( - automationAccountName, - runbook.PublishedRunbookVersionId.Value.ToString()).RunbookParameters.Select(runbookParameter => new RunbookParameter(runbookParameter)); + return runbook.Parameters.Cast().ToDictionary(k => k.Key.ToString(), k => (RunbookParameter)k.Value); } - private IEnumerable ProcessRunbookParameters(string automationAccountName, Guid runbookId, IDictionary parameters) + private IDictionary ProcessRunbookParameters(string automationAccountName, string runbookName, IDictionary parameters) { parameters = parameters ?? new Dictionary(); - IEnumerable runbookParameters = this.ListRunbookParameters(automationAccountName, runbookId); - var filteredParameters = new List(); + IEnumerable> runbookParameters = this.ListRunbookParameters(automationAccountName, runbookName); + var filteredParameters = new Dictionary(); foreach (var runbookParameter in runbookParameters) { - if (parameters.Contains(runbookParameter.Name)) + if (parameters.Contains(runbookParameter.Key)) { - object paramValue = parameters[runbookParameter.Name]; + object paramValue = parameters[runbookParameter.Key]; try { - filteredParameters.Add( - new AutomationManagement.Models.NameValuePair - { - Name = runbookParameter.Name, - Value = JsonConvert.SerializeObject(paramValue, new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }) - }); + filteredParameters.Add(runbookParameter.Key, PowerShellJsonConverter.Serialize(paramValue)); } catch (JsonSerializationException) { throw new ArgumentException( string.Format( - CultureInfo.CurrentCulture, Resources.RunbookParameterCannotBeSerializedToJson, runbookParameter.Name)); + CultureInfo.CurrentCulture, Resources.RunbookParameterCannotBeSerializedToJson, runbookParameter.Key)); } } - else if (runbookParameter.IsMandatory) + else if (runbookParameter.Value.IsMandatory) { throw new ArgumentException( string.Format( - CultureInfo.CurrentCulture, Resources.RunbookParameterValueRequired, runbookParameter.Name)); + CultureInfo.CurrentCulture, Resources.RunbookParameterValueRequired, runbookParameter.Key)); } } @@ -949,133 +1503,120 @@ private IEnumerable ListRunbookParameters(string automationAcc string.Format(CultureInfo.CurrentCulture, Resources.InvalidRunbookParameters)); } - bool hasJobStartedBy = filteredParameters.Any(filteredParameter => filteredParameter.Name == Constants.JobStartedByParameterName); + var hasJobStartedBy = filteredParameters.Any(filteredParameter => filteredParameter.Key == Constants.JobStartedByParameterName); if (!hasJobStartedBy) { - filteredParameters.Add(new AutomationManagement.Models.NameValuePair() { Name = Constants.JobStartedByParameterName, Value = Constants.ClientIdentity }); + filteredParameters.Add(Constants.JobStartedByParameterName, PowerShellJsonConverter.Serialize(Constants.ClientIdentity)); } return filteredParameters; } - private RunbookDefinition UpdateRunbookDefinition(string automationAccountName, Guid runbookId, Stream runbookStream, bool overwrite) + private JobStream CreateJobStreamFromJobStreamModel(AutomationManagement.Models.JobStream jobStream, string automationAccountName, Guid jobId) { - var runbook = new Runbook(this.GetRunbookModel(automationAccountName, runbookId, false)); - - if (runbook.DraftRunbookVersionId.HasValue && overwrite == false) - { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.RunbookAlreadyHasDraft)); - } - - Guid draftRunbookVersionId = runbook.DraftRunbookVersionId.HasValue - ? runbook.DraftRunbookVersionId.Value - : this.EditRunbook(automationAccountName, runbook.Id); - - var getRunbookDefinitionResponse = this.automationManagementClient.RunbookVersions.GetRunbookDefinition( - automationAccountName, - draftRunbookVersionId.ToString()); + Requires.Argument("jobStream", jobStream).NotNull(); + Requires.Argument("automationAccountName", automationAccountName).NotNull(); + Requires.Argument("jobId", jobId).NotNull(); + return new JobStream(jobStream, automationAccountName, jobId); + } - this.automationManagementClient.RunbookVersions.UpdateRunbookDefinition( - automationAccountName, - new AutomationManagement.Models.RunbookVersionUpdateRunbookDefinitionParameters - { - ETag = getRunbookDefinitionResponse.ETag, - RunbookVersionId = draftRunbookVersionId.ToString(), - RunbookStream = runbookStream, - }); + private Variable CreateVariableFromVariableModel(AutomationManagement.Models.Variable variable, string automationAccountName) + { + Requires.Argument("variable", variable).NotNull(); - IEnumerable runbookDefinitions = this.ListRunbookDefinitionsByRunbookVersionId(automationAccountName, draftRunbookVersionId, true); - return runbookDefinitions.First(); + return new Variable(variable, automationAccountName); } - private Runbook UpdateRunbookHelper( - string automationAccountName, - AutomationManagement.Models.Runbook runbook, - string description, - string[] tags, - bool? logDebug, - bool? logProgress, - bool? logVerbose) + private AutomationManagement.Models.Schedule GetScheduleModel(string automationAccountName, string scheduleName) { - if (description != null) + AutomationManagement.Models.Schedule scheduleModel; + try { - runbook.Description = description; + scheduleModel = this.automationManagementClient.Schedules.Get( + automationAccountName, + scheduleName) + .Schedule; } - - if (tags != null) + catch (CloudException cloudException) { - runbook.Tags = string.Join(Constants.RunbookTagsSeparatorString, tags); - } + if (cloudException.Response.StatusCode == HttpStatusCode.NotFound) + { + throw new ResourceNotFoundException(typeof(Schedule), string.Format(CultureInfo.CurrentCulture, Resources.ScheduleNotFound, scheduleName)); + } - if (logDebug.HasValue) - { - runbook.LogDebug = logDebug.Value; + throw; } - if (logProgress.HasValue) - { - runbook.LogProgress = logProgress.Value; - } + return scheduleModel; + } - if (logVerbose.HasValue) - { - runbook.LogVerbose = logVerbose.Value; - } + private string FormatDateTime(DateTimeOffset dateTime) + { + return string.Format(CultureInfo.InvariantCulture, "{0:O}", dateTime.ToUniversalTime()); + } - var runbookUpdateParameters = new AutomationManagement.Models.RunbookUpdateParameters + private Schedule UpdateScheduleHelper(string automationAccountName, + string scheduleName, bool? isEnabled, string description) + { + var scheduleUpdateParameters = new AutomationManagement.Models.ScheduleUpdateParameters { - Runbook = runbook + Name = scheduleName, + Properties = new AutomationManagement.Models.ScheduleUpdateProperties + { + Description = description, + IsEnabled = isEnabled + } }; - this.automationManagementClient.Runbooks.Update(automationAccountName, runbookUpdateParameters); + this.automationManagementClient.Schedules.Update( + automationAccountName, + scheduleUpdateParameters); - var runbookId = new Guid(runbook.Id); - return this.GetRunbook(automationAccountName, runbookId); + return this.GetSchedule(automationAccountName, scheduleName); } - private Schedule UpdateScheduleHelper(string automationAccountName, AutomationManagement.Models.Schedule schedule, bool? isEnabled, string description) + private Certificate CreateCertificateInternal(string automationAccountName, string name, string path, + SecureString password, string description, bool exportable) { - // StartTime and ExpiryTime need to specified as Utc - schedule.StartTime = DateTime.SpecifyKind(schedule.StartTime, DateTimeKind.Utc); - schedule.ExpiryTime = DateTime.SpecifyKind(schedule.ExpiryTime, DateTimeKind.Utc); - - if (isEnabled.HasValue) - { - schedule.IsEnabled = isEnabled.Value; - } - - if (description != null) - { - schedule.Description = description; - } + var cert = (password == null) + ? new X509Certificate2(path) + : new X509Certificate2(path, password); - var scheduleUpdateParameters = new AutomationManagement.Models.ScheduleUpdateParameters + var ccprop = new CertificateCreateProperties() { - Schedule = - schedule + Description = description, + Base64Value = Convert.ToBase64String(cert.Export(X509ContentType.Pkcs12)), + Thumbprint = cert.Thumbprint, + IsExportable = exportable }; - this.automationManagementClient.Schedules.Update( - automationAccountName, - scheduleUpdateParameters); + var ccparam = new CertificateCreateParameters() { Name = name, Properties = ccprop }; - var scheduleId = new Guid(schedule.Id); - return this.GetSchedule(automationAccountName, scheduleId); + var certificate = this.automationManagementClient.Certificates.Create(automationAccountName, ccparam).Certificate; + + return new Certificate(automationAccountName, certificate); } - // TODO: remove the helper which provides client-side schedule name validation once CDM TFS bug 662986 is resolved. - private void ValidateScheduleName(string automationAccountName, string scheduleName) + private WindowsAzure.Management.Automation.Models.Connection TryGetConnectionModel(string automationAccountName, string connectionName) { - IList scheduleModels = - this.automationManagementClient.Schedules.ListByName( - automationAccountName, scheduleName) - .Schedules; - - if (scheduleModels.Any()) + WindowsAzure.Management.Automation.Models.Connection connection = null; + try + { + connection = this.automationManagementClient.Connections.Get(automationAccountName, connectionName).Connection; + } + catch (CloudException e) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.ScheduleNameExists, scheduleName)); + if (e.Response.StatusCode == HttpStatusCode.NotFound) + { + connection = null; + } + else + { + throw; + } } + return connection; } #endregion diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs new file mode 100644 index 000000000000..ba5bb895c9eb --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs @@ -0,0 +1,106 @@ +// ---------------------------------------------------------------------------------- +// +// 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; + +namespace Microsoft.Azure.Commands.Automation.Common +{ + internal static class AutomationCmdletParameterSets + { + /// + /// By All parameter set. + /// + internal const string ByAll = "ByAll"; + + /// + /// By Name parameter set. + /// + internal const string ByName = "ByName"; + + /// + /// By Path parameter set. + /// + internal const string ByPath = "ByPath"; + + + /// + /// The one time schedule parameter set. + /// + internal const string ByOneTime = "ByOneTime"; + + /// + /// The daily schedule parameter set. + /// + internal const string ByDaily = "ByDaily"; + + /// + /// The hourly schedule parameter set. + /// + internal const string ByHourly = "ByHourly"; + + /// + /// The Job Id parameter set. + /// + internal const string ByJobId = "ByJobId"; + + /// + /// The Runbook name parameter set. + /// + internal const string ByRunbookName = "ByRunbookName"; + + /// + /// The Schedule name parameter set. + /// + internal const string ByScheduleName = "ByScheduleName"; + + /// + /// The certificate name parameter set. + /// + internal const string ByCertificateName = "ByCertificateName"; + + /// + /// The connection name parameter set. + /// + internal const string ByConnectionName = "ByConnectionName"; + + /// + /// The connection type name parameter set. + /// + internal const string ByConnectionTypeName = "ByConnectionTypeName"; + + /// + /// The Schedule name parameter set. + /// + internal const string ByRunbookNameAndScheduleName = "ByRunbookNameAndScheduleName"; + + /// + /// The Job Schedule Id parameter set. + /// + internal const string ByJobScheduleId = "ByJobScheduleId"; + + /// + /// Parameter set for updating variable value + /// + internal const string UpdateVariableValue = "UpdateVariableValue"; + + /// + /// Parameter set for updating variable description + /// + internal const string UpdateVariableDescription = "UpdateVariableDescription"; + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/AzureAutomationOperationException.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/AzureAutomationOperationException.cs new file mode 100644 index 000000000000..6dee5601c5e4 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/AzureAutomationOperationException.cs @@ -0,0 +1,34 @@ +// ---------------------------------------------------------------------------------- +// +// 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; + +namespace Microsoft.Azure.Commands.Automation.Common +{ + [Serializable] + public class AzureAutomationOperationException : Exception + { + public AzureAutomationOperationException(string message) + : base(message) + { + + } + + public AzureAutomationOperationException(string message, Exception exception) + : base(message, exception) + { + + } + } +} \ No newline at end of file diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/Constants.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/Constants.cs index 5b9a6c7a8b77..47f44fbb7455 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Common/Constants.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/Constants.cs @@ -13,28 +13,28 @@ // ---------------------------------------------------------------------------------- using System; -using Microsoft.Azure.Management.Automation.Models; namespace Microsoft.Azure.Commands.Automation.Common { public class Constants { - public class JobOutputParameter - { - public const string Any = "Any"; + public const string ClientIdentity = "PowerShell"; + + public const char RunbookTagsSeparatorChar = ','; - public const string Progress = JobStreamType.Progress; + public const string RunbookTagsSeparatorString = ","; - public const string Output = JobStreamType.Output; + public const string Published = "Published"; - public const string Warning = JobStreamType.Warning; + public const string Draft = "Draft"; - public const string Error = JobStreamType.Error; + public const string AutomationServicePrefix = "OaasCS"; - public const string Debug = JobStreamType.Debug; + public const string JobStartedByParameterName = "MicrosoftApplicationManagementStartedBy"; - public const string Verbose = JobStreamType.Verbose; - } + // default schedule expiry time for daily schedule, consistent with UX + // 12/31/9999 12:00:00 AM + public static readonly DateTimeOffset DefaultScheduleExpiryTime = DateTimeOffset.MaxValue; public class AutomationAccountState { @@ -43,16 +43,15 @@ public class AutomationAccountState public const string Suspended = "Suspended"; } - // default schedule expiry time for daily schedule, consistent with UX - // 12/31/9999 12:00:00 AM - public static readonly DateTime DefaultScheduleExpiryTime = new DateTime(9999, 12, 31, 0, 0, 0, DateTimeKind.Utc); + public const string PsCommandConvertToJson = "ConvertTo-Json"; - public const string JobStartedByParameterName = "MicrosoftApplicationManagementStartedBy"; + public const string PsCommandConvertFromJson = "ConvertFrom-Json"; - public const string ClientIdentity = "PowerShell"; + public const string PsCommandParamInputObject = "InputObject"; - public const char RunbookTagsSeparatorChar = ','; + public const string PsCommandParamDepth = "Depth"; + + public const int PsCommandValueDepth = 10; - public const string RunbookTagsSeparatorString = ","; } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/IAutomationClient.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/IAutomationClient.cs index cc001f934108..6f36e485a121 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Common/IAutomationClient.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/IAutomationClient.cs @@ -15,6 +15,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Security; using Microsoft.Azure.Commands.Automation.Model; using Microsoft.Azure.Common.Extensions.Models; @@ -24,96 +25,167 @@ public interface IAutomationClient { AzureSubscription Subscription { get; } - IEnumerable ListAutomationAccounts(string automationAccountName, string location); + #region JobStreams + + IEnumerable GetJobStream(string automationAccountname, Guid jobId, DateTimeOffset? time, string streamType); + + #endregion + + #region Variables + + Variable GetVariable(string automationAccountName, string variableName); + + IEnumerable ListVariables(string automationAccountName); + + Variable CreateVariable(Variable variable); + + void DeleteVariable(string automationAccountName, string variableName); - Job GetJob(string automationAccountName, Guid jobId); + Variable UpdateVariable(Variable variable, VariableUpdateFields updateFields); - IEnumerable ListJobs(string automationAccountName, DateTime? startTime, DateTime? endTime); + #endregion - IEnumerable ListJobsByRunbookId(string automationAccountName, Guid runbookId, DateTime? startTime, DateTime? endTime); + #region Scedules - IEnumerable ListJobsByRunbookName(string automationAccountName, string runbookName, DateTime? startTime, DateTime? endTime); + Schedule CreateSchedule(string automationAccountName, Schedule schedule); - IEnumerable ListJobStreamItems(string automationAccountName, Guid jobId, DateTime createdSince, string streamTypeName); + void DeleteSchedule(string automationAccountName, string scheduleName); + + Schedule GetSchedule(string automationAccountName, string scheduleName); + + IEnumerable ListSchedules(string automationAccountName); + + Schedule UpdateSchedule(string automationAccountName, string scheduleName, bool? isEnabled, string description); - Runbook GetRunbook(string automationAccountName, Guid runbookId); + #endregion + + #region Runbooks Runbook GetRunbook(string automationAccountName, string runbookName); IEnumerable ListRunbooks(string automationAccountName); - IEnumerable ListRunbookByScheduleName(string automationAccountName, string scheduleName); + Runbook CreateRunbookByName(string automationAccountName, string runbookName, string description, string[] tags); + + Runbook CreateRunbookByPath(string automationAccountName, string runbookPath, string description, string[] tags); + + void DeleteRunbook(string automationAccountName, string runbookName); + + Runbook PublishRunbook(string automationAccountName, string runbookName); + + Runbook UpdateRunbook(string automationAccountName, string runbookName, string description, string[] tags, bool? logProgress, bool? logVerbose); + + RunbookDefinition UpdateRunbookDefinition(string automationAccountName, string runbookName, string runbookPath, bool overwrite); IEnumerable ListRunbookDefinitionsByRunbookName(string automationAccountName, string runbookName, bool? isDraft); - IEnumerable ListRunbookDefinitionsByRunbookId(string automationAccountName, Guid runbookId, bool? isDraft); + Job StartRunbook(string automationAccountName, string runbookName, IDictionary parameters); - IEnumerable ListRunbookDefinitionsByRunbookVersionId(string automationAccountName, Guid runbookVersionId, bool? isDraft); + #endregion - Schedule GetSchedule(string automationAccountName, Guid scheduleId); + #region Credentials - Schedule GetSchedule(string automationAccountName, string scheduleName); + CredentialInfo CreateCredential(string automationAccountName, string name, string userName, string password, string description); - IEnumerable ListSchedules(string automationAccountName); + CredentialInfo UpdateCredential(string automationAccountName, string name, string userName, string password, string description); - Runbook CreateRunbookByName( - string automationAccountName, - string runbookName, - string description, - string[] tags); + CredentialInfo GetCredential(string automationAccountName, string name); - Runbook CreateRunbookByPath( - string automationAccountName, - string runbookPath, - string description, - string[] tags); + IEnumerable ListCredentials(string automationAccountName); - Schedule CreateSchedule(string automationAccountName, OneTimeSchedule schedule); + void DeleteCredential(string automationAccountName, string name); - Schedule CreateSchedule(string automationAccountName, DailySchedule schedule); + #endregion - Schedule CreateSchedule(string automationAccountName, HourlySchedule schedule); + #region Modules - Runbook PublishRunbook(string automationAccountName, Guid runbookId); + Module CreateModule(string automationAccountName, Uri contentLink, string moduleName, IDictionary tags); - Runbook PublishRunbook(string automationAccountName, string runbookName); + Module GetModule(string automationAccountName, string name); - Runbook RegisterScheduledRunbook(string automationAccountName, Guid runbookId, IDictionary parameters, string scheduleName); + Module UpdateModule(string automationAccountName, IDictionary tags, string name, Uri contentLink, string contentLinkVersion); - Runbook RegisterScheduledRunbook(string automationAccountName, string runbookName, IDictionary parameters, string scheduleName); + IEnumerable ListModules(string automationAccountName); - void DeleteRunbook(string automationAccountName, Guid runbookId); + void DeleteModule(string automationAccountName, string name); + + #endregion - void DeleteRunbook(string automationAccountName, string runbookName); + #region Jobs - void DeleteSchedule(string automationAccountName, Guid scheduleId); + Job GetJob(string automationAccountName, Guid id); - void DeleteSchedule(string automationAccountName, string scheduleName); + IEnumerable ListJobsByRunbookName(string automationAccountName, string runbookName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); - void ResumeJob(string automationAccountName, Guid jobId); + IEnumerable ListJobs(string automationAccountName, DateTimeOffset? startTime, DateTimeOffset? endTime, string jobStatus); - Runbook UpdateRunbook(string automationAccountName, Guid runbookId, string description, string[] tags, bool? logDebug, bool? logProgress, bool? logVerbose); + void ResumeJob(string automationAccountName, Guid id); - Runbook UpdateRunbook(string automationAccountName, string runbookName, string description, string[] tags, bool? logDebug, bool? logProgress, bool? logVerbose); + void StopJob(string automationAccountName, Guid id); - RunbookDefinition UpdateRunbookDefinition(string automationAccountName, Guid runbookId, string runbookPath, bool overwrite); + void SuspendJob(string automationAccountName, Guid id); - RunbookDefinition UpdateRunbookDefinition(string automationAccountName, string runbookName, string runbookPath, bool overwrite); + #endregion - Schedule UpdateSchedule(string automationAccountName, Guid scheduleId, bool? isEnabled, string description); + #region Accounts - Schedule UpdateSchedule(string automationAccountName, string scheduleName, bool? isEnabled, string description); + IEnumerable ListAutomationAccounts(string automationAccountName, string location); + + AutomationAccount CreateAutomationAccount(string automationAccountName, string location); + + void DeleteAutomationAccount(string automationAccountName); - Job StartRunbook(string automationAccountName, Guid runbookId, IDictionary parameters); + #endregion - Job StartRunbook(string automationAccountName, string runbookName, IDictionary parameters); + #region Certificates + + CertificateInfo CreateCertificate(string automationAccountName, string name, string path, SecureString password, string description, bool exportable); + + CertificateInfo UpdateCertificate(string automationAccountName, string name, string path, SecureString password, string description, bool? exportable); + + CertificateInfo GetCertificate(string automationAccountName, string name); + + IEnumerable ListCertificates(string automationAccountName); + + void DeleteCertificate(string automationAccountName, string name); + + #endregion + + #region Connection + + Connection CreateConnection(string automationAccountName, string name, string connectionTypeName, IDictionary connectionFieldValues, string description); + + Connection UpdateConnectionFieldValue(string automationAccountName, string name, string connectionFieldName, object value); + + Connection GetConnection(string automationAccountName, string name); + + IEnumerable ListConnectionsByType(string automationAccountName, string name); + + IEnumerable ListConnections(string automationAccountName); + + void DeleteConnection(string automationAccountName, string name); + + #endregion + + #region JobSchedules + + JobSchedule GetJobSchedule(string automationAccountName, Guid jobScheduleId); + + JobSchedule GetJobSchedule(string automationAccountName, string runbookName, string scheduleName); + + IEnumerable ListJobSchedules(string automationAccountName); + + IEnumerable ListJobSchedulesByRunbookName(string automationAccountName, string runbookName); + + IEnumerable ListJobSchedulesByScheduleName(string automationAccountName, string scheduleName); + + JobSchedule RegisterScheduledRunbook(string automationAccountName, string runbookName, string scheduleName, IDictionary parameters); - void StopJob(string automationAccountName, Guid jobId); + void UnregisterScheduledRunbook(string automationAccountName, Guid jobScheduleId); - void SuspendJob(string automationAccountName, Guid jobId); + void UnregisterScheduledRunbook(string automationAccountName, string runbookName, string scheduleName); - Runbook UnregisterScheduledRunbook(string automationAccountName, Guid runbookId, string scheduleName); - Runbook UnregisterScheduledRunbook(string automationAccountName, string runbookName, string scheduleName); + #endregion } } \ No newline at end of file diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs new file mode 100644 index 000000000000..0340ec5a0ccf --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs @@ -0,0 +1,113 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Properties; +using System; +using System.Collections; +using System.Collections.ObjectModel; +using System.Globalization; +using System.Management.Automation; +using System.Management.Automation.Runspaces; +using System.Text; + +namespace Microsoft.Azure.Commands.Automation.Common +{ + public static class PowerShellJsonConverter + { + public static string Serialize(object inputObject) + { + if (inputObject == null) + { + return null; + } + + Hashtable parameters = new Hashtable(); + parameters.Add(Constants.PsCommandParamInputObject, inputObject); + parameters.Add(Constants.PsCommandParamDepth, Constants.PsCommandValueDepth); + var result = PowerShellJsonConverter.InvokeScript(Constants.PsCommandConvertToJson, parameters); + + if (result.Count != 1) + { + return null; + } + + return result[0].ToString(); + } + + public static PSObject Deserialize(string json) + { + if (String.IsNullOrEmpty(json)) + { + return null; + } + + Hashtable parameters = new Hashtable(); + parameters.Add(Constants.PsCommandParamInputObject, json); + var result = PowerShellJsonConverter.InvokeScript(Constants.PsCommandConvertFromJson, parameters); + if (result.Count != 1) + { + return null; + } + + //count == 1. return the first psobject + return result[0]; + } + + /// + /// Invokes a powershell script using the same runspace as the caller. + /// + /// script name + /// parameters for the script + /// + private static Collection InvokeScript(string scriptName, Hashtable parameters) + { + using (Pipeline pipe = Runspace.DefaultRunspace.CreateNestedPipeline()) + { + Command scriptCommand = new Command(scriptName); + + foreach (DictionaryEntry parameter in parameters) + { + CommandParameter commandParm = new CommandParameter(parameter.Key.ToString(), parameter.Value); + scriptCommand.Parameters.Add(commandParm); + } + pipe.Commands.Add(scriptCommand); + + var result = pipe.Invoke(); + + //Error handling + if (pipe.Error.Count > 0) + { + StringBuilder errorStringBuilder = new StringBuilder(); + while (!pipe.Error.EndOfPipeline) + { + var value = pipe.Error.Read() as PSObject; + if (value != null) + { + var r = value.BaseObject as ErrorRecord; + if (r != null) + { + errorStringBuilder.AppendLine(r.InvocationInfo.MyCommand.Name + " : " + r.Exception.Message); + errorStringBuilder.AppendLine(r.InvocationInfo.PositionMessage); + } + } + } + + throw new AzureAutomationOperationException(string.Format(CultureInfo.CurrentCulture, + Resources.PowershellJsonDecrypterFailed, errorStringBuilder.ToString())); + } + return result; + } + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/ResourceCommonException.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/ResourceCommonException.cs new file mode 100644 index 000000000000..cc9cdda0ee06 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/ResourceCommonException.cs @@ -0,0 +1,44 @@ +// ---------------------------------------------------------------------------------- +// +// 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; + +namespace Microsoft.Azure.Commands.Automation.Common +{ + [Serializable] + public class ResourceCommonException : Exception + { + public ResourceCommonException(Type resourceType, string message) + : base(message) + { + this.ResourceType = resourceType; + } + + [NonSerialized] + private Type resourceType; + + public Type ResourceType + { + get + { + return this.resourceType; + } + + private set + { + this.resourceType = value; + } + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/JobParameter.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/StreamType.cs similarity index 56% rename from src/ServiceManagement/Automation/Commands.Automation/Model/JobParameter.cs rename to src/ServiceManagement/Automation/Commands.Automation/Common/StreamType.cs index 72827e525e0c..bdbaa7e80993 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/JobParameter.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/StreamType.cs @@ -12,42 +12,47 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -namespace Microsoft.Azure.Commands.Automation.Model -{ - using AutomationManagement = Management.Automation; +namespace Microsoft.Azure.Commands.Automation.Common +{ /// - /// The job parameter. + /// StreamType enum represents the 6 types of Powershell Streams supported. /// - public class JobParameter + public enum StreamType { - public JobParameter(AutomationManagement.Models.JobParameter jobParameter) - { - this.Name = jobParameter.Name; - this.Value = jobParameter.Value; - this.Type = jobParameter.Type; - } + /// + /// Indicates Progress Record streams + /// + Progress, + + /// + /// Indicates Output Record streams + /// + Output, + + /// + /// Indicates Warning Record streams + /// + Warning, /// - /// Initializes a new instance of the class. + /// Indicates Error Record streams /// - public JobParameter() - { - } + Error, /// - /// Gets or sets the name. + /// Indicates Debug Record streams /// - public string Name { get; set; } + Debug, /// - /// Gets or sets the value. + /// Indicates Verbose Record streams /// - public string Value { get; set; } + Verbose, /// - /// Gets or sets the type. + /// Indicates Generic stream. Used for querying all the streams regardless of the type. /// - public string Type { get; set; } + Any } -} \ No newline at end of file +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/VariableUpdateFields.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/VariableUpdateFields.cs new file mode 100644 index 000000000000..134b2501bb55 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/VariableUpdateFields.cs @@ -0,0 +1,24 @@ +// ---------------------------------------------------------------------------------- +// +// 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; + +namespace Microsoft.Azure.Commands.Automation.Common +{ + public enum VariableUpdateFields + { + OnlyDescription = 0, + OnlyValue = 1 + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/DataContract/ErrorResponse.cs b/src/ServiceManagement/Automation/Commands.Automation/DataContract/ErrorResponse.cs index aba21a04144b..46b469e1f9eb 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/DataContract/ErrorResponse.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/DataContract/ErrorResponse.cs @@ -23,9 +23,15 @@ namespace Microsoft.Azure.Commands.Automation.DataContract public class ErrorResponse { /// - /// Gets or sets the odata error. + /// Gets or sets the code. /// - [DataMember(Name = "odata.error")] - public OdataError OdataError { get; set; } + [DataMember(Name = "code")] + public string Code { get; set; } + + /// + /// Gets or sets the message. + /// + [DataMember(Name = "message")] + public string Message { get; set; } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/AutomationAccount.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/AutomationAccount.cs index f3259abd7d35..98cf0f26b108 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/AutomationAccount.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/AutomationAccount.cs @@ -16,7 +16,7 @@ namespace Microsoft.Azure.Commands.Automation.Model { - using AutomationManagement = Management.Automation; + using AutomationManagement = Microsoft.WindowsAzure.Management.Automation; /// /// The automation account. @@ -32,17 +32,13 @@ public class AutomationAccount /// /// The resource. /// - public AutomationAccount( - AutomationManagement.Models.CloudService cloudService, - AutomationManagement.Models.AutomationResource resource) + public AutomationAccount(AutomationManagement.Models.CloudService cloudService, AutomationManagement.Models.AutomationResource resource) { Requires.Argument("cloudService", cloudService).NotNull(); Requires.Argument("resource", resource).NotNull(); this.AutomationAccountName = resource.Name; this.Location = cloudService.GeoRegion; - this.Plan = resource.Plan; - switch (resource.State) { case AutomationManagement.Models.AutomationResourceState.Started: @@ -55,6 +51,8 @@ public AutomationAccount( this.State = resource.State; break; } + + if (resource.IntrinsicSettings != null) this.Plan = resource.IntrinsicSettings.SubscriptionPlan; } /// @@ -65,9 +63,9 @@ public AutomationAccount() } /// - /// Gets or sets the plan. + /// Gets or sets the automation account name. /// - public string Plan { get; set; } + public string AutomationAccountName { get; set; } /// /// Gets or sets the location. @@ -80,8 +78,8 @@ public AutomationAccount() public string State { get; set; } /// - /// Gets or sets the automation account name. + /// Gets or sets the plan. /// - public string AutomationAccountName { get; set; } + public string Plan { get; set; } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/CertificateInfo.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/CertificateInfo.cs new file mode 100644 index 000000000000..7849fef2d9d2 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/CertificateInfo.cs @@ -0,0 +1,76 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; +using System; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + public class CertificateInfo + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The account name. + /// + /// + /// The connection. + /// + /// + /// + public CertificateInfo(string accountAcccountName, WindowsAzure.Management.Automation.Models.Certificate certificate) + { + Requires.Argument("certificate", certificate).NotNull(); + this.AutomationAccountName = accountAcccountName; + this.Name = certificate.Name; + + if (certificate.Properties == null) return; + + this.Description = certificate.Properties.Description; + this.CreationTime = certificate.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = certificate.Properties.LastModifiedTime.ToLocalTime(); + this.ExpiryTime = certificate.Properties.ExpiryTime.ToLocalTime(); + this.Thumbprint = certificate.Properties.Thumbprint; + this.Exportable = certificate.Properties.IsExportable; + } + + /// + /// Initializes a new instance of the class. + /// + public CertificateInfo() + { + } + + /// + /// Gets or sets the automaiton account name. + /// + public string AutomationAccountName { get; set; } + + public string Name { get; set; } + + public string Thumbprint { get; set; } + + public string Description { get; set; } + + public bool Exportable { get; set; } + + public DateTimeOffset CreationTime { get; set; } + + public DateTimeOffset LastModifiedTime { get; set; } + + public DateTimeOffset ExpiryTime { get; set; } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/Connection.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/Connection.cs new file mode 100644 index 000000000000..ca2d4f15af33 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/Connection.cs @@ -0,0 +1,79 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; +using System; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + public class Connection + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The account name. + /// + /// + /// The connection. + /// + /// + /// + public Connection(string accountAcccountName, WindowsAzure.Management.Automation.Models.Connection connection) + { + Requires.Argument("connection", connection).NotNull(); + this.AutomationAccountName = accountAcccountName; + this.Name = connection.Name; + + if (connection.Properties == null) return; + + this.Description = connection.Properties.Description; + this.CreationTime = connection.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = connection.Properties.LastModifiedTime.ToLocalTime(); + this.ConnectionTypeName = connection.Properties.ConnectionType.Name; + this.FieldDefinitionValues = new Hashtable(StringComparer.InvariantCultureIgnoreCase); + foreach (var kvp in connection.Properties.FieldDefinitionValues) + { + this.FieldDefinitionValues.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); + } + } + + /// + /// Initializes a new instance of the class. + /// + public Connection() + { + } + + /// + /// Gets or sets the automaiton account name. + /// + public string AutomationAccountName { get; set; } + + public string Name { get; set; } + + public string Description { get; set; } + + public DateTimeOffset CreationTime { get; set; } + + public DateTimeOffset LastModifiedTime { get; set; } + + public string ConnectionTypeName { get; set; } + + public Hashtable FieldDefinitionValues { get; set; } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/CredentialInfo.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/CredentialInfo.cs new file mode 100644 index 000000000000..b4cb41f3f100 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/CredentialInfo.cs @@ -0,0 +1,63 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; +using System; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + public class CredentialInfo + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The Credential. + /// + public CredentialInfo(string accountAcccountName, WindowsAzure.Management.Automation.Models.Credential credential) + { + Requires.Argument("credential", credential).NotNull(); + this.AutomationAccountName = accountAcccountName; + this.Name = credential.Name; + + if (credential.Properties == null) return; + + this.Description = credential.Properties.Description; + this.CreationTime = credential.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = credential.Properties.LastModifiedTime.ToLocalTime(); + this.UserName = credential.Properties.UserName; + } + + /// + /// Initializes a new instance of the class. + /// + public CredentialInfo() + { + } + + /// + /// Gets or sets the automaiton account name. + /// + public string AutomationAccountName { get; set; } + public string Name { get; set; } + public string UserName { get; set; } + + public string Description { get; set; } + + public DateTimeOffset CreationTime { get; set; } + + public DateTimeOffset LastModifiedTime { get; set; } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/DailySchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/DailySchedule.cs deleted file mode 100644 index 1ebb28e058ca..000000000000 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/DailySchedule.cs +++ /dev/null @@ -1,69 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// 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.Globalization; -using Microsoft.Azure.Commands.Automation.Common; -using Microsoft.Azure.Commands.Automation.Properties; - -namespace Microsoft.Azure.Commands.Automation.Model -{ - /// - /// The daily schedule. - /// - public class DailySchedule : Schedule - { - /// - /// Initializes a new instance of the class. - /// - /// - /// The schedule. - /// - public DailySchedule(Azure.Management.Automation.Models.Schedule schedule) - { - Requires.Argument("schedule", schedule).NotNull(); - - if (!schedule.DayInterval.HasValue) - { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDailyScheduleModel, schedule.Name)); - } - - this.Id = new Guid(schedule.Id); - this.AccountId = new Guid(schedule.AccountId); - this.Name = schedule.Name; - this.Description = schedule.Description; - this.StartTime = DateTime.SpecifyKind(schedule.StartTime, DateTimeKind.Utc).ToLocalTime(); - this.ExpiryTime = DateTime.SpecifyKind(schedule.ExpiryTime, DateTimeKind.Utc).ToLocalTime(); - this.CreationTime = DateTime.SpecifyKind(schedule.CreationTime, DateTimeKind.Utc).ToLocalTime(); - this.LastModifiedTime = DateTime.SpecifyKind(schedule.LastModifiedTime, DateTimeKind.Utc).ToLocalTime(); - this.IsEnabled = schedule.IsEnabled; - this.NextRun = schedule.NextRun.HasValue - ? DateTime.SpecifyKind(schedule.NextRun.Value, DateTimeKind.Utc).ToLocalTime() - : this.NextRun; - this.DayInterval = schedule.DayInterval.Value; - } - - /// - /// Initializes a new instance of the class. - /// - public DailySchedule() - { - } - - /// - /// Gets or sets The next run time of the schedule. - /// - public int DayInterval { get; set; } - } -} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/HourlySchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/HourlySchedule.cs deleted file mode 100644 index 343e87c66ae2..000000000000 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/HourlySchedule.cs +++ /dev/null @@ -1,69 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// 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 Microsoft.Azure.Commands.Automation.Common; - using Microsoft.Azure.Commands.Automation.Properties; - using System; - using System.Globalization; - - /// - /// The hourly schedule. - /// - public class HourlySchedule : Schedule - { - /// - /// Initializes a new instance of the class. - /// - /// - /// The schedule. - /// - public HourlySchedule(Azure.Management.Automation.Models.Schedule schedule) - { - Requires.Argument("schedule", schedule).NotNull(); - - if (!schedule.HourInterval.HasValue) - { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidHourlyScheduleModel, schedule.Name)); - } - - this.Id = new Guid(schedule.Id); - this.AccountId = new Guid(schedule.AccountId); - this.Name = schedule.Name; - this.Description = schedule.Description; - this.StartTime = DateTime.SpecifyKind(schedule.StartTime, DateTimeKind.Utc).ToLocalTime(); - this.ExpiryTime = DateTime.SpecifyKind(schedule.ExpiryTime, DateTimeKind.Utc).ToLocalTime(); - this.CreationTime = DateTime.SpecifyKind(schedule.CreationTime, DateTimeKind.Utc).ToLocalTime(); - this.LastModifiedTime = DateTime.SpecifyKind(schedule.LastModifiedTime, DateTimeKind.Utc).ToLocalTime(); - this.IsEnabled = schedule.IsEnabled; - this.NextRun = schedule.NextRun.HasValue - ? DateTime.SpecifyKind(schedule.NextRun.Value, DateTimeKind.Utc).ToLocalTime() - : this.NextRun; - this.HourInterval = schedule.HourInterval.Value; - } - - /// - /// Initializes a new instance of the class. - /// - public HourlySchedule() - { - } - - /// - /// Gets or sets The next run time of the schedule. - /// - public int HourInterval { get; set; } - } -} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/Job.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/Job.cs index 74438b290ca9..691b0ff25fb2 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/Job.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/Job.cs @@ -1,6 +1,4 @@ -// ---------------------------------------------------------------------------------- -// -// Copyright Microsoft Corporation +// 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 @@ -12,57 +10,56 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using System.Collections; +using System.Globalization; +using Microsoft.Azure.Commands.Automation.Common; using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; -using Microsoft.Azure.Commands.Automation.Common; -using Microsoft.Azure.Commands.Automation.Properties; namespace Microsoft.Azure.Commands.Automation.Model { - using AutomationManagement = Management.Automation; - /// - /// The job. + /// The Job object. /// public class Job { /// /// Initializes a new instance of the class. /// - /// The job - /// The resource. - /// - /// Initializes a new instance of the class. - /// - public Job(AutomationManagement.Models.Job job) + /// + /// The account name. + /// + /// + /// The Job. + /// + /// + /// + public Job(string accountName, WindowsAzure.Management.Automation.Models.Job job) { Requires.Argument("job", job).NotNull(); - - if (job.Context == null || job.Context.RunbookVersion == null || job.Context.RunbookVersion.Runbook == null || job.Context.JobParameters == null) + 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; + this.Status = job.Properties.Status; + this.StatusDetails = job.Properties.StatusDetails; + this.RunbookName = job.Properties.Runbook.Name; + this.Exception = job.Properties.Exception; + this.EndTime = job.Properties.EndTime; + 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))) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidJobModel)); + this.JobParameters.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); } - - this.Id = new Guid(job.Id); - this.AccountId = new Guid(job.AccountId); - this.Exception = job.Exception; - this.CreationTime = DateTime.SpecifyKind(job.CreationTime, DateTimeKind.Utc).ToLocalTime(); - this.LastModifiedTime = DateTime.SpecifyKind(job.LastModifiedTime, DateTimeKind.Utc).ToLocalTime(); - this.StartTime = job.StartTime.HasValue - ? DateTime.SpecifyKind(job.StartTime.Value, DateTimeKind.Utc).ToLocalTime() - : this.StartTime; - this.EndTime = job.EndTime.HasValue - ? DateTime.SpecifyKind(job.EndTime.Value, DateTimeKind.Utc).ToLocalTime() - : this.EndTime; - this.LastStatusModifiedTime = DateTime.SpecifyKind(job.LastStatusModifiedTime, DateTimeKind.Utc).ToLocalTime(); - this.Status = job.Status; - this.StatusDetails = job.StatusDetails; - this.RunbookId = new Guid(job.Context.RunbookVersion.Runbook.Id); - this.RunbookName = job.Context.RunbookVersion.Runbook.Name; - this.ScheduleName = job.Context.Schedule != null ? job.Context.Schedule.Name : string.Empty; - this.JobParameters = from jobParameter in job.Context.JobParameters where jobParameter.Name != Constants.JobStartedByParameterName select new JobParameter(jobParameter); } /// @@ -73,73 +70,63 @@ public Job() } /// - /// Gets or sets the job id. + /// Gets or sets the automaiton account name. /// - public Guid Id { get; set; } + public string AutomationAccountName { get; set; } /// - /// Gets or sets the account id. - /// - public Guid AccountId { get; set; } - - /// - /// Gets or sets the job status. - /// - public string Status { get; set; } - - /// - /// Gets or sets the job status details. + /// Gets or sets the job id. /// - public string StatusDetails { get; set; } + public Guid Id { get; set; } /// - /// Gets or sets the start time. + /// Gets or sets the tags. /// - public DateTime? StartTime { get; set; } + public DateTimeOffset CreationTime { get; set; } /// - /// Gets or sets the end time. + /// Gets or sets the status of the job. /// - public DateTime? EndTime { get; set; } + public string Status { get; set; } /// - /// Gets or sets the creation time. + /// Gets or sets the status details of the job. /// - public DateTime CreationTime { get; set; } + public string StatusDetails { get; set; } /// - /// Gets or sets the last modified time. + /// Gets or sets the start time of the job. /// - public DateTime LastModifiedTime { get; set; } + public DateTimeOffset StartTime { get; set; } /// - /// Gets or sets the last time when the job status was modified. + /// Gets or sets the end time of the job. /// - public DateTime LastStatusModifiedTime { get; set; } + public DateTimeOffset EndTime { get; set; } /// - /// Gets or sets the job exception. + /// Gets or sets the exception of the job. /// public string Exception { get; set; } /// - /// Gets or sets the runnook id. + /// Gets or sets the last modified time of the job. /// - public Guid RunbookId { get; set; } + public DateTimeOffset LastModifiedTime { get; set; } /// - /// Gets or sets the runbook name. + /// Gets or sets the last status modified time of the job." /// - public string RunbookName { get; set; } + public DateTimeOffset LastStatusModifiedTime { get; set; } /// - /// Gets or sets the schedule name. + /// Gets or sets the parameters of the job. /// - public string ScheduleName { get; set; } + public Hashtable JobParameters { get; set; } /// - /// Gets or sets the job parameters. + /// Gets or sets the runbook. /// - public IEnumerable JobParameters { get; set; } + public string RunbookName { get; set; } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/JobSchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/JobSchedule.cs new file mode 100644 index 000000000000..a85261f53fc7 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/JobSchedule.cs @@ -0,0 +1,82 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Globalization; +using Microsoft.Azure.Commands.Automation.Common; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + /// + /// The Job Schedule. + /// + public class JobSchedule + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The job schedule. + /// + public JobSchedule(string automationAccountName, WindowsAzure.Management.Automation.Models.JobSchedule jobSchedule) + { + Requires.Argument("jobSchedule", jobSchedule).NotNull(); + this.AutomationAccountName = automationAccountName; + this.JobScheduleId = jobSchedule.Properties.Id; + this.RunbookName = jobSchedule.Properties.Runbook.Name; + this.ScheduleName = jobSchedule.Properties.Schedule.Name; + this.Parameters = new Hashtable(StringComparer.InvariantCultureIgnoreCase); + foreach (var kvp in jobSchedule.Properties.Parameters.Where(kvp => 0 != String.Compare(kvp.Key, Constants.JobStartedByParameterName, CultureInfo.InvariantCulture, + CompareOptions.IgnoreCase))) + { + this.Parameters.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); + } + } + + /// + /// Initializes a new instance of the class. + /// + public JobSchedule() + { + } + + /// + /// Gets or sets the automation account name. + /// + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the job schedule id. + /// + public string JobScheduleId { get; set; } + + /// + /// Gets or sets the runbook name. + /// + public string RunbookName { get; set; } + + /// + /// Gets or sets the schedule name. + /// + public string ScheduleName { get; set; } + + /// + /// Gets or sets the runbook parameters. + /// + public Hashtable Parameters { get; set; } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/JobStreamItem.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/JobStream.cs similarity index 55% rename from src/ServiceManagement/Automation/Commands.Automation/Model/JobStreamItem.cs rename to src/ServiceManagement/Automation/Commands.Automation/Model/JobStream.cs index 764e721d36cc..50b1233e04e5 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/JobStreamItem.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/JobStream.cs @@ -17,66 +17,68 @@ namespace Microsoft.Azure.Commands.Automation.Model { - using AutomationManagement = Management.Automation; + using AutomationManagement = WindowsAzure.Management.Automation; /// - /// The JobStreamItem. + /// The Job Stream. /// - public class JobStreamItem + public class JobStream { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - /// - /// The job stream item. + /// + /// The job stream. /// - public JobStreamItem(AutomationManagement.Models.JobStreamItem jobStreamItem) + /// + /// + public JobStream(AutomationManagement.Models.JobStream jobStream, string automationAccountName, Guid jobId ) { - Requires.Argument("jobStreamItem", jobStreamItem).NotNull(); + Requires.Argument("jobStream", jobStream).NotNull(); - this.AccountId = new Guid(jobStreamItem.AccountId); - this.JobId = new Guid(jobStreamItem.JobId); - this.RunbookVersionId = new Guid(jobStreamItem.RunbookVersionId); - this.Text = jobStreamItem.Text; - this.Time = DateTime.SpecifyKind(jobStreamItem.Time, DateTimeKind.Utc).ToLocalTime(); - this.Type = jobStreamItem.Type; + 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. + /// Initializes a new instance of the class. /// - public JobStreamItem() + public JobStream() { } /// - /// Gets or sets the account id. + /// Gets or sets the automation account name. /// - public Guid AccountId { get; set; } + public string AutomationAccountName { get; set; } /// - /// Gets or sets the job id. + /// Gets or sets the Job Id. /// - public Guid JobId { get; set; } + public Guid Id { get; set; } /// - /// Gets or sets the runbook version id. + /// Gets or sets the stream id /// - public Guid RunbookVersionId { get; set; } + public string JobStreamId { get; set; } /// - /// Gets or sets the text. + /// Gets or sets the stream time. /// - public string Text { get; set; } + public DateTimeOffset Time { get; set; } /// - /// Gets or sets the time. + /// Gets or sets the stream text. /// - public DateTime Time { get; set; } + public string Text { get; set; } /// - /// Gets or sets the type. + /// Gets or sets the stream Type. /// public string Type { get; set; } } -} \ No newline at end of file +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/Module.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/Module.cs new file mode 100644 index 000000000000..896e889647fc --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/Module.cs @@ -0,0 +1,147 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; +using System; +using System.Collections.Generic; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + public class Module + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The Module. + /// + public Module(string automationAccountName, WindowsAzure.Management.Automation.Models.Module module) + { + Requires.Argument("module", module).NotNull(); + this.AutomationAccountName = automationAccountName; + this.Name = module.Name; + this.Location = module.Location; + this.Tags = new Hashtable(StringComparer.InvariantCultureIgnoreCase); + foreach (var kvp in module.Tags) + { + this.Tags.Add(kvp.Key, kvp.Value); + } + + if (module.Properties == null) return; + + this.CreationTime = module.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = module.Properties.LastModifiedTime.ToLocalTime(); + this.IsGlobal = module.Properties.IsGlobal; + this.Version = module.Properties.Version; + this.ProvisioningState = module.Properties.ProvisioningState.ToString(); + this.ActivityCount = module.Properties.ActivityCount; + this.SizeInBytes = module.Properties.SizeInBytes; + } + + /// + /// Initializes a new instance of the class. + /// + public Module() + { + } + + /// + /// Gets or sets the automaiton account name. + /// + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the name. + /// + public string Name { get; set; } + + /// + /// Gets or sets the location. + /// + public string Location { get; set; } + + /// + /// Gets or sets the tags. + /// + public Hashtable Tags { get; set; } + + /// + /// Gets or sets the IsGlobal. + /// + public bool IsGlobal { get; set; } + + /// + /// Gets or sets the Version. + /// + public string Version { get; set; } + + /// + /// Gets or sets the SizeInBytes. + /// + public long SizeInBytes { get; set; } + + /// + /// Gets or sets the ActivityCount. + /// + public int ActivityCount { get; set; } + + /// + /// Gets or sets the CreationTime. + /// + public DateTimeOffset CreationTime { get; set; } + + /// + /// Gets or sets the LastPublishTime. + /// + public DateTimeOffset LastModifiedTime { get; set; } + + /// + /// Gets or sets the ProvisioningState. + /// + public string ProvisioningState { get; set; } + } + + public class ContentLink + { + /// + /// Gets or sets the Uri. + /// + public Uri Uri { get; set; } + + /// + /// Gets or sets the Content hash. + /// + public ContentHash ContentHash { get; set; } + + /// + /// Gets or sets the Version. + /// + public string Version { get; set; } + } + + public class ContentHash + { + /// + /// Gets or sets the Algorithm. + /// + public string Algorithm { get; set; } + + /// + /// Gets or sets the Value. + /// + public string Value { get; set; } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/OneTimeSchedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/OneTimeSchedule.cs deleted file mode 100644 index b1b48b5a3760..000000000000 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/OneTimeSchedule.cs +++ /dev/null @@ -1,56 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// 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 -{ - /// - /// The one time schedule. - /// - public class OneTimeSchedule : Schedule - { - /// - /// Initializes a new instance of the class. - /// - /// - /// The schedule. - /// - public OneTimeSchedule(Azure.Management.Automation.Models.Schedule schedule) - { - Requires.Argument("schedule", schedule).NotNull(); - - this.Id = new Guid(schedule.Id); - this.AccountId = new Guid(schedule.AccountId); - this.Name = schedule.Name; - this.Description = schedule.Description; - this.StartTime = DateTime.SpecifyKind(schedule.StartTime, DateTimeKind.Utc).ToLocalTime(); - this.ExpiryTime = DateTime.SpecifyKind(schedule.ExpiryTime, DateTimeKind.Utc).ToLocalTime(); - this.CreationTime = DateTime.SpecifyKind(schedule.CreationTime, DateTimeKind.Utc).ToLocalTime(); - this.LastModifiedTime = DateTime.SpecifyKind(schedule.LastModifiedTime, DateTimeKind.Utc).ToLocalTime(); - this.IsEnabled = schedule.IsEnabled; - this.NextRun = schedule.NextRun.HasValue - ? DateTime.SpecifyKind(schedule.NextRun.Value, DateTimeKind.Utc).ToLocalTime() - : this.NextRun; - } - - /// - /// Initializes a new instance of the class. - /// - public OneTimeSchedule() - { - } - } -} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/Runbook.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/Runbook.cs index 9cd51f2478fd..78b913dbb5d5 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/Runbook.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/Runbook.cs @@ -13,15 +13,16 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections; using System.Collections.Generic; -using System.Globalization; using System.Linq; using Microsoft.Azure.Commands.Automation.Common; -using Microsoft.Azure.Commands.Automation.Properties; +using Microsoft.WindowsAzure.Management.Automation.Models; +using Microsoft.WindowsAzure.Commands.Common; namespace Microsoft.Azure.Commands.Automation.Model { - using AutomationManagement = Management.Automation; + using AutomationManagement = WindowsAzure.Management.Automation; /// /// The Runbook. @@ -31,44 +32,41 @@ public class Runbook /// /// Initializes a new instance of the class. /// + /// + /// The account name. + /// /// /// The runbook. /// /// /// - public Runbook(AutomationManagement.Models.Runbook runbook) + public Runbook(string accountName, AutomationManagement.Models.Runbook runbook) { Requires.Argument("runbook", runbook).NotNull(); - if (runbook.Schedules == null) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidRunbookModel)); - } + Requires.Argument("accountName", accountName).NotNull(); - this.AccountId = new Guid(runbook.AccountId); - this.Id = new Guid(runbook.Id); + this.AutomationAccountName = accountName; this.Name = runbook.Name; - this.CreationTime = DateTime.SpecifyKind(runbook.CreationTime, DateTimeKind.Utc).ToLocalTime(); - this.LastModifiedTime = DateTime.SpecifyKind(runbook.LastModifiedTime, DateTimeKind.Utc).ToLocalTime(); - this.LastModifiedBy = runbook.LastModifiedBy; - this.Description = runbook.Description; - this.IsApiOnly = runbook.IsApiOnly; - this.IsGlobal = runbook.IsGlobal; - - if (runbook.PublishedRunbookVersionId != null) - { - this.PublishedRunbookVersionId = new Guid(runbook.PublishedRunbookVersionId); - } + this.Location = runbook.Location; + this.Tags = null; + + if (runbook.Properties == null) return; + + this.CreationTime = runbook.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = runbook.Properties.LastModifiedTime.ToLocalTime(); + this.Description = runbook.Properties.Description; + + this.LogVerbose = runbook.Properties.LogVerbose; + this.LogProgress = runbook.Properties.LogProgress; + this.State = runbook.Properties.State; + this.JobCount = runbook.Properties.JobCount; + this.RunbookType = runbook.Properties.RunbookType; - if (runbook.DraftRunbookVersionId != null) + this.Parameters = new Hashtable(StringComparer.InvariantCultureIgnoreCase); + foreach (var kvp in runbook.Properties.Parameters) { - this.DraftRunbookVersionId = new Guid(runbook.DraftRunbookVersionId); + this.Parameters.Add(kvp.Key, (object)kvp.Value); } - - this.Tags = runbook.Tags != null ? runbook.Tags.Split(Constants.RunbookTagsSeparatorChar) : new string[] { }; - this.LogDebug = runbook.LogDebug; - this.LogVerbose = runbook.LogVerbose; - this.LogProgress = runbook.LogProgress; - this.ScheduleNames = from schedule in runbook.Schedules where (schedule.NextRun != null) select schedule.Name; } /// @@ -79,14 +77,9 @@ public Runbook() } /// - /// Gets or sets the account id. - /// - public Guid AccountId { get; set; } - - /// - /// Gets or sets the id. + /// Gets or sets the automation account name. /// - public Guid Id { get; set; } + public string AutomationAccountName { get; set; } /// /// Gets or sets the name. @@ -94,54 +87,44 @@ public Runbook() public string Name { get; set; } /// - /// Gets or sets the creation time. - /// - public DateTime CreationTime { get; set; } - - /// - /// Gets or sets the last modified time. - /// - public DateTime LastModifiedTime { get; set; } - - /// - /// Gets or sets the last modified by. + /// Gets or sets the location. /// - public string LastModifiedBy { get; set; } + public string Location { get; set; } /// - /// Gets or sets the description. + /// Gets or sets the tags. /// - public string Description { get; set; } + public string[] Tags { get; set; } /// - /// Gets or sets a value indicating whether is api only. + /// Gets or sets the JobCount. /// - public bool IsApiOnly { get; set; } + public int JobCount { get; set; } /// - /// Gets or sets a value indicating whether is global. + /// Gets or sets the runbook type. /// - public bool IsGlobal { get; set; } + public string RunbookType { get; set; } /// - /// Gets or sets the published runbook version id. + /// Gets or sets the creation time. /// - public Guid? PublishedRunbookVersionId { get; set; } + public DateTimeOffset CreationTime { get; set; } /// - /// Gets or sets the draft runbook version id. + /// Gets or sets the last modified time. /// - public Guid? DraftRunbookVersionId { get; set; } + public DateTimeOffset LastModifiedTime { get; set; } /// - /// Gets or sets the tags. + /// Gets or sets the description. /// - public string[] Tags { get; set; } + public string Description { get; set; } /// - /// Gets or sets a value indicating whether log debug is enabled. + /// Gets or sets the parameters. /// - public bool LogDebug { get; set; } + public Hashtable Parameters { get; set; } /// /// Gets or sets a value indicating whether log verbose is enabled. @@ -154,8 +137,8 @@ public Runbook() public bool LogProgress { get; set; } /// - /// Gets or sets the schedule names. + /// Gets or sets the state of runbook. /// - public IEnumerable ScheduleNames { get; set; } + public string State { get; set; } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookDefinition.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookDefinition.cs index 0a8fc7698c34..27af7b494610 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookDefinition.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookDefinition.cs @@ -12,9 +12,12 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using System; +using Microsoft.Azure.Commands.Automation.Common; + namespace Microsoft.Azure.Commands.Automation.Model { - using AutomationManagement = Management.Automation; + using AutomationManagement = WindowsAzure.Management.Automation; /// /// The Runbook Definition. @@ -24,16 +27,35 @@ public class RunbookDefinition /// /// Initializes a new instance of the class. /// - /// + /// + /// The runbook version. + /// + /// /// The runbook version. /// /// /// The content. /// - public RunbookDefinition(AutomationManagement.Models.RunbookVersion runbookVersion, byte[] content) + /// + /// Slot published or draft. + /// + public RunbookDefinition(string accountName, AutomationManagement.Models.Runbook runbook, string content, string slot) { - this.RunbookVersion = new RunbookVersion(runbookVersion); - this.Content = System.Text.Encoding.UTF8.GetString(content); + Requires.Argument("runbook", runbook).NotNull(); + Requires.Argument("accountName", accountName).NotNull(); + Requires.Argument("slot", slot).NotNull(); + + this.AutomationAccountName = accountName; + this.Name = runbook.Name; + this.Content = content; + + if (runbook.Properties == null) return; + + this.CreationTime = runbook.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = runbook.Properties.LastModifiedTime.ToLocalTime(); + this.Slot = slot; + this.RunbookType = runbook.Properties.RunbookType; + } /// @@ -44,9 +66,34 @@ public RunbookDefinition() } /// - /// Gets or sets the runbook version. + /// Gets or sets the automaiton account name. + /// + public string AutomationAccountName { get; set; } + + /// + /// Gets or sets the name. + /// + public string Name { get; set; } + + /// + /// Gets or sets the slot (publised or draft) of runbook. + /// + public string Slot { get; set; } + + /// + /// Gets or sets the runbook type. + /// + public string RunbookType { get; set; } + + /// + /// Gets or sets the creation time. + /// + public DateTimeOffset CreationTime { get; set; } + + /// + /// Gets or sets the last modified time. /// - public RunbookVersion RunbookVersion { get; set; } + public DateTimeOffset LastModifiedTime { get; set; } /// /// Gets or sets the runbook version content. diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookParameter.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookParameter.cs deleted file mode 100644 index c994b9aabc47..000000000000 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookParameter.cs +++ /dev/null @@ -1,67 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// 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; - -namespace Microsoft.Azure.Commands.Automation.Model -{ - using AutomationManagement = Management.Automation; - - /// - /// The runbook parameter. - /// - public class RunbookParameter - { - public RunbookParameter(AutomationManagement.Models.RunbookParameter runbookParameter) - { - this.RunbookVersionId = new Guid(runbookParameter.RunbookVersionId); - this.Name = runbookParameter.Name; - this.Type = runbookParameter.Type; - this.IsMandatory = runbookParameter.IsMandatory; - this.Position = runbookParameter.Position; - } - - /// - /// Initializes a new instance of the class. - /// - public RunbookParameter() - { - } - - /// - /// Gets or sets the runbook version id. - /// - public Guid RunbookVersionId { get; set; } - - /// - /// Gets or sets the name. - /// - public string Name { get; set; } - - /// - /// Gets or sets the type. - /// - public string Type { get; set; } - - /// - /// Gets or sets a value indicating whether is mandatory. - /// - public bool IsMandatory { get; set; } - - /// - /// Gets or sets the position. - /// - public int Position { get; set; } - } -} \ No newline at end of file diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookVersion.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookVersion.cs deleted file mode 100644 index 11d4331ba8ed..000000000000 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/RunbookVersion.cs +++ /dev/null @@ -1,88 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// 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 = Management.Automation; - - /// - /// The Runbook. - /// - public class RunbookVersion - { - /// - /// Initializes a new instance of the class. - /// - /// - /// The runbook version. - /// - public RunbookVersion(AutomationManagement.Models.RunbookVersion runbookVersion) - { - Requires.Argument("runbookVersion", runbookVersion).NotNull(); - - this.AccountId = new Guid(runbookVersion.AccountId); - this.Id = new Guid(runbookVersion.Id); - this.RunbookId = new Guid(runbookVersion.RunbookId); - this.VersionNumber = runbookVersion.VersionNumber; - this.IsDraft = runbookVersion.IsDraft; - this.CreationTime = DateTime.SpecifyKind(runbookVersion.CreationTime, DateTimeKind.Utc).ToLocalTime(); - this.LastModifiedTime = DateTime.SpecifyKind(runbookVersion.LastModifiedTime, DateTimeKind.Utc).ToLocalTime(); - } - - /// - /// Initializes a new instance of the class. - /// - public RunbookVersion() - { - } - - /// - /// Gets or sets the account id. - /// - public Guid AccountId { get; set; } - - /// - /// Gets or sets the id. - /// - public Guid Id { get; set; } - - /// - /// Gets or sets the runbook id. - /// - public Guid RunbookId { get; set; } - - /// - /// Gets or sets the version number. - /// - public int VersionNumber { get; set; } - - /// - /// Gets or sets a value indicating whether is draft. - /// - public bool IsDraft { get; set; } - - /// - /// Gets or sets the creation time. - /// - public DateTime CreationTime { get; set; } - - /// - /// Gets or sets the last modified time. - /// - public DateTime LastModifiedTime { get; set; } - } -} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/Schedule.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/Schedule.cs index 661c300f92da..773ba84a798f 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/Schedule.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/Schedule.cs @@ -12,6 +12,8 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Properties; using System; namespace Microsoft.Azure.Commands.Automation.Model @@ -19,17 +21,43 @@ namespace Microsoft.Azure.Commands.Automation.Model /// /// The Schedule. /// - public abstract class Schedule + public class Schedule { /// - /// Gets or sets the id. + /// Initializes a new instance of the class. /// - public Guid Id { get; set; } + /// + /// The schedule. + /// + public Schedule(string automationAccountName, WindowsAzure.Management.Automation.Models.Schedule schedule) + { + Requires.Argument("schedule", schedule).NotNull(); + this.AutomationAccountName = automationAccountName; + this.Name = schedule.Name; + this.Description = schedule.Properties.Description; + this.StartTime = schedule.Properties.StartTime.ToLocalTime(); + this.ExpiryTime = schedule.Properties.ExpiryTime.ToLocalTime(); + this.CreationTime = schedule.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = schedule.Properties.LastModifiedTime.ToLocalTime(); + this.IsEnabled = schedule.Properties.IsEnabled; + this.NextRun = schedule.Properties.NextRun.HasValue + ? schedule.Properties.NextRun.Value.ToLocalTime() + : this.NextRun; + this.Interval = schedule.Properties.Interval.HasValue ? schedule.Properties.Interval.Value : this.Interval; + this.Frequency = (ScheduleFrequency)Enum.Parse(typeof(ScheduleFrequency), schedule.Properties.Frequency, true); + } /// - /// Gets or sets the account id. + /// Initializes a new instance of the class. /// - public Guid AccountId { get; set; } + public Schedule() + { + } + + /// + /// Gets or sets the automation account name. + /// + public string AutomationAccountName { get; set; } /// /// Gets or sets the name. @@ -44,22 +72,22 @@ public abstract class Schedule /// /// Gets or sets the start time. /// - public DateTime StartTime { get; set; } + public DateTimeOffset StartTime { get; set; } /// /// Gets or sets the expiry time. /// - public DateTime ExpiryTime { get; set; } + public DateTimeOffset ExpiryTime { get; set; } /// /// Gets or sets the creation time. /// - public DateTime CreationTime { get; set; } + public DateTimeOffset CreationTime { get; set; } /// /// Gets or sets the last modified time. /// - public DateTime LastModifiedTime { get; set; } + public DateTimeOffset LastModifiedTime { get; set; } /// /// Gets or sets a value indicating whether is enabled. @@ -69,6 +97,16 @@ public abstract class Schedule /// /// Gets or sets the next run. /// - public DateTime? NextRun { get; set; } + public DateTimeOffset? NextRun { get; set; } + + /// + /// Gets or sets the schedule interval. + /// + public byte? Interval { get; set; } + + /// + /// Gets or sets the schedule frequency. + /// + public ScheduleFrequency Frequency { get; set; } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/ScheduleFrequency.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/ScheduleFrequency.cs new file mode 100644 index 000000000000..97149351e5b1 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/ScheduleFrequency.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + public enum ScheduleFrequency + { + Onetime, + Day, + Hour + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs new file mode 100644 index 000000000000..179d9a2a8709 --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs @@ -0,0 +1,99 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Automation.Common; +using System; + +namespace Microsoft.Azure.Commands.Automation.Model +{ + using AutomationManagement = WindowsAzure.Management.Automation; + + /// + /// The Variable. + /// + public class Variable + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The varaiable. + /// + /// + /// + public Variable(AutomationManagement.Models.Variable variable, string automationAccoutName) + { + Requires.Argument("variable", variable).NotNull(); + + this.Name = variable.Name; + this.CreationTime = variable.Properties.CreationTime.ToLocalTime(); + this.LastModifiedTime = variable.Properties.LastModifiedTime.ToLocalTime(); + + if (variable.Properties.Value == null || variable.Properties.IsEncrypted) + { + this.Value = null; + } + else + { + this.Value = PowerShellJsonConverter.Deserialize(variable.Properties.Value); + } + + this.Description = variable.Properties.Description; + this.Encrypted = variable.Properties.IsEncrypted; + this.AutomationAccountName = automationAccoutName; + } + + /// + /// Initializes a new instance of the class. + /// + public Variable() + { + } + + /// + /// Gets or sets the name. + /// + public string Name { get; set; } + + /// + /// Gets or sets the creation time. + /// + public DateTimeOffset CreationTime { get; set; } + + /// + /// Gets or sets the last modified time. + /// + public DateTimeOffset LastModifiedTime { get; set; } + + /// + /// Gets or sets the value. + /// + public object Value { get; set; } + + /// + /// Gets or sets the description. + /// + public string Description { get; set; } + + /// + /// Gets or sets the description. + /// + public bool Encrypted { get; set; } + + /// + /// Gets or sets the automation account name. + /// + public string AutomationAccountName { get; set; } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs index d69dcb78d390..112e6f07708b 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs @@ -61,7 +61,25 @@ internal Resources() { } /// - /// Looks up a localized string similar to The Automation accout was not found.. + /// Looks up a localized string similar to Create account arguments are invalid. Provide valid account name and location. Account Name: {0}, Location: {1} . + /// + internal static string AccountCreateInvalidArgs { + get { + return ResourceManager.GetString("AccountCreateInvalidArgs", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The Automation account already exists. + /// + internal static string AutomationAccountAlreadyExists { + get { + return ResourceManager.GetString("AutomationAccountAlreadyExists", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The Automation account was not found.. /// internal static string AutomationAccountNotFound { get { @@ -70,52 +88,70 @@ internal static string AutomationAccountNotFound { } /// - /// Looks up a localized string similar to The file "{0}" was not found. Make sure the file exists and is accessible.. + /// Looks up a localized string similar to {0} {1} operation failed for object name: {2) under AutomationAccount: {3}. /// - internal static string FileNotFound { + internal static string AutomationOperationFailed { get { - return ResourceManager.GetString("FileNotFound", resourceCulture); + return ResourceManager.GetString("AutomationOperationFailed", resourceCulture); } } /// - /// Looks up a localized string similar to The daily schedule model is not valid. Schedule name: {0}.. + /// Looks up a localized string similar to The certificate already exists. Certificate name: {0}.. /// - internal static string InvalidDailyScheduleModel { + internal static string CertificateAlreadyExists { get { - return ResourceManager.GetString("InvalidDailyScheduleModel", resourceCulture); + return ResourceManager.GetString("CertificateAlreadyExists", resourceCulture); } } /// - /// Looks up a localized string similar to The hourly schedule model is not valid. Schedule name: {0}.. + /// Looks up a localized string similar to The certificate was not found. Certificate name {0}.. /// - internal static string InvalidHourlyScheduleModel { + internal static string CertificateNotFound { get { - return ResourceManager.GetString("InvalidHourlyScheduleModel", resourceCulture); + return ResourceManager.GetString("CertificateNotFound", resourceCulture); } } /// - /// Looks up a localized string similar to The job model is not valid.. + /// Looks up a localized string similar to The Connection already exists. Connection name: {0}.. /// - internal static string InvalidJobModel { + internal static string ConnectionAlreadyExists { get { - return ResourceManager.GetString("InvalidJobModel", resourceCulture); + return ResourceManager.GetString("ConnectionAlreadyExists", resourceCulture); } } /// - /// Looks up a localized string similar to The runbook model is not valid.. + /// Looks up a localized string similar to The connection field name was not found for connection name: {0}.. /// - internal static string InvalidRunbookModel { + internal static string ConnectionFieldNameNotFound { get { - return ResourceManager.GetString("InvalidRunbookModel", resourceCulture); + return ResourceManager.GetString("ConnectionFieldNameNotFound", resourceCulture); } } /// - /// Looks up a localized string similar to At least one parameter provided is not expected by the runbook.. + /// Looks up a localized string similar to The connection was not found. Connection name: {0}.. + /// + internal static string ConnectionNotFound { + get { + return ResourceManager.GetString("ConnectionNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The credential was not found. Credential name: {0}.. + /// + internal static string CredentialNotFound { + get { + return ResourceManager.GetString("CredentialNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Invalid runbook parameters.. /// internal static string InvalidRunbookParameters { get { @@ -124,11 +160,83 @@ internal static string InvalidRunbookParameters { } /// - /// Looks up a localized string similar to The job was not found. Job ID: {0}.. + /// Looks up a localized string similar to The Job having Id: {0} was not found.. + /// + internal static string JobNotFound { + get { + return ResourceManager.GetString("JobNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The job schedule was not found. Runbook name {0}. Schedule name {1}.. + /// + internal static string JobScheduleNotFound { + get { + return ResourceManager.GetString("JobScheduleNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The job schedule was not found. Job schedule id: {0}.. + /// + internal static string JobScheduleWithIdNotFound { + get { + return ResourceManager.GetString("JobScheduleWithIdNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The module was not found. Module name: {0}.. + /// + internal static string ModuleNotFound { + get { + return ResourceManager.GetString("ModuleNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to {0} is empty.. + /// + internal static string ParameterEmpty { + get { + return ResourceManager.GetString("ParameterEmpty", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Failed to decrypt. Error Details {0}. + /// + internal static string PowershellJsonDecrypterFailed { + get { + return ResourceManager.GetString("PowershellJsonDecrypterFailed", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Disassociating the Azure Automation runbook and schedule.. + /// + internal static string RemoveAzureAutomationJobScheduleDescription { + get { + return ResourceManager.GetString("RemoveAzureAutomationJobScheduleDescription", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Are you sure you want to disassociate the Azure Automation runbook and schedule?. /// - internal static string JobNotFoundById { + internal static string RemoveAzureAutomationJobScheduleWarning { get { - return ResourceManager.GetString("JobNotFoundById", resourceCulture); + return ResourceManager.GetString("RemoveAzureAutomationJobScheduleWarning", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Removing the Azure Automation {0}.. + /// + internal static string RemoveAzureAutomationResourceDescription { + get { + return ResourceManager.GetString("RemoveAzureAutomationResourceDescription", resourceCulture); } } @@ -169,7 +277,43 @@ internal static string RemoveAzureAutomationScheduleWarning { } /// - /// Looks up a localized string similar to Runbook already has a draft. Specify the parameter to force an overwrite of this draft.. + /// Looks up a localized string similar to Are you sure you want to remove the Azure Automation {0} ?. + /// + internal static string RemovingAzureAutomationResourceWarning { + get { + return ResourceManager.GetString("RemovingAzureAutomationResourceWarning", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Resource exist.. + /// + internal static string ResourceExists { + get { + return ResourceManager.GetString("ResourceExists", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Resource does not exist.. + /// + internal static string ResourceNotFound { + get { + return ResourceManager.GetString("ResourceNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The Runbook already exists. Runbook name: {0}.. + /// + internal static string RunbookAlreadyExists { + get { + return ResourceManager.GetString("RunbookAlreadyExists", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Runbook already has a draft. Specify the parameter to force an overwrite of this draft. Runbook name: {0}. /// internal static string RunbookAlreadyHasDraft { get { @@ -178,34 +322,34 @@ internal static string RunbookAlreadyHasDraft { } /// - /// Looks up a localized string similar to The runbook has no published version. Runbook ID: {0}.. + /// Looks up a localized string similar to The runbook has no draft version. Runbook name {0}.. /// - internal static string RunbookHasNoPublishedVersionById { + internal static string RunbookHasNoDraftVersion { get { - return ResourceManager.GetString("RunbookHasNoPublishedVersionById", resourceCulture); + return ResourceManager.GetString("RunbookHasNoDraftVersion", resourceCulture); } } /// - /// Looks up a localized string similar to The runbook was not found. Runbook ID: {0}.. + /// Looks up a localized string similar to The runbook has no published version. Runbook name {0}.. /// - internal static string RunbookNotFoundById { + internal static string RunbookHasNoPublishedVersion { get { - return ResourceManager.GetString("RunbookNotFoundById", resourceCulture); + return ResourceManager.GetString("RunbookHasNoPublishedVersion", resourceCulture); } } /// - /// Looks up a localized string similar to The runbook was not found. Runbook name: {0}.. + /// Looks up a localized string similar to The Runbook was not found. Runbook name: {0}.. /// - internal static string RunbookNotFoundByName { + internal static string RunbookNotFound { get { - return ResourceManager.GetString("RunbookNotFoundByName", resourceCulture); + return ResourceManager.GetString("RunbookNotFound", resourceCulture); } } /// - /// Looks up a localized string similar to The runbook parameter "{0}" cannot be serialized to JSON.. + /// Looks up a localized string similar to Runbook parameter cannot be serialized to json. Parameter name {0}.. /// internal static string RunbookParameterCannotBeSerializedToJson { get { @@ -214,7 +358,7 @@ internal static string RunbookParameterCannotBeSerializedToJson { } /// - /// Looks up a localized string similar to The runbook parameter "{0}" is mandatory.. + /// Looks up a localized string similar to Runbook mandatory parameter not specified. Parameter name {0}.. /// internal static string RunbookParameterValueRequired { get { @@ -223,38 +367,56 @@ internal static string RunbookParameterValueRequired { } /// - /// Looks up a localized string similar to The runbook version was not found. Runbook version ID: {0}.. + /// Looks up a localized string similar to The Automation schedule name is in use. Schedule name: {0}.. /// - internal static string RunbookVersionNotFoundById { + internal static string ScheduleNameExists { get { - return ResourceManager.GetString("RunbookVersionNotFoundById", resourceCulture); + return ResourceManager.GetString("ScheduleNameExists", resourceCulture); } } /// - /// Looks up a localized string similar to The Automation schedule name is in use. Schedule name: {0}.. + /// Looks up a localized string similar to The schedule was not found. Schedule name: {0}.. /// - internal static string ScheduleNameExists { + internal static string ScheduleNotFound { get { - return ResourceManager.GetString("ScheduleNameExists", resourceCulture); + return ResourceManager.GetString("ScheduleNotFound", resourceCulture); } } /// - /// Looks up a localized string similar to The schedule was not found. Schedule ID: {0}.. + /// Looks up a localized string similar to Password and Exportable parameters cannot be updated for an existing certificate. They can only be specified when overwriting this certificate with a new one, via the Path parameter. /// - internal static string ScheduleNotFoundById { + internal static string SetCertificateInvalidArgs { get { - return ResourceManager.GetString("ScheduleNotFoundById", resourceCulture); + return ResourceManager.GetString("SetCertificateInvalidArgs", resourceCulture); } } /// - /// Looks up a localized string similar to The schedule was not found. Schedule name: {0}.. + /// Looks up a localized string similar to The variable already exists. Variable name {0}.. + /// + internal static string VariableAlreadyExists { + get { + return ResourceManager.GetString("VariableAlreadyExists", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Cannot change encryption property of the variable. Variable name {0}. Encryption - {1}.. + /// + internal static string VariableEncryptionCannotBeChanged { + get { + return ResourceManager.GetString("VariableEncryptionCannotBeChanged", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The variable was not found. Variable name {0}.. /// - internal static string ScheduleNotFoundByName { + internal static string VariableNotFound { get { - return ResourceManager.GetString("ScheduleNotFoundByName", resourceCulture); + return ResourceManager.GetString("VariableNotFound", resourceCulture); } } } diff --git a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx index cde935f6970b..ac55f018ec24 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx +++ b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx @@ -121,40 +121,42 @@ The Automation account was not found. Automation - - The file "{0}" was not found. Make sure the file exists and is accessible. + + Removing the Azure Automation runbook. Automation - - The daily schedule model is not valid. Schedule name: {0}. + + Are you sure you want to remove the Azure Automation runbook? Automation - - The hourly schedule model is not valid. Schedule name: {0}. + + The Runbook already exists. Runbook name: {0}. Automation - - The job model is not valid. + + Runbook already has a draft. Specify the parameter to force an overwrite of this draft. Runbook name: {0} Automation - - The runbook model is not valid. + + {0} {1} operation failed for object name: {2) under AutomationAccount: {3} Automation - - At least one parameter provided is not expected by the runbook. - Automation + + The credential was not found. Credential name: {0}. - - The job was not found. Job ID: {0}. + + The Job having Id: {0} was not found. Automation - - Removing the Azure Automation runbook. + + The module was not found. Module name: {0}. + + + {0} is empty. Automation - - Are you sure you want to remove the Azure Automation runbook? + + Removing the Azure Automation {0}. Automation @@ -165,44 +167,112 @@ Are you sure you want to remove the Azure Automation schedule? Automation - - Runbook already has a draft. Specify the parameter to force an overwrite of this draft. + + Are you sure you want to remove the Azure Automation {0} ? Automation - - The runbook has no published version. Runbook ID: {0}. + + Resource exist. Automation - - The runbook was not found. Runbook ID: {0}. + + The Runbook was not found. Runbook name: {0}. + Autmation + + + The Automation schedule name is in use. Schedule name: {0}. + Automation + + + The schedule was not found. Schedule name: {0}. + Automation + + + The variable already exists. Variable name {0}. Automation - - The runbook was not found. Runbook name: {0}. + + The variable was not found. Variable name {0}. + Automation + + + The runbook has no published version. Runbook name {0}. + Automation + + + Invalid runbook parameters. + Automation + + + The job schedule was not found. Runbook name {0}. Schedule name {1}. + Automation + + + The job schedule was not found. Job schedule id: {0}. Automation - The runbook parameter "{0}" cannot be serialized to JSON. + Runbook parameter cannot be serialized to json. Parameter name {0}. Automation - The runbook parameter "{0}" is mandatory. + Runbook mandatory parameter not specified. Parameter name {0}. Automation - - The runbook version was not found. Runbook version ID: {0}. + + Disassociating the Azure Automation runbook and schedule. Automation - - The Automation schedule name is in use. Schedule name: {0}. + + Are you sure you want to disassociate the Azure Automation runbook and schedule? Automation - - The schedule was not found. Schedule ID: {0}. + + Cannot change encryption property of the variable. Variable name {0}. Encryption - {1}. Automation - - The schedule was not found. Schedule name: {0}. + + The Automation account already exists + Automation + + + The certificate was not found. Certificate name {0}. + Automation + + + The certificate already exists. Certificate name: {0}. + Automation + + + Failed to decrypt. Error Details {0} + Automation + + + Resource does not exist. + Automation + + + The Connection already exists. Connection name: {0}. + Automation + + + The connection field name was not found for connection name: {0}. + Automation + + + The connection was not found. Connection name: {0}. + Automation + + + Password and Exportable parameters cannot be updated for an existing certificate. They can only be specified when overwriting this certificate with a new one, via the Path parameter + Automation + + + The runbook has no draft version. Runbook name {0}. + Automation + + + Create account arguments are invalid. Provide valid account name and location. Account Name: {0}, Location: {1} Automation \ No newline at end of file diff --git a/src/ServiceManagement/Automation/Commands.Automation/packages.config b/src/ServiceManagement/Automation/Commands.Automation/packages.config index d3713d473656..a87b8713c6d8 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/packages.config +++ b/src/ServiceManagement/Automation/Commands.Automation/packages.config @@ -4,7 +4,7 @@ - +