Skip to content

Commit 7f66cc2

Browse files
singhabh27prsadhu-ms-idcisra-fel
authored
[Az.Migrate] Adding Tagging, CRN feature for Migrate and few fixes (#15520)
* move to 2021 swagger * Changing default recovery for CBT * Azure Migrate Initialize Replication Infrastructure UserObject cmdlet fix * Included SQL License Type and Resource Tagging Feature * Simplified Set-AzMigrateServerReplication.ps1 to avoid repetition of code * Few fixes in Set-AzMigrateServerReplication.ps1 * Changes in New-AzMigrateServerReplication.ps1 * Changed Variable Name * Removed SeedDiskTag * Updated SQL Server License Type Summary and fixed tag Count issue. * Adding CRN feature. * Error msg change * Recorded tests * Tagging change as per design review * Adding error msg when tag key is passed as empty string * Fix in Nickname spelling * add breaking change and signature suppression * Test fixed Co-authored-by: Prachetos Sadhukhan <[email protected]> Co-authored-by: Yeming Liu <[email protected]>
1 parent 0f0ef7d commit 7f66cc2

File tree

84 files changed

+2830
-1839
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2830
-1839
lines changed

src/Migrate/custom/Get-AzMigrateJob.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The Get-AzMigrateJob cmdlet retrives the status of an Azure Migrate job.
2222
https://docs.microsoft.com/powershell/module/az.migrate/get-azmigratejob
2323
#>
2424
function Get-AzMigrateJob {
25-
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.IJob])]
25+
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.IJob])]
2626
[CmdletBinding(DefaultParameterSetName = 'ListByName', PositionalBinding = $false)]
2727
param(
2828
[Parameter(ParameterSetName = 'GetById', Mandatory)]
@@ -53,7 +53,7 @@ function Get-AzMigrateJob {
5353

5454
[Parameter(ParameterSetName = 'GetByInputObject', Mandatory)]
5555
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Path')]
56-
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.IJob]
56+
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.IJob]
5757
# Specifies the job object of the replicating server.
5858
${InputObject},
5959

src/Migrate/custom/Get-AzMigrateServerReplication.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The Get-AzMigrateServerReplication cmdlet retrieves the object for the replicati
2222
https://docs.microsoft.com/powershell/module/az.migrate/get-azmigrateserverreplication
2323
#>
2424
function Get-AzMigrateServerReplication {
25-
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.IMigrationItem])]
25+
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.IMigrationItem])]
2626
[CmdletBinding(DefaultParameterSetName = 'ListByName', PositionalBinding = $false)]
2727
param(
2828
[Parameter(ParameterSetName = 'GetBySRSID', Mandatory)]
@@ -53,7 +53,7 @@ function Get-AzMigrateServerReplication {
5353

5454
[Parameter(ParameterSetName = 'GetByInputObject', Mandatory)]
5555
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Path')]
56-
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.IMigrationItem]
56+
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.IMigrationItem]
5757
# Specifies the machine object of the replicating server.
5858
${InputObject},
5959

src/Migrate/custom/Initialize-AzMigrateReplicationInfrastructure.ps1

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ function Initialize-AzMigrateReplicationInfrastructure {
159159
$LogStringCreated = "Created : "
160160
$LogStringSkipping = " already exists."
161161

162+
$userObject = Get-AzADUser -UserPrincipalName $context.Subscription.ExtendedProperties.Account
163+
164+
if (-not $userObject) {
165+
$userObject = Get-AzADUser -Mail $context.Subscription.ExtendedProperties.Account
166+
}
167+
168+
if (-not $userObject) {
169+
$mailNickname = "{0}#EXT#" -f $($context.Account.Id -replace '@', '_')
170+
171+
$userObject = Get-AzADUser |
172+
Where-Object {$_.MailNickname -eq $mailNickname}
173+
}
174+
175+
if (-not $userObject) {
176+
$userObject = Get-AzADServicePrincipal -ApplicationID $context.Account.Id
177+
}
178+
179+
if (-not $userObject)
180+
{
181+
throw 'User Object Id Not Found!'
182+
}
183+
162184
# Hash code source code
163185
$Source = @"
164186
using System;
@@ -181,10 +203,13 @@ public static int hashForArtifact(String artifact)
181203

182204
#Get vault name from SMS solution.
183205
$smsSolution = Get-AzMigrateSolution -MigrateProjectName $ProjectName -ResourceGroupName $ResourceGroupName -Name "Servers-Migration-ServerMigration"
206+
if (-not $smsSolution.DetailExtendedDetail.AdditionalProperties.vaultId) {
207+
throw 'Azure Migrate appliance not configured. Setup Azure Migrate appliance before proceeding.'
208+
}
184209
$VaultName = $smsSolution.DetailExtendedDetail.AdditionalProperties.vaultId.Split("/")[8]
185210

186211
# Get all appliances and sites in the project from SDS solution.
187-
$sdsSolution = Get-AzMigrateSolution -MigrateProjectName $ProjectName -ResourceGroupName $ResourceGroupName -Name "Servers-Discovery-ServerDiscovery"
212+
$sdsSolution = Get-AzMigrateSolution -MigrateProjectName $ProjectName -ResourceGroupName $ResourceGroupName -Name "Servers-Discovery-ServerDiscovery"
188213
$appMap = @{}
189214

190215
if ($null -ne $sdsSolution.DetailExtendedDetail["applianceNameToSiteIdMapV2"]) {
@@ -194,13 +219,13 @@ public static int hashForArtifact(String artifact)
194219
$appMap[$item.ApplianceName] = $item.SiteId
195220
}
196221
}
197-
222+
198223
if ($null -ne $sdsSolution.DetailExtendedDetail["applianceNameToSiteIdMapV3"]) {
199224
$appMapV3 = $sdsSolution.DetailExtendedDetail["applianceNameToSiteIdMapV3"] | ConvertFrom-Json
200225
foreach ($item in $appMapV3) {
201226
$t = $item.psobject.properties
202227
$appMap[$t.Name] = $t.Value.SiteId
203-
}
228+
}
204229
}
205230

