Skip to content

Commit 2409dc4

Browse files
committed
Fix API version used for polling cascade delete requests
1 parent ff198c3 commit 2409dc4

File tree

6 files changed

+164
-4
lines changed

6 files changed

+164
-4
lines changed

src/Resources/ResourceManager/Implementation/CmdletBase/DeploymentStacksCmdletBase.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1+
using Microsoft.Azure.Commands.Common.Authentication;
2+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
23
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
34
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
45
using Microsoft.Azure.Commands.ResourceManager.Common;
56
using Microsoft.WindowsAzure.Commands.Utilities.Common;
67
using System;
78
using System.Collections;
89
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Net.Http;
912
using System.Text;
1013

1114
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
@@ -17,6 +20,11 @@ public class DeploymentStacksCmdletBase : AzureRMCmdlet
1720
/// </summary>
1821
private DeploymentStacksSdkClient deploymentStacksSdkClient;
1922

23+
/// <summary>
24+
/// Deployment stacks client instance field
25+
/// </summary>
26+
private DeploymentStacksSdkClient deploymentStacksSdkClientForDelete;
27+
2028
/// <summary>
2129
/// Gets or sets the deployment stacks sdk client
2230
/// </summary>
@@ -37,6 +45,26 @@ public DeploymentStacksSdkClient DeploymentStacksSdkClient
3745
}
3846
}
3947

48+
/// <summary>
49+
/// Gets or sets the deployment stacks sdk client with delete handler
50+
/// </summary>
51+
public DeploymentStacksSdkClient DeploymentStacksSdkClientForDelete
52+
{
53+
get
54+
{
55+
if (this.deploymentStacksSdkClientForDelete == null)
56+
{
57+
this.deploymentStacksSdkClientForDelete = new DeploymentStacksSdkClient(DefaultContext, new StacksDeletePollingHandler());
58+
}
59+
return this.deploymentStacksSdkClientForDelete;
60+
}
61+
62+
set
63+
{
64+
this.deploymentStacksSdkClientForDelete = value;
65+
}
66+
}
67+
4068
protected Hashtable GetParameterObject(string parameterFile)
4169
{
4270
var parameters = new Hashtable();
@@ -61,5 +89,27 @@ protected Hashtable GetParameterObject(string parameterFile)
6189
}
6290
return parameters;
6391
}
92+
93+
/// <summary>
94+
/// Unregisters delegating handler if registered.
95+
/// </summary>
96+
protected void UnregisterDelegatingHandlerIfRegistered()
97+
{
98+
var apiExpandHandler = GetStacksHandler();
99+
100+
if (apiExpandHandler != null)
101+
{
102+
AzureSession.Instance.ClientFactory.RemoveHandler(apiExpandHandler.GetType());
103+
}
104+
}
105+
106+
/// <summary>
107+
/// Returns expand handler, if exists.
108+
/// </summary>
109+
private DelegatingHandler GetStacksHandler()
110+
{
111+
return AzureSession.Instance.ClientFactory.GetCustomHandlers()?
112+
.Where(handler => handler.GetType().Equals(typeof(StacksDeletePollingHandler))).FirstOrDefault();
113+
}
64114
}
65115
}

src/Resources/ResourceManager/Implementation/DeploymentStacks/RemoveAzResourceGroupDeploymentStack.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public override void ExecuteCmdlet()
7878
confirmationMessage,
7979
"Deleting Deployment Stack ...",
8080
Name,
81-
() => DeploymentStacksSdkClient.DeleteResourceGroupDeploymentStack(ResourceGroupName, Name, deleteBehavior)
81+
() => DeploymentStacksSdkClientForDelete.DeleteResourceGroupDeploymentStack(ResourceGroupName, Name, deleteBehavior)
8282
);
8383

8484
WriteObject(true);
@@ -90,6 +90,10 @@ public override void ExecuteCmdlet()
9090
else
9191
WriteExceptionError(ex);
9292
}
93+
finally
94+
{
95+
UnregisterDelegatingHandlerIfRegistered();
96+
}
9397
}
9498

9599
#endregion

src/Resources/ResourceManager/Implementation/DeploymentStacks/RemoveAzSubscriptionDeploymentStack.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override void ExecuteCmdlet()
6868
confirmationMessage,
6969
"Deleting Deployment Stack ...",
7070
Name,
71-
() => DeploymentStacksSdkClient.DeleteSubscriptionDeploymentStack(Name, deleteBehavior)
71+
() => DeploymentStacksSdkClientForDelete.DeleteSubscriptionDeploymentStack(Name, deleteBehavior)
7272
);
7373

