RiverMeadow.Development.Organization/RiverMeadow.Development.Organization.psm1


Import-Module -Name $PSScriptRoot\..\Util\Util
Import-Module -Name $PSScriptRoot\..\Common\Wrappers\Wrappers
function Switch-RMProject {
    param(
        [Alias("on")]
        [string] $OrganizationName,
        [Alias("pn")]
        [string] $ProjectName
    )

    $UserLoginStatus = Test-UserLoggedIn
    if (!$UserLoginStatus) {
        return
    }

    $RMLoginResult = Get-Variable -Name "RMContext-UserLogin"
    if (!(Confirm-RMUserInput -OrganizationName $OrganizationName -ProjectName $ProjectName)) {
        return
    }

    $ProjectObj = Get-RMProjectByName -OrganizationName $OrganizationName -ProjectName $ProjectName

    $Uri = Get-Variable -Name "RMContext-ReactorURI"

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

    $Params = @{
        Method = "Put"
        Uri = $Uri.Value + "/users/" + $RMLoginResult.Value.user_id + "/organization/" + $ProjectObj.id + "/switch"
        Headers = $Headers
    }

    Invoke-RMRestMethod -Params $Params

    Set-Variable -Name "RMContext-CurrentOrganizationName" -Value $OrganizationName -Scope Global
    Set-Variable -Name "RMContext-CurrentParentOrganizationId" -Value $ProjectObj.parent_organization_id -Scope Global

    Set-Variable -Name "RMContext-CurrentProjectName" -Value $ProjectName -Scope Global
    Set-Variable -Name "RMContext-CurrentProjectId" -Value $ProjectObj.id -Scope Global

    Write-Output "Current organization set to: $OrganizationName" | Out-Host
    Write-Output "Current project set to: $ProjectName" | Out-Host
}

function Confirm-RMUserInput {
    param(
        [string] $OrganizationName,
        [string] $ProjectName
    )

    if (!$OrganizationName -and $ProjectName) {
        Write-RMError -Message "Please provide the Organization Name"
        return $false
    }

    if ($OrganizationName -and !$ProjectName) {
        Write-RMError -Message "Please provide the Project Name"
        return $false
    }

    if (!($OrganizationName -and $ProjectName)) {
        Write-Output "User's Organizations and Projects:" | Out-Host
        (Get-Variable -Name "RMContext-UserOrganizations").Value | Format-Table @{Label="Organization Name"; Expression={if (!$_.is_project) { $_.name}else{ ""}}}, @{Label="Project Name"; Expression={if ($_.is_project){$_.name}else{""}}} -AutoSize -Wrap | Out-Host
        return $false
    }

    $UserOrgs = (Get-Variable -Name "RMContext-UserOrganizations").Value

    $OrgsAndProjectsMap = @{}
    $CurrentOrg
    foreach($UserOrg in $UserOrgs) {
        if (!$UserOrg.is_project) {
            $CurrentOrg = $UserOrg.name
            $OrgsAndProjectsMap.Add($UserOrg.name, @())
            continue
        }
        $OrgProjects = $OrgsAndProjectsMap[$CurrentOrg]
        $OrgProjects += $UserOrg.name
        $OrgsAndProjectsMap[$CurrentOrg] = $OrgProjects
    }

    if (!$OrgsAndProjectsMap.ContainsKey($OrganizationName)) {
        Write-RMError -Message "Organization '$OrganizationName' does not exist"
        return
    }


    if (!$OrgsAndProjectsMap[$OrganizationName].Contains($ProjectName)) {
        Write-RMError -Message "Project '$ProjectName' does not exist"
        return
    }

    return $true
}

function Get-RMProjectByName {
    param(
        [string] $OrganizationName,
        [string] $ProjectName
    )

    $UserOrgs = (Get-Variable -Name "RMContext-UserOrganizations").Value
    $OrgFound = $false
    foreach ($UserOrg in $UserOrgs) {
        if ($UserOrg.name -eq $OrganizationName) {
            $OrgFound = $true
            continue
        }

        # Default project is present in all Orgs, so we need to
        # make sure that the org is found and only then check
        # the project. The Data returned by FE is ordered by Org
        # followed by the projects of the Org; so we expect to find
        # Org first.
        if ($OrgFound -and $UserOrg.name -eq $ProjectName) {
            return $UserOrg
        }
    }

    return $null
}

Export-ModuleMember -Function Switch-RMProject -Alias urmproject