206231
if ($null -eq $sdsSolution.DetailExtendedDetail["applianceNameToSiteIdMapV2"] -And
@@ -225,7 +250,7 @@ public static int hashForArtifact(String artifact)
225250
if (($existingMapping) -and ($existingMapping.ProviderSpecificDetail.TargetLocation -ne $TargetRegion)) {
226251
$targetRegionMismatchExceptionMsg = $ProjectName + " is already configured for migrating servers to " + $TargetRegion + ". Target Region cannot be modified once configured."
227252
throw $targetRegionMismatchExceptionMsg
228-
}
253+
}
229254
}
230255
}
231256

@@ -241,7 +266,7 @@ public static int hashForArtifact(String artifact)
241266

242267
# Phase 1
243268
# Storage account
244-
$MigratePrefix = "migrate"
269+
$MigratePrefix = "migrate"
245270
$LogStorageAcName = $MigratePrefix + "lsa" + $hash
246271
$GateWayStorageAcName = $MigratePrefix + "gwsa" + $hash
247272
$StorageType = "Microsoft.Storage/storageAccounts"
@@ -299,13 +324,13 @@ public static int hashForArtifact(String artifact)
299324
else {
300325
Write-Host $ServiceBusNamespace, $LogStringSkipping
301326
}
302-
327+
303328
# Key vault
304329
$KeyVaultName = $MigratePrefix + "kv" + $hash
305330
$KeyVaultType = "Microsoft.KeyVault/vaults"
306331
$KeyVaultApiVersion = "2016-10-01"
307332
$KeyVaultKind = "KeyVault"
308-
333+
309334
$existingKeyVaultAccount = Get-AzResource -ResourceGroupName $ResourceGroupName -Name $KeyVaultName -ErrorVariable notPresent -ErrorAction SilentlyContinue
310335
if ($existingKeyVaultAccount) {
311336
Write-Host $KeyVaultName, $LogStringSkipping
@@ -326,8 +351,7 @@ public static int hashForArtifact(String artifact)
326351
if ($CloudEnvironMent -eq "AzureUSGovernment") {
327352
$HyperVManagerAppId = "AFAE2AF7-62E0-4AA4-8F66-B11F74F56326"
328353
}
329-
$hyperVManagerObject = Get-AzADServicePrincipal -ApplicationID $HyperVManagerAppId
330-
$userObject = Get-AzADUser -UserPrincipalName $context.Subscription.ExtendedProperties.Account
354+
$hyperVManagerObject = Get-AzADServicePrincipal -ApplicationID $HyperVManagerAppId
331355
$accessPolicies = @()
332356
$userAccessPolicy = @{
333357
"tenantId" = $tenantID;
@@ -427,13 +451,27 @@ public static int hashForArtifact(String artifact)
427451
$lsaStorageAccount = Get-AzResource -ResourceName $LogStorageAcName -ResourceGroupName $ResourceGroupName
428452
$gwyRoleAssignments = Get-AzRoleAssignment -ObjectId $kvspnid -Scope $gwyStorageAccount.Id -ErrorVariable notPresent -ErrorAction SilentlyContinue
429453
$lsaRoleAssignments = Get-AzRoleAssignment -ObjectId $kvspnid -Scope $lsaStorageAccount.Id -ErrorVariable notPresent -ErrorAction SilentlyContinue
430-
if (!$lsaRoleAssignments) {
454+
if (-not $lsaRoleAssignments) {
431455
$output = New-AzRoleAssignment -ObjectId $kvspnid -Scope $lsaStorageAccount.Id -RoleDefinitionId $roleDefinitionId
432456
}
433-
if (!$gwyRoleAssignments) {
457+
if (-not $gwyRoleAssignments) {
434458
$output = New-AzRoleAssignment -ObjectId $kvspnid -Scope $gwyStorageAccount.Id -RoleDefinitionId $roleDefinitionId
435459
}
436460

461+
if (-not $lsaRoleAssignments -or -not $gwyRoleAssignments) {
462+
for ($i = 1; $i -le 18; $i++) {
463+
Write-Information "Waiting for Role Assignments to be available... $( $i * 10 ) seconds" -InformationAction Continue
464+
Start-Sleep -Seconds 10
465+
466+
$gwyRoleAssignments = Get-AzRoleAssignment -ObjectId $kvspnid -Scope $gwyStorageAccount.Id -ErrorVariable notPresent -ErrorAction SilentlyContinue
467+
$lsaRoleAssignments = Get-AzRoleAssignment -ObjectId $kvspnid -Scope $lsaStorageAccount.Id -ErrorVariable notPresent -ErrorAction SilentlyContinue
468+
469+
if ($gwyRoleAssignments -and $lsaRoleAssignments) {
470+
break
471+
}
472+
}
473+
}
474+
437475
# SA. SAS definition
438476

439477
$gatewayStorageAccountSasSecretName = "gwySas"
@@ -453,7 +491,7 @@ public static int hashForArtifact(String artifact)
453491
Set-AzKeyVaultManagedStorageSasDefinition -AccountName $GateWayStorageAcName -VaultName $KeyVaultName -Name $gatewayStorageAccountSasSecretName -TemplateUri $gwyat -SasType 'account' -ValidityPeriod ([System.Timespan]::FromDays(30))
454492

455493
# Phase 2
456-
494+
457495
# ServiceBusConnectionString
458496
$serviceBusConnString = "ServiceBusConnectionString"
459497
$serviceBusSecretObject = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $serviceBusConnString -ErrorVariable notPresent -ErrorAction SilentlyContinue
@@ -465,16 +503,16 @@ public static int hashForArtifact(String artifact)
465503
$secret = ConvertTo-SecureString -String $serviceBusRootKey.PrimaryConnectionString -AsPlainText -Force
466504
$output = Set-AzKeyVaultSecret -VaultName $KeyVaultName -Name $serviceBusConnString -SecretValue $secret
467505
Write-Host $LogStringCreated, $serviceBusConnString, " for ", $applianceName
468-
}
506+
}
469507

470508
# Policy
471509
$policyName = $MigratePrefix + $SiteName + "policy"
472510
$existingPolicyObject = Get-AzMigrateReplicationPolicy -PolicyName $policyName -ResourceGroupName $ResourceGroupName -ResourceName $VaultName -ErrorVariable notPresent -ErrorAction SilentlyContinue
473511
if (!$existingPolicyObject) {
474-
$providerSpecificPolicy = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.VMwareCbtPolicyCreationInput]::new()
512+
$providerSpecificPolicy = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.VMwareCbtPolicyCreationInput]::new()
475513
$providerSpecificPolicy.AppConsistentFrequencyInMinute = 240
476514
$providerSpecificPolicy.InstanceType = "VMwareCbt"
477-
$providerSpecificPolicy.RecoveryPointHistoryInMinute = 4320
515+
$providerSpecificPolicy.RecoveryPointHistoryInMinute = 360
478516
$providerSpecificPolicy.CrashConsistentFrequencyInMinute = 60
479517
$existingPolicyObject = New-AzMigrateReplicationPolicy -PolicyName $policyName -ResourceGroupName $ResourceGroupName -ResourceName $VaultName -ProviderSpecificInput $providerSpecificPolicy
480518
Write-Host $LogStringCreated, $policyName
@@ -497,7 +535,7 @@ public static int hashForArtifact(String artifact)
497535
else {
498536
$keyVaultAccountDetails = Get-AzKeyVault -ResourceGroupName $ResourceGroupName -Name $KeyVaultName
499537
$gwyStorageAccount = Get-AzResource -ResourceGroupName $ResourceGroupName -ResourceName $GateWayStorageAcName
500-
$providerSpecificInput = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.VMwareCbtContainerMappingInput]::new()
538+
$providerSpecificInput = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.VMwareCbtContainerMappingInput]::new()
501539
$providerSpecificInput.InstanceType = "VMwareCbt"
502540
$providerSpecificInput.KeyVaultId = $keyVaultAccountDetails.ResourceId
503541
$providerSpecificInput.KeyVaultUri = $keyVaultAccountDetails.VaultUri
@@ -507,9 +545,9 @@ public static int hashForArtifact(String artifact)
507545
$providerSpecificInput.TargetLocation = $TargetRegion
508546
$output = New-AzMigrateReplicationProtectionContainerMapping -FabricName $fabric.Name -MappingName $mappingName -ProtectionContainerName $peContainer.Name -ResourceGroupName $ResourceGroupName -ResourceName $VaultName -PolicyId $existingPolicyObject.Id -ProviderSpecificInput $providerSpecificInput -TargetProtectionContainerId "Microsoft Azure"
509547
Write-Host $LogStringCreated, $mappingName, " for ", $applianceName
510-
}
548+
}
511549
}
512-
}
550+
}
513551
}
514552
Write-Host "Finished successfully."
515553
return $true