7474
WriteObject(true);
@@ -80,6 +80,10 @@ public override void ExecuteCmdlet()
8080
else
8181
WriteExceptionError(ex);
8282
}
83+
finally
84+
{
85+
UnregisterDelegatingHandlerIfRegistered();
86+
}
8387
}
8488

8589
#endregion

src/Resources/ResourceManager/SdkClient/DeploymentStacksSdkClient.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ public class DeploymentStacksSdkClient
3131

3232
public DeploymentStacksSdkClient(IDeploymentStacksClient deploymentStacksClient)
3333
{
34+
//Remove delegationHandler - Temporarily being used for purge delete calls
35+
//TODO: remove once cascade delete is implemented
36+
37+
var customHandlers = AzureSession.Instance.ClientFactory.GetCustomHandlers();
38+
var StacksDeletePollingHandler = customHandlers?.Where(handler => handler.GetType().Equals(typeof(StacksDeletePollingHandler))).FirstOrDefault();
39+
40+
if (StacksDeletePollingHandler != null)
41+
{
42+
AzureSession.Instance.ClientFactory.RemoveHandler(StacksDeletePollingHandler.GetType());
43+
}
44+
3445
this.DeploymentStacksClient = deploymentStacksClient;
3546
}
3647

@@ -49,6 +60,16 @@ public DeploymentStacksSdkClient(IAzureContext context)
4960
this.azureContext = context;
5061
}
5162

63+
//TODO: remove once cascade delete is implemented
64+
public DeploymentStacksSdkClient(IAzureContext context, StacksDeletePollingHandler handler)
65+
{
66+
AzureSession.Instance.ClientFactory.AddHandler(handler);
67+
this.DeploymentStacksClient =
68+
AzureSession.Instance.ClientFactory.CreateArmClient<DeploymentStacksClient>(context,
69+
AzureEnvironment.Endpoint.ResourceManager);
70+
this.azureContext = context;
71+
}
72+
5273
public PSDeploymentStack GetResourceGroupDeploymentStack(
5374
string resourceGroupName,
5475
string deploymentStackName,
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
16+
using System;
17+
using System.Linq;
18+
using System.Net.Http;
19+
using System.Threading;
20+
using System.Threading.Tasks;
21+
22+
//TODO: remove once cascade delete is implemented
23+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient
24+
{
25+
public class StacksDeletePollingHandler : DelegatingHandler, ICloneable
26+
{
27+
private const string SubscriptionSegment = "/subscriptions/";
28+
private const string ExpandString = "2021-04-01";
29+
30+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
31+
{
32+
33+
if (request.Method == HttpMethod.Get)
34+
{
35+
var requestUri = request.RequestUri.GetLeftPart(UriPartial.Path);
36+
var test = request.RequestUri.GetLeftPart(UriPartial.Query);
37+
var lastSubscriptionsSegmentIndex = requestUri.LastIndexOf(SubscriptionSegment, StringComparison.InvariantCultureIgnoreCase);
38+
39+
if (lastSubscriptionsSegmentIndex >= 0)
40+
{
41+
var segments = requestUri
42+
.Substring(lastSubscriptionsSegmentIndex)
43+
.CoalesceString()
44+
.Trim('/')
45+
.SplitRemoveEmpty('/');
46+
47+
if (IsDeletePollingRequest(segments))
48+
{
49+
50+
var fullUri = request.RequestUri.ToString();
51+
var splitUri = fullUri.Split('?');
52+
if(splitUri[1].Equals("api-version=2021-05-01-preview", StringComparison.InvariantCultureIgnoreCase))
53+
{
54+
string uriString = splitUri[0];
55+
UriBuilder uri = new UriBuilder(uriString);
56+
var addQueryString = "?api-version=" + ExpandString;
57+
var apiString = uri.ToString() + addQueryString;
58+
request.RequestUri = new Uri(apiString);
59+
}
60+
}
61+
}
62+
}
63+
64+
return base.SendAsync(request, cancellationToken);
65+
}
66+
67+
private bool IsDeletePollingRequest(string[] segments)
68+
{
69+
if("operationresults".Equals(segments[2], StringComparison.InvariantCultureIgnoreCase))
70+
{
71+
return true;
72+
}
73+
return false;
74+
}
75+
76+
public object Clone()
77+
{
78+
return new StacksDeletePollingHandler();
79+
}
80+
}
81+
}

src/Resources/Resources.Test/sampleTemplateParams.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"hostingPlanName": {
3-
"value": "xDeploymentTestHost26668"
3+
"value": "xDeploymentTestHost26669"
44
},
55
"siteLocation": {
66
"value": "East US"

0 commit comments

Comments
 (0)