@@ -395,47 +395,20 @@ while ($runCount -le $NumberOfIterations)
395395
396396 Invoke-Usecase - Name ' RoleAssignmentAndCustomRoleDefinition' - Description " Assign a reader role and create a custom role definition" - UsecaseBlock `
397397 {
398- $readerRole = Get-AzureRmRoleDefinition - Name Reader
399- $subscriptionID = (Get-AzureRmSubscription - SubscriptionName $tenantSubscriptionName ).SubscriptionId
400- $servicePrincipal = (Get-AzureRmADServicePrincipal )[0 ]
401- $customRoleName = " CustomCanaryRole-" + [Random ]::new().Next(1 , 99 )
402-
403- if ($readerRole )
404- {
405- Invoke-Usecase - Name ' ListAssignedRoles' - Description " List assigned roles to Service Principle - $ ( $servicePrincipal.DisplayName ) " - UsecaseBlock `
406- {
407- Get-AzureRmRoleAssignment - ObjectId $servicePrincipal.Id - ErrorAction Stop
408- }
409-
410- $allAssignedRoles = Get-AzureRmRoleAssignment - ObjectId $servicePrincipal.Id - ErrorAction Stop
411- if (-not $allAssignedRoles -or ($allAssignedRoles -and -not ($allAssignedRoles | Where-Object {$_.RoleDefinitionName -eq $readerRole.Name })))
412- {
413- Invoke-Usecase - Name ' AssignReaderRole' - Description " Assign Reader role to Service Principle - $ ( $servicePrincipal.DisplayName ) " - UsecaseBlock `
414- {
415- New-AzureRmRoleAssignment - Scope " /Subscriptions/$subscriptionID " - RoleDefinitionName $readerRole.Name - ObjectId $servicePrincipal.Id - ErrorAction Stop
416- }
417-
418- Invoke-Usecase - Name ' VerifyReaderRoleAssignment' - Description " Verify if the Service Principle has got Reader role assigned successfully" - UsecaseBlock `
419- {
420- if (-not (Get-AzureRmRoleAssignment - RoleDefinitionName $readerRole.Name - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop))
421- {
422- throw [System.Exception ] " Unable to assign role ($readerRole .Name) to Service Principle ($servicePrincipal .Id) for subscription $tenantSubscriptionName "
423- }
424- }
425-
426- if (Get-AzureRmRoleAssignment - RoleDefinitionName $readerRole.Name - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop)
427- {
428- Invoke-Usecase - Name ' RemoveReaderRoleAssignment' - Description " Remove Reader role assignment from Service Principle - $ ( $servicePrincipal.DisplayName ) " - UsecaseBlock `
429- {
430- Remove-AzureRmRoleAssignment - Scope " /Subscriptions/$subscriptionID " - RoleDefinitionName $readerRole.Name - ObjectId $servicePrincipal.Id - Force - ErrorAction Stop
431- }
432- }
433- }
398+ if (-not $ListAvailable.IsPresent )
399+ {
400+ $servicePrincipal = (Get-AzureRmADServicePrincipal )[0 ]
401+ $customRoleName = " CustomCanaryRole-" + [Random ]::new().Next(1 , 99 )
402+ }
403+
404+ Invoke-Usecase - Name ' ListAssignedRoles' - Description " List assigned roles to Service Principle - $ ( $servicePrincipal.DisplayName ) " - UsecaseBlock `
405+ {
406+ Get-AzureRmRoleAssignment - ObjectId $servicePrincipal.Id - ErrorAction Stop
434407 }
435408
436409 Invoke-Usecase - Name ' ListExistingRoleDefinitions' - Description " List existing Role Definitions" - UsecaseBlock `
437410 {
438- $availableRoles = Get-AzureRmRoleDefinition - ErrorAction Stop
411+ $availableRoles = Get-AzureRmRoleDefinition - ErrorAction Stop
439412 if (-not $availableRoles )
440413 {
441414 throw [System.Exception ] " No roles are available."
@@ -453,40 +426,12 @@ while ($runCount -le $NumberOfIterations)
453426 }
454427 }
455428 }
456-
457- if (-not (Get-AzureRmRoleDefinition - Name $customRoleName ))
458- {
459- Invoke-Usecase - Name ' CustomRoleDefinition' - Description " Create a custom Role Definition - $customRoleName " - UsecaseBlock `
460- {
461- $role = Get-AzureRmRoleDefinition - Name Reader
462- $role.Id = $null
463- $role.Name = $customRoleName
464- $role.Description = " Custom role definition for Canary"
465- $role.Actions.Clear ()
466- $role.Actions.Add (" Microsoft.Authorization/*/Read" )
467- $role.AssignableScopes.Clear ()
468- $role.AssignableScopes.Add (" /Subscriptions/$subscriptionID " )
469- New-AzureRmRoleDefinition - Role $role - ErrorAction Stop
470- if (-not (Get-AzureRmRoleDefinition - Name $customRoleName - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop))
471- {
472- throw [System.Exception ] " Unable to create custom role ($customRoleName ) for subscription $tenantSubscriptionName "
473- }
474- }
475-
476- if (Get-AzureRmRoleDefinition - Name $customRoleName - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop)
477- {
478- Invoke-Usecase - Name ' RemoveCustomRoleDefinition' - Description " Remove custom role definition - $customRoleName " - UsecaseBlock `
479- {
480- Remove-AzureRmRoleDefinition - Name $customRoleName - Scope " /Subscriptions/$subscriptionID " - Force - ErrorAction Stop
481- }
482- }
483- }
484429
485430 Invoke-Usecase - Name ' GetProviderOperations' - Description " Get provider operations for all resource providers" - UsecaseBlock `
486431 {
487- $resourceProviders = Get-AzureRmResourceProvider - ListAvailable
432+ $resourceProviders = Get-AzureRmResourceProvider - ListAvailable
488433 # Some of the RPs have not implemented their operations API yet. So update this exclusion list whenever any RP implements its operations API
489- $rpOperationsExclusionList = @ (" Microsoft.Compute " , " Microsoft. Commerce" , " Microsoft.Gallery" , " Microsoft.Insights" )
434+ $rpOperationsExclusionList = @ (" Microsoft.Commerce" , " Microsoft.Gallery" , " Microsoft.Insights" )
490435 $totalOperationsPerRP = @ ()
491436 foreach ($rp in $resourceProviders )
492437 {
@@ -515,7 +460,94 @@ while ($runCount -le $NumberOfIterations)
515460 }
516461 }
517462 }
518- }
463+
464+ Invoke-Usecase - Name ' AssignReaderRole' - Description " Assign Reader role to Service Principle - $ ( $servicePrincipal.DisplayName ) " - UsecaseBlock `
465+ {
466+ $readerRole = Get-AzureRmRoleDefinition - Name Reader
467+ $subscriptionID = (Get-AzureRmSubscription - SubscriptionName $tenantSubscriptionName ).SubscriptionId
468+ $allAssignedRoles = Get-AzureRmRoleAssignment - ObjectId $servicePrincipal.Id - ErrorAction Stop
469+ if ($subscriptionID -and $readerRole -and (-not $allAssignedRoles -or ($allAssignedRoles -and -not ($allAssignedRoles | Where-Object {$_.RoleDefinitionName -eq $readerRole.Name }))))
470+ {
471+ New-AzureRmRoleAssignment - Scope " /Subscriptions/$subscriptionID " - RoleDefinitionName $readerRole.Name - ObjectId $servicePrincipal.Id - ErrorAction Stop
472+ }
473+ }
474+
475+ Invoke-Usecase - Name ' VerifyReaderRoleAssignment' - Description " Verify if the Service Principle has got Reader role assigned successfully" - UsecaseBlock `
476+ {
477+ $readerRole = Get-AzureRmRoleDefinition - Name Reader
478+ $subscriptionID = (Get-AzureRmSubscription - SubscriptionName $tenantSubscriptionName ).SubscriptionId
479+ if ($subscriptionID -and $readerRole -and (-not (Get-AzureRmRoleAssignment - RoleDefinitionName $readerRole.Name - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop)))
480+ {
481+ throw [System.Exception ] " Unable to assign role ($readerRole .Name) to Service Principle ($servicePrincipal .Id) for subscription $tenantSubscriptionName "
482+ }
483+ }
484+
485+ Invoke-Usecase - Name ' RemoveReaderRoleAssignment' - Description " Remove Reader role assignment from Service Principle - $ ( $servicePrincipal.DisplayName ) " - UsecaseBlock `
486+ {
487+ $readerRole = Get-AzureRmRoleDefinition - Name Reader
488+ $subscriptionID = (Get-AzureRmSubscription - SubscriptionName $tenantSubscriptionName ).SubscriptionId
489+ if ($subscriptionID -and $readerRole -and (Get-AzureRmRoleAssignment - RoleDefinitionName $readerRole.Name - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop))
490+ {
491+ Remove-AzureRmRoleAssignment - Scope " /Subscriptions/$subscriptionID " - RoleDefinitionName $readerRole.Name - ObjectId $servicePrincipal.Id - Force - ErrorAction Stop
492+ }
493+ }
494+
495+ Invoke-Usecase - Name ' CustomRoleDefinition' - Description " Create a custom Role Definition - $customRoleName " - UsecaseBlock `
496+ {
497+ $subscriptionID = (Get-AzureRmSubscription - SubscriptionName $tenantSubscriptionName ).SubscriptionId
498+ if (Get-AzureRmRoleDefinition - Name $customRoleName )
499+ {
500+ Remove-AzureRmRoleDefinition - Name $customRoleName - Scope " /Subscriptions/$subscriptionID " - Force - ErrorAction Stop
501+ }
502+ $role = Get-AzureRmRoleDefinition - Name Reader
503+ $role.Id = $null
504+ $role.Name = $customRoleName
505+ $role.Description = " Custom role definition for Canary"
506+ $role.Actions.Clear ()
507+ $role.Actions.Add (" Microsoft.Authorization/*/Read" )
508+ $role.AssignableScopes.Clear ()
509+ $role.AssignableScopes.Add (" /Subscriptions/$subscriptionID " )
510+ New-AzureRmRoleDefinition - Role $role - ErrorAction Stop
511+ if (-not (Get-AzureRmRoleDefinition - Name $customRoleName - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop))
512+ {
513+ throw [System.Exception ] " Unable to create custom role definition ($customRoleName ) for subscription $tenantSubscriptionName "
514+ }
515+ }
516+
517+ Invoke-Usecase - Name ' ListRoleDefinitionsAfterCustomRoleCreation' - Description " List existing Role Definitions" - UsecaseBlock `
518+ {
519+ $availableRoles = Get-AzureRmRoleDefinition - ErrorAction Stop
520+ if (-not $availableRoles )
521+ {
522+ throw [System.Exception ] " No roles are available."
523+ }
524+ else
525+ {
526+ $availableRoles
527+ $availableRolesNames = $availableRoles.Name
528+ $mustHaveRoles = @ (" Owner" , " Reader" , " Contributor" )
529+ $match = Compare-Object $mustHaveRoles $availableRolesNames
530+ if ($match -and ($match | Where-Object {$_.SideIndicator -eq " <=" }))
531+ {
532+ $notAvailableRoles = ($match | Where-Object {$_.SideIndicator -eq " <=" }).InputObject
533+ throw [System.Exception ] " Some must have Role Definitions are not available. Number of missing Role Definitions - $ ( $notAvailableRoles.Count ) . Missing Role Definitions - $notAvailableRoles "
534+ }
535+ }
536+ }
537+
538+ Invoke-Usecase - Name ' RemoveCustomRoleDefinition' - Description " Remove custom role definition - $customRoleName " - UsecaseBlock `
539+ {
540+ $subscriptionID = (Get-AzureRmSubscription - SubscriptionName $tenantSubscriptionName ).SubscriptionId
541+ if (Get-AzureRmRoleDefinition - Name $customRoleName - Scope " /Subscriptions/$subscriptionID " - ErrorAction Stop)
542+ {
543+ Remove-AzureRmRoleDefinition - Name $customRoleName - Scope " /Subscriptions/$subscriptionID " - Force - ErrorAction Stop
544+ }
545+ else
546+ {
547+ throw [System.Exception ] " Custom role definition ($customRoleName ) for subscription $tenantSubscriptionName is not available"
548+ }
549+ }
550+ }
519551
520552 Invoke-Usecase - Name ' RegisterResourceProviders' - Description " Register resource providers" - UsecaseBlock `
521553 {
0 commit comments