Skip to content

Commit 45e4a33

Browse files
author
Klein Hu
committed
Remove dependency of Commands.Resources.Rest
1 parent fc6479f commit 45e4a33

File tree

8 files changed

+306
-200
lines changed

8 files changed

+306
-200
lines changed

src/ResourceManager/MachineLearning/Commands.MachineLearning/Cmdlets/GetAzureMLWebService.cs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections.Generic;
1617
using System.Management.Automation;
1718
using System.Threading.Tasks;
18-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1919
using Microsoft.Azure.Management.MachineLearning.WebServices.Models;
2020

2121
namespace Microsoft.Azure.Commands.MachineLearning
@@ -48,40 +48,25 @@ protected override void RunCmdlet()
4848
}
4949
else
5050
{
51-
// This is a collection of web services get call, so determine which flavor it is
52-
Func<Task<ResponseWithContinuation<WebService[]>>> getFirstServicesPageCall =
53-
() => this.WebServicesClient.GetAzureMlWebServicesBySubscriptionAsync(
54-
null,
55-
this.CancellationToken);
56-
Func<string, Task<ResponseWithContinuation<WebService[]>>> getNextPageCall =
57-
nextLink => this.WebServicesClient.GetAzureMlWebServicesBySubscriptionAsync(
58-
nextLink,
59-
this.CancellationToken);
51+
IList<WebService> services;
6052
if (!string.IsNullOrWhiteSpace(this.ResourceGroupName))
6153
{
62-
// This is a call for resource retrieval within a resource group
63-
getFirstServicesPageCall =
64-
() => this.WebServicesClient.GetAzureMlWebServicesBySubscriptionAndGroupAsync(
54+
services = this.WebServicesClient.GetAzureMlWebServicesBySubscriptionAndGroupAsync(
6555
this.ResourceGroupName,
6656
null,
67-
this.CancellationToken);
68-
getNextPageCall = nextLink => this.WebServicesClient.GetAzureMlWebServicesBySubscriptionAndGroupAsync(
69-
this.ResourceGroupName,
70-
nextLink,
71-
this.CancellationToken);
57+
this.CancellationToken).Result;
58+
}
59+
else
60+
{
61+
services = this.WebServicesClient.GetAzureMlWebServicesBySubscriptionAsync(
62+
null,
63+
this.CancellationToken).Result;
7264
}
7365

74-
PaginatedResponseHelper.ForEach<WebService>(
75-
getFirstPage: getFirstServicesPageCall,
76-
getNextPage: getNextPageCall,
77-
cancellationToken: this.CancellationToken,
78-
action: webServices =>
79-
{
80-
foreach (var service in webServices)
81-
{
82-
this.WriteObject(service, true);
83-
}
84-
});
66+
foreach (var service in services)
67+
{
68+
this.WriteObject(service, true);
69+
}
8570
}
8671
}
8772
}

src/ResourceManager/MachineLearning/Commands.MachineLearning/Cmdlets/WebServicesCmdletBase.cs

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
1618
using System.Management.Automation;
1719
using System.Runtime.ExceptionServices;
20+
using System.Runtime.InteropServices;
21+
using System.Security;
22+
using System.Text;
1823
using System.Threading;
19-
using Microsoft.Azure.Commands.MachineLearning.Extensions;
24+
2025
using Microsoft.Azure.Commands.MachineLearning.Utilities;
21-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
22-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2326
using Microsoft.Azure.Commands.ResourceManager.Common;
2427
using Microsoft.Rest.Azure;
2528

