Skip to content

Commit ccf9ded

Browse files
committed
Added RetryAfter support for New-AzureRmResourceGroupDeployment
1 parent c089f4b commit ccf9ded

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Commands.Resources.Rest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<Compile Include="Extensions\JsonExtensions.cs" />
135135
<Compile Include="Extensions\ObjectExtensions.cs" />
136136
<Compile Include="Extensions\PsObjectExtensions.cs" />
137+
<Compile Include="Extensions\SdkClientExtensions.cs" />
137138
<Compile Include="Extensions\StringExtensions.cs" />
138139
<Compile Include="Handlers\AuthenticationHandler.cs" />
139140
<Compile Include="Handlers\RetryHandler.cs" />
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.ResourceManager.Cmdlets.Extensions
16+
{
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using System.Net.Http;
21+
using System.Threading;
22+
using System.Threading.Tasks;
23+
using Microsoft.Azure.Management.ResourceManager;
24+
using Microsoft.Azure.Management.ResourceManager.Models;
25+
26+
/// <summary>
27+
/// SDK client extension methods
28+
/// </summary>
29+
public static class SdkClientExtensions
30+
{
31+
/// <summary>
32+
/// Get a deployment with HTTP response (so headers are accessible).
33+
/// </summary>
34+
/// <param name="operations">The operations group for this extension method.</param>
35+
/// <param name="resourceGroupName">The name of the resource group to get. The name is case insensitive.</param>
36+
/// <param name="deploymentName">The name of the deployment.</param>
37+
/// <returns>A tuple of the deployment and the HTTP response.</returns>
38+
public static Tuple<DeploymentExtended, HttpResponseMessage> GetDeploymentWithResponseMessage(this IDeploymentsOperations operations, string resourceGroupName, string deploymentName)
39+
{
40+
return Task.Factory.StartNew(s => ((IDeploymentsOperations)s).GetDeploymentWithResponseMessageAsync(resourceGroupName, deploymentName), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
41+
}
42+
43+
/// <summary>
44+
/// Get a deployment with HTTP response (so headers are accessible).
45+
/// </summary>
46+
/// <param name="operations">The operations group for this extension method.</param>
47+
/// <param name="resourceGroupName">The name of the resource group to get. The name is case insensitive.</param>
48+
/// <param name="deploymentName">The name of the deployment.</param>
49+
/// <param name="cancellationToken">The cancellation token.</param>
50+
/// <returns>A tuple of the deployment and the HTTP response.</returns>
51+
public static async Task<Tuple<DeploymentExtended, HttpResponseMessage>> GetDeploymentWithResponseMessageAsync(this IDeploymentsOperations operations, string resourceGroupName, string deploymentName, CancellationToken cancellationToken = default(CancellationToken))
52+
{
53+
using (var _result = await operations.GetWithHttpMessagesAsync(resourceGroupName, deploymentName, null, cancellationToken).ConfigureAwait(false))
54+
{
55+
return Tuple.Create(_result.Body, _result.Response);
56+
}
57+
}
58+
}
59+
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/RestClients/ResourceManagerRestRestClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public Task<OperationResult> GetOperationResult<TType>(
285285
/// <summary>
286286
/// Put a single resource.
287287
/// </summary>
288-
/// <param name="resourceId">The subscription Id.</param>
288+
/// <param name="resourceId">The resource Id.</param>
289289
/// <param name="apiVersion">The API version to use.</param>
290290
/// <param name="resource">The resource to put.</param>
291291
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to cancel the request.</param>

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/SdkClient/ResourceManagerSdkClient.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.IO;
1919
using System.Linq;
2020
using System.Net;
21+
using System.Net.Http;
2122
using System.Runtime.Serialization.Formatters;
2223
using Microsoft.Azure.Commands.Common.Authentication;
2324
using Microsoft.Azure.Commands.Common.Authentication.Models;
@@ -313,8 +314,9 @@ private DeploymentExtended WaitDeploymentStatus(
313314
params ProvisioningState[] status)
314315
{
315316
DeploymentExtended deployment;
317+
HttpResponseMessage response;
316318

317-
// Poll deployment state and deployment operations with two phases. In phase one, poll every 5 seconds. Phase one
319+
// Poll deployment state and deployment operations after RetryAfter, or if no RetryAfter provided, with two phases: In phase one, poll every 5 seconds. Phase one
318320
// takes 400 seconds. In phase two, poll every 60 seconds.
319321
const int counterUnit = 1000;
320322
int step = 5;
@@ -335,9 +337,17 @@ private DeploymentExtended WaitDeploymentStatus(
335337
job(resourceGroup, deploymentName, basicDeployment);
336338
}
337339

338-
deployment = ResourceManagementClient.Deployments.Get(resourceGroup, deploymentName);
339-
340-
step = phaseOne > 0 ? 5 : 60;
340+
var tuple = ResourceManagementClient.Deployments.GetDeploymentWithResponseMessage(resourceGroup, deploymentName);
341+
deployment = tuple.Item1;
342+
response = tuple.Item2;
343+
if (response.Headers.RetryAfter != null && response.Headers.RetryAfter.Delta.HasValue)
344+
{
345+
step = response.Headers.RetryAfter.Delta.Value.Seconds;
346+
}
347+
else
348+
{
349+
step = phaseOne > 0 ? 5 : 60;
350+
}
341351

342352
} while (!status.Any(s => s.ToString().Equals(deployment.Properties.ProvisioningState, StringComparison.OrdinalIgnoreCase)));
343353

0 commit comments

Comments
 (0)