From ad76a77c2088fd9201e2e2ee2f424cd9746caf5d Mon Sep 17 00:00:00 2001 From: William Lam Date: Mon, 23 Jul 2018 13:23:39 -0700 Subject: [PATCH] PowerShell module for VMware Cloud Services Portal API --- Modules/VMware.CSP/VMware.CSP.psm1 | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Modules/VMware.CSP/VMware.CSP.psm1 diff --git a/Modules/VMware.CSP/VMware.CSP.psm1 b/Modules/VMware.CSP/VMware.CSP.psm1 new file mode 100644 index 0000000..b4b4515 --- /dev/null +++ b/Modules/VMware.CSP/VMware.CSP.psm1 @@ -0,0 +1,54 @@ +Function Get-CSPAccessToken { + <# + .NOTES + =========================================================================== + Created by: William Lam + Date: 07/23/2018 + Organization: VMware + Blog: https://www.virtuallyghetto.com + Twitter: @lamw + =========================================================================== + + .DESCRIPTION + Converts a Refresh Token from the VMware Console Services Portal + to CSP Access Token to access CSP API + .PARAMETER RefreshToken + The Refresh Token from the VMware Console Services Portal + .EXAMPLE + Get-CSPAccessToken -RefreshToken $RefreshToken + #> + Param ( + [Parameter(Mandatory=$true)][String]$RefreshToken + ) + + $results = Invoke-WebRequest -Uri "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize?refresh_token=$RefreshToken" -Method POST -ContentType "application/json" -UseBasicParsing -Headers @{"csp-auth-token"="$RefreshToken"} + if($results.StatusCode -ne 200) { + Write-Host -ForegroundColor Red "Failed to retrieve Access Token, please ensure your VMC Refresh Token is valid and try again" + break + } + $accessToken = ($results | ConvertFrom-Json).access_token + Write-Host "CSP Auth Token has been successfully retrieved and saved to `$env:cspAuthToken" + $env:cspAuthToken = $accessToken +} + +Function Get-CSPServices { + <# + .NOTES + =========================================================================== + Created by: William Lam + Date: 07/23/2018 + Organization: VMware + Blog: https://www.virtuallyghetto.com + Twitter: @lamw + =========================================================================== + + .DESCRIPTION + Returns the list of CSP Services avialable for given user + .EXAMPLE + Get-CSPServices + #> + If (-Not $env:cspAuthToken) { Write-error "CSP Auth Token not found, please run Get-CSPAccessToken" } Else { + $results = Invoke-WebRequest -Uri "https://console.cloud.vmware.com/csp/gateway/slc/api/definitions?expand=1" -Method GET -ContentType "application/json" -UseBasicParsing -Headers @{"csp-auth-token"="$env:cspAuthToken"} + ((($results.Content) | ConvertFrom-Json).results | where {$_.visible -eq $true}).displayName + } +} \ No newline at end of file