Skip to content

Commit 7c36581

Browse files
committed
fixed a long-running bug which I never got round to earlier! This ensures that any async message failures generate a fluent exception
1 parent 5e1b5fb commit 7c36581

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

Elastacloud.AzureManagement.Fluent/Commands/Services/GetAsyncStatusCommand.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
using System.IO;
1212
using System.Linq;
1313
using System.Net;
14+
using System.Xml;
1415
using System.Xml.Linq;
16+
using Elastacloud.AzureManagement.Fluent.Helpers;
1517

1618
namespace Elastacloud.AzureManagement.Fluent.Commands.Services
1719
{
@@ -32,14 +34,33 @@ internal class GetAsyncStatusCommand : ServiceCommand
3234
/// <returns>An OperationStatus value</returns>
3335
internal OperationStatus GetOperationStatus()
3436
{
35-
if (XmlResponsePayload == null)
36-
throw new ApplicationException("unable to determine response for async operation");
37-
XDocument document = XDocument.Parse(XmlResponsePayload, LoadOptions.None);
37+
var document = GetXmlAsyncPayload();
3838
string services =
3939
(from item in document.Descendants(QueryManager.BuildDefaultNamespaceXmlEntity("Operation"))
4040
select (string) item.Element(QueryManager.BuildDefaultNamespaceXmlEntity("Status"))).FirstOrDefault();
4141
return (OperationStatus) Enum.Parse(typeof (OperationStatus), services);
4242
}
43+
/// <summary>
44+
/// Used to get the basic payload for the request in XML
45+
/// </summary>
46+
private XDocument GetXmlAsyncPayload()
47+
{
48+
if (XmlResponsePayload == null)
49+
throw new ApplicationException("unable to determine response for async operation");
50+
XDocument document = XDocument.Parse(XmlResponsePayload, LoadOptions.None);
51+
return document;
52+
}
53+
/// <summary>
54+
/// Used to get the failure text from an async request-response
55+
/// </summary>
56+
internal string GetFailureText()
57+
{
58+
var document = GetXmlAsyncPayload();
59+
return document.Element(Namespaces.NsWindowsAzure + "Operation")
60+
.Element(Namespaces.NsWindowsAzure + "Error")
61+
.Element(Namespaces.NsWindowsAzure + "Message")
62+
.Value;
63+
}
4364

4465
/// <summary>
4566
/// Gets the Xml payload from the response and releases the lock

Elastacloud.AzureManagement.Fluent/Commands/Services/ServiceCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Threading;
1717
using Elastacloud.AzureManagement.Fluent.Commands.Parsers;
1818
using Elastacloud.AzureManagement.Fluent.Helpers;
19+
using Elastacloud.AzureManagement.Fluent.Types.Exceptions;
1920

2021
namespace Elastacloud.AzureManagement.Fluent.Commands.Services
2122
{
@@ -30,6 +31,7 @@ public class ServiceCommand : ICommand
3031
private static IQueryManager _queryManager;
3132
private string _subscriptionId;
3233
private WebException _exception;
34+
private string _lastFailureResponse = null;
3335

3436
#endregion
3537

@@ -81,6 +83,7 @@ protected virtual void ResponseCallback(HttpWebResponse webResponse)
8183
break;
8284
case OperationStatus.Failed:
8385
//Trace.WriteLine(String.Format("Hosted Service Response Id: {0}", MsftAsyncResponseId);
86+
_lastFailureResponse = asyncCommand.GetFailureText();
8487
SitAndWait.Set();
8588
return;
8689
case OperationStatus.Succeeded:
@@ -267,10 +270,13 @@ public virtual void Execute()
267270
RequestWithoutCertificate =
268271
!(UseCertificate.HasValue && UseCertificate.Value)
269272
};
273+
270274
CurrentQueryManager = CurrentQueryManager ?? new QueryManager();
271275
CurrentQueryManager.MakeASyncRequest(serviceManagementRequest, ResponseCallback, ErrorResponseCallback);
272276
// wait for up to 30 minutes - if a deployment takes longer than that ... it's probably HPC!
273277
SitAndWait.WaitOne(200000);
278+
if(_lastFailureResponse != null)
279+
throw new FluentManagementException(_lastFailureResponse, "ServiceCommand");
274280
if (_exception != null)
275281
throw _exception;
276282
}

Elastacloud.FluentManagement.FSTest/VirtualMachines.fsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,10 @@ let properties = new LinuxVirtualMachineProperties(
5656
vmClient.CreateNewVirtualMachineDeploymentFromTemplateGallery(
5757
List<LinuxVirtualMachineProperties>([|properties|]),
5858
"briskit1000")
59-
59+
// test 1: Ensure that above contains no subnets when it's created and returns the address range + 1 ip
60+
// test 2: Receive events on state changes and ensure readyrole
61+
vmClient.LinuxVirtualMachineStatusEvent.Subscribe(fun vmstatus -> printfn "from %s to %s" (vmstatus.OldStatus.ToString()) (vmstatus.NewStatus.ToString()))
62+
// test 3: When created delete the subnet from the vnet - should generate a subnet busy exception of some sort
63+
let vnClient = VirtualNetworkClient(subscriptionId, (getFromBizsparkPlus subscriptionId))
64+
vnClient.RemoveSubnet("fsnet", "Subnet-3")
6065
let images = vmClient.GetCurrentUbuntuImage()

0 commit comments

Comments
 (0)