6
6
using System . Management . Automation ;
7
7
using Newtonsoft . Json ;
8
8
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 ;
9
15
10
16
namespace Microsoft . Azure . Commands . Reservations . Cmdlets
11
17
{
12
18
[ Cmdlet ( VerbsData . Update , "AzureRmReservation" , DefaultParameterSetName = Constants . ParameterSetNames . CommandParameterSet , SupportsShouldProcess = true ) , OutputType ( typeof ( PSReservation ) ) ]
13
- public class PatchReservation : AzureReservationsAutoRegisterCmdletBase
19
+ public class PatchReservation : AzureReservationsCmdletBase
14
20
{
15
21
[ Parameter ( ParameterSetName = Constants . ParameterSetNames . CommandParameterSet ,
16
22
Mandatory = true ) ]
@@ -37,8 +43,6 @@ public class PatchReservation : AzureReservationsAutoRegisterCmdletBase
37
43
[ ValidateNotNull ]
38
44
public PSReservation Reservation { get ; set ; }
39
45
40
- protected override bool ShouldRegister => AppliedScope != null ;
41
-
42
46
public override void ExecuteCmdlet ( )
43
47
{
44
48
if ( ParameterSetName . Equals ( Constants . ParameterSetNames . ObjectParameterSet ) )
@@ -55,6 +59,10 @@ public override void ExecuteCmdlet()
55
59
Patch Patch ;
56
60
if ( AppliedScope != null )
57
61
{
62
+ //Pre-register for Microsoft.Compute
63
+ string subscriptionId = ValidateAndGetAppliedSubscription ( ) ;
64
+ PreRegister ( subscriptionId ) ;
65
+
58
66
Patch = new Patch ( AppliedScopeType , new List < string > ( ) { AppliedScope } ) ;
59
67
}
60
68
else
@@ -65,5 +73,68 @@ public override void ExecuteCmdlet()
65
73
WriteObject ( response ) ;
66
74
}
67
75
}
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
+ }
68
139
}
69
140
}
0 commit comments