Skip to content

Commit

Permalink
handle exception
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 30, 2017
1 parent 54a26f7 commit 3ed267b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public void Wait(Task task)
Thread.Yield();
}
HandleActions();
if (task.IsFaulted)
{
throw task.Exception.InnerException;
}
}

void HandleActions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@ public string ResourceGroupName

// Gets or sets the virtual machine zones.
public IList<string> Zones { get; set; }

public string Fqdn { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,6 +48,9 @@ public void WriteVerbose(string message)

public Task<bool> ShouldProcessAsync(string target, string action)
=> Scheduler.Invoke(() => _Cmdlet.ShouldProcess(target, action));

public void WriteObject(object value)
=> Scheduler.BeginInvoke(() => _Cmdlet.WriteObject(value));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -7,5 +21,7 @@ interface IAsyncCmdlet
void WriteVerbose(string message);

Task<bool> ShouldProcessAsync(string target, string action);

void WriteObject(object value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand All @@ -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<PSVirtualMachine>(result);
asyncCmdlet.WriteObject(psResult);
}

public void DefaultExecuteCmdlet()
Expand Down

0 comments on commit 3ed267b

Please sign in to comment.