66using System . Management . Automation ;
77using Newtonsoft . Json ;
88using 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
1016namespace Microsoft . Azure . Commands . Reservations . Cmdlets
1117{
@@ -53,6 +59,10 @@ public override void ExecuteCmdlet()
5359 Patch Patch ;
5460 if ( AppliedScope != null )
5561 {
62+ //Pre-register for Microsoft.Compute
63+ string subscriptionId = ValidateAndGetAppliedSubscription ( ) ;
64+ PreRegister ( subscriptionId ) ;
65+
5666 Patch = new Patch ( AppliedScopeType , new List < string > ( ) { AppliedScope } ) ;
5767 }
5868 else
@@ -63,5 +73,69 @@ public override void ExecuteCmdlet()
6373 WriteObject ( response ) ;
6474 }
6575 }
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 . IndexOf ( prefix , StringComparison . InvariantCultureIgnoreCase ) >= 0
125+ && subscriptionId . Length > prefix . Length )
126+ {
127+ subscriptionId = subscriptionId . Substring ( prefix . Length ) ;
128+ }
129+
130+ Guid result ;
131+ if ( Guid . TryParse ( subscriptionId , out result ) )
132+ {
133+ return result . ToString ( ) ;
134+ }
135+ else
136+ {
137+ throw new PSArgumentException ( "Invalid applied scope provided" ) ;
138+ }
139+ }
66140 }
67141}
0 commit comments