src/Migrate/custom/New-AzMigrateDiskMapping.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The New-AzMigrateDiskMapping cmdlet creates a mapping of the source disk attache
2222
https://docs.microsoft.com/powershell/module/az.migrate/new-azmigratediskmapping
2323
#>
2424
function New-AzMigrateDiskMapping {
25-
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.IVMwareCbtDiskInput])]
25+
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.IVMwareCbtDiskInput])]
2626
[CmdletBinding(DefaultParameterSetName = 'VMwareCbt', PositionalBinding = $false)]
2727
param(
2828
[Parameter(Mandatory)]
@@ -55,7 +55,7 @@ function New-AzMigrateDiskMapping {
5555
)
5656

5757
process {
58-
$DiskObject = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.VMwareCbtDiskInput]::new()
58+
$DiskObject = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.VMwareCbtDiskInput]::new()
5959
$DiskObject.DiskId = $DiskID
6060

6161
$validDiskTypeSpellings = @{

src/Migrate/custom/New-AzMigrateNicMapping.ps1

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The New-AzMigrateNicMapping cmdlet creates a mapping of the source NIC attached
2222
https://docs.microsoft.com/powershell/module/az.migrate/new-azmigratenicmapping
2323
#>
2424
function New-AzMigrateNicMapping {
25-
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.IVMwareCbtNicInput])]
25+
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.IVMwareCbtNicInput])]
2626
[CmdletBinding(DefaultParameterSetName = 'VMwareCbt', PositionalBinding = $false)]
2727
param(
2828
[Parameter(Mandatory)]
@@ -45,6 +45,12 @@ function New-AzMigrateNicMapping {
4545
# Specifies the Subnet name for the NIC in the destination Virtual Network to which the server needs to be migrated.
4646
${TargetNicSubnet},
4747

48+
[Parameter()]
49+
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Path')]
50+
[System.String]
51+
# Specifies the name of the NIC to be created.
52+
${TargetNicName},
53+
4854
[Parameter()]
4955
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Path')]
5056
[System.String]
@@ -53,7 +59,7 @@ function New-AzMigrateNicMapping {
5359
)
5460

5561
process {
56-
$NicObject = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20180110.VMwareCbtNicInput]::new()
62+
$NicObject = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.VMwareCbtNicInput]::new()
5763
$NicObject.NicId = $NicID
5864
if ($PSBoundParameters.ContainsKey('TargetNicSelectionType')) {
5965
if ($TargetNicSelectionType -eq 'primary') {
@@ -76,6 +82,18 @@ function New-AzMigrateNicMapping {
7682
if ($PSBoundParameters.ContainsKey('TargetNicIP')) {
7783
$NicObject.TargetStaticIPAddress = $TargetNicIP
7884
}
85+
86+
if ($PSBoundParameters.ContainsKey('TargetNicName')) {
87+
if ($TargetNicName.length -gt 80 -or $TargetNicName.length -eq 0) {
88+
throw "The NIC name must be between 1 and 80 characters long."
89+
}
90+
91+
if ($TargetNicName -notmatch "^[^_\W][a-zA-Z0-9_\-\.]{0,79}(?<![-.])$") {
92+
throw "The NIC name must begin with a letter or number, end with a letter, number or underscore, and may contain only letters, numbers, underscores, periods, or hyphens."
93+
}
94+
$NicObject.TargetNicName = $TargetNicName
95+
}
96+
7997
return $NicObject
8098
}
8199

0 commit comments

Comments
 (0)