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
{
@@ -53,6 +59,10 @@ public override void ExecuteCmdlet()
53
59
Patch Patch ;
54
60
if ( AppliedScope != null )
55
61
{
62
+ //Pre-register for Microsoft.Compute
63
+ string subscriptionId = ValidateAndGetAppliedSubscription ( ) ;
64
+ PreRegister ( subscriptionId ) ;
65
+
56
66
Patch = new Patch ( AppliedScopeType , new List < string > ( ) { AppliedScope } ) ;
57
67
}
58
68
else
@@ -63,5 +73,69 @@ public override void ExecuteCmdlet()
63
73
WriteObject ( response ) ;
64
74
}
65
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 . 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
+ }
66
140
}
67
141
}
0 commit comments