From 3ed267bea36cb0f2f4a40e62334546a66bd22fad Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Thu, 30 Nov 2017 14:32:37 -0800 Subject: [PATCH] handle exception --- .../SyncTaskScheduler.cs | 4 ++++ .../Models/PSVirtualMachine.cs | 2 ++ .../Strategies/AsyncCmdletExtensions.cs | 19 ++++++++++++++++++- .../Commands.Compute/Strategies/Client.cs | 16 +++++++++++++++- .../Strategies/IAsyncCmdlet.cs | 18 +++++++++++++++++- .../Operation/NewAzureVMCommand.cs | 12 ++++++++---- 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/ResourceManager/Common/Commands.Common.Strategies/SyncTaskScheduler.cs b/src/ResourceManager/Common/Commands.Common.Strategies/SyncTaskScheduler.cs index e9ee866533a0..67989355cef3 100644 --- a/src/ResourceManager/Common/Commands.Common.Strategies/SyncTaskScheduler.cs +++ b/src/ResourceManager/Common/Commands.Common.Strategies/SyncTaskScheduler.cs @@ -47,6 +47,10 @@ public void Wait(Task task) Thread.Yield(); } HandleActions(); + if (task.IsFaulted) + { + throw task.Exception.InnerException; + } } void HandleActions() diff --git a/src/ResourceManager/Compute/Commands.Compute/Models/PSVirtualMachine.cs b/src/ResourceManager/Compute/Commands.Compute/Models/PSVirtualMachine.cs index 60ee62321a79..ca66e3d02f6e 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Models/PSVirtualMachine.cs +++ b/src/ResourceManager/Compute/Commands.Compute/Models/PSVirtualMachine.cs @@ -98,5 +98,7 @@ public string ResourceGroupName // Gets or sets the virtual machine zones. public IList Zones { get; set; } + + public string Fqdn { get; set; } } } diff --git a/src/ResourceManager/Compute/Commands.Compute/Strategies/AsyncCmdletExtensions.cs b/src/ResourceManager/Compute/Commands.Compute/Strategies/AsyncCmdletExtensions.cs index 267f7913448c..b85407cbff1c 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Strategies/AsyncCmdletExtensions.cs +++ b/src/ResourceManager/Compute/Commands.Compute/Strategies/AsyncCmdletExtensions.cs @@ -1,4 +1,18 @@ -using Microsoft.Azure.Commands.Common.Strategies; +// ---------------------------------------------------------------------------------- +// +// 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.Common.Strategies; using System; using System.Management.Automation; using System.Threading.Tasks; @@ -34,6 +48,9 @@ public void WriteVerbose(string message) public Task ShouldProcessAsync(string target, string action) => Scheduler.Invoke(() => _Cmdlet.ShouldProcess(target, action)); + + public void WriteObject(object value) + => Scheduler.BeginInvoke(() => _Cmdlet.WriteObject(value)); } } } diff --git a/src/ResourceManager/Compute/Commands.Compute/Strategies/Client.cs b/src/ResourceManager/Compute/Commands.Compute/Strategies/Client.cs index 512d9d871878..fd9280bfa2fa 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Strategies/Client.cs +++ b/src/ResourceManager/Compute/Commands.Compute/Strategies/Client.cs @@ -1,4 +1,18 @@ -using Microsoft.Azure.Commands.Common.Authentication; +// ---------------------------------------------------------------------------------- +// +// 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.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; using Microsoft.Azure.Commands.Common.Strategies; using Microsoft.Rest; diff --git a/src/ResourceManager/Compute/Commands.Compute/Strategies/IAsyncCmdlet.cs b/src/ResourceManager/Compute/Commands.Compute/Strategies/IAsyncCmdlet.cs index eed661b0b8ce..64d0a9a101b1 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Strategies/IAsyncCmdlet.cs +++ b/src/ResourceManager/Compute/Commands.Compute/Strategies/IAsyncCmdlet.cs @@ -1,4 +1,18 @@ -using System.Threading.Tasks; +// ---------------------------------------------------------------------------------- +// +// 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.Threading.Tasks; namespace Microsoft.Azure.Commands.Compute.Strategies { @@ -7,5 +21,7 @@ interface IAsyncCmdlet void WriteVerbose(string message); Task ShouldProcessAsync(string target, string action); + + void WriteObject(object value); } } diff --git a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs index 70d2a03878b9..fbde56dc81f7 100644 --- a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs +++ b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs @@ -49,7 +49,7 @@ namespace Microsoft.Azure.Commands.Compute ProfileNouns.VirtualMachine, SupportsShouldProcess = true, DefaultParameterSetName = "DefaultParameterSet")] - [OutputType(typeof(PSAzureOperationResponse))] + [OutputType(typeof(PSAzureOperationResponse), typeof(PSVirtualMachine))] public class NewAzureVMCommand : VirtualMachineBaseCmdlet { public const string DefaultParameterSet = "DefaultParameterSet"; @@ -210,8 +210,9 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet) adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password, image: image.Image); - // get state var client = new Client(DefaultProfile.DefaultContext); + + // get current Azure state var current = await virtualMachine.GetStateAsync(client, new CancellationToken()); if (Location == null) @@ -227,14 +228,17 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet) var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location); // apply target state - var result = await virtualMachine + var newState = await virtualMachine .UpdateStateAsync( client, target, new CancellationToken(), new ShouldProcess(asyncCmdlet), new ProgressReport(asyncCmdlet)); - WriteObject(result); + + var result = newState.Get(virtualMachine); + var psResult = ComputeAutoMapperProfile.Mapper.Map(result); + asyncCmdlet.WriteObject(psResult); } public void DefaultExecuteCmdlet()