77// ---------------------------------------------------------------------------------- 
88
99using  Microsoft . Azure . Commands . Network . Models ; 
10- using  Microsoft . Azure . Management . Network ; 
10+ using  Microsoft . Rest . Azure ; 
11+ using  Microsoft . Azure . Commands . ResourceManager . Common . Tags ; 
12+ using  Newtonsoft . Json ; 
13+ using  System . Net . Http ; 
14+ using  System ; 
1115using  System . Management . Automation ; 
12- using  Microsoft . Azure . Commands . ResourceManager . Common . ArgumentCompleters ; 
1316using  System . Collections . Generic ; 
14- using  CNM  =  Microsoft . Azure . Commands . Network . Models ; 
17+ using  Microsoft . Azure . Commands . ResourceManager . Common . ArgumentCompleters ; 
18+ 
1519
1620namespace  Microsoft . Azure . Commands . Network . VirtualNetworkGateway 
1721{ 
1822    [ Cmdlet ( VerbsCommon . Get ,  ResourceManager . Common . AzureRMConstants . AzureRMPrefix  +  "VirtualNetworkGatewayFailoverAllTestsDetails" ,  DefaultParameterSetName  =  "ByName" ) ,  OutputType ( typeof ( List < PSExpressRouteFailoverTestDetails > ) ) ] 
1923    public  class  GetAzureVirtualNetworkGatewayFailoverAllTestsDetails  :  NetworkBaseCmdlet 
2024    { 
21-          private  const  string  ByName  =  "ByName " ; 
25+        private  const  string  GetByNameParameterSet  =  "GetByNameParameterSet " ; 
2226
2327        [ Parameter ( 
2428            Mandatory  =  true , 
2529            HelpMessage  =  "The resource group name of the virtual network gateway." , 
26-             ParameterSetName  =  ByName ) ] 
30+             ParameterSetName  =  GetByNameParameterSet ) ] 
2731        [ ResourceGroupCompleter ] 
2832        [ ValidateNotNullOrEmpty ] 
2933        public  string  ResourceGroupName  {  get ;  set ;  } 
3034
3135        [ Parameter ( 
3236            Mandatory  =  true , 
3337            HelpMessage  =  "The name of the virtual network gateway." , 
34-             ParameterSetName  =  ByName ) ] 
38+             ParameterSetName  =  GetByNameParameterSet ) ] 
3539        [ ValidateNotNullOrEmpty ] 
3640        public  string  VirtualNetworkGatewayName  {  get ;  set ;  } 
3741
3842        [ Parameter ( 
3943            Mandatory  =  true , 
40-             HelpMessage  =  "The type of failover test. Example: SingleSiteFailover, MultiSiteFailover, All " , 
41-             ParameterSetName  =  ByName ) ] 
44+             HelpMessage  =  "The type of failover test." , 
45+             ParameterSetName  =  GetByNameParameterSet ) ] 
4246        [ ValidateNotNullOrEmpty ] 
4347        public  string  Type  {  get ;  set ;  } 
4448
4549        [ Parameter ( 
4650            Mandatory  =  true , 
47-             HelpMessage  =  "Fetch only the latest test  for each peering location." , 
48-             ParameterSetName  =  ByName ) ] 
51+             HelpMessage  =  "Fetch only the latest tests  for each peering location." , 
52+             ParameterSetName  =  GetByNameParameterSet ) ] 
4953        public  bool  FetchLatest  {  get ;  set ;  } 
5054
5155        public  override  void  Execute ( ) 
5256        { 
5357            base . Execute ( ) ; 
5458
55-             var  response  =  NetworkClient . NetworkManagementClient . VirtualNetworkGateways . GetFailoverAllTestDetails ( 
56-                 ResourceGroupName , 
57-                 VirtualNetworkGatewayName , 
58-                 Type , 
59-                 FetchLatest 
60-             ) ; 
59+             // Start the operation to get failover test details 
60+             var  response  =  NetworkClient . NetworkManagementClient . VirtualNetworkGateways 
61+                 . GetFailoverAllTestDetailsWithHttpMessagesAsync ( 
62+                     resourceGroupName :  ResourceGroupName , 
63+                     virtualNetworkGatewayName :  VirtualNetworkGatewayName , 
64+                     type :  Type , 
65+                     fetchLatest :  FetchLatest ) 
66+                 . GetAwaiter ( ) . GetResult ( ) ; 
67+ 
68+             // If the response status is 202 (Accepted), poll for the result 
69+             if  ( response . Response . StatusCode  ==  System . Net . HttpStatusCode . Accepted ) 
70+             { 
71+                 var  locationUrl  =  response . Response . Headers . Location ? . ToString ( ) ; 
72+                 if  ( ! string . IsNullOrEmpty ( locationUrl ) ) 
73+                 { 
74+                     WriteVerbose ( "Operation accepted. Polling the Location URL until completion..." ) ; 
75+                     var  testDetails  =  PollAndParse ( locationUrl ) ; 
76+                     var  fullJson  =  JsonConvert . SerializeObject ( new  PSExpressRouteFailoverTestResponse  {  Value  =  testDetails  } ,  Formatting . Indented ) ; 
77+                     WriteObject ( fullJson ) ; 
78+ 
79+                 } 
80+                 else 
81+                 { 
82+                     throw  new  InvalidOperationException ( "Location header is missing in 202 Accepted response." ) ; 
83+                 } 
84+             } 
85+             else  if  ( response . Response . StatusCode  ==  System . Net . HttpStatusCode . OK ) 
86+             { 
87+                 // If the response status is 200 (OK), directly deserialize the response 
88+                 var  testDetails  =  DeserializeJsonResponse ( response . Response ) ; 
89+                 var  fullJson  =  JsonConvert . SerializeObject ( new  PSExpressRouteFailoverTestResponse  {  Value  =  testDetails  } ,  Formatting . Indented ) ; 
90+                 WriteObject ( fullJson ) ; 
91+             } 
92+             else 
93+             { 
94+                 throw  new  InvalidOperationException ( $ "Unexpected response status: { response . Response . StatusCode } ") ; 
95+             } 
96+         } 
97+ 
98+         // Wrapper class for the response containing a list of test details 
99+         public  class  PSExpressRouteFailoverTestResponse 
100+         { 
101+             [ JsonProperty ( "value" ) ] 
102+             public  List < PSExpressRouteFailoverTestDetails >  Value  {  get ;  set ;  } 
103+         } 
104+ 
105+         // Deserialize the JSON response into the wrapper class 
106+         private  List < PSExpressRouteFailoverTestDetails >  DeserializeJsonResponse ( HttpResponseMessage  responseMessage ) 
107+         { 
108+             var  json  =  responseMessage . Content . ReadAsStringAsync ( ) . GetAwaiter ( ) . GetResult ( ) ; 
109+             var  response  =  JsonConvert . DeserializeObject < PSExpressRouteFailoverTestResponse > ( json ) ; 
110+             return  response . Value ; 
111+         } 
112+ 
113+         // Poll until the operation completes and the status changes to 200 OK 
114+         private  List < PSExpressRouteFailoverTestDetails >  PollAndParse ( string  locationUrl ) 
115+         { 
116+             using  ( var  httpClient  =  new  HttpClient ( ) ) 
117+             { 
118+                 while  ( true ) 
119+                 { 
120+                     System . Threading . Thread . Sleep ( 5000 ) ;  // Wait 5 seconds between polls 
121+ 
122+                     var  pollResponse  =  httpClient . GetAsync ( locationUrl ) . GetAwaiter ( ) . GetResult ( ) ; 
61123
62-             var  psResult  =  NetworkResourceManagerProfile . Mapper . Map < List < PSExpressRouteFailoverTestDetails > > ( response ) ; 
63-             WriteObject ( psResult ,  enumerateCollection :  true ) ; 
124+                     if  ( pollResponse . StatusCode  ==  System . Net . HttpStatusCode . Accepted ) 
125+                     { 
126+                         continue ;  // keep polling 
127+                     } 
128+                     else  if  ( pollResponse . StatusCode  ==  System . Net . HttpStatusCode . OK ) 
129+                     { 
130+                         var  json  =  pollResponse . Content . ReadAsStringAsync ( ) . GetAwaiter ( ) . GetResult ( ) ; 
131+                         var  response  =  JsonConvert . DeserializeObject < PSExpressRouteFailoverTestResponse > ( json ) ; 
132+                         return  response . Value ; 
133+                     } 
134+                     else 
135+                     { 
136+                         throw  new  InvalidOperationException ( $ "Polling failed. Status code: { pollResponse . StatusCode } ") ; 
137+                     } 
138+                 } 
139+             } 
64140        } 
65141    } 
66- } 
142+ } 
0 commit comments