|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +<# |
| 16 | + .SYNOPSIS |
| 17 | + Tests for managing Active Directory Administrator on managed instance |
| 18 | +#> |
| 19 | +function Test-ManagedInstanceActiveDirectoryAdministrator |
| 20 | +{ |
| 21 | + # Setup |
| 22 | + $rg = Create-ResourceGroupForTest |
| 23 | + $vnetName = "cl_initial" |
| 24 | + $subnetName = "Cool" |
| 25 | + |
| 26 | + # Setup VNET |
| 27 | + $virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location |
| 28 | + $subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id |
| 29 | + |
| 30 | + $managedInstance = Create-ManagedInstanceForTest $rg $subnetId |
| 31 | + |
| 32 | + # If there is a need to re-record this test, these values must be changed to correspond to existing group and user from Azure Active Directory related to current subscription. |
| 33 | + $activeDirectoryGroup1 = "aadadmin" |
| 34 | + $activeDirectoryGroup1ObjectId = "52b6d571-5ff9-4b8f-92de-4a5b1bcdbbef" |
| 35 | + $activeDirectoryUser1 = "CL AAD Test User" |
| 36 | + $activeDirectoryUser1ObjectId = "034bb7d9-ca26-4c6f-abe0-4aff74fdca50" |
| 37 | + |
| 38 | + try |
| 39 | + { |
| 40 | + # Verify there is no Active Directory Administrator set |
| 41 | + $activeDirectoryAdmin = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName |
| 42 | + |
| 43 | + Assert-Null $activeDirectoryAdmin |
| 44 | + |
| 45 | + # Set an Active Directory Administrator Group on Managed Instance |
| 46 | + # This command uses the Graph API to check if there is a user/group for provided DisplayName and ObjectId. Graph authentication blocks test passes, so if you need to record this test again, you must provide real token in |
| 47 | + # MockTokenAuthenticationFactory constructor and change SetAuthenticationFactory in EnvironmentSetupHelper. |
| 48 | + $activeDirectoryAdmin1 = Set-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -DisplayName $activeDirectoryGroup1 -ObjectId $activeDirectoryGroup1ObjectId |
| 49 | + |
| 50 | + Assert-NotNull $activeDirectoryAdmin1 |
| 51 | + |
| 52 | + # Verify the correct Active Directory Administrator is set |
| 53 | + Assert-AreEqual $activeDirectoryAdmin1.DisplayName $activeDirectoryGroup1 |
| 54 | + Assert-AreEqual $activeDirectoryAdmin1.ObjectId $activeDirectoryGroup1ObjectId |
| 55 | + |
| 56 | + # Get an Active Directory Administrator |
| 57 | + $activeDirectoryAdmin2 = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName |
| 58 | + |
| 59 | + Assert-AreEqual $activeDirectoryAdmin2.DisplayName $activeDirectoryGroup1 |
| 60 | + Assert-AreEqual $activeDirectoryAdmin2.ObjectId $activeDirectoryGroup1ObjectId |
| 61 | + |
| 62 | + # Set an Active Directory Administrator User on Managed Instance |
| 63 | + $activeDirectoryAdmin3 = Set-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -DisplayName $activeDirectoryUser1 -ObjectId $activeDirectoryUser1ObjectId |
| 64 | + |
| 65 | + Assert-AreEqual $activeDirectoryAdmin3.DisplayName $activeDirectoryUser1 |
| 66 | + Assert-AreEqual $activeDirectoryAdmin3.ObjectId $activeDirectoryUser1ObjectId |
| 67 | + |
| 68 | + # Remove an Active Directory Administrator User from Managed Instance |
| 69 | + $activeDirectoryAdmin4 = Remove-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -Force |
| 70 | + |
| 71 | + # Verify that Active Directory Administrator was deleted |
| 72 | + $activeDirectoryAdmin5 = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName |
| 73 | + |
| 74 | + Assert-Null $activeDirectoryAdmin5 |
| 75 | + } |
| 76 | + finally |
| 77 | + { |
| 78 | + Remove-ResourceGroupForTest $rg |
| 79 | + } |
| 80 | +} |
0 commit comments