29+
using Microsoft.WindowsAzure.Commands.Common;
30+
2631
namespace Microsoft.Azure.Commands.MachineLearning
2732
{
2833
public abstract class WebServicesCmdletBase : AzureRMCmdlet
@@ -77,7 +82,7 @@ protected override void BeginProcessing()
7782
}
7883
catch (Exception ex)
7984
{
80-
if (ex.IsFatal())
85+
if (this.IsFatalException(ex))
8186
{
8287
ThrowTerminatingError(new ErrorRecord(ex, string.Empty, ErrorCategory.InvalidOperation, this));
8388
}
@@ -95,7 +100,7 @@ protected override void EndProcessing()
95100
}
96101
catch (Exception ex)
97102
{
98-
if (ex.IsFatal())
103+
if (this.IsFatalException(ex))
99104
{
100105
ThrowTerminatingError(new ErrorRecord(ex, string.Empty, ErrorCategory.InvalidOperation, this));
101106
}
@@ -123,7 +128,7 @@ protected override void StopProcessing()
123128
}
124129
catch (Exception ex)
125130
{
126-
if (ex.IsFatal())
131+
if (this.IsFatalException(ex))
127132
{
128133
throw;
129134
}
@@ -154,7 +159,7 @@ public override void ExecuteCmdlet()
154159
}
155160
catch (Exception ex)
156161
{
157-
if (ex.IsFatal())
162+
if (this.IsFatalException(ex))
158163
{
159164
throw;
160165
}
@@ -192,11 +197,11 @@ private void HandleException(ExceptionDispatchInfo capturedException)
192197
var cloudException = capturedException.SourceException as CloudException;
193198
if (cloudException != null)
194199
{
195-
errorRecord = cloudException.ToErrorRecord();
200+
errorRecord = this.CreateErrorRecordForCloudException(cloudException); ////new ErrorRecord(cloudException, cloudException.Message, ErrorCategory.CloseError, null);
196201
}
197202
else
198203
{
199-
var errorResponseException =
204+
var errorResponseException =
200205
capturedException.SourceException as ErrorResponseMessageException;
201206
if (errorResponseException != null)
202207
{
@@ -208,20 +213,20 @@ private void HandleException(ExceptionDispatchInfo capturedException)
208213
capturedException.SourceException as AggregateException;
209214
if (aggregateException != null)
210215
{
211-
errorResponseException =
216+
errorResponseException =
212217
aggregateException.InnerException as ErrorResponseMessageException;
213218
if (errorResponseException != null)
214219
{
215220
errorRecord = errorResponseException.ToErrorRecord();
216221
}
217222
else
218223
{
219-
errorRecord = aggregateException.InnerException.ToErrorRecord();
224+
errorRecord = new ErrorRecord(aggregateException.InnerException, aggregateException.InnerException.Message, ErrorCategory.CloseError, null);
220225
}
221226
}
222227
else
223228
{
224-
errorRecord = capturedException.SourceException.ToErrorRecord();
229+
errorRecord = new ErrorRecord(capturedException.SourceException, capturedException.SourceException.Message, ErrorCategory.CloseError, null);
225230
}
226231
}
227232
}
@@ -233,5 +238,75 @@ private void HandleException(ExceptionDispatchInfo capturedException)
233238
this.DisposeOfCancellationSource();
234239
}
235240
}
241+
242+
243+
/// <summary>
244+
/// Converts <see cref="CloudException"/> objects into <see cref="ErrorRecord"/>
245+
/// </summary>
246+
/// <param name="cloudException">The exception</param>
247+
private ErrorRecord CreateErrorRecordForCloudException(CloudException cloudException)
248+
{
249+
var errorReport = new StringBuilder();
250+
251+
string requestId = cloudException.RequestId;
252+
if (string.IsNullOrWhiteSpace(requestId) && cloudException.Response != null)
253+
{
254+
// Try to obtain the request id from the HTTP response associated with the exception
255+
IEnumerable<string> headerValues = Enumerable.Empty<string>();
256+
if (cloudException.Response.Headers != null &&
257+
cloudException.Response.Headers.TryGetValue("x-ms-request-id", out headerValues))
258+
{
259+
requestId = headerValues.First();
260+
}
261+
}
262+
263+
errorReport.AppendLine();
264+
errorReport.AppendLine("Request Id: {0}".FormatInvariant(requestId));
265+
errorReport.AppendLine("Error Code: {0}".FormatInvariant(cloudException.Body.Code));
266+
errorReport.AppendLine("Error Message: {0}".FormatInvariant(cloudException.Body.Message));
267+
errorReport.AppendLine("Error Target: {0}".FormatInvariant(cloudException.Body.Target));
268+
if (cloudException.Body.Details.Any())
269+
{
270+
errorReport.AppendLine("Error Details:");
271+
foreach (var errorDetail in cloudException.Body.Details)
272+
{
273+
errorReport.AppendLine("\t[Code={0}, Message={1}]".FormatInvariant(errorDetail.Code, errorDetail.Message));
274+
}
275+
}
276+
277+
var returnedError = new Exception(errorReport.ToString(), cloudException);
278+
return new ErrorRecord(returnedError, "Resource Provider Error", ErrorCategory.CloseError, null);
279+
}
280+
281+
/// <summary>
282+
/// Test if an exception is a fatal exception.
283+
/// </summary>
284+
/// <param name="ex">Exception object.</param>
285+
private bool IsFatalException(Exception ex)
286+
{
287+
if (ex is AggregateException)
288+
{
289+
return ((AggregateException)ex).Flatten().InnerExceptions.Any(exception => this.IsFatalException(exception));
290+
}
291+
292+
if (ex.InnerException != null && this.IsFatalException(ex.InnerException))
293+
{
294+
return true;
295+
}
296+
297+
return
298+
ex is TypeInitializationException ||
299+
ex is AppDomainUnloadedException ||
300+
ex is ThreadInterruptedException ||
301+
ex is AccessViolationException ||
302+
ex is InvalidProgramException ||
303+
ex is BadImageFormatException ||
304+
ex is StackOverflowException ||
305+
ex is ThreadAbortException ||
306+
ex is OutOfMemoryException ||
307+
ex is SecurityException ||
308+
ex is SEHException;
309+
}
310+
236311
}
237312
}

