diff --git a/src/ResourceManager/Automation/Commands.Automation.Test/Commands.ResourceManagement.Automation.Test.csproj b/src/ResourceManager/Automation/Commands.Automation.Test/Commands.ResourceManagement.Automation.Test.csproj index d06f40fbb192..9e01eca03e71 100644 --- a/src/ResourceManager/Automation/Commands.Automation.Test/Commands.ResourceManagement.Automation.Test.csproj +++ b/src/ResourceManager/Automation/Commands.Automation.Test/Commands.ResourceManagement.Automation.Test.csproj @@ -111,9 +111,13 @@ + + + + diff --git a/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationWebhookTest.cs b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationWebhookTest.cs new file mode 100644 index 000000000000..d0d3c6011b4c --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/GetAzureAutomationWebhookTest.cs @@ -0,0 +1,67 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class GetAzureAutomationWebhookTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private GetAzureAutomationWebhook cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new GetAzureAutomationWebhook + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void GetAzureAutomationWebhookByNameSuccessful() + { + // Setup + string resourceGroupName = "resourceGroup"; + string accountName = "account"; + string webhookName = "webhookName"; + this.cmdlet.SetParameterSet("ByName"); + + this.mockAutomationClient.Setup(f => f.GetWebhook(resourceGroupName, accountName, webhookName)); + + // Test + this.cmdlet.ResourceGroupName = resourceGroupName; + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = webhookName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.GetWebhook(resourceGroupName, accountName, webhookName), Times.Once()); + } + } +} \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationWebhookTest.cs b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationWebhookTest.cs new file mode 100644 index 000000000000..de06407b96d2 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationWebhookTest.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 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; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class NewAzureAutomationWebhookTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private NewAzureAutomationWebhook cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new NewAzureAutomationWebhook + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void NewAzureAutomationWebhookByNameSuccessful() + { + // Setup + string resourceGroupName = "resourceGroup"; + string accountName = "account"; + string name = "webhookName"; + string runbookName = "runbookName"; + DateTimeOffset expiryTime = DateTimeOffset.Now.AddDays(1); + + this.mockAutomationClient.Setup( + f => f.CreateWebhook(resourceGroupName, accountName, name, runbookName, true, expiryTime, null)); + + // Test + this.cmdlet.ResourceGroupName = resourceGroupName; + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = name; + this.cmdlet.RunbookName = runbookName; + this.cmdlet.ExpiryTime = expiryTime; + this.cmdlet.IsEnabled = true; + this.cmdlet.Parameters = null; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify( + f => f.CreateWebhook(resourceGroupName, accountName, name, runbookName, true, expiryTime, null), + Times.Once()); + } + } +} \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationWebhookTest.cs b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationWebhookTest.cs new file mode 100644 index 000000000000..469d514ca486 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/RemoveAzureAutomationWebhookTest.cs @@ -0,0 +1,67 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.WindowsAzure.Commands.Utilities.Common; +using Moq; + +namespace Microsoft.Azure.Commands.Automation.Test.UnitTests +{ + [TestClass] + public class RemoveAzureAutomationWebhookTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private RemoveAzureAutomationWebhook cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new RemoveAzureAutomationWebhook + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void RemoveAzureAutomationWebhookByNameSuccessful() + { + // Setup + string resourceGroupName = "resourceGroup"; + string accountName = "account"; + string webhookName = "webhookName"; + this.cmdlet.SetParameterSet("ByName"); + + this.mockAutomationClient.Setup(f => f.DeleteWebhook(resourceGroupName, accountName, webhookName)); + + // Test + this.cmdlet.ResourceGroupName = resourceGroupName; + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = webhookName; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify(f => f.DeleteWebhook(resourceGroupName, accountName, webhookName), Times.Once()); + } + } +} \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationWebhookTest.cs b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationWebhookTest.cs new file mode 100644 index 000000000000..bb837670fe6b --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation.Test/UnitTests/SetAzureAutomationWebhookTest.cs @@ -0,0 +1,70 @@ +// ---------------------------------------------------------------------------------- +// +// 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 SetAzureAutomationWebhookTest : TestBase + { + private Mock mockAutomationClient; + + private MockCommandRuntime mockCommandRuntime; + + private SetAzureAutomationWebhook cmdlet; + + [TestInitialize] + public void SetupTest() + { + this.mockAutomationClient = new Mock(); + this.mockCommandRuntime = new MockCommandRuntime(); + this.cmdlet = new SetAzureAutomationWebhook + { + AutomationClient = this.mockAutomationClient.Object, + CommandRuntime = this.mockCommandRuntime + }; + } + + [TestMethod] + public void SetAzureAutomationWebhookToDisabledSuccessful() + { + // Setup + string resourceGroupName = "resourceGroup"; + string accountName = "account"; + string name = "webhookName"; + + this.mockAutomationClient.Setup( + f => f.UpdateWebhook(resourceGroupName, accountName, name, null, false)); + + // Test + this.cmdlet.ResourceGroupName = resourceGroupName; + this.cmdlet.AutomationAccountName = accountName; + this.cmdlet.Name = name; + this.cmdlet.IsEnabled = false; + this.cmdlet.Parameters = null; + this.cmdlet.ExecuteCmdlet(); + + // Assert + this.mockAutomationClient.Verify( + f => f.UpdateWebhook(resourceGroupName, accountName, name, null, false), + Times.Once()); + } + } +} \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationWebhook.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationWebhook.cs new file mode 100644 index 000000000000..8c1e3afcc215 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/GetAzureAutomationWebhook.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 Microsoft.Azure.Commands.Automation.Common; +using Microsoft.Azure.Commands.Automation.Model; +using System.Collections.Generic; +using System.Management.Automation; +using System.Security.Permissions; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Get Webhook for automation. + /// + [Cmdlet(VerbsCommon.Get, "AzureAutomationWebhook", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)] + [OutputType(typeof(Webhook))] + public class GetAzureAutomationWebhook : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the Webhook name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Webhook name.")] + [Alias("WebhookName")] + public string Name { get; set; } + + /// + /// Gets or sets the Webhook name. + /// + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Runbook name.")] + public string RunbookName { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + IEnumerable webhooks = null; + if (this.ParameterSetName == AutomationCmdletParameterSets.ByAll) + { + var nextLink = string.Empty; + do + { + webhooks = this.AutomationClient.ListWebhooks(this.ResourceGroupName, this.AutomationAccountName, null, ref nextLink); + this.GenerateCmdletOutput(webhooks); + } + while (!string.IsNullOrEmpty(nextLink)); + + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByName) + { + webhooks = new List + { + this.AutomationClient.GetWebhook( + this.ResourceGroupName, + this.AutomationAccountName, + this.Name) + }; + this.GenerateCmdletOutput(webhooks); + } + else if (this.ParameterSetName == AutomationCmdletParameterSets.ByRunbookName) + { + var nextLink = string.Empty; + do + { + webhooks = this.AutomationClient.ListWebhooks(this.ResourceGroupName, this.AutomationAccountName, this.RunbookName, ref nextLink); + this.GenerateCmdletOutput(webhooks); + } + while (!string.IsNullOrEmpty(nextLink)); + } + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/NewAzureAutomationWebhook.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/NewAzureAutomationWebhook.cs new file mode 100644 index 000000000000..b27e5446681d --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/NewAzureAutomationWebhook.cs @@ -0,0 +1,98 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Model; +using Microsoft.Azure.Commands.Automation.Properties; +using Microsoft.WindowsAzure.Commands.Common; +using System; +using System.Collections.Generic; +using System.Management.Automation; +using System.Security.Permissions; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Create a new Webhook for automation. + /// + [Cmdlet(VerbsCommon.New, "AzureAutomationWebhook")] + [OutputType(typeof(Webhook))] + public class NewAzureAutomationWebhook : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the module name. + /// + [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The webhook name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the contentLink + /// + [Parameter(Position = 3, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Runbook Name associated with the webhook.")] + [ValidateNotNullOrEmpty] + public string RunbookName { get; set; } + + /// + /// Gets or sets the Enabled property of the Webhook + /// + [Parameter(Position = 4, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Enable/Disable property of the Webhook")] + [ValidateNotNullOrEmpty] + public bool IsEnabled { get; set; } + + /// + /// Gets or sets the Expiry Time + /// + [Parameter(Position = 5, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Expiry Time for webhook.")] + [ValidateNotNullOrEmpty] + public DateTimeOffset ExpiryTime { get; set; } + + /// + /// Gets or sets the Runbook parameters + /// + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Runbook parameters name/value.")] + public IDictionary Parameters { get; set; } + + [Parameter(Mandatory = false, HelpMessage = "Skip warning message about one-time viewable webhook URL")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + this.ConfirmAction( + Force.IsPresent, + string.Format(Resources.WebhookOneTimeURL, "Webhook"), + string.Format(Resources.WebhookOneTimeURL, "Webhook"), + Name, + () => + this.WriteObject( + this.AutomationClient.CreateWebhook( + this.ResourceGroupName, + this.AutomationAccountName, + this.Name, + this.RunbookName, + this.IsEnabled, + this.ExpiryTime, + this.Parameters.ToHashtable()))); + + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationWebhook.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationWebhook.cs new file mode 100644 index 000000000000..607fe797245f --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationWebhook.cs @@ -0,0 +1,55 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Management.Automation; +using System.Security.Permissions; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Remove a new Webhook for automation. + /// + [Cmdlet(VerbsCommon.Remove, "AzureAutomationWebhook")] + public class RemoveAzureAutomationWebhook : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the Webhook name. + /// + [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The webhook name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + [Parameter(Position = 3, HelpMessage = "Confirm the removal of the webhook")] + public SwitchParameter Force { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + ConfirmAction( + Force.IsPresent, + string.Format(Resources.RemovingAzureAutomationResourceWarning, "Webhook"), + string.Format(Resources.RemoveAzureAutomationResourceDescription, "Webhook"), + Name, + () => this.AutomationClient.DeleteWebhook( + this.ResourceGroupName, + this.AutomationAccountName, + this.Name)); + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Cmdlet/SetAzureAutomationWebhook.cs b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/SetAzureAutomationWebhook.cs new file mode 100644 index 000000000000..ed80470c1f86 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Cmdlet/SetAzureAutomationWebhook.cs @@ -0,0 +1,68 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Model; +using Microsoft.WindowsAzure.Commands.Common; +using System.Collections.Generic; +using System.Management.Automation; +using System.Security.Permissions; + +namespace Microsoft.Azure.Commands.Automation.Cmdlet +{ + /// + /// Update a Webhook for automation. + /// + [Cmdlet(VerbsCommon.Set, "AzureAutomationWebhook")] + [OutputType(typeof(Webhook))] + public class SetAzureAutomationWebhook : AzureAutomationBaseCmdlet + { + /// + /// Gets or sets the Webhook name. + /// + [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Webhook name.")] + [ValidateNotNullOrEmpty] + public string Name { get; set; } + + /// + /// Gets or sets the IsEnabled Property + /// + [Parameter(Position = 3, Mandatory = true, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Enable/Disable property of the Webhook")] + [ValidateNotNullOrEmpty] + public bool? IsEnabled { get; set; } + + /// + /// Gets or sets the Runbook parameters + /// + [Parameter(Position = 4, Mandatory = false, ValueFromPipelineByPropertyName = true, + HelpMessage = "The Runbook parameters name/value.")] + public IDictionary Parameters { get; set; } + + /// + /// Execute this cmdlet. + /// + [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] + protected override void AutomationExecuteCmdlet() + { + var updatedWebhook = this.AutomationClient.UpdateWebhook( + this.ResourceGroupName, + this.AutomationAccountName, + this.Name, + this.Parameters.ToHashtable(), + this.IsEnabled); + this.WriteObject(updatedWebhook); + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj index 9761d68d1ca8..41f228777e04 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj +++ b/src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj @@ -124,10 +124,16 @@ + + Code + + + + @@ -143,6 +149,7 @@ + @@ -169,6 +176,7 @@ + True diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientWebhook.cs b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientWebhook.cs new file mode 100644 index 000000000000..bff062dc7152 --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientWebhook.cs @@ -0,0 +1,215 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Hyak.Common; +using Microsoft.Azure.Commands.Automation.Properties; +using Microsoft.Azure.Management.Automation; +using Microsoft.Azure.Management.Automation.Models; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Net; + +namespace Microsoft.Azure.Commands.Automation.Common +{ + using System.Collections; + using System.Linq; + + using Microsoft.WindowsAzure.Commands.Utilities.Common; + + public partial class AutomationClient : IAutomationClient + { + public Model.Webhook CreateWebhook( + string resourceGroupName, + string automationAccountName, + string name, + string runbookName, + bool isEnabled, + DateTimeOffset expiryTime, + Hashtable runbookParameters) + { + Requires.Argument("ResourceGroupName", resourceGroupName).NotNull(); + Requires.Argument("AutomationAccountName", automationAccountName).NotNull(); + using (var request = new RequestSettings(this.automationManagementClient)) + { + var rbAssociationProperty = new RunbookAssociationProperty { Name = runbookName }; + var createOrUpdateProperties = new WebhookCreateOrUpdateProperties + { + IsEnabled = isEnabled, + ExpiryTime = expiryTime, + Runbook = rbAssociationProperty, + Uri = + this.automationManagementClient + .Webhooks.GenerateUri( + resourceGroupName, + automationAccountName).Uri + }; + if (runbookParameters != null) + { + createOrUpdateProperties.Parameters = + runbookParameters.Cast() + .ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value); + } + + var webhookCreateOrUpdateParameters = new WebhookCreateOrUpdateParameters( + name, + createOrUpdateProperties); + + var webhook = + this.automationManagementClient.Webhooks.CreateOrUpdate( + resourceGroupName, + automationAccountName, + webhookCreateOrUpdateParameters).Webhook; + + return new Model.Webhook( + resourceGroupName, + automationAccountName, + webhook, + webhookCreateOrUpdateParameters.Properties.Uri); + } + } + + public Model.Webhook GetWebhook(string resourceGroupName, string automationAccountName, string name) + { + Requires.Argument("ResourceGroupName", resourceGroupName).NotNull(); + Requires.Argument("AutomationAccountName", automationAccountName).NotNull(); + using (var request = new RequestSettings(this.automationManagementClient)) + { + try + { + var webhook = + this.automationManagementClient.Webhooks.Get(resourceGroupName, automationAccountName, name) + .Webhook; + if (webhook == null) + { + throw new ResourceNotFoundException( + typeof(Webhook), + string.Format(CultureInfo.CurrentCulture, Resources.WebhookNotFound, name)); + } + + return new Model.Webhook(resourceGroupName, automationAccountName, webhook); + } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NotFound) + { + throw new ResourceNotFoundException( + typeof(Webhook), + string.Format(CultureInfo.CurrentCulture, Resources.WebhookNotFound, name)); + } + + throw; + } + } + } + + public IEnumerable ListWebhooks(string resourceGroupName, string automationAccountName, string runbookName, ref string nextLink) + { + Requires.Argument("ResourceGroupName", resourceGroupName).NotNull(); + Requires.Argument("AutomationAccountName", automationAccountName).NotNull(); + WebhookListResponse response; + using (var request = new RequestSettings(this.automationManagementClient)) + { + if (string.IsNullOrEmpty(nextLink)) + { + if (runbookName == null) + { + response = this.automationManagementClient.Webhooks.List( + resourceGroupName, + automationAccountName, + null); + } + else + { + response = this.automationManagementClient.Webhooks.List( + resourceGroupName, + automationAccountName, + runbookName); + } + } + else + { + response = this.automationManagementClient.Webhooks.ListNext(nextLink); + } + + nextLink = response.NextLink; + return + response.Webhooks.Select(w => new Model.Webhook(resourceGroupName, automationAccountName, w)) + .ToList(); + } + } + + public Model.Webhook UpdateWebhook( + string resourceGroupName, + string automationAccountName, + string name, + Hashtable parameters, + bool? isEnabled) + { + Requires.Argument("ResourceGroupName", resourceGroupName).NotNull(); + Requires.Argument("AutomationAccountName", automationAccountName).NotNull(); + using (var request = new RequestSettings(this.automationManagementClient)) + { + var webhookModel = + this.automationManagementClient.Webhooks.Get(resourceGroupName, automationAccountName, name).Webhook; + var webhookPatchProperties = new WebhookPatchProperties(); + if (webhookModel != null) + { + if (isEnabled != null) + { + webhookPatchProperties.IsEnabled = isEnabled.Value; + } + if (parameters != null) + { + webhookPatchProperties.Parameters = + parameters.Cast() + .ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value); + } + } + + var webhookPatchParameters = new WebhookPatchParameters(name) { Properties = webhookPatchProperties }; + var webhook = + this.automationManagementClient.Webhooks.Patch( + resourceGroupName, + automationAccountName, + webhookPatchParameters).Webhook; + + return new Model.Webhook(resourceGroupName, automationAccountName, webhook); + } + } + + public void DeleteWebhook(string resourceGroupName, string automationAccountName, string name) + { + Requires.Argument("ResourceGroupName", resourceGroupName).NotNull(); + Requires.Argument("AutomationAccountName", automationAccountName).NotNull(); + using (var request = new RequestSettings(this.automationManagementClient)) + { + try + { + this.automationManagementClient.Webhooks.Delete(resourceGroupName, automationAccountName, name); + } + catch (CloudException cloudException) + { + if (cloudException.Response.StatusCode == HttpStatusCode.NoContent) + { + throw new ResourceNotFoundException( + typeof(Webhook), + string.Format(CultureInfo.CurrentCulture, Resources.WebhookNotFound, name)); + } + throw; + } + } + } + } +} diff --git a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs index 68bf835e1a25..910b3f1e394e 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs @@ -129,5 +129,26 @@ IEnumerable ListDscNodesByConfiguration( DirectoryInfo GetDscNodeReportContent(string resourceGroupName, string automationAccountName, Guid nodeId, Guid reportId, string outputFolder, bool overwriteExistingFile); #endregion + + #region Webhooks + + Model.Webhook CreateWebhook( + string resourceGroupName, + string automationAccountName, + string name, + string runbookName, + bool isEnabled, + DateTimeOffset expiryTime, + Hashtable parameters); + + Model.Webhook GetWebhook(string resourceGroupName, string automationAccountName, string name); + + IEnumerable ListWebhooks(string resourceGroupName, string automationAccountName, string runbooName, ref string nextLink); + + Model.Webhook UpdateWebhook(string resourceGroupName, string automationAccountName, string name, Hashtable parameters, bool? isEnabled); + + void DeleteWebhook(string resourceGroupName, string automationAccountName, string name); + + #endregion } } \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation/Model/Webhook.cs b/src/ResourceManager/Automation/Commands.Automation/Model/Webhook.cs new file mode 100644 index 000000000000..802c55f6f2cd --- /dev/null +++ b/src/ResourceManager/Automation/Commands.Automation/Model/Webhook.cs @@ -0,0 +1,97 @@ +// ---------------------------------------------------------------------------------- +// +// 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 +{ + using System.Linq; + + public class Webhook + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The resource group name. + /// + /// + /// The account name. + /// + /// + /// The Webhook. + /// + /// + /// The Webhook URI + /// + public Webhook( + string resourceGroupName, + string automationAccountName, + Azure.Management.Automation.Models.Webhook webhook, + string webhookUri = "") + { + Requires.Argument("resourceGroupName", resourceGroupName).NotNull(); + Requires.Argument("automationAccountName", automationAccountName).NotNull(); + Requires.Argument("webhook", webhook).NotNull(); + + this.ResourceGroupName = resourceGroupName; + this.AutomationAccountName = automationAccountName; + this.Name = webhook.Name; + + if (webhook.Properties == null) return; + + this.CreationTime = webhook.Properties.CreationTime.ToLocalTime(); + this.Description = webhook.Properties.Description; + this.ExpiryTime = webhook.Properties.ExpiryTime.ToLocalTime(); + this.IsEnabled = webhook.Properties.IsEnabled; + if (webhook.Properties.LastInvokedTime.HasValue) + { + this.LastInvokedTime = webhook.Properties.LastInvokedTime.Value.ToLocalTime(); + } + + this.LastModifiedTime = webhook.Properties.LastModifiedTime.ToLocalTime(); + this.Parameters = new Hashtable(webhook.Properties.Parameters.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); + this.RunbookName = webhook.Properties.Runbook.Name; + this.WebhookURI = webhookUri; + } + + public string ResourceGroupName { get; set; } + + public string AutomationAccountName { get; set; } + + public string Name { get; set; } + + public DateTimeOffset CreationTime { get; set; } + + public string Description { get; set; } + + public DateTimeOffset ExpiryTime { get; set; } + + public bool? IsEnabled { get; set; } + + public DateTimeOffset LastInvokedTime { get; set; } + + public DateTimeOffset LastModifiedTime { get; set; } + + public Hashtable Parameters { get; set; } + + public string RunbookName { get; set; } + + public string WebhookURI { get; set; } + } +} \ No newline at end of file diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs index 09e07f15522f..6b6d54c8584e 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.18449 +// Runtime Version:4.0.30319.34014 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -662,5 +662,23 @@ internal static string VariableNotFound { return ResourceManager.GetString("VariableNotFound", resourceCulture); } } + + /// + /// Looks up a localized string similar to The Webhook with Name: {0} was not found.. + /// + internal static string WebhookNotFound { + get { + return ResourceManager.GetString("WebhookNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to For security purposes, the URL of the created webhook will only be viewable in the output of this command. No other commands will return the webhook URL. Make sure to copy down the webhook URL from this command's output before closing your PowerShell session, and to store it securely.. + /// + internal static string WebhookOneTimeURL { + get { + return ResourceManager.GetString("WebhookOneTimeURL", resourceCulture); + } + } } } diff --git a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx index 9ef6d499c9d4..6a7091667d81 100644 --- a/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx +++ b/src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx @@ -384,4 +384,12 @@ The Dsc Configuration was not found. Configuration name {0}. Automation + + The Webhook with Name: {0} was not found. + Automation + + + For security purposes, the URL of the created webhook will only be viewable in the output of this command. No other commands will return the webhook URL. Make sure to copy down the webhook URL from this command's output before closing your PowerShell session, and to store it securely. + Automation + \ No newline at end of file