|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
15 | 15 | using Microsoft.Azure.Commands.Network.Models;
|
16 |
| -using Microsoft.Azure.Management.Network; |
17 |
| -using Microsoft.Azure.Management.Network.Models; |
| 16 | +using Microsoft.Rest.Azure; |
| 17 | +using Newtonsoft.Json; |
| 18 | +using System; |
| 19 | +using System.Net.Http; |
18 | 20 | using System.Management.Automation;
|
19 |
| -using System.Collections.Generic; |
20 | 21 | using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
|
21 |
| -using CNM = Microsoft.Azure.Commands.Network.Models; |
| 22 | +using System.Collections.Generic; |
22 | 23 |
|
23 | 24 | namespace Microsoft.Azure.Commands.Network.VirtualNetworkGateway
|
24 | 25 | {
|
25 |
| - [Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VirtualNetworkGatewayFailoverSingleTestDetails", DefaultParameterSetName = "ByName"), OutputType(typeof(List<PSExpressRouteFailoverSingleTestDetails>))] |
| 26 | + [Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VirtualNetworkGatewayFailoverSingleTestDetails", DefaultParameterSetName = "GetByNameParameterSet"), OutputType(typeof(List<PSExpressRouteFailoverSingleTestDetails>))] |
26 | 27 | public class GetAzureVirtualNetworkGatewayFailoverSingleTestDetails : NetworkBaseCmdlet
|
27 | 28 | {
|
28 |
| - private const string ByName = "ByName"; |
| 29 | + private const string GetByNameParameterSet = "GetByNameParameterSet"; |
29 | 30 |
|
30 | 31 | [Parameter(
|
31 | 32 | Mandatory = true,
|
32 | 33 | HelpMessage = "The resource group name of the virtual network gateway.",
|
33 |
| - ParameterSetName = ByName)] |
| 34 | + ParameterSetName = GetByNameParameterSet)] |
34 | 35 | [ResourceGroupCompleter]
|
35 | 36 | [ValidateNotNullOrEmpty]
|
36 | 37 | public string ResourceGroupName { get; set; }
|
37 | 38 |
|
38 | 39 | [Parameter(
|
39 | 40 | Mandatory = true,
|
40 | 41 | HelpMessage = "The name of the virtual network gateway.",
|
41 |
| - ParameterSetName = ByName)] |
| 42 | + ParameterSetName = GetByNameParameterSet)] |
42 | 43 | [ValidateNotNullOrEmpty]
|
43 | 44 | public string VirtualNetworkGatewayName { get; set; }
|
44 | 45 |
|
45 | 46 | [Parameter(
|
46 | 47 | Mandatory = true,
|
47 | 48 | HelpMessage = "Peering location of the test.",
|
48 |
| - ParameterSetName = ByName)] |
| 49 | + ParameterSetName = GetByNameParameterSet)] |
49 | 50 | [ValidateNotNullOrEmpty]
|
50 | 51 | public string PeeringLocation { get; set; }
|
51 | 52 |
|
52 | 53 | [Parameter(
|
53 | 54 | Mandatory = true,
|
54 | 55 | HelpMessage = "The unique Guid value which identifies the test.",
|
55 |
| - ParameterSetName = ByName)] |
| 56 | + ParameterSetName = GetByNameParameterSet)] |
56 | 57 | [ValidateNotNullOrEmpty]
|
57 | 58 | public string FailoverTestId { get; set; }
|
58 | 59 |
|
59 | 60 | public override void Execute()
|
60 | 61 | {
|
61 | 62 | base.Execute();
|
62 | 63 |
|
63 |
| - // Call the underlying SDK API (the method name may differ slightly depending on SDK version) |
64 |
| - var response = NetworkClient.NetworkManagementClient.VirtualNetworkGateways.GetFailoverSingleTestDetails( |
65 |
| - ResourceGroupName, |
66 |
| - VirtualNetworkGatewayName, |
67 |
| - PeeringLocation, |
68 |
| - FailoverTestId); |
| 64 | + var response = NetworkClient.NetworkManagementClient.VirtualNetworkGateways |
| 65 | + .GetFailoverSingleTestDetailsWithHttpMessagesAsync( |
| 66 | + resourceGroupName: ResourceGroupName, |
| 67 | + virtualNetworkGatewayName: VirtualNetworkGatewayName, |
| 68 | + peeringLocation: PeeringLocation, |
| 69 | + failoverTestId: FailoverTestId) |
| 70 | + .GetAwaiter().GetResult(); |
| 71 | + |
| 72 | + if (response.Response.StatusCode == System.Net.HttpStatusCode.Accepted) |
| 73 | + { |
| 74 | + WriteVerbose("Operation accepted. Polling for results..."); |
| 75 | + var locationUrl = response.Response.Headers.Location?.ToString(); |
| 76 | + if (!string.IsNullOrEmpty(locationUrl)) |
| 77 | + { |
| 78 | + var testDetails = PollAndParse(locationUrl); |
| 79 | + var fullJson = JsonConvert.SerializeObject(new FailoverTestDetailsWrapper { Value = testDetails }, Formatting.Indented); |
| 80 | + WriteObject(fullJson); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + throw new InvalidOperationException("Location header missing in 202 Accepted response."); |
| 85 | + } |
| 86 | + } |
| 87 | + else if (response.Response.StatusCode == System.Net.HttpStatusCode.OK) |
| 88 | + { |
| 89 | + var testDetails = DeserializeJsonResponse(response.Response); |
| 90 | + var fullJson = JsonConvert.SerializeObject(new FailoverTestDetailsWrapper { Value = testDetails }, Formatting.Indented); |
| 91 | + WriteObject(fullJson); |
| 92 | + |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + throw new InvalidOperationException($"Unexpected response status: {response.Response.StatusCode}"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private class FailoverTestDetailsWrapper |
| 101 | + { |
| 102 | + [JsonProperty("value")] |
| 103 | + public List<PSExpressRouteFailoverSingleTestDetails> Value { get; set; } |
| 104 | + } |
| 105 | + |
| 106 | + private List<PSExpressRouteFailoverSingleTestDetails> DeserializeJsonResponse(HttpResponseMessage responseMessage) |
| 107 | + { |
| 108 | + var json = responseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult(); |
| 109 | + WriteVerbose("Response JSON: " + json); |
| 110 | + |
| 111 | + var wrapper = JsonConvert.DeserializeObject<FailoverTestDetailsWrapper>(json); |
| 112 | + return wrapper?.Value ?? new List<PSExpressRouteFailoverSingleTestDetails>(); |
| 113 | + } |
| 114 | + |
| 115 | + private List<PSExpressRouteFailoverSingleTestDetails> PollAndParse(string locationUrl) |
| 116 | + { |
| 117 | + using (var httpClient = new HttpClient()) |
| 118 | + { |
| 119 | + while (true) |
| 120 | + { |
| 121 | + System.Threading.Thread.Sleep(5000); // wait before polling |
| 122 | + var pollResponse = httpClient.GetAsync(locationUrl).GetAwaiter().GetResult(); |
69 | 123 |
|
70 |
| - // Map SDK model to PS model |
71 |
| - var psResult = NetworkResourceManagerProfile.Mapper.Map<List<PSExpressRouteFailoverSingleTestDetails>>(response); |
| 124 | + WriteVerbose($"Polling response status code: {pollResponse.StatusCode}"); |
72 | 125 |
|
73 |
| - WriteObject(psResult, enumerateCollection: true); |
| 126 | + if (pollResponse.StatusCode == System.Net.HttpStatusCode.Accepted) |
| 127 | + { |
| 128 | + continue; // still processing |
| 129 | + } |
| 130 | + else if (pollResponse.StatusCode == System.Net.HttpStatusCode.OK) |
| 131 | + { |
| 132 | + var json = pollResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult(); |
| 133 | + var response = JsonConvert.DeserializeObject<FailoverTestDetailsWrapper>(json); |
| 134 | + return response.Value; |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + throw new InvalidOperationException($"Polling failed. Status code: {pollResponse.StatusCode}"); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
74 | 142 | }
|
75 | 143 | }
|
76 | 144 | }
|
0 commit comments