src/ResourceManager/MachineLearning/Commands.MachineLearning/Commands.MachineLearning.csproj

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,18 @@
8282
<SpecificVersion>False</SpecificVersion>
8383
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
8484
</Reference>
85-
<ProjectReference Include="..\..\..\Common\Commands.Common.Authentication\Commands.Common.Authentication.csproj">
86-
<Project>{d3804b64-c0d3-48f8-82ec-1f632f833c9e}</Project>
87-
<Name>Commands.Common.Authentication</Name>
88-
</ProjectReference>
89-
<ProjectReference Include="..\..\..\Common\Commands.Common\Commands.Common.csproj">
90-
<Project>{5ee72c53-1720-4309-b54b-5fb79703195f}</Project>
91-
<Name>Commands.Common</Name>
92-
</ProjectReference>
93-
<ProjectReference Include="..\..\Common\Commands.ResourceManager.Common\Commands.ResourceManager.Common.csproj">
94-
<Project>{3819d8a7-c62c-4c47-8ddd-0332d9ce1252}</Project>
95-
<Name>Commands.ResourceManager.Common</Name>
96-
</ProjectReference>
97-
<ProjectReference Include="..\..\Resources\Commands.ResourceManager\Cmdlets\Commands.Resources.Rest.csproj">
98-
<Project>{8058d403-06e3-4bed-8924-d166ce303961}</Project>
99-
<Name>Commands.Resources.Rest</Name>
100-
</ProjectReference>
85+
<ProjectReference Include="..\..\..\Common\Commands.Common.Authentication\Commands.Common.Authentication.csproj">
86+
<Project>{d3804b64-c0d3-48f8-82ec-1f632f833c9e}</Project>
87+
<Name>Commands.Common.Authentication</Name>
88+
</ProjectReference>
89+
<ProjectReference Include="..\..\..\Common\Commands.Common\Commands.Common.csproj">
90+
<Project>{5ee72c53-1720-4309-b54b-5fb79703195f}</Project>
91+
<Name>Commands.Common</Name>
92+
</ProjectReference>
93+
<ProjectReference Include="..\..\Common\Commands.ResourceManager.Common\Commands.ResourceManager.Common.csproj">
94+
<Project>{3819d8a7-c62c-4c47-8ddd-0332d9ce1252}</Project>
95+
<Name>Commands.ResourceManager.Common</Name>
96+
</ProjectReference>
10197
<Reference Include="System" />
10298
<Reference Include="System.Core" />
10399
<Reference Include="System.Net.Http" />
@@ -118,7 +114,9 @@
118114
<Compile Include="Cmdlets\NewAzureMLWebService.cs" />
119115
<Compile Include="Cmdlets\UpdateAzureMLWebService.cs" />
120116
<Compile Include="Cmdlets\WebServicesCmdletBase.cs" />
121-
<Compile Include="Extensions\ErrorResponseMessageExceptionExtensions.cs" />
117+
<Compile Include="Exception\ErrorResponseMessage.cs" />
118+
<Compile Include="Exception\ErrorResponseMessageException.cs" />
119+
<Compile Include="Exception\ExtendedErrorInfo.cs" />
122120
<Compile Include="Properties\AssemblyInfo.cs" />
123121
<Compile Include="Resources.Designer.cs">
124122
<AutoGen>True</AutoGen>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.MachineLearning
16+
{
17+
using Newtonsoft.Json;
18+
19+
/// <summary>
20+
/// The error response message.
21+
/// </summary>
22+
public class ErrorResponseMessage
23+
{
24+
/// <summary>
25+
/// Gets or sets the extended error info.
26+
/// </summary>
27+
[JsonProperty(Required = Required.Always)]
28+
public ExtendedErrorInfo Error { get; set; }
29+
}
30+
}

0 commit comments

Comments
 (0)