@@ -270,46 +270,64 @@ public PSADServicePrincipal EnsureServicePrincipal()
270
270
/// Ensures the role assignment.
271
271
/// </summary>
272
272
/// <param name="serverPrincipal">The server principal.</param>
273
+ /// <param name="storageAccountSubscriptionId">The storage account subscription identifier.</param>
273
274
/// <param name="storageAccountResourceId">The storage account resource identifier.</param>
274
275
/// <returns>RoleAssignment.</returns>
275
- /// <exception cref="PSArgumentException">roleDefinition</exception>
276
- public RoleAssignment EnsureRoleAssignment ( PSADServicePrincipal serverPrincipal , string storageAccountResourceId )
276
+ public RoleAssignment EnsureRoleAssignment ( PSADServicePrincipal serverPrincipal , string storageAccountSubscriptionId , string storageAccountResourceId )
277
277
{
278
- var resourceIdentifier = new ResourceIdentifier ( storageAccountResourceId ) ;
279
- string roleDefinitionScope = "/" ;
280
- RoleDefinition roleDefinition = AuthorizationManagementClient . RoleDefinitions . Get ( roleDefinitionScope , BuiltInRoleDefinitionId ) ;
281
-
282
- var serverPrincipalId = serverPrincipal . Id . ToString ( ) ;
283
- var roleAssignments = AuthorizationManagementClient . RoleAssignments
284
- . ListForResource (
285
- resourceIdentifier . ResourceGroupName ,
286
- ResourceIdentifier . GetProviderFromResourceType ( resourceIdentifier . ResourceType ) ,
287
- resourceIdentifier . ParentResource ?? "/" ,
288
- ResourceIdentifier . GetTypeFromResourceType ( resourceIdentifier . ResourceType ) ,
289
- resourceIdentifier . ResourceName ,
290
- odataQuery : new ODataQuery < RoleAssignmentFilter > ( f => f . AssignedTo ( serverPrincipalId ) ) ) ;
291
- var roleAssignmentScope = storageAccountResourceId ;
292
- Guid roleAssignmentId = StorageSyncResourceManager . GetGuid ( ) ;
293
-
294
- RoleAssignment roleAssignment = roleAssignments . FirstOrDefault ( ) ;
295
- if ( roleAssignment == null )
278
+ string currentSubscriptionId = AuthorizationManagementClient . SubscriptionId ;
279
+ bool hasMismatchSubscription = currentSubscriptionId != storageAccountSubscriptionId ;
280
+
281
+ try
296
282
{
297
- VerboseLogger . Invoke ( StorageSyncResources . CreateRoleAssignmentMessage ) ;
298
- var createParameters = new RoleAssignmentCreateParameters
283
+ if ( hasMismatchSubscription )
284
+ {
285
+ AuthorizationManagementClient . SubscriptionId = storageAccountSubscriptionId ;
286
+ }
287
+
288
+ var resourceIdentifier = new ResourceIdentifier ( storageAccountResourceId ) ;
289
+ string roleDefinitionScope = "/" ;
290
+ RoleDefinition roleDefinition = AuthorizationManagementClient . RoleDefinitions . Get ( roleDefinitionScope , BuiltInRoleDefinitionId ) ;
291
+
292
+ var serverPrincipalId = serverPrincipal . Id . ToString ( ) ;
293
+ var roleAssignments = AuthorizationManagementClient . RoleAssignments
294
+ . ListForResource (
295
+ resourceIdentifier . ResourceGroupName ,
296
+ ResourceIdentifier . GetProviderFromResourceType ( resourceIdentifier . ResourceType ) ,
297
+ resourceIdentifier . ParentResource ?? "/" ,
298
+ ResourceIdentifier . GetTypeFromResourceType ( resourceIdentifier . ResourceType ) ,
299
+ resourceIdentifier . ResourceName ,
300
+ odataQuery : new ODataQuery < RoleAssignmentFilter > ( f => f . AssignedTo ( serverPrincipalId ) ) ) ;
301
+ var roleAssignmentScope = storageAccountResourceId ;
302
+ Guid roleAssignmentId = StorageSyncResourceManager . GetGuid ( ) ;
303
+
304
+ RoleAssignment roleAssignment = roleAssignments . FirstOrDefault ( ) ;
305
+ if ( roleAssignment == null )
299
306
{
300
- Properties = new RoleAssignmentProperties
307
+ VerboseLogger . Invoke ( StorageSyncResources . CreateRoleAssignmentMessage ) ;
308
+ var createParameters = new RoleAssignmentCreateParameters
301
309
{
302
- PrincipalId = serverPrincipalId ,
303
- RoleDefinitionId = AuthorizationHelper . ConstructFullyQualifiedRoleDefinitionIdFromSubscriptionAndIdAsGuid ( resourceIdentifier . Subscription , BuiltInRoleDefinitionId )
304
- }
305
- } ;
310
+ Properties = new RoleAssignmentProperties
311
+ {
312
+ PrincipalId = serverPrincipalId ,
313
+ RoleDefinitionId = AuthorizationHelper . ConstructFullyQualifiedRoleDefinitionIdFromSubscriptionAndIdAsGuid ( resourceIdentifier . Subscription , BuiltInRoleDefinitionId )
314
+ }
315
+ } ;
306
316
307
- roleAssignment = AuthorizationManagementClient . RoleAssignments . Create ( roleAssignmentScope , roleAssignmentId . ToString ( ) , createParameters ) ;
308
- StorageSyncResourceManager . Wait ( ) ;
317
+ roleAssignment = AuthorizationManagementClient . RoleAssignments . Create ( roleAssignmentScope , roleAssignmentId . ToString ( ) , createParameters ) ;
318
+ StorageSyncResourceManager . Wait ( ) ;
309
319
310
- }
320
+ }
311
321
312
- return roleAssignment ;
322
+ return roleAssignment ;
323
+ }
324
+ finally
325
+ {
326
+ if ( hasMismatchSubscription )
327
+ {
328
+ AuthorizationManagementClient . SubscriptionId = currentSubscriptionId ;
329
+ }
330
+ }
313
331
}
314
332
315
333
/// <summary>
0 commit comments