@@ -152,6 +152,8 @@ public override void ExecuteCmdlet()
152
152
if ( ShouldProcess ( Target , ActionMessage ) )
153
153
{
154
154
StorageSyncModels . StorageSyncService storageSyncService = StorageSyncClientWrapper . StorageSyncManagementClient . StorageSyncServices . Get ( resourceGroupName , resourceName ) ;
155
+ bool needPropogationDelay = storageSyncService ? . Identity ? . Type != ManagedServiceIdentityType . SystemAssigned . ToString ( ) ;
156
+
155
157
// 1. Check if any server available for migration
156
158
IEnumerable < StorageSyncModels . RegisteredServer > registeredServers = StorageSyncClientWrapper . StorageSyncManagementClient . RegisteredServers . ListByStorageSyncService ( resourceGroupName , resourceName ) ;
157
159
var candidateServersLookup = new Dictionary < string , StorageSyncModels . RegisteredServer > ( StringComparer . InvariantCultureIgnoreCase ) ;
@@ -211,71 +213,99 @@ public override void ExecuteCmdlet()
211
213
StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "Storage Sync Service is capable with identity { storageSyncService . Identity . PrincipalId } ") ;
212
214
}
213
215
214
- StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "If you are creating this principal and then immediately assigning a role, you will get error PrincipalNotFound which is related to a replication delay. In this case, set the role assignment principalType property to a value, such as ServicePrincipal, User, or Group. See\r \n https://aka.ms/docs-principaltype") ;
215
- StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "Sleeping for 120 seconds...") ;
216
- Thread . Sleep ( TimeSpan . FromSeconds ( 120 ) ) ;
216
+ if ( needPropogationDelay )
217
+ {
218
+ StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "If you are creating this principal and then immediately assigning a role, you will get error PrincipalNotFound which is related to a replication delay. In this case, set the role assignment principalType property to a value, such as ServicePrincipal, User, or Group. See\r \n https://aka.ms/docs-principaltype") ;
219
+ StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "Sleeping for 120 seconds...") ;
220
+ Thread . Sleep ( TimeSpan . FromSeconds ( 120 ) ) ;
221
+ }
217
222
218
223
// 3. RBAC permission set for Cloud Endpoints and Server Endpoints
219
224
IEnumerable < StorageSyncModels . SyncGroup > syncGroups = StorageSyncClientWrapper . StorageSyncManagementClient . SyncGroups . ListByStorageSyncService ( resourceGroupName , resourceName ) ;
225
+ Exception syncGroupFirstException = null ;
220
226
foreach ( var syncGroup in syncGroups )
221
227
{
222
- IEnumerable < StorageSyncModels . CloudEndpoint > cloudEndpoints = StorageSyncClientWrapper . StorageSyncManagementClient . CloudEndpoints . ListBySyncGroup ( resourceGroupName , resourceName , syncGroup . Name ) ;
223
- StorageSyncModels . CloudEndpoint cloudEndpoint = cloudEndpoints . FirstOrDefault ( ) ;
224
-
225
- if ( cloudEndpoint == null )
228
+ try
226
229
{
227
- StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "Skipping SyncGroup. No cloud Endpoint found for sync group { syncGroup . Name } ") ;
228
- continue ;
229
- }
230
- var storageAccountResourceIdentifier = new ResourceIdentifier ( cloudEndpoint . StorageAccountResourceId ) ;
230
+ IEnumerable < StorageSyncModels . CloudEndpoint > cloudEndpoints = StorageSyncClientWrapper . StorageSyncManagementClient . CloudEndpoints . ListBySyncGroup ( resourceGroupName , resourceName , syncGroup . Name ) ;
231
+ StorageSyncModels . CloudEndpoint cloudEndpoint = cloudEndpoints . FirstOrDefault ( ) ;
231
232
232
- // Identity , RoleDef, Scope
233
- var scope = cloudEndpoint . StorageAccountResourceId ;
234
- var identityRoleAssignmentForSAScope = StorageSyncClientWrapper . EnsureRoleAssignmentWithIdentity ( storageAccountResourceIdentifier . Subscription ,
235
- storageSyncService . Identity . PrincipalId . Value ,
236
- Common . StorageSyncClientWrapper . StorageAccountContributorRoleDefinitionId ,
237
- scope ) ;
233
+ if ( cloudEndpoint == null )
234
+ {
235
+ StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "Skipping SyncGroup. No cloud Endpoint found for sync group { syncGroup . Name } " ) ;
236
+ continue ;
237
+ }
238
+ var storageAccountResourceIdentifier = new ResourceIdentifier ( cloudEndpoint . StorageAccountResourceId ) ;
238
239
239
- scope = $ "{ cloudEndpoint . StorageAccountResourceId } /fileServices/default/fileshares/{ cloudEndpoint . AzureFileShareName } ";
240
- var identityRoleAssignmentForFilsShareScope = StorageSyncClientWrapper . EnsureRoleAssignmentWithIdentity ( storageAccountResourceIdentifier . Subscription ,
241
- storageSyncService . Identity . PrincipalId . Value ,
242
- Common . StorageSyncClientWrapper . StorageFileDataPrivilegedContributorRoleDefinitionId ,
243
- scope ) ;
240
+ // Identity , RoleDef, Scope
241
+ var scope = cloudEndpoint . StorageAccountResourceId ;
242
+ var identityRoleAssignmentForSAScope = StorageSyncClientWrapper . EnsureRoleAssignmentWithIdentity ( storageAccountResourceIdentifier . Subscription ,
243
+ storageSyncService . Identity . PrincipalId . Value ,
244
+ Common . StorageSyncClientWrapper . StorageAccountContributorRoleDefinitionId ,
245
+ scope ) ;
244
246
245
- IEnumerable < StorageSyncModels . ServerEndpoint > serverEndpoints = StorageSyncClientWrapper . StorageSyncManagementClient . ServerEndpoints . ListBySyncGroup ( resourceGroupName , resourceName , syncGroup . Name ) ;
246
- foreach ( var serverEndpoint in serverEndpoints )
247
- {
248
- var associatedServers = new List < RegisteredServer > ( ) ;
247
+ scope = $ "{ cloudEndpoint . StorageAccountResourceId } /fileServices/default/fileshares/{ cloudEndpoint . AzureFileShareName } ";
248
+ var identityRoleAssignmentForFilsShareScope = StorageSyncClientWrapper . EnsureRoleAssignmentWithIdentity ( storageAccountResourceIdentifier . Subscription ,
249
+ storageSyncService . Identity . PrincipalId . Value ,
250
+ Common . StorageSyncClientWrapper . StorageFileDataPrivilegedContributorRoleDefinitionId ,
251
+ scope ) ;
249
252
250
- // It is expected that multiple migration script might have caused to have role assignment already in the system. We are fault tolerant to existing role assignment.
251
- if ( candidateServersLookup . ContainsKey ( serverEndpoint . ServerResourceId ) )
252
- {
253
- // Standalone Server scenario
254
- associatedServers . Add ( candidateServersLookup [ serverEndpoint . ServerResourceId ] ) ;
255
- }
256
- else if ( clusterNameServersLookup . ContainsKey ( serverEndpoint . ServerResourceId ) )
253
+ IEnumerable < StorageSyncModels . ServerEndpoint > serverEndpoints = StorageSyncClientWrapper . StorageSyncManagementClient . ServerEndpoints . ListBySyncGroup ( resourceGroupName , resourceName , syncGroup . Name ) ;
254
+ Exception serverEndpointFirstException = null ;
255
+ foreach ( var serverEndpoint in serverEndpoints )
257
256
{
258
- // ClusterNode Server scenario
259
- associatedServers . AddRange ( clusterNameServersLookup [ serverEndpoint . ServerResourceId ] ) ;
260
- }
257
+ try
258
+ {
259
+ var associatedServers = new List < RegisteredServer > ( ) ;
261
260
262
- StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "ServerEndpoint { serverEndpoint . Name } has { associatedServers . Count } associated registered servers.") ;
263
- foreach ( var associatedServer in associatedServers )
264
- {
265
- if ( ! Guid . TryParse ( associatedServer . LatestApplicationId , out Guid applicationGuid ) )
261
+ // It is expected that multiple migration script might have caused to have role assignment already in the system. We are fault tolerant to existing role assignment.
262
+ if ( candidateServersLookup . ContainsKey ( serverEndpoint . ServerResourceId ) )
263
+ {
264
+ // Standalone Server scenario
265
+ associatedServers . Add ( candidateServersLookup [ serverEndpoint . ServerResourceId ] ) ;
266
+ }
267
+ else if ( clusterNameServersLookup . ContainsKey ( serverEndpoint . ServerResourceId ) )
268
+ {
269
+ // ClusterNode Server scenario
270
+ associatedServers . AddRange ( clusterNameServersLookup [ serverEndpoint . ServerResourceId ] ) ;
271
+ }
272
+
273
+ StorageSyncClientWrapper . VerboseLogger . Invoke ( $ "ServerEndpoint { serverEndpoint . Name } has { associatedServers . Count } associated registered servers.") ;
274
+ foreach ( var associatedServer in associatedServers )
275
+ {
276
+ if ( ! Guid . TryParse ( associatedServer . LatestApplicationId , out Guid applicationGuid ) )
277
+ {
278
+ applicationGuid = Guid . Parse ( associatedServer . ApplicationId ) ;
279
+ }
280
+ // Identity , RoleDef, Scope
281
+ scope = $ "{ cloudEndpoint . StorageAccountResourceId } /fileServices/default/fileshares/{ cloudEndpoint . AzureFileShareName } ";
282
+ identityRoleAssignmentForFilsShareScope = StorageSyncClientWrapper . EnsureRoleAssignmentWithIdentity ( storageAccountResourceIdentifier . Subscription ,
283
+ applicationGuid ,
284
+ Common . StorageSyncClientWrapper . StorageFileDataPrivilegedContributorRoleDefinitionId ,
285
+ scope ) ;
286
+ }
287
+ }
288
+ catch ( Exception ex )
266
289
{
267
- applicationGuid = Guid . Parse ( associatedServer . ApplicationId ) ;
290
+ StorageSyncClientWrapper . ErrorLogger . Invoke ( $ "ServerEndpoint { serverEndpoint . Name } has failed with an exception { ex . Message } .") ;
291
+ serverEndpointFirstException = serverEndpointFirstException ?? ex ;
268
292
}
269
- // Identity , RoleDef, Scope
270
- scope = $ "{ cloudEndpoint . StorageAccountResourceId } /fileServices/default/fileshares/{ cloudEndpoint . AzureFileShareName } ";
271
- identityRoleAssignmentForFilsShareScope = StorageSyncClientWrapper . EnsureRoleAssignmentWithIdentity ( storageAccountResourceIdentifier . Subscription ,
272
- applicationGuid ,
273
- Common . StorageSyncClientWrapper . StorageFileDataPrivilegedContributorRoleDefinitionId ,
274
- scope ) ;
293
+ } // Iterating server endpoints
294
+ if ( serverEndpointFirstException != null )
295
+ {
296
+ throw serverEndpointFirstException ;
275
297
}
276
298
}
299
+ catch ( Exception ex )
300
+ {
301
+ StorageSyncClientWrapper . ErrorLogger . Invoke ( $ "SyncGroup { syncGroup . Name } has failed with an exception { ex . Message } .") ;
302
+ syncGroupFirstException = syncGroupFirstException ?? ex ;
303
+ }
304
+ } // Iterating sync groups
305
+ if ( syncGroupFirstException != null )
306
+ {
307
+ throw syncGroupFirstException ;
277
308
}
278
-
279
309
// 4 Set UseIdentity for given Storage Sync Service
280
310
updateParameters = new StorageSyncServiceUpdateParameters ( )
281
311
{
0 commit comments