ExportedFunctions/Connect-Pulumi.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function Connect-Pulumi {
    param(
        [Parameter(Mandatory=$true)]
        $Org,

        $Url = "https://api.pulumi.com",

        [Switch]$NotDefault
    )
    $CredPath = Join-Path $Home ".pulumi/credentials.json"
    if(!(Test-Path $CredPath)) {
        Write-Error "Please log-in to pulumi using 'pulumi login' and then re-run Connect-Pulumi."
        return
    }
    $Credentials = Get-Content $CredPath | ConvertFrom-Json | ForEach-Object {$_}

    $TokenKeys = $Credentials.accessTokens | Get-Member -MemberType NoteProperty | ForEach-Object {$_.Name}
    if($TokenKeys -notcontains $Url) {
        Write-Error "Unable to find a credential for Pulumi at $CredPath"
    } else {
        $Pulumi = [PSCustomObject]@{
            PulumiUrl = $Url
            Token = $Credentials.accessTokens."$Url"
            Org = $Org
        }
        if(!$NotDefault) {
            $Global:DefaultPulumi = $Pulumi
        }
        $Pulumi
    }
}