Skip to content

Instantly share code, notes, and snippets.

@dimitertodorov
Created May 29, 2016 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitertodorov/6d31edcfe1143bbf9d3694ff82463508 to your computer and use it in GitHub Desktop.
Save dimitertodorov/6d31edcfe1143bbf9d3694ff82463508 to your computer and use it in GitHub Desktop.
#ORCA API Cmdlets for Powershell
function Connect-ORCA{
<#
.SYNOPSIS
Log into an ORCA API Endpoint.
.DESCRIPTION
This Cmdlet will log you in and create a token for use during your session. This is stored in $env:ORCA_USER and $env:ORCA_TOKEN
.PARAMETER base_url
Full URL to the ORCA Instance. e.g. https://intra.oo.local.dom.com:9005/orca
.PARAMETER credentials
PSCreds
.INPUTS
None. You cannot pipe objects to the script.
.OUTPUTS
None.
.EXAMPLE
C:\PS> ORCA-Login -base_url https://intra.oo.local.dom.com:9005/orca -credentials $pscredentialsobject
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[string]$base_url,
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]$credentials
)
####
$user = New-Object PSObject -Property @{
"username"=$credentials.GetNetworkCredential().UserName
"password"=$credentials.GetNetworkCredential().Password
}
$uri = $base_url + "/users/sign_in.json"
$body=ConvertTo-Json $user
$token = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Body $body
If($token){
Write-Host "Logged Into $base_url as $username. ORCA_URL and ORCA_TOKEN have been exported to the environment"
}
$env:ORCA_TOKEN=$token.user.auth_token
$env:ORCA_USER=$token.user.username
$env:ORCA_URL=$base_url
return $null
}
function Disconnect-ORCA{
<#
.SYNOPSIS
Log out of ORCA
.DESCRIPTION
This Cmdlet will log you out and clear the environment variables for ORCA_URL and ORCA_TOKEN
.INPUTS
None. You cannot pipe objects to the script.
.OUTPUTS
None.
.EXAMPLE
C:\PS> Disconnect-ORCA
#>
If (($env:ORCA_URL) -and ($env:ORCA_TOKEN)){
ORCA Delete "/users/sign_out.json"
}
$env:ORCA_URL=$null
$env:ORCA_USER=$null
$env:ORCA_TOKEN=$null
Write-Host "Successfully Logged Out"
return $null
}
function ORCA{
<#
.SYNOPSIS
Perform REST Actions against an ORCA Server
.DESCRIPTION
This Cmdlet allows you to perform GET, POST PATCH, PUT, DELETE Requests against an ORCA Endpoint
.PARAMETER method
Can be one of GET, POST PATCH, PUT, DELETE
.PARAMETER path
URL Endpoint. e.g. "/esmt/rfcs/CRQ000001180625"
.PARAMETER body
Optional. This should be a JSON-formatted string.
.PARAMETER Verbose
Optional. If this flag is set the JSON object is not printed out to screen.
.INPUTS
None. You cannot pipe objects to the script.
.OUTPUTS
Powershell Object matching the JSON returned from ORCA.
.EXAMPLE
PS C:\> $crq=ORCA GET '/esmt/rfcs/CRQ000001180625'
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true, Position=1)]
[ValidateSet("Get","Post","Patch","Put","Delete")]
[string]$method,
[Parameter(Mandatory=$true, Position=2)]
[string]$path,
[Parameter(Position=3)]
[string]$body,
[switch]$print
)
If(($env:ORCA_URL) -and ($env:ORCA_TOKEN) -and ($env:ORCA_USER)){
$headers = @{"X-API-ORCA-USER"=$env:ORCA_USER;"X-API-TOKEN"=$env:ORCA_TOKEN; "Accept"="application/json"}
$action_method=$method.ToString().ToUpper()
try{
switch ($action_method){
"GET"{$output=Invoke-RestMethod -Uri $env:ORCA_URL$path -Method Get -ContentType "application/json" -Headers $headers}
"POST"{$output=Invoke-RestMethod -Uri $env:ORCA_URL$path -Method Post -ContentType "application/json" -Headers $headers -Body $body}
"PATCH"{$output=Invoke-RestMethod -Uri $env:ORCA_URL$path -Method Patch -ContentType "application/json" -Headers $headers -Body $body}
"PUT"{$output=Invoke-RestMethod -Uri $env:ORCA_URL$path -Method Put -ContentType "application/json" -Headers $headers -Body $body}
"DELETE"{$output=Invoke-RestMethod -Uri $env:ORCA_URL$path -Method Delete -ContentType "application/json" -Headers $headers}
}
}catch{
$error_exception = $_.Exception
if(($error_exception.GetType().Name -eq "WebException") -and ($_.Exception.Response)){
$error_response=$_.Exception.Response
Write-Host $_.Exception
Write-Host $error_response
$response_stream=$error_response.GetResponseStream()
$reader=New-Object System.IO.StreamReader($response_stream)
$response_text=$reader.ReadToEnd()
$formatted_error=@"
ORCA Error: $($error_exception.Message)
URI: $($error_response.responseuri.AbsoluteUri)
Server: $($error_response.Server)
Full Error Text:
$response_text
"@
}else{
$formatted_error=$error_exception
}
Write-Host -BackgroundColor DarkRed $formatted_error
throw $_
}
If($PSBoundParameters['Verbose']){
if($output){
$output_length=@($output).Length
Write-Verbose "Object-Count: $output_length"
}
}
If($print){
$json_output=$output | ConvertTo-Json -Depth 100
Write-Host $json_output
}
return $output
}else{
throw "ORCA_URL and ORCA_USER,ORCA_TOKEN not found in environment. Please use ORCA-Login to log in."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment