Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll committed Oct 16, 2019
1 parent f9d54db commit f53b44a
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
118 changes: 118 additions & 0 deletions UniversalDashboard.Outrest.psm1
@@ -0,0 +1,118 @@
function Out-UDRestApi {
param(
[Parameter(Mandatory, ParameterSetName = "Module")]
[string]$Module,
[Parameter(Mandatory, ParameterSetName = "Command")]
[string]$Command
)

if ($PSCmdlet.ParameterSetName -eq 'Module')
{
Out-UDModuleRestApi -Module $Module
}

if ($PSCmdlet.ParameterSetName -eq 'Command')
{
$CommandDefinition = Get-Command $Command
Out-UDRestEndpoint -CommandDefinition $CommandDefinition
}
}

function Out-UDModuleRestApi {
param(
[Parameter(Mandatory, ParameterSetName = "Module")]
[string]$Module
)

Get-Command -Module $Module | ForEach-Object {
Out-UDRestEndpoint -CommandDefinition $_
}
}

function Out-UDRestEndpoint {
param(
[Parameter(Mandatory)]
$CommandDefinition
)



$RestMethod = ConvertTo-RestMethod -Verb $CommandDefinition.Verb
$CommandDefinition.ParameterSets | ForEach-Object {
$RestUrl = New-RestUrl -ParameterSet $_
$MandatoryParameters = $_.Parameters.Where({$_.IsMandatory})

$Endpoint = ""
if ($MandatoryParameters.Length -gt 0)
{
$paramBlock = "param("
$_.Parameters.Where({$_.IsMandatory}) | ForEach-Object {
$paramBlock += "`$$($_.Name),"
}
$paramBlock = $paramBlock.TrimEnd(',')
$paramBlock += ")`r`n"
$Endpoint += $paramBlock
}

$Endpoint += '
$Parameters = @{}
$PSBoundParameters.Keys | ForEach-Object {
$Parameters[$_] = $PSBoundParameters[$_]
}
$Request.Query.GetEnumerator() | ForEach-Object {
$Parameters[$_.Key] = $_.Value[0]
}
'

$Endpoint += $CommandDefinition.Name + " @Parameters | ConvertTo-Json"

$ScriptBlock = [scriptblock]::Create($Endpoint)
New-UDEndpoint -Url $RestUrl -Method $RestMethod -Endpoint $ScriptBlock
}

}

function New-RestUrl {
param(
[Parameter(Mandatory)]
[System.Management.Automation.CommandParameterSetInfo]$ParameterSet
)

$Url = "/$($CommandDefinition.Noun)"

$ParameterSet.Parameters.Where({$_.IsMandatory}) | ForEach-Object {
$Url += "/:$($_.Name)"
}

$Url
}

function ConvertTo-RestMethod {
param(
[Parameter(Mandatory)]
[string]$Verb
)

$GetVerbs = @('Get')
$PostVerbs = @('Invoke', 'New')
$PutVerbs = @('Set')
$DeleteVerbs = @('Remove')

if ($GetVerbs -contains $Verb) {
return 'GET'
}

if ($PostVerbs -contains $Verb) {
return 'POST'
}

if ($PutVerbs -contains $Verb) {
return 'PUT'
}

if ($DeleteVerbs -contains $Verb) {
return 'DELETE'
}
}
4 changes: 4 additions & 0 deletions test.ps1
@@ -0,0 +1,4 @@
Import-Module "$PSScriptRoot/UniversalDashboard.Outrest.psm1" -Force
$Endpoints = Out-UDRestApi -Command "Get-Service"
Start-UDRestApi -Endpoint $Endpoints -Port 10000
Invoke-RestMethod -Method Get -Uri "http://localhost:10000/api/Service"

0 comments on commit f53b44a

Please sign in to comment.