MigrationProfile/AWSMigrationProfile.psm1

Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util)
function New-RMAWSMigrationProfile {
    param(
        [Parameter(Mandatory)]
        [System.Object] $CloudAccount,

        [Parameter(Mandatory)]
        [System.Object] $Entitlement,

        [Parameter(Mandatory)]
        [System.Object] $Source,

        [Parameter(Mandatory)]
        [string] $TargetVMName,

        [Parameter(Mandatory)]
        [System.Object[]] $SelectedMounts,

        [Parameter(Mandatory)]
        [bool] $EncryptVolumes,

        [Parameter(Mandatory)]
        [string] $VolumeType,

        [string] $IOPS,

        [Parameter(Mandatory)]
        [string] $Region,

        [Parameter(Mandatory)]
        [string] $VPCID,

        [Parameter(Mandatory)]
        [string] $SubnetID,

        [Parameter(Mandatory)]
        [bool] $AutoAssignPublicIP,

        [string] $StaticPrivateIP,

        [Parameter(Mandatory)]
        [string] $InstanceType,

        [Parameter(Mandatory)]
        [bool] $EnforceTargetNetworkIsolation,

        [Parameter(Mandatory)]
        [string[]] $SecurityGroups,

        [string] $IAMRole,

        [Parameter(Mandatory)]
        [bool] $ShouldCreateAMI,

        [hashtable] $InstanceTags,

        [Parameter(Mandatory)]
        [bool] $ShutdownSource,

        [Parameter(Mandatory)]
        [bool] $ShutdownTarget,

        [Parameter(Mandatory)]
        [bool] $RemoveRMSAgent,

        [hashtable] $MigrationInstructions
    )

    $CloudAttributes = Get-CachedCloudAttribute -CloudAccount $CloudAccount
    $Subnet = Get-SubnetById -SubnetId $SubnetID -CloudAttributes $CloudAttributes
    if ($null -eq $Subnet) {
        throw "Subnet with ID '$SubnetID' does not exists"
    }

    $CurrentTime = Get-Date -Format "yyyy/MM/dd HH:mm:ss"
    $MigrationProfile = @{
        "name"= "powershell-" + $Source.host + "-" + $CurrentTime
        "tags"= @()
        "entitlement"=$Entitlement.id
        "cloud_account"=$CloudAccount.id
        "is_data_only"= $false
        "is_vdi"= $false
        "appliance_id"=$CloudAccount.appliance.id
        "schedule"= $null
        "transfer_mode"="run-once"
        "sources"= @(
            @{
                "source"= $Source.id
                "target_config"= @{
                    "vm_details"= @{
                        "vm_name"= $TargetVMName
                        "vapp_name"= $TargetVMName
                        "tags"= $InstanceTags
                        "flavor"= @{
                            "flavor_type"= $InstanceType
                            "volume_type"= $VolumeType
                            "iops"= if ("" -eq $IOPS) { $null } else { $IOPS }
                        }
                        "security"= @{
                            "vpc_id"= $VPCID
                            "security_group_ids"= $null
                            "security_group_names"= $SecurityGroups
                        }
                        "encrypt_volumes"= $EncryptVolumes
                        "capture_ami"= $ShouldCreateAMI
                        "iam_instance_profile"= $IAMRole
                    }
                    "properties"= @{
                        "network"= @{
                            "interfaces"= @{
                                "eth0"= @{
                                    "ip_type"= "dhcp"
                                    "type"= "Ethernet"
                                    "network_name"= $SubnetID
                                    "assign_public_ip"= $AutoAssignPublicIP
                                    "ip_addr"= $StaticPrivateIP
                                }
                            }
                            "automatic_dns_registration"= !$DisableTargetDNSRegistration
                        }
                        "selected_mounts"= $SelectedMounts
                        "name"= $TargetVMName
                        "mounts_new_size"= @{}
                    }
                    "options"= @{
                        "region"= $Region
                        "az"= $Subnet.availability_zone
                        "power_on"= $true
                        "tenancy"= "default"
                        "affinity"= $null
                        "host_id"= $null
                    }
                }
                "verify_ssl_certificates"= $false
                "publish_migration_hub"= $false
                "os_type"= $Source.os_type
                "name"= $Source.hostname
                "collection_type"= $null
                "shutdown_source"= $ShutdownSource
                "shutdown_target"= $ShutdownTarget
                "remove_target_agent"= $RemoveRMSAgent
                "in_place_upgrade"= $false
                "upgrade_os_version"= $null
                "migration_instructions"= $MigrationInstructions
                "data_transfer_port"= 5995
                "ignore_validation_errors"= $true #TODO
                "preflight_warning"= $false
            }
        )
    }

    # Giving max depth otherwise the cmdlet 'ConvertTo-Json' will truncate the JSON
    $MigrationProfileJson = $MigrationProfile |ConvertTo-Json -Depth 100

    $RMLoginResult = Get-Variable -Name "RMContext-UserLogin" -ValueOnly
    $Uri = Get-Variable -Name "RMContext-ReactorURI" -ValueOnly

    $Headers = @{
        Accept = "application/rm+json"
        "X-Auth-Token" = $RMLoginResult.token
    }

    $Params = @{
        Method = "Post"
        Uri = $Uri + "/migrationprofiles"
        Body = $MigrationProfileJson
        ContentType = "application/json"
        Headers = $Headers
    }

    return Invoke-RMRestMethod -Params $Params
}

function Get-SubnetById {
    param(
        [string] $SubnetId,
        [System.Object] $CloudAttributes
    )

    $VPCS = $CloudAttributes.properties.regions[0].vpcs
    foreach ($VPC in $VPCS) {
        foreach ($Subnet in $VPC.subnets) {
            if ($SubnetId -eq $Subnet.id) {
                return $Subnet
            }
        }
    }

    return $null
}

#TODO=try to make all the methods in the module as private, yet accessible from our code(other modules)
Export-ModuleMember -Function New-RMAWSMigrationProfile