Misc/Get-ProperArtifactUrl.ps1

function Get-ProperArtifactUrl {
    Param(
        [Parameter(Mandatory=$true)]
        [ref]$Version,
        [Parameter(Mandatory=$false)]
        [string]$CU = "",
        [Parameter(Mandatory=$true)]
        [ref]$Country,
        [Parameter(Mandatory=$false)]
        [string]$ArtifactUrl = "",
        [Parameter(Mandatory=$true)]
        [ref]$target,
        [Parameter(Mandatory=$true)]
        [ref]$imageName,
        [Parameter(Mandatory=$false)]
        [string]$SourcePath = (Get-Location)
    )

    $settings = Import-Config
    if ($null -eq $imageName.Value -or $imageName.value -eq "") {
        $imageName.Value = $settings.prefix
    }

    $aliasBC = @("current","latest","nextminor","nextmajor")
    $aliasNAV = @(2016,2017,2018)
    $aliasNAVNumeric = @(9,10,11)

    if ($ArtifactUrl -ne "") {
        if ($ArtifactUrl.Contains("onprem")) {
            $target.Value = "OnPrem"
        }
        else {
            $target.Value = "Sandbox"
        }
        $Country.Value = $ArtifactUrl.Split("/")[-1]
        $Version.Value = $ArtifactUrl.Split("/")[-2]
    }

    switch($target.Value) {
        "" {
                if (@("","Cloud","Extension").Contains((Get-AppKeyValue -KeyName "Target" -SourcePath $SourcePath))) {
                    $target.Value = "Sandbox"
                }
                else {
                    $target.Value = "OnPrem"
                }
        }
        "Cloud" {
            $target.Value = "Sandbox"
        }
    }

    if ($Country.Value -eq "") {
        $Country.Value = "us"
    }

    if ($null -eq $Version.Value -or $Version.Value -eq "") {
        $Version.Value = Get-AppKeyValue -SourcePath $SourcePath -KeyName "platform"
    }
    if ($null -eq $Version.Value -or $Version.Value -eq "") {
        $Version.Value = "Current"
    }

    if ($aliasBC.Contains($Version.Value)) {
        $target.Value = "Sandbox"
    }

    # adjusting Country, if older versions of BC/NAV
    if (!($aliasBC.Contains($Version.Value.ToLower())) -and ($Version.Value -lt "15" -or $Version.Value -gt "2000" -or ([version]$Version.Value).Major -lt 15)) {
        $Country.Value = "na"
    }

    try {
        switch -Wildcard ($Version.Value) {
            {"Current", "Latest" -eq $_ } {
                $ArtifactUrl = Get-BCArtifactUrl -country $Country.Value -select Latest
                $Version.Value = $ArtifactUrl.Split('/')[-2]
                break;
            }
            "NextM*" {
                try {
                    $saasToken = Get-Secret -secretName "insiderToken"
                    $artifactUrl = Get-BCArtifactUrl -country $Country.Value -select $Version.Value -sasToken "$saasToken"
                    $Version.Value = $ArtifactUrl.Split('/')[-2]
                }
                catch {
                    throw "Could not retrieve SaaSToken"
                }
                break;
            }
            default {
                if (!($Version.Value -is [ValueType] -or [Double]::TryParse($Version.Value,[ref]$null) -or [version]::TryParse($Version.Value, [ref]$null) )) {
                    throw "The version '$($Version.Value)' is not a valid version"
                }
                if (!$Version.Value.Contains('.')) {
                    if ($CU -eq "") {
                        $cu = Get-EnvironmentKeyValue -KeyName "cu" -SourcePath $SourcePath
                    }
                    if ($CU -ne "") {
                        $Version.Value = ("{0}.{1}" -f $Version.Value, $CU)
                    }
                    else {
                        $Version.Value = ("{0}.0" -f $Version.Value)
                    }
                }
                break;
            }
        }
    }
    catch {
        throw "Could not retrieve version '$($Version.Value)'"
    }

    # there is a bug in the 17.0 container images, so always using 17.1 then
    if ($Version.Value -eq "17.0") {
        $Version.Value = "17.1"
    }

    $Major = ([version]$Version.Value).Major
    $Minor = ([version]$Version.Value).Minor

    if ($aliasNAVNumeric.Contains($Major)) {
        switch ($Major) {
            9 {
                $Major = 2016
                break
            }
            10 {
                $Major = 2017
                break
            }
            11 {
                $Major = 2018
                break
            }
        }

        $tempArtifactUrl = ""
        [int]$cu = -1
        try {
            do {
                $cu = $cu + 1
                $tempArtifactUrl = Get-NavArtifactUrl -nav $Major -cu $cu -country $Country.Value -select Latest
            } while ($tempArtifactUrl -ne $ArtifactUrl)
        }
        catch {
            throw "Could not evaluate artifact url"
        }
        $Minor = $cu
    }
    else {
        if ($ArtifactUrl -ne "") {
            if ($aliasNAV.Contains($Major)) {
                $tempUrl = Get-NavArtifactUrl -nav $Major -cu $Minor -country $Country.Value -select Latest
                if ($tempUrl -ne $ArtifactUrl) {
                    $ArtifactUrl = $tempUrl
                }
            }
            else {

            }
        }
    }

    if ($aliasNAV.Contains($Major)) {
        if ($ArtifactUrl -eq "") {
            $ArtifactUrl = Get-NavArtifactUrl -nav $Major -cu $Minor -select Latest -country $Country.Value.ToLower()
        }
        if (!$Version.Value.StartsWith('navx:')) {
            $imageName.Value = "$($imageName.Value):$Major-$Minor-$($country.Value)"
        }
        else {
            $imageName.Value = $Version.Value
        }

        $Version.Value = $Major
        $target.Value = "OnPrem"
    }
    else {
        $parameters = @{}
        if ($aliasBC.Contains($Major)) {
            $saasToken = Get-Secret -secretName "insiderToken"
            if ($null -ne $saasToken -and $saasToken -ne "") {
                $parameters.Add("sasToken", $saasToken)
            }
        }
        if ($ArtifactUrl -eq "") {
            $artifactUrl = Get-BCArtifactUrl -version $Version.Value -select Latest -country $Country.Value.ToLower() -Type $EnvironmentType @parameters
        }
        if (!$Version.Value.StartsWith('navx:')) {
            if ($Version.Value -eq "NextMinor" -or $Version.Value -eq "NextMajor") {
                if ($artifactUrl.StartsWith("https://bcinsider.azureedge.net/sandbox/")) {
                    $tempVersion = [version]$artifactUrl.Substring(40).SubString(0, $artifactUrl.Substring(40).IndexOf("/"))
                    $Version.Value = ("{0}.{1}" -f $tempVersion.Major, $tempVersion.Minor)
                }
            }
            $imageName.Value = "$($imageName.Value):$($Version.Value)-$($Country.Value)-$($target.Value)"
        }
        else {
            $imageName.Value = $Version.Value
        }
    }

    return $ArtifactUrl
}