Skip to content

Commit

Permalink
Microsoft.DataReplication Az.Migrate Update for HCI scenario align wi…
Browse files Browse the repository at this point in the history
…th preview schedule (#24594)

* Update NIC logic

* Add retries for operations

* Refactor retry logic

* Add validation & bug fix

* Fix typo

* Bug fix

* Expose get protected item error

* Update autogenerated files including doc

---------

Co-authored-by: Sam Lee <[email protected]>
  • Loading branch information
minhsuanlee and Sam Lee authored Jun 5, 2024
1 parent fb67034 commit f1a7228
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 253 deletions.
2 changes: 1 addition & 1 deletion src/Migrate/Migrate.Autorest/Az.Migrate.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DotNetFrameworkVersion = '4.7.2'
RequiredAssemblies = './bin/Az.Migrate.private.dll'
FormatsToProcess = './Az.Migrate.format.ps1xml'
FunctionsToExport = 'Get-AzMigrateDiscoveredServer', 'Get-AzMigrateJob', 'Get-AzMigrateProject', 'Get-AzMigrateReplicationEligibilityResult', 'Get-AzMigrateReplicationFabric', 'Get-AzMigrateReplicationPolicy', 'Get-AzMigrateReplicationProtectionContainer', 'Get-AzMigrateReplicationProtectionContainerMapping', 'Get-AzMigrateReplicationProtectionIntent', 'Get-AzMigrateReplicationRecoveryServicesProvider', 'Get-AzMigrateReplicationVaultSetting', 'Get-AzMigrateRunAsAccount', 'Get-AzMigrateServerReplication', 'Get-AzMigrateSite', 'Get-AzMigrateSolution', 'Get-AzMigrateSupportedOperatingSystem', 'Initialize-AzMigrateReplicationInfrastructure', 'New-AzMigrateDiskMapping', 'New-AzMigrateNicMapping', 'New-AzMigrateProject', 'New-AzMigrateReplicationPolicy', 'New-AzMigrateReplicationProtectionContainerMapping', 'New-AzMigrateReplicationProtectionIntent', 'New-AzMigrateReplicationVaultSetting', 'New-AzMigrateServerReplication', 'Register-AzMigrateProjectTool', 'Remove-AzMigrateProject', 'Remove-AzMigrateServerReplication', 'Restart-AzMigrateServerReplication', 'Set-AzMigrateDiskMapping', 'Set-AzMigrateServerReplication', 'Start-AzMigrateServerMigration', 'Start-AzMigrateTestMigration', 'Start-AzMigrateTestMigrationCleanup', '*'
FunctionsToExport = 'Get-AzMigrateDiscoveredServer', 'Get-AzMigrateHCIJob', 'Get-AzMigrateHCIReplicationFabric', 'Get-AzMigrateHCIServerReplication', 'Get-AzMigrateJob', 'Get-AzMigrateProject', 'Get-AzMigrateReplicationFabric', 'Get-AzMigrateReplicationPolicy', 'Get-AzMigrateReplicationProtectionContainer', 'Get-AzMigrateReplicationProtectionContainerMapping', 'Get-AzMigrateReplicationRecoveryServicesProvider', 'Get-AzMigrateRunAsAccount', 'Get-AzMigrateServerReplication', 'Get-AzMigrateSite', 'Get-AzMigrateSolution', 'Initialize-AzMigrateHCIReplicationInfrastructure', 'Initialize-AzMigrateReplicationInfrastructure', 'New-AzMigrateDiskMapping', 'New-AzMigrateHCIDiskMappingObject', 'New-AzMigrateHCINicMappingObject', 'New-AzMigrateHCIServerReplication', 'New-AzMigrateNicMapping', 'New-AzMigrateProject', 'New-AzMigrateReplicationPolicy', 'New-AzMigrateReplicationProtectionContainerMapping', 'New-AzMigrateServerReplication', 'New-AzMigrateTestNicMapping', 'Register-AzMigrateProjectTool', 'Remove-AzMigrateHCIServerReplication', 'Remove-AzMigrateProject', 'Remove-AzMigrateServerReplication', 'Restart-AzMigrateServerReplication', 'Resume-AzMigrateServerReplication', 'Set-AzMigrateDiskMapping', 'Set-AzMigrateHCIServerReplication', 'Set-AzMigrateServerReplication', 'Start-AzMigrateHCIServerMigration', 'Start-AzMigrateServerMigration', 'Start-AzMigrateTestMigration', 'Start-AzMigrateTestMigrationCleanup', 'Suspend-AzMigrateServerReplication', '*'
AliasesToExport = '*'
PrivateData = @{
PSData = @{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,58 +162,45 @@ function Get-AzMigrateHCIServerReplication {

# Occasionally, Get Machine Site will not return machine site even when the site exist,
# hence retry get machine site.
$attempts = 4
for ($i = 1; $i -le $attempts; $i++) {
try {
if ($siteType -eq $SiteTypes.VMwareSites) {
$siteObject = Az.Migrate\Get-AzMigrateSite @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
elseif ($siteType -eq $SiteTypes.HyperVSites) {
$siteObject = Az.Migrate.Internal\Get-AzMigrateHyperVSite @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
if ($siteType -eq $SiteTypes.VMwareSites) {
$siteObject = InvokeAzMigrateGetCommandWithRetries `
-CommandName 'Az.Migrate\Get-AzMigrateSite' `
-Parameters $PSBoundParameters `
-ErrorMessage "Machine site '$siteName' with Type '$siteType' not found."
} elseif ($siteType -eq $SiteTypes.HyperVSites) {
$siteObject = InvokeAzMigrateGetCommandWithRetries `
-CommandName 'Az.Migrate.Internal\Get-AzMigrateHyperVSite' `
-Parameters $PSBoundParameters `
-ErrorMessage "Machine site '$siteName' with Type '$siteType' not found."
}

if ($null -eq $siteObject) {
throw "Machine site not found."
}
else {
$ProjectName = $siteObject.DiscoverySolutionId.Split("/")[8]
}
# $siteObject is not null or exception would have been thrown
$ProjectName = $siteObject.DiscoverySolutionId.Split("/")[8]

break;
}
catch {
if ($i -lt $attempts)
{
Write-Host "Machine site not found. Retrying in 30 seconds..."
Start-Sleep -Seconds 30
}
else
{
throw "Machine site '$siteName' with Type '$siteType' not found."
}
}
}

$null = $PSBoundParameters.Remove('SiteName')

$null = $PSBoundParameters.Add("Name", "Servers-Migration-ServerMigration_DataReplication")
# Get the migrate solution.
$amhSolutionName = "Servers-Migration-ServerMigration_DataReplication"
$null = $PSBoundParameters.Add("Name", $amhSolutionName)
$null = $PSBoundParameters.Add("MigrateProjectName", $ProjectName)

$solution = Az.Migrate\Get-AzMigrateSolution @PSBoundParameters
if ($solution -and ($solution.Count -ge 1)) {
$VaultName = $solution.DetailExtendedDetail.AdditionalProperties.vaultId.Split("/")[8]
}
else {
throw "Solution not found."
}

$solution = InvokeAzMigrateGetCommandWithRetries `
-CommandName 'Az.Migrate\Get-AzMigrateSolution' `
-Parameters $PSBoundParameters `
-ErrorMessage "No Data Replication Service Solution '$amhSolutionName' found in resource group '$ResourceGroupName' and project '$ProjectName'. Please verify your appliance setup."

$null = $PSBoundParameters.Remove("Name")
$null = $PSBoundParameters.Remove("MigrateProjectName")

$VaultName = $solution.DetailExtendedDetail.AdditionalProperties.vaultId.Split("/")[8]
if ([string]::IsNullOrEmpty($VaultName)) {
throw "Azure Migrate Project not configured: missing replication vault. Setup Azure Migrate Project and run the Initialize-AzMigrateHCIReplicationInfrastructure script before proceeding."
}

$null = $PSBoundParameters.Add("VaultName", $VaultName)
$null = $PSBoundParameters.Add("Name", $ProtectedItemName)

return Az.Migrate.Internal\Get-AzMigrateProtectedItem @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
return Az.Migrate.Internal\Get-AzMigrateProtectedItem @PSBoundParameters
}

if (($parameterSet -match 'List') -or ($parameterSet -eq 'GetByMachineName')) {
Expand All @@ -234,23 +221,27 @@ function Get-AzMigrateHCIServerReplication {
$ProjectName = $projectIdArray[8]
}

$amhSolutionName = "Servers-Migration-ServerMigration_DataReplication"
$null = $PSBoundParameters.Add("ResourceGroupName", $ResourceGroupName)
$null = $PSBoundParameters.Add("Name", "Servers-Migration-ServerMigration_DataReplication")
$null = $PSBoundParameters.Add("Name", $amhSolutionName)
$null = $PSBoundParameters.Add("MigrateProjectName", $ProjectName)

$solution = Az.Migrate\Get-AzMigrateSolution @PSBoundParameters
if ($solution -and ($solution.Count -ge 1)) {
$VaultName = $solution.DetailExtendedDetail.AdditionalProperties.vaultId.Split("/")[8]
}
else {
throw "Solution not found."
}

$solution = InvokeAzMigrateGetCommandWithRetries `
-CommandName 'Az.Migrate\Get-AzMigrateSolution' `
-Parameters $PSBoundParameters `
-ErrorMessage "No Data Replication Service Solution '$amhSolutionName' found in resource group '$ResourceGroupName' and project '$ProjectName'. Please verify your appliance setup."

$null = $PSBoundParameters.Remove("Name")
$null = $PSBoundParameters.Remove("MigrateProjectName")

$VaultName = $solution.DetailExtendedDetail.AdditionalProperties.vaultId.Split("/")[8]
if ([string]::IsNullOrEmpty($VaultName)) {
throw "Azure Migrate Project not configured: missing replication vault. Setup Azure Migrate Project and run the Initialize-AzMigrateHCIReplicationInfrastructure script before proceeding."
}

$null = $PSBoundParameters.Add("VaultName", $VaultName)

$replicatingItems = Az.Migrate.Internal\Get-AzMigrateProtectedItem @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
$replicatingItems = Az.Migrate.Internal\Get-AzMigrateProtectedItem @PSBoundParameters

if ($parameterSet -eq "GetByMachineName") {
$replicatingItems = $replicatingItems | Where-Object { $_.Property.FabricObjectName -eq $MachineName }
Expand All @@ -274,7 +265,7 @@ function Get-AzMigrateHCIServerReplication {
$null = $PSBoundParameters.Add("VaultName", $VaultName)
$null = $PSBoundParameters.Add("Name", $ProtectedItemName)

return Az.Migrate.Internal\Get-AzMigrateProtectedItem @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
return Az.Migrate.Internal\Get-AzMigrateProtectedItem @PSBoundParameters
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ enum StorageAccountProvisioningState
Creating
ResolvingDNS
Succeeded
}

# Selection state for VM NIC
$VMNicSelection = @{
SelectedByUser = "SelectedByUser";
NotSelected = "NotSelected";
}
54 changes: 54 additions & 0 deletions src/Migrate/Migrate.Autorest/custom/Helper/CommonHelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,58 @@ function GenerateHashForArtifact {
else {
return $hashCode
}
}

function InvokeAzMigrateGetCommandWithRetries {
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.DoNotExportAttribute()]
param(
[Parameter(Mandatory)]
[System.String]
# Specifies the name of Az.Migrate command.
${CommandName},

[Parameter(Mandatory)]
[System.Collections.Hashtable]
# Specifies the parameters for Az.Migrate command.
${Parameters},

[Parameter()]
[System.Int32]
# Specifies the maximum number of retries.
${MaxRetryCount} = 3,

[Parameter()]
[System.Int32]
# Specifies the delay between retries in seconds.
${RetryDelayInSeconds} = 30,

[Parameter()]
[System.String]
# Specifies the error message to throw if command fails.
${ErrorMessage} = "Internal Az.Migrate commands failed to execute."
)

process {
for ($i = 0; $i -le $MaxRetryCount; $i++) {
try {
$result = & $CommandName @Parameters -ErrorVariable notPresent -ErrorAction SilentlyContinue

if ($null -eq $result) {
throw $ErrorMessage
}

break
}
catch {
if ($i -lt $MaxRetryCount) {
Start-Sleep -Seconds $RetryDelayInSeconds
}
else {
throw $ErrorMessage
}
}
}

return $result
}
}
Loading

0 comments on commit f1a7228

Please sign in to comment.