-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign Items and use OpenAPi Datafile
- Loading branch information
Showing
9 changed files
with
10,375 additions
and
119 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Function Import-OpenApiData { | ||
param ( | ||
[String]$FilePath | ||
) | ||
$OpenApiData = Get-Content -Path $FilePath | ConvertFrom-Json | ||
$OpenApiHash = @{} | ||
foreach ($path in $OpenApiData.paths.psobject.properties) { | ||
$Module = $path.name.split('/')[1] | ||
|
||
foreach ($prop in $path.value.PSObject.properties) { | ||
if ($prop.value.operationId) { | ||
if ($prop.value.operationId -match "(.*?)$Module(.*)") { | ||
|
||
$Action = $matches[1] | ||
$Object = $matches[2] | ||
|
||
if (-Not $OpenApiHash.containskey($Module)) { | ||
$OpenApiHash.Add($module, @{}) | ||
} | ||
if (-Not $OpenApiHash.$Module.containskey($Action)) { | ||
$OpenApiHash.$Module.Add($Action, @{}) | ||
} | ||
if (-Not $OpenApiHash.$Module.$Action.containskey($Object)) { | ||
$OpenApiHash.$Module.$Action.Add($Object, @{}) | ||
} | ||
|
||
$NamespaceIn = $prop.value.requestBody.content.'application/json'.schema.'$ref' | ||
$NamespaceOut = $prop.value.responses.'200'.content.'application/json'.schema.'$ref' | ||
|
||
# Find parameter objects from the schema based on the schema name in the path | ||
$parameters = $OPNsenseOpenApi.components.parameters.psobject.Properties | | ||
Where-Object { ( '#/components/parameters/{0}' -f $_.Name) -in $prop.value.parameters.'$ref' } | ||
|
||
$OpenApiHash.$Module.$Action.$Object = | ||
[PSCustomObject]@{ | ||
# Module = $Module | ||
# Action = $Action | ||
# Object = $Object | ||
Method = $Prop.name | ||
Path = $Path.Name | ||
Parameters = $Parameters | ||
NameSpaceIn = $NamespaceIn | ||
NamespoaceOut = $NamespaceOut | ||
} | ||
|
||
} else { | ||
#Write-Warning $prop.name | ||
} | ||
} | ||
} | ||
} | ||
return $OpenApiHash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<# MIT License | ||
Copyright (c) 2018 fvanroie, NetwiZe.be | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
#> | ||
|
||
# Invoke an API call based on the OpenAPI specification file | ||
Function Invoke-OPNsenseOpenApiPath { | ||
# .EXTERNALHELP ../PS_OPNsense.psd1-Help.xml | ||
[CmdletBinding()] | ||
Param( | ||
[parameter(Mandatory = $true, position = 0)] | ||
[ValidateSet("AcmeClient", "ArpScanner", "C-ICAP", "CaptivePortal", "ClamAV", "Collectd", "Cron", "FreeRadius", "FtpProxy", "HAProxy", "HelloWorld", | ||
"IDS", "Iperf", "LLDPd", "MDNSrepeater", "Monit", "Netflow", "NodeExporter", "Nut", "OpenConnect", "Postfix", "Proxy", "ProxySSO", "ProxyUserACl", | ||
"Quagga", "Redis", "Relayd", "Routes", "Rspamd", "ShadowSocks", "Siproxd", "Telegraf", "Tinc", "Tor", "TrafficShaper", | ||
"ZabbixAgent", "ZabbixProxy", "ZeroTier")] | ||
[String]$Module, | ||
|
||
[parameter(Mandatory = $true, position = 1)] | ||
[ValidateSet("Get", "Set", "Search", "Toggle", "Add", "Delete", | ||
"Status", "Start", "Restart", "Stop", "Reconfigure", "Test")] | ||
[String]$Action, | ||
|
||
[parameter(Mandatory = $true, position = 2)][String]$Object, | ||
|
||
[parameter(Mandatory = $false, ParameterSetName = "Json")] | ||
$Body, | ||
|
||
# [parameter(Mandatory = $false)][HashTable]$AddProperty, | ||
[parameter(Mandatory = $false)][String]$Uuid, | ||
[parameter(Mandatory = $false)][Boolean]$Enabled | ||
) | ||
|
||
# Search the Appropriate API Call for this Action | ||
#$call = Search-OPNsenseOpenApiPath -Module $Module -Action $Action -Object $Object | ||
$call = $OPNsenseOpenApi.$Module.$Action.$Object | ||
if (!$Call) { | ||
Write-Error ("{0} {2} does not implement the {1} action. (#17)" -f $Module, $Action, $Object) | ||
return $null | ||
} | ||
|
||
$null, $Module, $Controller, $CmdParams = $Call.Path.Split('/', 4) | ||
|
||
# Replace Path parameters with values | ||
foreach ($parameter in $call.parameters) { | ||
if ($parameter.Value.in -eq 'Path') { | ||
$key = '{{{0}}}' -f $parameter.Value.Name | ||
switch ($key) { | ||
'{uuid}' { $value = $uuid } | ||
'{enabled}' { $value = $enabled } | ||
'{disabled}' { $value = -Not $enabled } | ||
default { $value = '' } | ||
} | ||
$CmdParams = $CmdParams.replace($key, $value) | ||
} | ||
} | ||
|
||
# Build Parameters Splat | ||
$Splat = @{ | ||
'Module' = $Module | ||
'Controller' = $Controller | ||
'Command' = $CmdParams | ||
'Method' = $Call.Method | ||
'Verbose' = $VerbosePreference | ||
'Debug' = $DebugPreference | ||
} | ||
Switch ($Action) { | ||
'get' { $Splat.Add('Property', $Object) } | ||
'search' { $Splat.Add('Property', 'rows') } | ||
default {} | ||
} | ||
if ($Body) { | ||
$Splat.Add('Json', $Body) | ||
} | ||
# Add UUID, except for OPNsense.CaptivePortal.Template already as it already has a UUID | ||
if ($Action -eq 'get' -and $returntype -ne 'OPNsense.CaptivePortal.Template') { | ||
$splat.Add("AddProperty", @{ 'uuid' = $Uuid} ) | ||
} | ||
|
||
# Run this command thru the Api | ||
$result = Invoke-OPNsenseCommand @Splat | ||
|
||
# for OPNsense.CaptivePortal.Template all Templates are returned, select the one with the correct UUID | ||
if ($Action -eq 'get' -and $returntype -eq 'OPNsense.CaptivePortal.Template') { | ||
$result = $result | Where-Object { $_.UUID -eq $Uuid } | ||
} | ||
|
||
# Can we cast this into our own object? | ||
if ($returntype) { | ||
Write-Verbose "Converting object to $returntype" | ||
return ConvertTo-OPNsenseObject -TypeName $returntype -InputObject $result | ||
} | ||
|
||
# Return PSCustomObject instead | ||
return $result | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.