Skip to content

Commit 3ebf14c

Browse files
author
Sean Oh
committed
Removed handler and moved pre-register logic to cmdlet
1 parent 1e6b56c commit 3ebf14c

File tree

4 files changed

+74
-142
lines changed

4 files changed

+74
-142
lines changed

src/ResourceManager/Reservations/Commands.Reservations/Cmdlets/PatchReservation.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
using System.Management.Automation;
77
using Newtonsoft.Json;
88
using Microsoft.Azure.Management.Reservations;
9+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
10+
using Microsoft.Azure.Management.Internal.Resources;
11+
using Microsoft.Azure.Commands.Common.Authentication;
12+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
13+
using Microsoft.Azure.Management.Internal.Resources.Models;
14+
using Microsoft.Rest.Azure;
915

1016
namespace Microsoft.Azure.Commands.Reservations.Cmdlets
1117
{
1218
[Cmdlet(VerbsData.Update, "AzureRmReservation", DefaultParameterSetName = Constants.ParameterSetNames.CommandParameterSet, SupportsShouldProcess = true), OutputType(typeof(PSReservation))]
13-
public class PatchReservation : AzureReservationsAutoRegisterCmdletBase
19+
public class PatchReservation : AzureReservationsCmdletBase
1420
{
1521
[Parameter(ParameterSetName = Constants.ParameterSetNames.CommandParameterSet,
1622
Mandatory = true)]
@@ -37,8 +43,6 @@ public class PatchReservation : AzureReservationsAutoRegisterCmdletBase
3743
[ValidateNotNull]
3844
public PSReservation Reservation { get; set; }
3945

40-
protected override bool ShouldRegister => AppliedScope != null;
41-
4246
public override void ExecuteCmdlet()
4347
{
4448
if (ParameterSetName.Equals(Constants.ParameterSetNames.ObjectParameterSet))
@@ -55,6 +59,10 @@ public override void ExecuteCmdlet()
5559
Patch Patch;
5660
if (AppliedScope != null)
5761
{
62+
//Pre-register for Microsoft.Compute
63+
string subscriptionId = ValidateAndGetAppliedSubscription();
64+
PreRegister(subscriptionId);
65+
5866
Patch = new Patch(AppliedScopeType, new List<string>() { AppliedScope });
5967
}
6068
else
@@ -65,5 +73,68 @@ public override void ExecuteCmdlet()
6573
WriteObject(response);
6674
}
6775
}
76+
77+
private void PreRegister(string subscriptionId)
78+
{
79+
try
80+
{
81+
IAzureContext context;
82+
if (TryGetDefaultContext(out context)
83+
&& context.Account != null
84+
&& context.Subscription != null)
85+
{
86+
var client = new ResourceManagementClient(
87+
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
88+
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager));
89+
client.SubscriptionId = subscriptionId;
90+
91+
string ComputeProviderNamespace = "Microsoft.Compute";
92+
var maxRetryCount = 10;
93+
94+
var provider = client.Providers.Get(ComputeProviderNamespace);
95+
if (provider.RegistrationState != RegistrationState.Registered)
96+
{
97+
short retryCount = 0;
98+
do
99+
{
100+
if (retryCount++ > maxRetryCount)
101+
{
102+
throw new TimeoutException();
103+
}
104+
provider = client.Providers.Register(ComputeProviderNamespace);
105+
TestMockSupport.Delay(2000);
106+
} while (provider.RegistrationState != RegistrationState.Registered);
107+
}
108+
}
109+
}
110+
catch (Exception e)
111+
{
112+
if (e.Message?.IndexOf("does not have authorization") >= 0 && e.Message?.IndexOf("register/action",
113+
StringComparison.InvariantCultureIgnoreCase) >= 0)
114+
{
115+
throw new CloudException(e.Message);
116+
}
117+
}
118+
}
119+
120+
private string ValidateAndGetAppliedSubscription()
121+
{
122+
string subscriptionId = AppliedScope;
123+
string prefix = "/subscriptions/";
124+
if (subscriptionId.Contains(prefix) && subscriptionId.Length > prefix.Length)
125+
{
126+
subscriptionId = subscriptionId.Substring(prefix.Length);
127+
}
128+
129+
Guid result;
130+
if (Guid.TryParse(subscriptionId, out result))
131+
{
132+
return result.ToString();
133+
}
134+
else
135+
{
136+
throw new PSArgumentException("Invalid applied scope provided");
137+
}
138+
}
68139
}
69140
}

src/ResourceManager/Reservations/Commands.Reservations/Commands.Reservations.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@
6262
<Compile Include="Cmdlets\MergeReservation.cs" />
6363
<Compile Include="Cmdlets\PatchReservation.cs" />
6464
<Compile Include="Cmdlets\SplitReservation.cs" />
65-
<Compile Include="Common\AzureReservationsAutoRegisterCmdletBase.cs" />
6665
<Compile Include="Common\AzureReservationsCmdletBase.cs" />
67-
<Compile Include="Common\AzureReservationsAutoRegisterDelegatingHandler.cs" />
6866
<Compile Include="Common\Constants.cs" />
6967
<Compile Include="Custom\PSCatalog.cs" />
7068
<Compile Include="Custom\PSAppliedReservationOrderId.cs" />

src/ResourceManager/Reservations/Commands.Reservations/Common/AzureReservationsAutoRegisterCmdletBase.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/ResourceManager/Reservations/Commands.Reservations/Common/AzureReservationsAutoRegisterDelegatingHandler.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)