Public/FlyProjectApi.ps1
<#
.SYNOPSIS Create a new migration project .DESCRIPTION Create a new migration project .PARAMETER Name Specify the name of project .PARAMETER SourceConnection Specify the name of source connection .PARAMETER DestinationConnection Specify the name of destination connection .PARAMETER Policy Specify the name of policy which applied to the project .PARAMETER Tags Specify a list of tags to the project .OUTPUTS ProjectModel<PSCustomObject> #> function New-FlyMigrationProject { [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] [String] ${Name}, [Parameter(Mandatory = $true)] [String] ${SourceConnection}, [Parameter(Mandatory = $true)] [String] ${DestinationConnection}, [Parameter(Mandatory = $true)] [String] ${Policy}, [Parameter(Mandatory = $false)] [String[]] ${Tags} ) Process { 'Calling method: New-FlyMigrationProject' | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug Try { Resolve-FlyProjectName -ProjectName $Name $source = Get-FlyConnectionByName -ConnectionName $SourceConnection $destination = Get-FlyConnectionByName -ConnectionName $DestinationConnection $targetPolicy = Get-FlyPolicyByName -PolicyName $Policy -PlatformType $source.ConnectionType #Construct the project creation model $projectModel = [PSCustomObject]@{ "name" = $Name "sourcePlatform" = $source.ConnectionType "sourceConnectionId" = $source.Id "destinationPlatform" = $destination.ConnectionType "destinationConnectionId" = $destination.id "policyId" = $targetPolicy.Id "tagIds" = @() } if ($Tags -and $Tags.Count -gt 0) { $tagIds = $Tags | ForEach-Object { Get-FlyTagByName $PSItem } | Select-Object -Property Id | ForEach-Object { "$($_.Id)" } $projectModel.tagIds = @($tagIds) } #Create a new project $result = New-FlyProject -ProjectCreationModel $projectModel if ($result) { Write-Host 'Successfully created the project.' -ForegroundColor Green } return $result } Catch { Write-Host 'Failed to create the project.' -ForegroundColor Red ErrorDetail $_ throw } } } <# .SYNOPSIS Generate error report for the specified projects .DESCRIPTION Generate error report for the specified projects .PARAMETER Projects Specify a list of project names to generate their error report. .PARAMETER FileType Specify the format of the generated report file, CSV(default) or Excel .PARAMETER OutFolder Specify the folder path of error report file to download .PARAMETER TimeZoneOffset Specify the UTC time offset of current browser. This value will be used to adjust time values when generating the report file. .OUTPUTS None #> function Export-FlyErrorReport { [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] [String[]] ${Projects}, [Parameter(Mandatory = $true)] [String] ${OutFolder}, [Parameter(Mandatory = $false)] [ValidateSet('CSV', 'Excel')] [String] ${FileType} = [ReportFileType]::CSV, [Parameter(Mandatory = $false)] [Int32] ${TimeZoneOffset} ) Process { 'Calling method: Export-FlyErrorReport' | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug Try { $targetProjects = $Projects | ForEach-Object { Get-FlyProjectByName $PSItem } $projectIds = $targetProjects | Select-Object -Property Id | ForEach-Object { "$($_.Id)" } #Construct the settings of the error report $reportSetting = [PSCustomObject]@{ "reportFileType" = $FileType "projectIds" = @($projectIds) "timeZone" = $TimeZoneOffset } #Trigger the error report job and get the job id $jobId = Start-FlyErrorReportJob -GenerateProjectErrorReportSettingsModel $reportSetting #Monitor the job status and download the report file when job is finished while ($true) { Write-Host 'The report generation job is running.' -ForegroundColor Green Start-Sleep -Seconds 60 $job = Get-FlyReportJobs -RequestBody @($jobId) $jobStatus = $job.data[0].Status if ($jobStatus -eq [MappingJobStatus]::Finished -or $jobStatus -eq [MappingJobStatus]::FinishedWithException) { Write-Host 'The report generation job is finished.' -ForegroundColor Green $result = Get-FlyReportUrl -JobId $jobId if ($null -ne $result.ReportUrl) { $fileName = Split-Path $([uri]$result.ReportUrl).AbsolutePath -Leaf $filePath = Join-Path -Path $OutFolder -ChildPath $fileName If (-not (Test-Path $OutFolder)) { # Folder does not exist, create it [void](New-Item -Path $OutFolder -ItemType Directory) } Invoke-WebRequest -URI $result.ReportUrl -OutFile $filePath Write-Host 'Successfully downloaded the job report. Report path:' $filePath -ForegroundColor Green } break; } elseif ($jobStatus -eq [MappingJobStatus]::Failed -or $jobStatus -eq [MappingJobStatus]::Stopped) { throw ('The report generation job is {0} with id {1}' -f [MappingJobStatus].GetEnumName($jobStatus), $job.data[0].JobName) } } } Catch { Write-Host 'Failed to generate the error report.' -ForegroundColor Red ErrorDetail $_ throw } } } |